23 |
#include <sstream> |
#include <sstream> |
24 |
#include "DiskThread.h" |
#include "DiskThread.h" |
25 |
#include "Voice.h" |
#include "Voice.h" |
26 |
|
#include "EGADSR.h" |
27 |
|
|
28 |
#include "Engine.h" |
#include "Engine.h" |
29 |
|
#include <malloc.h> |
30 |
|
|
31 |
namespace LinuxSampler { namespace gig { |
namespace LinuxSampler { namespace gig { |
32 |
|
|
41 |
pEventGenerator = NULL; |
pEventGenerator = NULL; |
42 |
pSysexBuffer = new RingBuffer<uint8_t>(SYSEX_BUFFER_SIZE, 0); |
pSysexBuffer = new RingBuffer<uint8_t>(SYSEX_BUFFER_SIZE, 0); |
43 |
pEventQueue = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0); |
pEventQueue = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0); |
44 |
pEventPool = new RTELMemoryPool<Event>(MAX_EVENTS_PER_FRAGMENT); |
pEventPool = new Pool<Event>(MAX_EVENTS_PER_FRAGMENT); |
45 |
pVoicePool = new RTELMemoryPool<Voice>(MAX_AUDIO_VOICES); |
pVoicePool = new Pool<Voice>(MAX_AUDIO_VOICES); |
46 |
pActiveKeys = new RTELMemoryPool<uint>(128); |
pActiveKeys = new Pool<uint>(128); |
47 |
pVoiceStealingQueue = new RTEList<Event>(pEventPool); |
pVoiceStealingQueue = new RTList<Event>(pEventPool); |
48 |
pEvents = new RTEList<Event>(pEventPool); |
pEvents = new RTList<Event>(pEventPool); |
49 |
pCCEvents = new RTEList<Event>(pEventPool); |
pCCEvents = new RTList<Event>(pEventPool); |
50 |
for (uint i = 0; i < Event::destination_count; i++) { |
for (uint i = 0; i < Event::destination_count; i++) { |
51 |
pSynthesisEvents[i] = new RTEList<Event>(pEventPool); |
pSynthesisEvents[i] = new RTList<Event>(pEventPool); |
52 |
} |
} |
53 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
54 |
pMIDIKeyInfo[i].pActiveVoices = new RTEList<Voice>(pVoicePool); |
pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pVoicePool); |
55 |
pMIDIKeyInfo[i].KeyPressed = false; |
pMIDIKeyInfo[i].KeyPressed = false; |
56 |
pMIDIKeyInfo[i].Active = false; |
pMIDIKeyInfo[i].Active = false; |
57 |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
58 |
pMIDIKeyInfo[i].pSelf = NULL; |
pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEventPool); |
|
pMIDIKeyInfo[i].pEvents = new RTEList<Event>(pEventPool); |
|
59 |
} |
} |
60 |
for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) { |
for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { |
61 |
pVoice->SetEngine(this); |
iterVoice->SetEngine(this); |
62 |
} |
} |
63 |
pVoicePool->clear(); |
pVoicePool->clear(); |
64 |
|
|
77 |
|
|
78 |
Engine::~Engine() { |
Engine::~Engine() { |
79 |
if (pDiskThread) { |
if (pDiskThread) { |
80 |
|
dmsg(1,("Stopping disk thread...")); |
81 |
pDiskThread->StopThread(); |
pDiskThread->StopThread(); |
82 |
delete pDiskThread; |
delete pDiskThread; |
83 |
|
dmsg(1,("OK\n")); |
84 |
} |
} |
85 |
if (pGig) delete pGig; |
if (pGig) delete pGig; |
86 |
if (pRIFF) delete pRIFF; |
if (pRIFF) delete pRIFF; |
91 |
for (uint i = 0; i < Event::destination_count; i++) { |
for (uint i = 0; i < Event::destination_count; i++) { |
92 |
if (pSynthesisEvents[i]) delete pSynthesisEvents[i]; |
if (pSynthesisEvents[i]) delete pSynthesisEvents[i]; |
93 |
} |
} |
|
delete[] pSynthesisEvents; |
|
94 |
if (pEvents) delete pEvents; |
if (pEvents) delete pEvents; |
95 |
if (pCCEvents) delete pCCEvents; |
if (pCCEvents) delete pCCEvents; |
96 |
if (pEventQueue) delete pEventQueue; |
if (pEventQueue) delete pEventQueue; |
97 |
if (pEventPool) delete pEventPool; |
if (pEventPool) delete pEventPool; |
98 |
if (pVoicePool) delete pVoicePool; |
if (pVoicePool) { |
99 |
|
pVoicePool->clear(); |
100 |
|
delete pVoicePool; |
101 |
|
} |
102 |
if (pActiveKeys) delete pActiveKeys; |
if (pActiveKeys) delete pActiveKeys; |
103 |
if (pSysexBuffer) delete pSysexBuffer; |
if (pSysexBuffer) delete pSysexBuffer; |
104 |
if (pEventGenerator) delete pEventGenerator; |
if (pEventGenerator) delete pEventGenerator; |
105 |
if (pMainFilterParameters) delete[] pMainFilterParameters; |
if (pMainFilterParameters) delete[] pMainFilterParameters; |
106 |
if (pBasicFilterParameters) delete[] pBasicFilterParameters; |
if (pBasicFilterParameters) delete[] pBasicFilterParameters; |
107 |
if (pSynthesisParameters[0]) delete[] pSynthesisParameters[0]; |
if (pSynthesisParameters[0]) free(pSynthesisParameters[0]); |
108 |
if (pVoiceStealingQueue) delete pVoiceStealingQueue; |
if (pVoiceStealingQueue) delete pVoiceStealingQueue; |
109 |
} |
} |
110 |
|
|
165 |
GlobalVolume = 1.0; |
GlobalVolume = 1.0; |
166 |
|
|
167 |
// reset voice stealing parameters |
// reset voice stealing parameters |
168 |
pLastStolenVoice = NULL; |
itLastStolenVoice = RTList<Voice>::Iterator(); |
169 |
puiLastStolenKey = NULL; |
iuiLastStolenKey = RTList<uint>::Iterator(); |
170 |
pVoiceStealingQueue->clear(); |
pVoiceStealingQueue->clear(); |
171 |
|
|
172 |
// reset to normal chromatic scale (means equal temper) |
// reset to normal chromatic scale (means equal temper) |
182 |
pMIDIKeyInfo[i].KeyPressed = false; |
pMIDIKeyInfo[i].KeyPressed = false; |
183 |
pMIDIKeyInfo[i].Active = false; |
pMIDIKeyInfo[i].Active = false; |
184 |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
185 |
pMIDIKeyInfo[i].pSelf = NULL; |
pMIDIKeyInfo[i].itSelf = Pool<uint>::Iterator(); |
186 |
} |
} |
187 |
|
|
188 |
// reset all key groups |
// reset all key groups |
190 |
for (; iter != ActiveKeyGroups.end(); iter++) iter->second = NULL; |
for (; iter != ActiveKeyGroups.end(); iter++) iter->second = NULL; |
191 |
|
|
192 |
// reset all voices |
// reset all voices |
193 |
for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) { |
for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { |
194 |
pVoice->Reset(); |
iterVoice->Reset(); |
195 |
} |
} |
196 |
pVoicePool->clear(); |
pVoicePool->clear(); |
197 |
|
|
319 |
this->MaxSamplesPerCycle = pAudioOutputDevice->MaxSamplesPerCycle(); |
this->MaxSamplesPerCycle = pAudioOutputDevice->MaxSamplesPerCycle(); |
320 |
this->SampleRate = pAudioOutputDevice->SampleRate(); |
this->SampleRate = pAudioOutputDevice->SampleRate(); |
321 |
|
|
322 |
|
// FIXME: audio drivers with varying fragment sizes might be a problem here |
323 |
|
MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * EG_MIN_RELEASE_TIME) - 1; |
324 |
|
if (MaxFadeOutPos < 0) |
325 |
|
throw LinuxSamplerException("EG_MIN_RELEASE_TIME in EGADSR.h to big for current audio fragment size / sampling rate!"); |
326 |
|
|
327 |
// (re)create disk thread |
// (re)create disk thread |
328 |
if (this->pDiskThread) { |
if (this->pDiskThread) { |
329 |
|
dmsg(1,("Stopping disk thread...")); |
330 |
this->pDiskThread->StopThread(); |
this->pDiskThread->StopThread(); |
331 |
delete this->pDiskThread; |
delete this->pDiskThread; |
332 |
|
dmsg(1,("OK\n")); |
333 |
} |
} |
334 |
this->pDiskThread = new DiskThread(((pAudioOut->MaxSamplesPerCycle() << MAX_PITCH) << 1) + 6); //FIXME: assuming stereo |
this->pDiskThread = new DiskThread(((pAudioOut->MaxSamplesPerCycle() << MAX_PITCH) << 1) + 6); //FIXME: assuming stereo |
335 |
if (!pDiskThread) { |
if (!pDiskThread) { |
337 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
338 |
} |
} |
339 |
|
|
340 |
for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) { |
for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { |
341 |
pVoice->pDiskThread = this->pDiskThread; |
iterVoice->pDiskThread = this->pDiskThread; |
342 |
dmsg(3,("d")); |
dmsg(3,("d")); |
343 |
} |
} |
344 |
pVoicePool->clear(); |
pVoicePool->clear(); |
348 |
pEventGenerator = new EventGenerator(pAudioOut->SampleRate()); |
pEventGenerator = new EventGenerator(pAudioOut->SampleRate()); |
349 |
|
|
350 |
// (re)allocate synthesis parameter matrix |
// (re)allocate synthesis parameter matrix |
351 |
if (pSynthesisParameters[0]) delete[] pSynthesisParameters[0]; |
if (pSynthesisParameters[0]) free(pSynthesisParameters[0]); |
352 |
pSynthesisParameters[0] = new float[Event::destination_count * pAudioOut->MaxSamplesPerCycle()]; |
pSynthesisParameters[0] = (float *) memalign(16,(Event::destination_count * sizeof(float) * pAudioOut->MaxSamplesPerCycle())); |
353 |
for (int dst = 1; dst < Event::destination_count; dst++) |
for (int dst = 1; dst < Event::destination_count; dst++) |
354 |
pSynthesisParameters[dst] = pSynthesisParameters[dst - 1] + pAudioOut->MaxSamplesPerCycle(); |
pSynthesisParameters[dst] = pSynthesisParameters[dst - 1] + pAudioOut->MaxSamplesPerCycle(); |
355 |
|
|
363 |
pDiskThread->StartThread(); |
pDiskThread->StartThread(); |
364 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
365 |
|
|
366 |
for (Voice* pVoice = pVoicePool->first(); pVoice; pVoice = pVoicePool->next()) { |
for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { |
367 |
if (!pVoice->pDiskThread) { |
if (!iterVoice->pDiskThread) { |
368 |
dmsg(0,("Engine -> voice::trigger: !pDiskThread\n")); |
dmsg(0,("Engine -> voice::trigger: !pDiskThread\n")); |
369 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
370 |
} |
} |
405 |
} |
} |
406 |
|
|
407 |
|
|
408 |
|
// update time of start and end of this audio fragment (as events' time stamps relate to this) |
409 |
|
pEventGenerator->UpdateFragmentTime(Samples); |
410 |
|
|
411 |
|
|
412 |
// empty the event lists for the new fragment |
// empty the event lists for the new fragment |
413 |
pEvents->clear(); |
pEvents->clear(); |
414 |
pCCEvents->clear(); |
pCCEvents->clear(); |
415 |
for (uint i = 0; i < Event::destination_count; i++) { |
for (uint i = 0; i < Event::destination_count; i++) { |
416 |
pSynthesisEvents[i]->clear(); |
pSynthesisEvents[i]->clear(); |
417 |
} |
} |
418 |
for (uint* puiKey = pActiveKeys->first(); puiKey; puiKey = pActiveKeys->next()) { |
{ |
419 |
midi_key_info_t* pKey = &pMIDIKeyInfo[*puiKey]; |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
420 |
pKey->pEvents->clear(); // free all events on the key |
RTList<uint>::Iterator end = pActiveKeys->end(); |
421 |
} |
for(; iuiKey != end; ++iuiKey) { |
422 |
|
pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key |
423 |
// read and copy events from input queue |
} |
|
Event event = pEventGenerator->CreateEvent(); |
|
|
while (true) { |
|
|
if (!pEventQueue->pop(&event)) break; |
|
|
pEvents->alloc_assign(event); |
|
424 |
} |
} |
425 |
|
|
426 |
|
|
427 |
// update time of start and end of this audio fragment (as events' time stamps relate to this) |
// get all events from the input event queue which belong to the current fragment |
428 |
pEventGenerator->UpdateFragmentTime(Samples); |
{ |
429 |
|
RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
430 |
|
Event* pEvent; |
431 |
|
while (true) { |
432 |
|
// get next event from input event queue |
433 |
|
if (!(pEvent = eventQueueReader.pop())) break; |
434 |
|
// if younger event reached, ignore that and all subsequent ones for now |
435 |
|
if (pEvent->FragmentPos() >= Samples) { |
436 |
|
eventQueueReader--; |
437 |
|
dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples)); |
438 |
|
pEvent->ResetFragmentPos(); |
439 |
|
break; |
440 |
|
} |
441 |
|
// copy event to internal event list |
442 |
|
if (pEvents->poolIsEmpty()) { |
443 |
|
dmsg(1,("Event pool emtpy!\n")); |
444 |
|
break; |
445 |
|
} |
446 |
|
*pEvents->allocAppend() = *pEvent; |
447 |
|
} |
448 |
|
eventQueueReader.free(); // free all copied events from input queue |
449 |
|
} |
450 |
|
|
451 |
|
|
452 |
// process events |
// process events |
453 |
Event* pNextEvent = pEvents->first(); |
{ |
454 |
while (pNextEvent) { |
RTList<Event>::Iterator itEvent = pEvents->first(); |
455 |
Event* pEvent = pNextEvent; |
RTList<Event>::Iterator end = pEvents->end(); |
456 |
pEvents->set_current(pEvent); |
for (; itEvent != end; ++itEvent) { |
457 |
pNextEvent = pEvents->next(); |
switch (itEvent->Type) { |
458 |
switch (pEvent->Type) { |
case Event::type_note_on: |
459 |
case Event::type_note_on: |
dmsg(5,("Engine: Note on received\n")); |
460 |
dmsg(5,("Engine: Note on received\n")); |
ProcessNoteOn(itEvent); |
461 |
ProcessNoteOn(pEvent); |
break; |
462 |
break; |
case Event::type_note_off: |
463 |
case Event::type_note_off: |
dmsg(5,("Engine: Note off received\n")); |
464 |
dmsg(5,("Engine: Note off received\n")); |
ProcessNoteOff(itEvent); |
465 |
ProcessNoteOff(pEvent); |
break; |
466 |
break; |
case Event::type_control_change: |
467 |
case Event::type_control_change: |
dmsg(5,("Engine: MIDI CC received\n")); |
468 |
dmsg(5,("Engine: MIDI CC received\n")); |
ProcessControlChange(itEvent); |
469 |
ProcessControlChange(pEvent); |
break; |
470 |
break; |
case Event::type_pitchbend: |
471 |
case Event::type_pitchbend: |
dmsg(5,("Engine: Pitchbend received\n")); |
472 |
dmsg(5,("Engine: Pitchbend received\n")); |
ProcessPitchbend(itEvent); |
473 |
ProcessPitchbend(pEvent); |
break; |
474 |
break; |
case Event::type_sysex: |
475 |
case Event::type_sysex: |
dmsg(5,("Engine: Sysex received\n")); |
476 |
dmsg(5,("Engine: Sysex received\n")); |
ProcessSysex(itEvent); |
477 |
ProcessSysex(pEvent); |
break; |
478 |
break; |
} |
479 |
} |
} |
480 |
} |
} |
481 |
|
|
482 |
|
|
|
// render audio from all active voices |
|
483 |
int active_voices = 0; |
int active_voices = 0; |
484 |
uint* piKey = pActiveKeys->first(); |
|
485 |
while (piKey) { // iterate through all active keys |
// render audio from all active voices |
486 |
midi_key_info_t* pKey = &pMIDIKeyInfo[*piKey]; |
{ |
487 |
pActiveKeys->set_current(piKey); |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
488 |
piKey = pActiveKeys->next(); |
RTList<uint>::Iterator end = pActiveKeys->end(); |
489 |
|
while (iuiKey != end) { // iterate through all active keys |
490 |
Voice* pVoiceNext = pKey->pActiveVoices->first(); |
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
491 |
while (pVoiceNext) { // iterate through all voices on this key |
++iuiKey; |
492 |
// already get next voice on key |
|
493 |
Voice* pVoice = pVoiceNext; |
RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first(); |
494 |
pKey->pActiveVoices->set_current(pVoice); |
RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end(); |
495 |
pVoiceNext = pKey->pActiveVoices->next(); |
for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key |
496 |
|
// now render current voice |
497 |
// now render current voice |
itVoice->Render(Samples); |
498 |
pVoice->Render(Samples); |
if (itVoice->IsActive()) active_voices++; // still active |
499 |
if (pVoice->IsActive()) active_voices++; // still active |
else { // voice reached end, is now inactive |
500 |
else { // voice reached end, is now inactive |
FreeVoice(itVoice); // remove voice from the list of active voices |
501 |
KillVoiceImmediately(pVoice); // remove voice from the list of active voices |
} |
502 |
} |
} |
503 |
} |
} |
504 |
} |
} |
505 |
|
|
506 |
|
|
507 |
// now render all postponed voices from voice stealing |
// now render all postponed voices from voice stealing |
508 |
Event* pVoiceStealEvent = pVoiceStealingQueue->first(); |
{ |
509 |
while (pVoiceStealEvent) { |
RTList<Event>::Iterator itVoiceStealEvent = pVoiceStealingQueue->first(); |
510 |
Voice* pNewVoice = LaunchVoice(pVoiceStealEvent, pVoiceStealEvent->Param.Note.Layer, pVoiceStealEvent->Param.Note.ReleaseTrigger, false); |
RTList<Event>::Iterator end = pVoiceStealingQueue->end(); |
511 |
if (pNewVoice) { |
for (; itVoiceStealEvent != end; ++itVoiceStealEvent) { |
512 |
pNewVoice->Render(Samples); |
Pool<Voice>::Iterator itNewVoice = LaunchVoice(itVoiceStealEvent, itVoiceStealEvent->Param.Note.Layer, itVoiceStealEvent->Param.Note.ReleaseTrigger, false); |
513 |
if (pNewVoice->IsActive()) active_voices++; // still active |
if (itNewVoice) { |
514 |
else { // voice reached end, is now inactive |
for (; itNewVoice; itNewVoice = itNewVoice->itChildVoice) { |
515 |
KillVoiceImmediately(pNewVoice); // remove voice from the list of active voices |
itNewVoice->Render(Samples); |
516 |
|
if (itNewVoice->IsActive()) active_voices++; // still active |
517 |
|
else { // voice reached end, is now inactive |
518 |
|
FreeVoice(itNewVoice); // remove voice from the list of active voices |
519 |
|
} |
520 |
|
} |
521 |
} |
} |
522 |
|
else dmsg(1,("gig::Engine: ERROR, voice stealing didn't work out!\n")); |
523 |
} |
} |
|
else dmsg(1,("Ouch, voice stealing didn't work out!\n")); |
|
|
pVoiceStealEvent = pVoiceStealingQueue->next(); |
|
524 |
} |
} |
525 |
// reset voice stealing for the new fragment |
// reset voice stealing for the new fragment |
526 |
pVoiceStealingQueue->clear(); |
pVoiceStealingQueue->clear(); |
527 |
pLastStolenVoice = NULL; |
itLastStolenVoice = RTList<Voice>::Iterator(); |
528 |
puiLastStolenKey = NULL; |
iuiLastStolenKey = RTList<uint>::Iterator(); |
529 |
|
|
530 |
|
|
531 |
|
// free all keys which have no active voices left |
532 |
|
{ |
533 |
|
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
534 |
|
RTList<uint>::Iterator end = pActiveKeys->end(); |
535 |
|
while (iuiKey != end) { // iterate through all active keys |
536 |
|
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
537 |
|
++iuiKey; |
538 |
|
if (pKey->pActiveVoices->isEmpty()) FreeKey(pKey); |
539 |
|
#if DEVMODE |
540 |
|
else { // FIXME: should be removed before the final release (purpose: just a sanity check for debugging) |
541 |
|
RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first(); |
542 |
|
RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end(); |
543 |
|
for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key |
544 |
|
if (itVoice->itKillEvent) { |
545 |
|
dmsg(1,("gig::Engine: ERROR, killed voice survived !!!\n")); |
546 |
|
} |
547 |
|
} |
548 |
|
} |
549 |
|
#endif // DEVMODE |
550 |
|
} |
551 |
|
} |
552 |
|
|
553 |
|
|
554 |
// write that to the disk thread class so that it can print it |
// write that to the disk thread class so that it can print it |
656 |
/** |
/** |
657 |
* Assigns and triggers a new voice for the respective MIDI key. |
* Assigns and triggers a new voice for the respective MIDI key. |
658 |
* |
* |
659 |
* @param pNoteOnEvent - key, velocity and time stamp of the event |
* @param itNoteOnEvent - key, velocity and time stamp of the event |
660 |
*/ |
*/ |
661 |
void Engine::ProcessNoteOn(Event* pNoteOnEvent) { |
void Engine::ProcessNoteOn(Pool<Event>::Iterator& itNoteOnEvent) { |
662 |
midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOnEvent->Param.Note.Key]; |
midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
663 |
|
|
664 |
pKey->KeyPressed = true; // the MIDI key was now pressed down |
pKey->KeyPressed = true; // the MIDI key was now pressed down |
665 |
|
|
666 |
// cancel release process of voices on this key if needed |
// cancel release process of voices on this key if needed |
667 |
if (pKey->Active && !SustainPedal) { |
if (pKey->Active && !SustainPedal) { |
668 |
Event* pCancelReleaseEvent = pKey->pEvents->alloc(); |
RTList<Event>::Iterator itCancelReleaseEvent = pKey->pEvents->allocAppend(); |
669 |
if (pCancelReleaseEvent) { |
if (itCancelReleaseEvent) { |
670 |
*pCancelReleaseEvent = *pNoteOnEvent; |
*itCancelReleaseEvent = *itNoteOnEvent; // copy event |
671 |
pCancelReleaseEvent->Type = Event::type_cancel_release; // transform event type |
itCancelReleaseEvent->Type = Event::type_cancel_release; // transform event type |
672 |
} |
} |
673 |
else dmsg(1,("Event pool emtpy!\n")); |
else dmsg(1,("Event pool emtpy!\n")); |
674 |
} |
} |
675 |
|
|
676 |
// allocate and trigger a new voice for the key |
// move note on event to the key's own event list |
677 |
LaunchVoice(pNoteOnEvent); |
RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents); |
678 |
|
|
679 |
// finally move note on event to the key's own event list |
// allocate and trigger a new voice for the key |
680 |
pEvents->move(pNoteOnEvent, pKey->pEvents); |
LaunchVoice(itNoteOnEventOnKeyList, 0, false, true); |
681 |
} |
} |
682 |
|
|
683 |
/** |
/** |
686 |
* sustain pedal will be released or voice turned inactive by itself (e.g. |
* sustain pedal will be released or voice turned inactive by itself (e.g. |
687 |
* due to completion of sample playback). |
* due to completion of sample playback). |
688 |
* |
* |
689 |
* @param pNoteOffEvent - key, velocity and time stamp of the event |
* @param itNoteOffEvent - key, velocity and time stamp of the event |
690 |
*/ |
*/ |
691 |
void Engine::ProcessNoteOff(Event* pNoteOffEvent) { |
void Engine::ProcessNoteOff(Pool<Event>::Iterator& itNoteOffEvent) { |
692 |
midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOffEvent->Param.Note.Key]; |
midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key]; |
693 |
|
|
694 |
pKey->KeyPressed = false; // the MIDI key was now released |
pKey->KeyPressed = false; // the MIDI key was now released |
695 |
|
|
696 |
// release voices on this key if needed |
// release voices on this key if needed |
697 |
if (pKey->Active && !SustainPedal) { |
if (pKey->Active && !SustainPedal) { |
698 |
pNoteOffEvent->Type = Event::type_release; // transform event type |
itNoteOffEvent->Type = Event::type_release; // transform event type |
699 |
} |
} |
700 |
|
|
701 |
|
// move event to the key's own event list |
702 |
|
RTList<Event>::Iterator itNoteOffEventOnKeyList = itNoteOffEvent.moveToEndOf(pKey->pEvents); |
703 |
|
|
704 |
// spawn release triggered voice(s) if needed |
// spawn release triggered voice(s) if needed |
705 |
if (pKey->ReleaseTrigger) { |
if (pKey->ReleaseTrigger) { |
706 |
LaunchVoice(pNoteOffEvent, 0, true); |
LaunchVoice(itNoteOffEventOnKeyList, 0, true, false); //FIXME: for the moment we don't perform voice stealing for release triggered samples |
707 |
pKey->ReleaseTrigger = false; |
pKey->ReleaseTrigger = false; |
708 |
} |
} |
|
|
|
|
// move event to the key's own event list |
|
|
pEvents->move(pNoteOffEvent, pKey->pEvents); |
|
709 |
} |
} |
710 |
|
|
711 |
/** |
/** |
712 |
* Moves pitchbend event from the general (input) event list to the pitch |
* Moves pitchbend event from the general (input) event list to the pitch |
713 |
* event list. |
* event list. |
714 |
* |
* |
715 |
* @param pPitchbendEvent - absolute pitch value and time stamp of the event |
* @param itPitchbendEvent - absolute pitch value and time stamp of the event |
716 |
*/ |
*/ |
717 |
void Engine::ProcessPitchbend(Event* pPitchbendEvent) { |
void Engine::ProcessPitchbend(Pool<Event>::Iterator& itPitchbendEvent) { |
718 |
this->Pitch = pPitchbendEvent->Param.Pitch.Pitch; // store current pitch value |
this->Pitch = itPitchbendEvent->Param.Pitch.Pitch; // store current pitch value |
719 |
pEvents->move(pPitchbendEvent, pSynthesisEvents[Event::destination_vco]); |
itPitchbendEvent.moveToEndOf(pSynthesisEvents[Event::destination_vco]); |
720 |
} |
} |
721 |
|
|
722 |
/** |
/** |
724 |
* called by the ProcessNoteOn() method and by the voices itself |
* called by the ProcessNoteOn() method and by the voices itself |
725 |
* (e.g. to spawn further voices on the same key for layered sounds). |
* (e.g. to spawn further voices on the same key for layered sounds). |
726 |
* |
* |
727 |
* @param pNoteOnEvent - key, velocity and time stamp of the event |
* @param itNoteOnEvent - key, velocity and time stamp of the event |
728 |
* @param iLayer - layer index for the new voice (optional - only |
* @param iLayer - layer index for the new voice (optional - only |
729 |
* in case of layered sounds of course) |
* in case of layered sounds of course) |
730 |
* @param ReleaseTriggerVoice - if new voice is a release triggered voice |
* @param ReleaseTriggerVoice - if new voice is a release triggered voice |
735 |
* @returns pointer to new voice or NULL if there was no free voice or |
* @returns pointer to new voice or NULL if there was no free voice or |
736 |
* if an error occured while trying to trigger the new voice |
* if an error occured while trying to trigger the new voice |
737 |
*/ |
*/ |
738 |
Voice* Engine::LaunchVoice(Event* pNoteOnEvent, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing) { |
Pool<Voice>::Iterator Engine::LaunchVoice(Pool<Event>::Iterator& itNoteOnEvent, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing) { |
739 |
midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOnEvent->Param.Note.Key]; |
midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
740 |
|
|
741 |
// allocate a new voice for the key |
// allocate a new voice for the key |
742 |
Voice* pNewVoice = pKey->pActiveVoices->alloc(); |
Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend(); |
743 |
if (pNewVoice) { |
if (itNewVoice) { |
744 |
// launch the new voice |
// launch the new voice |
745 |
if (pNewVoice->Trigger(pNoteOnEvent, this->Pitch, this->pInstrument, iLayer, ReleaseTriggerVoice) < 0) { |
if (itNewVoice->Trigger(itNoteOnEvent, this->Pitch, this->pInstrument, iLayer, ReleaseTriggerVoice, VoiceStealing) < 0) { |
746 |
dmsg(1,("Triggering new voice failed!\n")); |
dmsg(1,("Triggering new voice failed!\n")); |
747 |
pKey->pActiveVoices->free(pNewVoice); |
pKey->pActiveVoices->free(itNewVoice); |
748 |
} |
} |
749 |
else { // on success |
else { // on success |
750 |
uint** ppKeyGroup = NULL; |
uint** ppKeyGroup = NULL; |
751 |
if (pNewVoice->KeyGroup) { // if this voice / key belongs to a key group |
if (itNewVoice->KeyGroup) { // if this voice / key belongs to a key group |
752 |
ppKeyGroup = &ActiveKeyGroups[pNewVoice->KeyGroup]; |
ppKeyGroup = &ActiveKeyGroups[itNewVoice->KeyGroup]; |
753 |
if (*ppKeyGroup) { // if there's already an active key in that key group |
if (*ppKeyGroup) { // if there's already an active key in that key group |
754 |
midi_key_info_t* pOtherKey = &pMIDIKeyInfo[**ppKeyGroup]; |
midi_key_info_t* pOtherKey = &pMIDIKeyInfo[**ppKeyGroup]; |
755 |
// kill all voices on the (other) key |
// kill all voices on the (other) key |
756 |
Voice* pVoiceToBeKilled = pOtherKey->pActiveVoices->first(); |
RTList<Voice>::Iterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first(); |
757 |
while (pVoiceToBeKilled) { |
RTList<Voice>::Iterator end = pOtherKey->pActiveVoices->end(); |
758 |
Voice* pVoiceToBeKilledNext = pOtherKey->pActiveVoices->next(); |
for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) { |
759 |
if (pVoiceToBeKilled->Type != Voice::type_release_trigger) pVoiceToBeKilled->Kill(pNoteOnEvent); |
if (itVoiceToBeKilled->Type != Voice::type_release_trigger) itVoiceToBeKilled->Kill(itNoteOnEvent); |
|
pOtherKey->pActiveVoices->set_current(pVoiceToBeKilled); |
|
|
pVoiceToBeKilled = pVoiceToBeKilledNext; |
|
760 |
} |
} |
761 |
} |
} |
762 |
} |
} |
763 |
if (!pKey->Active) { // mark as active key |
if (!pKey->Active) { // mark as active key |
764 |
pKey->Active = true; |
pKey->Active = true; |
765 |
pKey->pSelf = pActiveKeys->alloc(); |
pKey->itSelf = pActiveKeys->allocAppend(); |
766 |
*pKey->pSelf = pNoteOnEvent->Param.Note.Key; |
*pKey->itSelf = itNoteOnEvent->Param.Note.Key; |
767 |
} |
} |
768 |
if (pNewVoice->KeyGroup) { |
if (itNewVoice->KeyGroup) { |
769 |
*ppKeyGroup = pKey->pSelf; // put key as the (new) active key to its key group |
*ppKeyGroup = &*pKey->itSelf; // put key as the (new) active key to its key group |
770 |
} |
} |
771 |
if (pNewVoice->Type == Voice::type_release_trigger_required) pKey->ReleaseTrigger = true; // mark key for the need of release triggered voice(s) |
if (itNewVoice->Type == Voice::type_release_trigger_required) pKey->ReleaseTrigger = true; // mark key for the need of release triggered voice(s) |
772 |
return pNewVoice; // success |
return itNewVoice; // success |
773 |
|
} |
774 |
|
} |
775 |
|
else if (VoiceStealing) { |
776 |
|
// first, get total amount of required voices (dependant on amount of layers) |
777 |
|
::gig::Region* pRegion = pInstrument->GetRegion(itNoteOnEvent->Param.Note.Key); |
778 |
|
if (!pRegion) return Pool<Voice>::Iterator(); // nothing defined for this MIDI key, so no voice needed |
779 |
|
int voicesRequired = pRegion->Layers; |
780 |
|
|
781 |
|
// now steal the (remaining) amount of voices |
782 |
|
for (int i = iLayer; i < voicesRequired; i++) |
783 |
|
StealVoice(itNoteOnEvent); |
784 |
|
|
785 |
|
// put note-on event into voice-stealing queue, so it will be reprocessed after killed voice died |
786 |
|
RTList<Event>::Iterator itStealEvent = pVoiceStealingQueue->allocAppend(); |
787 |
|
if (itStealEvent) { |
788 |
|
*itStealEvent = *itNoteOnEvent; // copy event |
789 |
|
itStealEvent->Param.Note.Layer = iLayer; |
790 |
|
itStealEvent->Param.Note.ReleaseTrigger = ReleaseTriggerVoice; |
791 |
} |
} |
792 |
|
else dmsg(1,("Voice stealing queue full!\n")); |
793 |
} |
} |
|
else if (VoiceStealing) StealVoice(pNoteOnEvent, iLayer, ReleaseTriggerVoice); // no free voice left, so steal one |
|
794 |
|
|
795 |
return NULL; // no free voice or error |
return Pool<Voice>::Iterator(); // no free voice or error |
796 |
} |
} |
797 |
|
|
798 |
/** |
/** |
801 |
* voice stealing and postpone the note-on event until the selected |
* voice stealing and postpone the note-on event until the selected |
802 |
* voice actually died. |
* voice actually died. |
803 |
* |
* |
804 |
* @param pNoteOnEvent - key, velocity and time stamp of the event |
* @param itNoteOnEvent - key, velocity and time stamp of the event |
|
* @param iLayer - layer index for the new voice |
|
|
* @param ReleaseTriggerVoice - if new voice is a release triggered voice |
|
805 |
*/ |
*/ |
806 |
void Engine::StealVoice(Event* pNoteOnEvent, int iLayer, bool ReleaseTriggerVoice) { |
void Engine::StealVoice(Pool<Event>::Iterator& itNoteOnEvent) { |
807 |
if (!pEventPool->pool_is_empty()) { |
if (!pEventPool->poolIsEmpty()) { |
808 |
|
|
809 |
uint* puiOldestKey; |
RTList<uint>::Iterator iuiOldestKey; |
810 |
Voice* pOldestVoice; |
RTList<Voice>::Iterator itOldestVoice; |
811 |
|
|
812 |
// Select one voice for voice stealing |
// Select one voice for voice stealing |
813 |
switch (VOICE_STEAL_ALGORITHM) { |
switch (VOICE_STEAL_ALGORITHM) { |
817 |
// key, or no voice left to kill there, then procceed with |
// key, or no voice left to kill there, then procceed with |
818 |
// 'oldestkey' algorithm |
// 'oldestkey' algorithm |
819 |
case voice_steal_algo_keymask: { |
case voice_steal_algo_keymask: { |
820 |
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[pNoteOnEvent->Param.Note.Key]; |
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
821 |
if (pLastStolenVoice) { |
if (itLastStolenVoice) { |
822 |
pOldestKey->pActiveVoices->set_current(pLastStolenVoice); |
itOldestVoice = itLastStolenVoice; |
823 |
pOldestVoice = pOldestKey->pActiveVoices->next(); |
++itOldestVoice; |
824 |
} |
} |
825 |
else { // no voice stolen in this audio fragment cycle yet |
else { // no voice stolen in this audio fragment cycle yet |
826 |
pOldestVoice = pOldestKey->pActiveVoices->first(); |
itOldestVoice = pOldestKey->pActiveVoices->first(); |
827 |
} |
} |
828 |
if (pOldestVoice) { |
if (itOldestVoice) { |
829 |
puiOldestKey = pOldestKey->pSelf; |
iuiOldestKey = pOldestKey->itSelf; |
830 |
break; // selection succeeded |
break; // selection succeeded |
831 |
} |
} |
832 |
} // no break - intentional ! |
} // no break - intentional ! |
834 |
// try to pick the oldest voice on the oldest active key |
// try to pick the oldest voice on the oldest active key |
835 |
// (caution: must stay after 'keymask' algorithm !) |
// (caution: must stay after 'keymask' algorithm !) |
836 |
case voice_steal_algo_oldestkey: { |
case voice_steal_algo_oldestkey: { |
837 |
if (pLastStolenVoice) { |
if (itLastStolenVoice) { |
838 |
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*puiLastStolenKey]; |
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiLastStolenKey]; |
839 |
pOldestKey->pActiveVoices->set_current(pLastStolenVoice); |
itOldestVoice = itLastStolenVoice; |
840 |
pOldestVoice = pOldestKey->pActiveVoices->next(); |
++itOldestVoice; |
841 |
if (!pOldestVoice) { |
if (!itOldestVoice) { |
842 |
pActiveKeys->set_current(puiLastStolenKey); |
iuiOldestKey = iuiLastStolenKey; |
843 |
puiOldestKey = pActiveKeys->next(); |
++iuiOldestKey; |
844 |
if (puiOldestKey) { |
if (iuiOldestKey) { |
845 |
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*puiOldestKey]; |
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiOldestKey]; |
846 |
pOldestVoice = pOldestKey->pActiveVoices->first(); |
itOldestVoice = pOldestKey->pActiveVoices->first(); |
847 |
} |
} |
848 |
else { // too less voices, even for voice stealing |
else { |
849 |
dmsg(1,("Voice overflow! - You might recompile with higher MAX_AUDIO_VOICES!\n")); |
dmsg(1,("gig::Engine: Warning, too less voices, even for voice stealing! - Better recompile with higher MAX_AUDIO_VOICES.\n")); |
850 |
return; |
return; |
851 |
} |
} |
852 |
} |
} |
853 |
else puiOldestKey = puiLastStolenKey; |
else iuiOldestKey = iuiLastStolenKey; |
854 |
} |
} |
855 |
else { // no voice stolen in this audio fragment cycle yet |
else { // no voice stolen in this audio fragment cycle yet |
856 |
puiOldestKey = pActiveKeys->first(); |
iuiOldestKey = pActiveKeys->first(); |
857 |
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*puiOldestKey]; |
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiOldestKey]; |
858 |
pOldestVoice = pOldestKey->pActiveVoices->first(); |
itOldestVoice = pOldestKey->pActiveVoices->first(); |
859 |
} |
} |
860 |
break; |
break; |
861 |
} |
} |
868 |
} |
} |
869 |
} |
} |
870 |
|
|
871 |
|
//FIXME: can be removed, just a sanity check for debugging |
872 |
|
if (!itOldestVoice->IsActive()) dmsg(1,("gig::Engine: ERROR, tried to steal a voice which was not active !!!\n")); |
873 |
|
|
874 |
// now kill the selected voice |
// now kill the selected voice |
875 |
pOldestVoice->Kill(pNoteOnEvent); |
itOldestVoice->Kill(itNoteOnEvent); |
876 |
// remember which voice on which key we stole, so we can simply proceed for the next voice stealing |
// remember which voice on which key we stole, so we can simply proceed for the next voice stealing |
877 |
this->pLastStolenVoice = pOldestVoice; |
this->itLastStolenVoice = itOldestVoice; |
878 |
this->puiLastStolenKey = puiOldestKey; |
this->iuiLastStolenKey = iuiOldestKey; |
|
// put note-on event into voice-stealing queue, so it will be reprocessed after killed voice died |
|
|
Event* pStealEvent = pVoiceStealingQueue->alloc(); |
|
|
*pStealEvent = *pNoteOnEvent; |
|
|
pStealEvent->Param.Note.Layer = iLayer; |
|
|
pStealEvent->Param.Note.ReleaseTrigger = ReleaseTriggerVoice; |
|
879 |
} |
} |
880 |
else dmsg(1,("Event pool emtpy!\n")); |
else dmsg(1,("Event pool emtpy!\n")); |
881 |
} |
} |
882 |
|
|
883 |
/** |
/** |
884 |
* Immediately kills the voice given with pVoice (no matter if sustain is |
* Removes the given voice from the MIDI key's list of active voices. |
885 |
* pressed or not) and removes it from the MIDI key's list of active voice. |
* This method will be called when a voice went inactive, e.g. because |
886 |
* This method will e.g. be called if a voice went inactive by itself. |
* it finished to playback its sample, finished its release stage or |
887 |
|
* just was killed. |
888 |
* |
* |
889 |
* @param pVoice - points to the voice to be killed |
* @param itVoice - points to the voice to be freed |
890 |
*/ |
*/ |
891 |
void Engine::KillVoiceImmediately(Voice* pVoice) { |
void Engine::FreeVoice(Pool<Voice>::Iterator& itVoice) { |
892 |
if (pVoice) { |
if (itVoice) { |
893 |
if (pVoice->IsActive()) pVoice->KillImmediately(); |
midi_key_info_t* pKey = &pMIDIKeyInfo[itVoice->MIDIKey]; |
894 |
|
|
895 |
midi_key_info_t* pKey = &pMIDIKeyInfo[pVoice->MIDIKey]; |
uint keygroup = itVoice->KeyGroup; |
896 |
|
|
897 |
// free the voice object |
// free the voice object |
898 |
pVoicePool->free(pVoice); |
pVoicePool->free(itVoice); |
899 |
|
|
900 |
// check if there are no voices left on the MIDI key and update the key info if so |
// if no other voices left and member of a key group, remove from key group |
901 |
if (pKey->pActiveVoices->is_empty()) { |
if (pKey->pActiveVoices->isEmpty() && keygroup) { |
902 |
if (pVoice->KeyGroup) { // if voice / key belongs to a key group |
uint** ppKeyGroup = &ActiveKeyGroups[keygroup]; |
903 |
uint** ppKeyGroup = &ActiveKeyGroups[pVoice->KeyGroup]; |
if (*ppKeyGroup == &*pKey->itSelf) *ppKeyGroup = NULL; // remove key from key group |
|
if (*ppKeyGroup == pKey->pSelf) *ppKeyGroup = NULL; // remove key from key group |
|
|
} |
|
|
pKey->Active = false; |
|
|
pActiveKeys->free(pKey->pSelf); // remove key from list of active keys |
|
|
pKey->pSelf = NULL; |
|
|
pKey->ReleaseTrigger = false; |
|
|
pKey->pEvents->clear(); |
|
|
dmsg(3,("Key has no more voices now\n")); |
|
904 |
} |
} |
905 |
} |
} |
906 |
else std::cerr << "Couldn't release voice! (pVoice == NULL)\n" << std::flush; |
else std::cerr << "Couldn't release voice! (!itVoice)\n" << std::flush; |
907 |
|
} |
908 |
|
|
909 |
|
/** |
910 |
|
* Called when there's no more voice left on a key, this call will |
911 |
|
* update the key info respectively. |
912 |
|
* |
913 |
|
* @param pKey - key which is now inactive |
914 |
|
*/ |
915 |
|
void Engine::FreeKey(midi_key_info_t* pKey) { |
916 |
|
if (pKey->pActiveVoices->isEmpty()) { |
917 |
|
pKey->Active = false; |
918 |
|
pActiveKeys->free(pKey->itSelf); // remove key from list of active keys |
919 |
|
pKey->itSelf = RTList<uint>::Iterator(); |
920 |
|
pKey->ReleaseTrigger = false; |
921 |
|
pKey->pEvents->clear(); |
922 |
|
dmsg(3,("Key has no more voices now\n")); |
923 |
|
} |
924 |
|
else dmsg(1,("gig::Engine: Oops, tried to free a key which contains voices.\n")); |
925 |
} |
} |
926 |
|
|
927 |
/** |
/** |
928 |
* Reacts on supported control change commands (e.g. pitch bend wheel, |
* Reacts on supported control change commands (e.g. pitch bend wheel, |
929 |
* modulation wheel, aftertouch). |
* modulation wheel, aftertouch). |
930 |
* |
* |
931 |
* @param pControlChangeEvent - controller, value and time stamp of the event |
* @param itControlChangeEvent - controller, value and time stamp of the event |
932 |
*/ |
*/ |
933 |
void Engine::ProcessControlChange(Event* pControlChangeEvent) { |
void Engine::ProcessControlChange(Pool<Event>::Iterator& itControlChangeEvent) { |
934 |
dmsg(4,("Engine::ContinuousController cc=%d v=%d\n", pControlChangeEvent->Param.CC.Controller, pControlChangeEvent->Param.CC.Value)); |
dmsg(4,("Engine::ContinuousController cc=%d v=%d\n", itControlChangeEvent->Param.CC.Controller, itControlChangeEvent->Param.CC.Value)); |
935 |
|
|
936 |
switch (pControlChangeEvent->Param.CC.Controller) { |
switch (itControlChangeEvent->Param.CC.Controller) { |
937 |
case 64: { |
case 64: { |
938 |
if (pControlChangeEvent->Param.CC.Value >= 64 && !SustainPedal) { |
if (itControlChangeEvent->Param.CC.Value >= 64 && !SustainPedal) { |
939 |
dmsg(4,("PEDAL DOWN\n")); |
dmsg(4,("PEDAL DOWN\n")); |
940 |
SustainPedal = true; |
SustainPedal = true; |
941 |
|
|
942 |
// cancel release process of voices if necessary |
// cancel release process of voices if necessary |
943 |
uint* piKey = pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
944 |
if (piKey) { |
if (iuiKey) { |
945 |
pControlChangeEvent->Type = Event::type_cancel_release; // transform event type |
itControlChangeEvent->Type = Event::type_cancel_release; // transform event type |
946 |
while (piKey) { |
while (iuiKey) { |
947 |
midi_key_info_t* pKey = &pMIDIKeyInfo[*piKey]; |
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
948 |
pActiveKeys->set_current(piKey); |
++iuiKey; |
|
piKey = pActiveKeys->next(); |
|
949 |
if (!pKey->KeyPressed) { |
if (!pKey->KeyPressed) { |
950 |
Event* pNewEvent = pKey->pEvents->alloc(); |
RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend(); |
951 |
if (pNewEvent) *pNewEvent = *pControlChangeEvent; // copy event to the key's own event list |
if (itNewEvent) *itNewEvent = *itControlChangeEvent; // copy event to the key's own event list |
952 |
else dmsg(1,("Event pool emtpy!\n")); |
else dmsg(1,("Event pool emtpy!\n")); |
953 |
} |
} |
954 |
} |
} |
955 |
} |
} |
956 |
} |
} |
957 |
if (pControlChangeEvent->Param.CC.Value < 64 && SustainPedal) { |
if (itControlChangeEvent->Param.CC.Value < 64 && SustainPedal) { |
958 |
dmsg(4,("PEDAL UP\n")); |
dmsg(4,("PEDAL UP\n")); |
959 |
SustainPedal = false; |
SustainPedal = false; |
960 |
|
|
961 |
// release voices if their respective key is not pressed |
// release voices if their respective key is not pressed |
962 |
uint* piKey = pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
963 |
if (piKey) { |
if (iuiKey) { |
964 |
pControlChangeEvent->Type = Event::type_release; // transform event type |
itControlChangeEvent->Type = Event::type_release; // transform event type |
965 |
while (piKey) { |
while (iuiKey) { |
966 |
midi_key_info_t* pKey = &pMIDIKeyInfo[*piKey]; |
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
967 |
pActiveKeys->set_current(piKey); |
++iuiKey; |
|
piKey = pActiveKeys->next(); |
|
968 |
if (!pKey->KeyPressed) { |
if (!pKey->KeyPressed) { |
969 |
Event* pNewEvent = pKey->pEvents->alloc(); |
RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend(); |
970 |
if (pNewEvent) *pNewEvent = *pControlChangeEvent; // copy event to the key's own event list |
if (itNewEvent) *itNewEvent = *itControlChangeEvent; // copy event to the key's own event list |
971 |
else dmsg(1,("Event pool emtpy!\n")); |
else dmsg(1,("Event pool emtpy!\n")); |
972 |
} |
} |
973 |
} |
} |
978 |
} |
} |
979 |
|
|
980 |
// update controller value in the engine's controller table |
// update controller value in the engine's controller table |
981 |
ControllerTable[pControlChangeEvent->Param.CC.Controller] = pControlChangeEvent->Param.CC.Value; |
ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value; |
982 |
|
|
983 |
// move event from the unsorted event list to the control change event list |
// move event from the unsorted event list to the control change event list |
984 |
pEvents->move(pControlChangeEvent, pCCEvents); |
itControlChangeEvent.moveToEndOf(pCCEvents); |
985 |
} |
} |
986 |
|
|
987 |
/** |
/** |
988 |
* Reacts on MIDI system exclusive messages. |
* Reacts on MIDI system exclusive messages. |
989 |
* |
* |
990 |
* @param pSysexEvent - sysex data size and time stamp of the sysex event |
* @param itSysexEvent - sysex data size and time stamp of the sysex event |
991 |
*/ |
*/ |
992 |
void Engine::ProcessSysex(Event* pSysexEvent) { |
void Engine::ProcessSysex(Pool<Event>::Iterator& itSysexEvent) { |
993 |
RingBuffer<uint8_t>::NonVolatileReader reader = pSysexBuffer->get_non_volatile_reader(); |
RingBuffer<uint8_t>::NonVolatileReader reader = pSysexBuffer->get_non_volatile_reader(); |
994 |
|
|
995 |
uint8_t exclusive_status, id; |
uint8_t exclusive_status, id; |
1037 |
} |
} |
1038 |
|
|
1039 |
free_sysex_data: // finally free sysex data |
free_sysex_data: // finally free sysex data |
1040 |
pSysexBuffer->increment_read_ptr(pSysexEvent->Param.Sysex.Size); |
pSysexBuffer->increment_read_ptr(itSysexEvent->Param.Sysex.Size); |
1041 |
} |
} |
1042 |
|
|
1043 |
/** |
/** |
1171 |
} |
} |
1172 |
|
|
1173 |
String Engine::Version() { |
String Engine::Version() { |
1174 |
String s = "$Revision: 1.14 $"; |
String s = "$Revision: 1.20 $"; |
1175 |
return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword |
return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword |
1176 |
} |
} |
1177 |
|
|