/[svn]/linuxsampler/trunk/src/engines/common/MidiKeyboardManager.h
ViewVC logotype

Annotation of /linuxsampler/trunk/src/engines/common/MidiKeyboardManager.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2043 - (hide annotations) (download) (as text)
Sat Jan 9 09:37:01 2010 UTC (8 years, 9 months ago) by persson
File MIME type: text/x-c++hdr
File size: 31119 byte(s)
* gig engine: implemented the "round robin keyboard" dimension
* gig engine: fixed round robin and random dimensions for cases when
  number of dimension zones is not a power of two
* gig engine: made round robin use a counter for each region instead
  of each key
* fixed building with libgig installed in a non-standard directory

1 iliev 2012 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5     * Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck *
6     * Copyright (C) 2005-2009 Christian Schoenebeck *
7     * Copyright (C) 2009 Grigor Iliev *
8     * *
9     * This program is free software; you can redistribute it and/or modify *
10     * it under the terms of the GNU General Public License as published by *
11     * the Free Software Foundation; either version 2 of the License, or *
12     * (at your option) any later version. *
13     * *
14     * This program is distributed in the hope that it will be useful, *
15     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17     * GNU General Public License for more details. *
18     * *
19     * You should have received a copy of the GNU General Public License *
20     * along with this program; if not, write to the Free Software *
21     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
22     * MA 02111-1307 USA *
23     ***************************************************************************/
24    
25     #ifndef __LS_MIDIKEYBOARDMANAGER_H__
26     #define __LS_MIDIKEYBOARDMANAGER_H__
27    
28     #include "Event.h"
29     #include "Stream.h"
30     #include "../../EventListeners.h"
31     #include "../../common/Pool.h"
32     #include "../../common/global_private.h"
33    
34     namespace LinuxSampler {
35    
36     /**
37     * This class is used as a listener, which is notified
38     * when MIDI keyboard events occur like note on, note off, etc.
39     * Note that all events are triggered even when the channel is muted
40     */
41     class MidiKeyboardListener {
42     public:
43     /** Called before the engine start processing the note on event */
44     virtual void PreProcessNoteOn(uint8_t key, uint8_t velocity) = 0;
45    
46     /** Called after the engine has processed the note on event */
47     virtual void PostProcessNoteOn(uint8_t key, uint8_t velocity) = 0;
48    
49     /** Called before the engine start processing the note off event */
50     virtual void PreProcessNoteOff(uint8_t key, uint8_t velocity) = 0;
51    
52     /** Called after the engine has processed the note off event */
53     virtual void PostProcessNoteOff(uint8_t key, uint8_t velocity) = 0;
54    
55     /** Called before the engine start processing the sustain pedal up event */
56     virtual void PreProcessSustainPedalUp() = 0;
57    
58     /** Called after the engine has processed the sustain pedal up event */
59     virtual void PostProcessSustainPedalUp() = 0;
60    
61     /** Called before the engine start processing the sustain pedal down event */
62     virtual void PreProcessSustainPedalDown() = 0;
63    
64     /** Called after the engine has processed the sustain pedal down event */
65     virtual void PostProcessSustainPedalDown() = 0;
66    
67     /** Called before the engine start processing the sostenuto pedal up event */
68     virtual void PreProcessSostenutoPedalUp() = 0;
69    
70     /** Called after the engine has processed the sostenuto pedal up event */
71     virtual void PostProcessSostenutoPedalUp() = 0;
72    
73     /** Called before the engine start processing the sostenuto pedal down event */
74     virtual void PreProcessSostenutoPedalDown() = 0;
75    
76     /** Called after the engine has processed the sostenuto pedal down event */
77     virtual void PostProcessSostenutoPedalDown() = 0;
78     };
79    
80     /**
81     * This class exists as convenience for creating listener objects.
82     * The methods in this class are empty.
83     */
84     class MidiKeyboardAdapter : public MidiKeyboardListener {
85     public:
86     virtual void PreProcessNoteOn(uint8_t key, uint8_t velocity) { }
87     virtual void PostProcessNoteOn(uint8_t key, uint8_t velocity) { }
88     virtual void PreProcessNoteOff(uint8_t key, uint8_t velocity) { }
89     virtual void PostProcessNoteOff(uint8_t key, uint8_t velocity) { }
90     virtual void PreProcessSustainPedalUp() { }
91     virtual void PostProcessSustainPedalUp() { }
92     virtual void PreProcessSustainPedalDown() { }
93     virtual void PostProcessSustainPedalDown() { }
94     virtual void PreProcessSostenutoPedalUp() { }
95     virtual void PostProcessSostenutoPedalUp() { }
96     virtual void PreProcessSostenutoPedalDown() { }
97     virtual void PostProcessSostenutoPedalDown() { }
98     };
99    
100     template <class V>
101     class MidiKeyboardManager {
102     public:
103     /** @brief Voice Stealing Algorithms
104     *
105     * Enumeration of all possible voice stealing algorithms.
106     */
107     enum voice_steal_algo_t {
108     voice_steal_algo_none, ///< Voice stealing disabled.
109     voice_steal_algo_oldestvoiceonkey, ///< Try to kill the oldest voice from same key where the new voice should be spawned.
110     voice_steal_algo_oldestkey ///< Try to kill the oldest voice from the oldest active key.
111     };
112    
113 persson 2043
114 iliev 2012 /** @brief MIDI key runtime informations
115     *
116     * Reflects runtime informations for one MIDI key.
117     */
118     class MidiKey {
119     public:
120     RTList<V>* pActiveVoices; ///< Contains the active voices associated with the MIDI key.
121     bool KeyPressed; ///< Is true if the respective MIDI key is currently pressed.
122     bool Active; ///< If the key contains active voices.
123     bool ReleaseTrigger; ///< If we have to launch release triggered voice(s) when the key is released
124     Pool<uint>::Iterator itSelf; ///< hack to allow fast deallocation of the key from the list of active keys
125     RTList<Event>* pEvents; ///< Key specific events (only Note-on, Note-off and sustain pedal currently)
126     int VoiceTheftsQueued; ///< Amount of voices postponed due to shortage of voices.
127 persson 2043 uint32_t* pRoundRobinIndex; ///< For the round robin dimension: current articulation for this key, will be incremented for each note on
128 iliev 2012 uint8_t Velocity; ///< Latest Note-on velocity for this key
129     unsigned long NoteOnTime; ///< Time for latest Note-on event for this key
130    
131     MidiKey() {
132     pActiveVoices = NULL;
133     KeyPressed = false;
134     Active = false;
135     ReleaseTrigger = false;
136     pEvents = NULL;
137     VoiceTheftsQueued = 0;
138     }
139    
140     void Reset() {
141     if (pActiveVoices) pActiveVoices->clear();
142     if (pEvents) pEvents->clear();
143     KeyPressed = false;
144     Active = false;
145     ReleaseTrigger = false;
146     itSelf = Pool<uint>::Iterator();
147     VoiceTheftsQueued = 0;
148     }
149     };
150    
151     typedef typename RTList<V>::Iterator RTListVoiceIterator;
152     typedef typename Pool<V>::Iterator PoolVoiceIterator;
153    
154     /**
155     * Override this class to iterate through all active keys/voices
156     * using ProcessActiveVoices() method.
157     */
158     class VoiceHandler {
159     public:
160     /**
161     * @returns true if the voices on the specified key should be processed
162     * adn false to cancel the processing of the active voices for the
163     * specified key
164     */
165     virtual bool Process(MidiKey* pMidiKey) = 0;
166    
167     virtual void Process(RTListVoiceIterator& itVoice) = 0;
168     };
169    
170     class VoiceHandlerBase : public VoiceHandler {
171     public:
172     virtual bool Process(MidiKey* pMidiKey) { return true; }
173     virtual void Process(RTListVoiceIterator& itVoice) { }
174     };
175    
176     MidiKey* pMIDIKeyInfo; ///< Contains all active voices sorted by MIDI key number and other informations to the respective MIDI key
177     Pool<uint>* pActiveKeys; ///< Holds all keys in it's allocation list with active voices.
178     std::map<uint,uint*> ActiveKeyGroups; ///< Contains active keys (in case they belong to a key group) ordered by key group ID.
179     bool SoloMode; ///< in Solo Mode we only play one voice (group) at a time
180     int SoloKey; ///< Currently 'active' solo key, that is the key to which the currently sounding voice belongs to (only if SoloMode is enabled)
181     bool SustainPedal; ///< true if sustain pedal is down
182     bool SostenutoPedal; ///< true if sostenuto pedal is down
183     int SostenutoKeys[128];
184     int SostenutoKeyCount;
185 persson 2043 uint32_t RoundRobinIndexes[128];
186 iliev 2012
187     MidiKeyboardManager() {
188     pMIDIKeyInfo = new MidiKey[128];
189     pActiveKeys = new Pool<uint>(128);
190     SoloMode = false;
191     SustainPedal = false;
192     SostenutoPedal = false;
193 persson 2043 for (int i = 0 ; i < 128 ; i++) {
194     RoundRobinIndexes[i] = 0;
195    
196     // by default use one counter for each key (the
197     // gig engine will change this to one counter per
198     // region)
199     pMIDIKeyInfo[i].pRoundRobinIndex = &RoundRobinIndexes[i];
200     }
201 iliev 2012 }
202    
203     virtual ~MidiKeyboardManager() {
204     listeners.RemoveAllListeners();
205     if (pActiveKeys) delete pActiveKeys;
206     if (pMIDIKeyInfo) delete[] pMIDIKeyInfo;
207     }
208    
209     void Reset(){
210     SoloKey = -1; // no solo key active yet
211    
212     // reset all key groups
213     std::map<uint,uint*>::iterator iter = ActiveKeyGroups.begin();
214     for (; iter != ActiveKeyGroups.end(); iter++) iter->second = NULL;
215    
216     // reset key info
217     for (uint i = 0; i < 128; i++) pMIDIKeyInfo[i].Reset();
218    
219     // free all active keys
220     pActiveKeys->clear();
221     }
222    
223     void AllocateActiveVoices(Pool<V>* pVoicePool) {
224     DeleteActiveVoices();
225    
226     for (uint i = 0; i < 128; i++) {
227     pMIDIKeyInfo[i].pActiveVoices = new RTList<V>(pVoicePool);
228     }
229     }
230    
231     void DeleteActiveVoices() {
232     for (uint i = 0; i < 128; i++) {
233     if (pMIDIKeyInfo[i].pActiveVoices) {
234     delete pMIDIKeyInfo[i].pActiveVoices;
235     pMIDIKeyInfo[i].pActiveVoices = NULL;
236     }
237     }
238     }
239    
240     void AllocateEvents(Pool<Event>* pEventPool) {
241     DeleteEvents();
242    
243     for (uint i = 0; i < 128; i++) {
244     pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEventPool);
245     }
246     }
247    
248     void DeleteEvents() {
249     for (uint i = 0; i < 128; i++) {
250     if (pMIDIKeyInfo[i].pEvents) {
251     delete pMIDIKeyInfo[i].pEvents;
252     pMIDIKeyInfo[i].pEvents = NULL;
253     }
254     }
255     }
256    
257     void ClearAllActiveKeyEvents() {
258     RTList<uint>::Iterator iuiKey = pActiveKeys->first();
259     RTList<uint>::Iterator end = pActiveKeys->end();
260     for(; iuiKey != end; ++iuiKey) {
261     pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key
262     }
263     }
264    
265     /**
266     * Removes the given voice from the MIDI key's list of active voices.
267     * This method will be called when a voice went inactive, e.g. because
268     * it finished to playback its sample, finished its release stage or
269     * just was killed.
270     *
271     * @param itVoice - points to the voice to be freed
272     */
273     void FreeVoice(PoolVoiceIterator& itVoice) {
274     if (itVoice) {
275     MidiKey* pKey = &pMIDIKeyInfo[itVoice->MIDIKey];
276    
277     uint keygroup = itVoice->KeyGroup;
278    
279     // if the sample and dimension region belong to an
280     // instrument that is unloaded, tell the disk thread to
281     // release them
282     if (itVoice->Orphan) {
283     if(itVoice->pDiskThread != NULL) {
284     itVoice->pDiskThread->OrderDeletionOfRegion(itVoice->GetRegion());
285     }
286     }
287    
288     // free the voice object
289     pKey->pActiveVoices->free(itVoice);
290    
291     // if no other voices left and member of a key group, remove from key group
292     if (pKey->pActiveVoices->isEmpty() && keygroup) {
293     uint** ppKeyGroup = &ActiveKeyGroups[keygroup];
294     if (*ppKeyGroup == &*pKey->itSelf) *ppKeyGroup = NULL; // remove key from key group
295     }
296     }
297     else std::cerr << "Couldn't release voice! (!itVoice)\n" << std::flush;
298     }
299    
300     /**
301     * Called when there's no more voice left on a key, this call will
302     * update the key info respectively.
303     *
304     * @param pEngineChannel - engine channel on which this event occured on
305     * @param pKey - key which is now inactive
306     */
307     void FreeKey(MidiKey* pKey) {
308     if (pKey->pActiveVoices->isEmpty()) {
309     pKey->Active = false;
310     pActiveKeys->free(pKey->itSelf); // remove key from list of active keys
311     pKey->itSelf = RTList<uint>::Iterator();
312     pKey->ReleaseTrigger = false;
313     pKey->pEvents->clear();
314     dmsg(3,("Key has no more voices now\n"));
315     }
316     else dmsg(1,("MidiKeyboardManager: Oops, tried to free a key which contains voices.\n"));
317     }
318    
319     /**
320     * Free all keys which have no active voices left
321     */
322     void FreeAllInactiveKyes() {
323     RTList<uint>::Iterator iuiKey = pActiveKeys->first();
324     RTList<uint>::Iterator end = pActiveKeys->end();
325     while (iuiKey != end) { // iterate through all active keys
326     MidiKey* pKey = &pMIDIKeyInfo[*iuiKey];
327     ++iuiKey;
328     if (pKey->pActiveVoices->isEmpty()) FreeKey(pKey);
329     #if CONFIG_DEVMODE
330     else { // just a sanity check for debugging
331     RTListVoiceIterator itVoice = pKey->pActiveVoices->first();
332     RTListVoiceIterator = pKey->pActiveVoices->end();
333     for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key
334     if (itVoice->itKillEvent) {
335     dmsg(1,("gig::Engine: ERROR, killed voice survived !!!\n"));
336     }
337     }
338     }
339     #endif // CONFIG_DEVMODE
340     }
341     }
342    
343     int StealVoice (
344     Pool<Event>::Iterator& itNoteOnEvent,
345     RTListVoiceIterator* LastStolenVoice,
346     RTList<uint>::Iterator* LastStolenKey
347     ) {
348     RTListVoiceIterator itSelectedVoice;
349    
350     // Select one voice for voice stealing
351     switch (CONFIG_VOICE_STEAL_ALGO) {
352    
353     // try to pick the oldest voice on the key where the new
354     // voice should be spawned, if there is no voice on that
355     // key, or no voice left to kill, then procceed with
356     // 'oldestkey' algorithm
357     case voice_steal_algo_oldestvoiceonkey: {
358     MidiKey* pSelectedKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];
359     itSelectedVoice = pSelectedKey->pActiveVoices->first();
360     // proceed iterating if voice was created in this fragment cycle
361     while (itSelectedVoice && !itSelectedVoice->IsStealable()) ++itSelectedVoice;
362     // if we haven't found a voice then proceed with algorithm 'oldestkey'
363     if (itSelectedVoice && itSelectedVoice->IsStealable()) break;
364     } // no break - intentional !
365    
366     // try to pick the oldest voice on the oldest active key
367     // from the same engine channel
368     // (caution: must stay after 'oldestvoiceonkey' algorithm !)
369     case voice_steal_algo_oldestkey: {
370     // if we already stole in this fragment, try to proceed on same key
371     if (*LastStolenVoice) {
372     itSelectedVoice = *LastStolenVoice;
373     do {
374     ++itSelectedVoice;
375     } while (itSelectedVoice && !itSelectedVoice->IsStealable()); // proceed iterating if voice was created in this fragment cycle
376     // found a "stealable" voice ?
377     if (itSelectedVoice && itSelectedVoice->IsStealable()) {
378     // remember which voice we stole, so we can simply proceed on next voice stealing
379     *LastStolenVoice = itSelectedVoice;
380     break; // selection succeeded
381     }
382     }
383     // get (next) oldest key
384     RTList<uint>::Iterator iuiSelectedKey = (*LastStolenKey) ? ++(*LastStolenKey) : pActiveKeys->first();
385     while (iuiSelectedKey) {
386     MidiKey* pSelectedKey = &pMIDIKeyInfo[*iuiSelectedKey];
387     itSelectedVoice = pSelectedKey->pActiveVoices->first();
388     // proceed iterating if voice was created in this fragment cycle
389     while (itSelectedVoice && !itSelectedVoice->IsStealable()) ++itSelectedVoice;
390     // found a "stealable" voice ?
391     if (itSelectedVoice && itSelectedVoice->IsStealable()) {
392     // remember which voice on which key we stole, so we can simply proceed on next voice stealing
393     *LastStolenKey = iuiSelectedKey;
394     *LastStolenVoice = itSelectedVoice;
395     break; // selection succeeded
396     }
397     ++iuiSelectedKey; // get next oldest key
398     }
399     break;
400     }
401    
402     // don't steal anything
403     case voice_steal_algo_none:
404     default: {
405     dmsg(1,("No free voice (voice stealing disabled)!\n"));
406     return -1;
407     }
408     }
409    
410     if (!itSelectedVoice || !itSelectedVoice->IsStealable()) return -1;
411    
412     #if CONFIG_DEVMODE
413     if (!itSelectedVoice->IsActive()) {
414     dmsg(1,("gig::Engine: ERROR, tried to steal a voice which was not active !!!\n"));
415     return -1;
416     }
417     #endif // CONFIG_DEVMODE
418    
419     // now kill the selected voice
420     itSelectedVoice->Kill(itNoteOnEvent);
421    
422     return 0;
423     }
424    
425     /**
426     * Releases all voices. All voices will go into
427     * the release stage and thus it might take some time (e.g. dependant to
428     * their envelope release time) until they actually die.
429     *
430     * @param itReleaseEvent - event which caused this releasing of all voices
431     */
432     void ReleaseAllVoices(Pool<Event>::Iterator& itReleaseEvent) {
433     RTList<uint>::Iterator iuiKey = pActiveKeys->first();
434     while (iuiKey) {
435     MidiKey* pKey = &pMIDIKeyInfo[*iuiKey];
436     ++iuiKey;
437     // append a 'release' event to the key's own event list
438     RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();
439     if (itNewEvent) {
440     *itNewEvent = *itReleaseEvent; // copy original event (to the key's event list)
441     itNewEvent->Type = Event::type_release; // transform event type
442     }
443     else dmsg(1,("Event pool emtpy!\n"));
444     }
445     }
446     /**
447     * Kill all active voices.
448     * @returns The number of voices.
449     */
450     int KillAllVoices(Pool<Event>::Iterator& itKillEvent) {
451     int count = 0;
452    
453     RTList<uint>::Iterator iuiKey = pActiveKeys->first();
454     RTList<uint>::Iterator end = pActiveKeys->end();
455     for (; iuiKey != end; ++iuiKey) { // iterate through all active keys
456     MidiKey* pKey = &pMIDIKeyInfo[*iuiKey];
457     RTListVoiceIterator itVoice = pKey->pActiveVoices->first();
458     RTListVoiceIterator itVoicesEnd = pKey->pActiveVoices->end();
459     for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key
460     itVoice->Kill(itKillEvent);
461     count++;
462     }
463     }
464    
465     return count;
466     }
467    
468     /**
469     * Kill all voices the *die hard* way.
470     * @returns The number of pending stream deletions
471     */
472     int KillAllVoicesImmediately() {
473     int iPendingStreamDeletions = 0;
474    
475     RTList<uint>::Iterator iuiKey = pActiveKeys->first();
476     RTList<uint>::Iterator end = pActiveKeys->end();
477     for (; iuiKey != end; ++iuiKey) { // iterate through all active keys
478     MidiKey* pKey = &pMIDIKeyInfo[*iuiKey];
479     RTListVoiceIterator itVoice = pKey->pActiveVoices->first();
480     RTListVoiceIterator itVoicesEnd = pKey->pActiveVoices->end();
481     for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key
482     // request a notification from disk thread side for stream deletion
483     const Stream::Handle hStream = itVoice->KillImmediately(true);
484     if (hStream != Stream::INVALID_HANDLE) { // voice actually used a stream
485     iPendingStreamDeletions++;
486     }
487     // free the voice to the voice pool and update key info
488     FreeVoice(itVoice);
489     }
490     }
491    
492     return iPendingStreamDeletions;
493     }
494    
495     /**
496     * Mark all currently active voices as "orphans", which means that the regions and
497     * samples they use should be released to the instrument manager when the voices die.
498     */
499     void MarkAllActiveVoicesAsOrphans() {
500     RTList<uint>::Iterator iuiKey = pActiveKeys->first();
501     RTList<uint>::Iterator end = pActiveKeys->end();
502     for (; iuiKey != end; ++iuiKey) { // iterate through all active keys
503     MidiKey* pKey = &pMIDIKeyInfo[*iuiKey];
504     RTListVoiceIterator itVoice = pKey->pActiveVoices->first();
505     RTListVoiceIterator itVoicesEnd = pKey->pActiveVoices->end();
506     for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key
507     itVoice->Orphan = true;
508     }
509     }
510     }
511    
512     void ProcessActiveVoices(VoiceHandler* pVoiceHandler) {
513     if (pVoiceHandler == NULL) return;
514    
515     RTList<uint>::Iterator iuiKey = pActiveKeys->first();
516     RTList<uint>::Iterator end = pActiveKeys->end();
517     for (; iuiKey != end; ++iuiKey) { // iterate through all active keys
518     MidiKey* pKey = &pMIDIKeyInfo[*iuiKey];
519     if (!pVoiceHandler->Process(pKey)) continue;
520    
521     RTListVoiceIterator itVoice = pKey->pActiveVoices->first();
522     RTListVoiceIterator itVoicesEnd = pKey->pActiveVoices->end();
523     for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key
524     pVoiceHandler->Process(itVoice);
525     }
526     }
527     }
528    
529     void ProcessSustainPedalDown(Pool<Event>::Iterator& itEvent) {
530     // Cancel release process of all voices
531     RTList<uint>::Iterator iuiKey = pActiveKeys->first();
532     for (; iuiKey; ++iuiKey) {
533     MidiKey* pKey = &pMIDIKeyInfo[*iuiKey];
534     if (!pKey->KeyPressed) {
535     RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();
536     if (itNewEvent) {
537     *itNewEvent = *itEvent; // copy event to the key's own event list
538     itNewEvent->Type = Event::type_cancel_release; // transform event type
539     }
540     else dmsg(1,("Event pool emtpy!\n"));
541     }
542     }
543     }
544    
545     void ProcessSustainPedalUp(Pool<Event>::Iterator& itEvent) {
546     // release voices if their respective key is not pressed
547     RTList<uint>::Iterator iuiKey = pActiveKeys->first();
548     for (; iuiKey; ++iuiKey) {
549     MidiKey* pKey = &pMIDIKeyInfo[*iuiKey];
550     if (!pKey->KeyPressed && ShouldReleaseVoice(*iuiKey)) {
551     RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();
552     if (itNewEvent) {
553     *itNewEvent = *itEvent; // copy event to the key's own event list
554     itNewEvent->Type = Event::type_release; // transform event type
555     }
556     else dmsg(1,("Event pool emtpy!\n"));
557     }
558     }
559     }
560    
561     /**
562     * Determines whether the specified voice should be released.
563     *
564     * @param pEngineChannel - The engine channel on which the voice should be checked
565     * @param Key - The key number
566     * @returns true if the specified voice should be released, false otherwise.
567     */
568     bool ShouldReleaseVoice(int Key) {
569     if (SustainPedal) return false;
570    
571     if (SostenutoPedal) {
572     for (int i = 0; i < SostenutoKeyCount; i++)
573     if (Key == SostenutoKeys[i]) return false;
574     }
575    
576     return true;
577     }
578    
579     void ProcessSostenutoPedalDown() {
580     SostenutoKeyCount = 0;
581     // Remeber the pressed keys
582     RTList<uint>::Iterator iuiKey = pActiveKeys->first();
583     for (; iuiKey; ++iuiKey) {
584     MidiKey* pKey = &pMIDIKeyInfo[*iuiKey];
585     if (pKey->KeyPressed && SostenutoKeyCount < 128) SostenutoKeys[SostenutoKeyCount++] = *iuiKey;
586     }
587     }
588    
589     void ProcessSostenutoPedalUp(Pool<Event>::Iterator& itEvent) {
590     // release voices if the damper pedal is up and their respective key is not pressed
591     for (int i = 0; i < SostenutoKeyCount; i++) {
592     MidiKey* pKey = &pMIDIKeyInfo[SostenutoKeys[i]];
593     if (!pKey->KeyPressed && !SustainPedal) {
594     RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();
595     if (itNewEvent) {
596     *itNewEvent = *itEvent; // copy event to the key's own event list
597     itNewEvent->Type = Event::type_release; // transform event type
598     }
599     else dmsg(1,("Event pool emtpy!\n"));
600     }
601     }
602     }
603    
604     void AddMidiKeyboardListener(MidiKeyboardListener* l) { listeners.AddListener(l); }
605    
606     void RemoveMidiKeyboardListener(MidiKeyboardListener* l) { listeners.RemoveListener(l); }
607    
608     protected:
609     class Listeners : public MidiKeyboardListener, public ListenerList<MidiKeyboardListener*> {
610     public:
611     REGISTER_FIRE_EVENT_METHOD_ARG2(PreProcessNoteOn, uint8_t, uint8_t)
612     REGISTER_FIRE_EVENT_METHOD_ARG2(PostProcessNoteOn, uint8_t, uint8_t)
613     REGISTER_FIRE_EVENT_METHOD_ARG2(PreProcessNoteOff, uint8_t, uint8_t)
614     REGISTER_FIRE_EVENT_METHOD_ARG2(PostProcessNoteOff, uint8_t, uint8_t)
615     REGISTER_FIRE_EVENT_METHOD(PreProcessSustainPedalUp)
616     REGISTER_FIRE_EVENT_METHOD(PostProcessSustainPedalUp)
617     REGISTER_FIRE_EVENT_METHOD(PreProcessSustainPedalDown)
618     REGISTER_FIRE_EVENT_METHOD(PostProcessSustainPedalDown)
619     REGISTER_FIRE_EVENT_METHOD(PreProcessSostenutoPedalUp)
620     REGISTER_FIRE_EVENT_METHOD(PostProcessSostenutoPedalUp)
621     REGISTER_FIRE_EVENT_METHOD(PreProcessSostenutoPedalDown)
622     REGISTER_FIRE_EVENT_METHOD(PostProcessSostenutoPedalDown)
623     } listeners;
624     };
625     } // namespace LinuxSampler
626    
627     #endif /* __LS_MIDIKEYBOARDMANAGER_H__ */
628    

  ViewVC Help
Powered by ViewVC