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 |
|
|
40 |
pEventGenerator = NULL; |
pEventGenerator = NULL; |
41 |
pSysexBuffer = new RingBuffer<uint8_t>(SYSEX_BUFFER_SIZE, 0); |
pSysexBuffer = new RingBuffer<uint8_t>(SYSEX_BUFFER_SIZE, 0); |
42 |
pEventQueue = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0); |
pEventQueue = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0); |
43 |
pEventPool = new RTELMemoryPool<Event>(MAX_EVENTS_PER_FRAGMENT); |
pEventPool = new Pool<Event>(MAX_EVENTS_PER_FRAGMENT); |
44 |
pVoicePool = new RTELMemoryPool<Voice>(MAX_AUDIO_VOICES); |
pVoicePool = new Pool<Voice>(MAX_AUDIO_VOICES); |
45 |
pActiveKeys = new RTELMemoryPool<uint>(128); |
pActiveKeys = new Pool<uint>(128); |
46 |
pEvents = new RTEList<Event>(pEventPool); |
pVoiceStealingQueue = new RTList<Event>(pEventPool); |
47 |
pCCEvents = new RTEList<Event>(pEventPool); |
pEvents = new RTList<Event>(pEventPool); |
48 |
|
pCCEvents = new RTList<Event>(pEventPool); |
49 |
for (uint i = 0; i < Event::destination_count; i++) { |
for (uint i = 0; i < Event::destination_count; i++) { |
50 |
pSynthesisEvents[i] = new RTEList<Event>(pEventPool); |
pSynthesisEvents[i] = new RTList<Event>(pEventPool); |
51 |
} |
} |
52 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
53 |
pMIDIKeyInfo[i].pActiveVoices = new RTEList<Voice>(pVoicePool); |
pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pVoicePool); |
54 |
pMIDIKeyInfo[i].KeyPressed = false; |
pMIDIKeyInfo[i].KeyPressed = false; |
55 |
pMIDIKeyInfo[i].Active = false; |
pMIDIKeyInfo[i].Active = false; |
56 |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
57 |
pMIDIKeyInfo[i].pSelf = NULL; |
pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEventPool); |
|
pMIDIKeyInfo[i].pEvents = new RTEList<Event>(pEventPool); |
|
58 |
} |
} |
59 |
for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) { |
for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { |
60 |
pVoice->SetEngine(this); |
iterVoice->SetEngine(this); |
61 |
} |
} |
62 |
pVoicePool->clear(); |
pVoicePool->clear(); |
63 |
|
|
100 |
if (pMainFilterParameters) delete[] pMainFilterParameters; |
if (pMainFilterParameters) delete[] pMainFilterParameters; |
101 |
if (pBasicFilterParameters) delete[] pBasicFilterParameters; |
if (pBasicFilterParameters) delete[] pBasicFilterParameters; |
102 |
if (pSynthesisParameters[0]) delete[] pSynthesisParameters[0]; |
if (pSynthesisParameters[0]) delete[] pSynthesisParameters[0]; |
103 |
|
if (pVoiceStealingQueue) delete pVoiceStealingQueue; |
104 |
} |
} |
105 |
|
|
106 |
void Engine::Enable() { |
void Engine::Enable() { |
159 |
ActiveVoiceCountMax = 0; |
ActiveVoiceCountMax = 0; |
160 |
GlobalVolume = 1.0; |
GlobalVolume = 1.0; |
161 |
|
|
162 |
|
// reset voice stealing parameters |
163 |
|
itLastStolenVoice = RTList<Voice>::Iterator(); |
164 |
|
iuiLastStolenKey = RTList<uint>::Iterator(); |
165 |
|
pVoiceStealingQueue->clear(); |
166 |
|
|
167 |
// reset to normal chromatic scale (means equal temper) |
// reset to normal chromatic scale (means equal temper) |
168 |
memset(&ScaleTuning[0], 0x00, 12); |
memset(&ScaleTuning[0], 0x00, 12); |
169 |
|
|
177 |
pMIDIKeyInfo[i].KeyPressed = false; |
pMIDIKeyInfo[i].KeyPressed = false; |
178 |
pMIDIKeyInfo[i].Active = false; |
pMIDIKeyInfo[i].Active = false; |
179 |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
180 |
pMIDIKeyInfo[i].pSelf = NULL; |
pMIDIKeyInfo[i].itSelf = Pool<uint>::Iterator(); |
181 |
} |
} |
182 |
|
|
183 |
// reset all key groups |
// reset all key groups |
185 |
for (; iter != ActiveKeyGroups.end(); iter++) iter->second = NULL; |
for (; iter != ActiveKeyGroups.end(); iter++) iter->second = NULL; |
186 |
|
|
187 |
// reset all voices |
// reset all voices |
188 |
for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) { |
for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { |
189 |
pVoice->Reset(); |
iterVoice->Reset(); |
190 |
} |
} |
191 |
pVoicePool->clear(); |
pVoicePool->clear(); |
192 |
|
|
314 |
this->MaxSamplesPerCycle = pAudioOutputDevice->MaxSamplesPerCycle(); |
this->MaxSamplesPerCycle = pAudioOutputDevice->MaxSamplesPerCycle(); |
315 |
this->SampleRate = pAudioOutputDevice->SampleRate(); |
this->SampleRate = pAudioOutputDevice->SampleRate(); |
316 |
|
|
317 |
|
// FIXME: audio drivers with varying fragment sizes might be a problem here |
318 |
|
MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * EG_MIN_RELEASE_TIME) - 1; |
319 |
|
if (MaxFadeOutPos < 0) |
320 |
|
throw LinuxSamplerException("EG_MIN_RELEASE_TIME in EGADSR.h to big for current audio fragment size / sampling rate!"); |
321 |
|
|
322 |
// (re)create disk thread |
// (re)create disk thread |
323 |
if (this->pDiskThread) { |
if (this->pDiskThread) { |
324 |
this->pDiskThread->StopThread(); |
this->pDiskThread->StopThread(); |
330 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
331 |
} |
} |
332 |
|
|
333 |
for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) { |
for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { |
334 |
pVoice->pDiskThread = this->pDiskThread; |
iterVoice->pDiskThread = this->pDiskThread; |
335 |
dmsg(3,("d")); |
dmsg(3,("d")); |
336 |
} |
} |
337 |
pVoicePool->clear(); |
pVoicePool->clear(); |
356 |
pDiskThread->StartThread(); |
pDiskThread->StartThread(); |
357 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
358 |
|
|
359 |
for (Voice* pVoice = pVoicePool->first(); pVoice; pVoice = pVoicePool->next()) { |
for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { |
360 |
if (!pVoice->pDiskThread) { |
if (!iterVoice->pDiskThread) { |
361 |
dmsg(0,("Engine -> voice::trigger: !pDiskThread\n")); |
dmsg(0,("Engine -> voice::trigger: !pDiskThread\n")); |
362 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
363 |
} |
} |
398 |
} |
} |
399 |
|
|
400 |
|
|
401 |
|
// update time of start and end of this audio fragment (as events' time stamps relate to this) |
402 |
|
pEventGenerator->UpdateFragmentTime(Samples); |
403 |
|
|
404 |
|
|
405 |
// empty the event lists for the new fragment |
// empty the event lists for the new fragment |
406 |
pEvents->clear(); |
pEvents->clear(); |
407 |
pCCEvents->clear(); |
pCCEvents->clear(); |
408 |
for (uint i = 0; i < Event::destination_count; i++) { |
for (uint i = 0; i < Event::destination_count; i++) { |
409 |
pSynthesisEvents[i]->clear(); |
pSynthesisEvents[i]->clear(); |
410 |
} |
} |
411 |
|
{ |
412 |
// read and copy events from input queue |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
413 |
Event event = pEventGenerator->CreateEvent(); |
RTList<uint>::Iterator end = pActiveKeys->end(); |
414 |
while (true) { |
for(; iuiKey != end; ++iuiKey) { |
415 |
if (!pEventQueue->pop(&event)) break; |
pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key |
416 |
pEvents->alloc_assign(event); |
} |
417 |
} |
} |
418 |
|
|
419 |
|
|
420 |
// 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 |
421 |
pEventGenerator->UpdateFragmentTime(Samples); |
{ |
422 |
|
RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
423 |
|
Event* pEvent; |
424 |
|
while (true) { |
425 |
|
// get next event from input event queue |
426 |
|
if (!(pEvent = eventQueueReader.pop())) break; |
427 |
|
// if younger event reached, ignore that and all subsequent ones for now |
428 |
|
if (pEvent->FragmentPos() >= Samples) { |
429 |
|
eventQueueReader--; |
430 |
|
dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples)); |
431 |
|
pEvent->ResetFragmentPos(); |
432 |
|
break; |
433 |
|
} |
434 |
|
// copy event to internal event list |
435 |
|
if (pEvents->poolIsEmpty()) { |
436 |
|
dmsg(1,("Event pool emtpy!\n")); |
437 |
|
break; |
438 |
|
} |
439 |
|
*pEvents->allocAppend() = *pEvent; |
440 |
|
} |
441 |
|
eventQueueReader.free(); // free all copied events from input queue |
442 |
|
} |
443 |
|
|
444 |
|
|
445 |
// process events |
// process events |
446 |
Event* pNextEvent = pEvents->first(); |
{ |
447 |
while (pNextEvent) { |
RTList<Event>::Iterator itEvent = pEvents->first(); |
448 |
Event* pEvent = pNextEvent; |
RTList<Event>::Iterator end = pEvents->end(); |
449 |
pEvents->set_current(pEvent); |
for (; itEvent != end; ++itEvent) { |
450 |
pNextEvent = pEvents->next(); |
switch (itEvent->Type) { |
451 |
switch (pEvent->Type) { |
case Event::type_note_on: |
452 |
case Event::type_note_on: |
dmsg(5,("Engine: Note on received\n")); |
453 |
dmsg(5,("Engine: Note on received\n")); |
ProcessNoteOn(itEvent); |
454 |
ProcessNoteOn(pEvent); |
break; |
455 |
break; |
case Event::type_note_off: |
456 |
case Event::type_note_off: |
dmsg(5,("Engine: Note off received\n")); |
457 |
dmsg(5,("Engine: Note off received\n")); |
ProcessNoteOff(itEvent); |
458 |
ProcessNoteOff(pEvent); |
break; |
459 |
break; |
case Event::type_control_change: |
460 |
case Event::type_control_change: |
dmsg(5,("Engine: MIDI CC received\n")); |
461 |
dmsg(5,("Engine: MIDI CC received\n")); |
ProcessControlChange(itEvent); |
462 |
ProcessControlChange(pEvent); |
break; |
463 |
break; |
case Event::type_pitchbend: |
464 |
case Event::type_pitchbend: |
dmsg(5,("Engine: Pitchbend received\n")); |
465 |
dmsg(5,("Engine: Pitchbend received\n")); |
ProcessPitchbend(itEvent); |
466 |
ProcessPitchbend(pEvent); |
break; |
467 |
break; |
case Event::type_sysex: |
468 |
case Event::type_sysex: |
dmsg(5,("Engine: Sysex received\n")); |
469 |
dmsg(5,("Engine: Sysex received\n")); |
ProcessSysex(itEvent); |
470 |
ProcessSysex(pEvent); |
break; |
471 |
break; |
} |
472 |
} |
} |
473 |
} |
} |
474 |
|
|
475 |
|
|
|
// render audio from all active voices |
|
476 |
int active_voices = 0; |
int active_voices = 0; |
477 |
uint* piKey = pActiveKeys->first(); |
|
478 |
while (piKey) { // iterate through all active keys |
// render audio from all active voices |
479 |
midi_key_info_t* pKey = &pMIDIKeyInfo[*piKey]; |
{ |
480 |
pActiveKeys->set_current(piKey); |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
481 |
piKey = pActiveKeys->next(); |
RTList<uint>::Iterator end = pActiveKeys->end(); |
482 |
|
while (iuiKey != end) { // iterate through all active keys |
483 |
Voice* pVoiceNext = pKey->pActiveVoices->first(); |
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
484 |
while (pVoiceNext) { // iterate through all voices on this key |
++iuiKey; |
485 |
// already get next voice on key |
|
486 |
Voice* pVoice = pVoiceNext; |
RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first(); |
487 |
pKey->pActiveVoices->set_current(pVoice); |
RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end(); |
488 |
pVoiceNext = pKey->pActiveVoices->next(); |
for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key |
489 |
|
// now render current voice |
490 |
// now render current voice |
itVoice->Render(Samples); |
491 |
pVoice->Render(Samples); |
if (itVoice->IsActive()) active_voices++; // still active |
492 |
if (pVoice->IsActive()) active_voices++; // still active |
else { // voice reached end, is now inactive |
493 |
else { // voice reached end, is now inactive |
FreeVoice(itVoice); // remove voice from the list of active voices |
494 |
KillVoiceImmediately(pVoice); // remove voice from the list of active voices |
} |
495 |
} |
} |
496 |
} |
} |
497 |
pKey->pEvents->clear(); // free all events on the key |
} |
498 |
|
|
499 |
|
|
500 |
|
// now render all postponed voices from voice stealing |
501 |
|
{ |
502 |
|
RTList<Event>::Iterator itVoiceStealEvent = pVoiceStealingQueue->first(); |
503 |
|
RTList<Event>::Iterator end = pVoiceStealingQueue->end(); |
504 |
|
for (; itVoiceStealEvent != end; ++itVoiceStealEvent) { |
505 |
|
Pool<Voice>::Iterator itNewVoice = LaunchVoice(itVoiceStealEvent, itVoiceStealEvent->Param.Note.Layer, itVoiceStealEvent->Param.Note.ReleaseTrigger, false); |
506 |
|
if (itNewVoice) { |
507 |
|
for (; itNewVoice; itNewVoice = itNewVoice->itChildVoice) { |
508 |
|
itNewVoice->Render(Samples); |
509 |
|
if (itNewVoice->IsActive()) active_voices++; // still active |
510 |
|
else { // voice reached end, is now inactive |
511 |
|
FreeVoice(itNewVoice); // remove voice from the list of active voices |
512 |
|
} |
513 |
|
} |
514 |
|
} |
515 |
|
else dmsg(1,("gig::Engine: ERROR, voice stealing didn't work out!\n")); |
516 |
|
} |
517 |
|
} |
518 |
|
// reset voice stealing for the new fragment |
519 |
|
pVoiceStealingQueue->clear(); |
520 |
|
itLastStolenVoice = RTList<Voice>::Iterator(); |
521 |
|
iuiLastStolenKey = RTList<uint>::Iterator(); |
522 |
|
|
523 |
|
|
524 |
|
// free all keys which have no active voices left |
525 |
|
{ |
526 |
|
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
527 |
|
RTList<uint>::Iterator end = pActiveKeys->end(); |
528 |
|
while (iuiKey != end) { // iterate through all active keys |
529 |
|
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
530 |
|
++iuiKey; |
531 |
|
if (pKey->pActiveVoices->isEmpty()) FreeKey(pKey); |
532 |
|
#if DEVMODE |
533 |
|
else { // FIXME: should be removed before the final release (purpose: just a sanity check for debugging) |
534 |
|
RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first(); |
535 |
|
RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end(); |
536 |
|
for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key |
537 |
|
if (itVoice->itKillEvent) { |
538 |
|
dmsg(1,("gig::Engine: ERROR, killed voice survived !!!\n")); |
539 |
|
} |
540 |
|
} |
541 |
|
} |
542 |
|
#endif // DEVMODE |
543 |
|
} |
544 |
} |
} |
545 |
|
|
546 |
|
|
649 |
/** |
/** |
650 |
* Assigns and triggers a new voice for the respective MIDI key. |
* Assigns and triggers a new voice for the respective MIDI key. |
651 |
* |
* |
652 |
* @param pNoteOnEvent - key, velocity and time stamp of the event |
* @param itNoteOnEvent - key, velocity and time stamp of the event |
653 |
*/ |
*/ |
654 |
void Engine::ProcessNoteOn(Event* pNoteOnEvent) { |
void Engine::ProcessNoteOn(Pool<Event>::Iterator& itNoteOnEvent) { |
655 |
midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOnEvent->Param.Note.Key]; |
midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
656 |
|
|
657 |
pKey->KeyPressed = true; // the MIDI key was now pressed down |
pKey->KeyPressed = true; // the MIDI key was now pressed down |
658 |
|
|
659 |
// cancel release process of voices on this key if needed |
// cancel release process of voices on this key if needed |
660 |
if (pKey->Active && !SustainPedal) { |
if (pKey->Active && !SustainPedal) { |
661 |
Event* pCancelReleaseEvent = pKey->pEvents->alloc(); |
RTList<Event>::Iterator itCancelReleaseEvent = pKey->pEvents->allocAppend(); |
662 |
if (pCancelReleaseEvent) { |
if (itCancelReleaseEvent) { |
663 |
*pCancelReleaseEvent = *pNoteOnEvent; |
*itCancelReleaseEvent = *itNoteOnEvent; // copy event |
664 |
pCancelReleaseEvent->Type = Event::type_cancel_release; // transform event type |
itCancelReleaseEvent->Type = Event::type_cancel_release; // transform event type |
665 |
} |
} |
666 |
else dmsg(1,("Event pool emtpy!\n")); |
else dmsg(1,("Event pool emtpy!\n")); |
667 |
} |
} |
668 |
|
|
669 |
// allocate and trigger a new voice for the key |
// move note on event to the key's own event list |
670 |
LaunchVoice(pNoteOnEvent); |
RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents); |
671 |
|
|
672 |
// finally move note on event to the key's own event list |
// allocate and trigger a new voice for the key |
673 |
pEvents->move(pNoteOnEvent, pKey->pEvents); |
LaunchVoice(itNoteOnEventOnKeyList, 0, false, true); |
674 |
} |
} |
675 |
|
|
676 |
/** |
/** |
679 |
* 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. |
680 |
* due to completion of sample playback). |
* due to completion of sample playback). |
681 |
* |
* |
682 |
* @param pNoteOffEvent - key, velocity and time stamp of the event |
* @param itNoteOffEvent - key, velocity and time stamp of the event |
683 |
*/ |
*/ |
684 |
void Engine::ProcessNoteOff(Event* pNoteOffEvent) { |
void Engine::ProcessNoteOff(Pool<Event>::Iterator& itNoteOffEvent) { |
685 |
midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOffEvent->Param.Note.Key]; |
midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key]; |
686 |
|
|
687 |
pKey->KeyPressed = false; // the MIDI key was now released |
pKey->KeyPressed = false; // the MIDI key was now released |
688 |
|
|
689 |
// release voices on this key if needed |
// release voices on this key if needed |
690 |
if (pKey->Active && !SustainPedal) { |
if (pKey->Active && !SustainPedal) { |
691 |
pNoteOffEvent->Type = Event::type_release; // transform event type |
itNoteOffEvent->Type = Event::type_release; // transform event type |
692 |
} |
} |
693 |
|
|
694 |
|
// move event to the key's own event list |
695 |
|
RTList<Event>::Iterator itNoteOffEventOnKeyList = itNoteOffEvent.moveToEndOf(pKey->pEvents); |
696 |
|
|
697 |
// spawn release triggered voice(s) if needed |
// spawn release triggered voice(s) if needed |
698 |
if (pKey->ReleaseTrigger) { |
if (pKey->ReleaseTrigger) { |
699 |
LaunchVoice(pNoteOffEvent, 0, true); |
LaunchVoice(itNoteOffEventOnKeyList, 0, true, false); //FIXME: for the moment we don't perform voice stealing for release triggered samples |
700 |
pKey->ReleaseTrigger = false; |
pKey->ReleaseTrigger = false; |
701 |
} |
} |
|
|
|
|
// move event to the key's own event list |
|
|
pEvents->move(pNoteOffEvent, pKey->pEvents); |
|
702 |
} |
} |
703 |
|
|
704 |
/** |
/** |
705 |
* Moves pitchbend event from the general (input) event list to the pitch |
* Moves pitchbend event from the general (input) event list to the pitch |
706 |
* event list. |
* event list. |
707 |
* |
* |
708 |
* @param pPitchbendEvent - absolute pitch value and time stamp of the event |
* @param itPitchbendEvent - absolute pitch value and time stamp of the event |
709 |
*/ |
*/ |
710 |
void Engine::ProcessPitchbend(Event* pPitchbendEvent) { |
void Engine::ProcessPitchbend(Pool<Event>::Iterator& itPitchbendEvent) { |
711 |
this->Pitch = pPitchbendEvent->Param.Pitch.Pitch; // store current pitch value |
this->Pitch = itPitchbendEvent->Param.Pitch.Pitch; // store current pitch value |
712 |
pEvents->move(pPitchbendEvent, pSynthesisEvents[Event::destination_vco]); |
itPitchbendEvent.moveToEndOf(pSynthesisEvents[Event::destination_vco]); |
713 |
} |
} |
714 |
|
|
715 |
/** |
/** |
717 |
* called by the ProcessNoteOn() method and by the voices itself |
* called by the ProcessNoteOn() method and by the voices itself |
718 |
* (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). |
719 |
* |
* |
720 |
* @param pNoteOnEvent - key, velocity and time stamp of the event |
* @param itNoteOnEvent - key, velocity and time stamp of the event |
721 |
* @param iLayer - layer index for the new voice (optional - only |
* @param iLayer - layer index for the new voice (optional - only |
722 |
* in case of layered sounds of course) |
* in case of layered sounds of course) |
723 |
* @param ReleaseTriggerVoice - if new voice is a release triggered voice |
* @param ReleaseTriggerVoice - if new voice is a release triggered voice |
724 |
* (optional, default = false) |
* (optional, default = false) |
725 |
|
* @param VoiceStealing - if voice stealing should be performed |
726 |
|
* when there is no free voice |
727 |
|
* (optional, default = true) |
728 |
|
* @returns pointer to new voice or NULL if there was no free voice or |
729 |
|
* if an error occured while trying to trigger the new voice |
730 |
*/ |
*/ |
731 |
void Engine::LaunchVoice(Event* pNoteOnEvent, int iLayer, bool ReleaseTriggerVoice) { |
Pool<Voice>::Iterator Engine::LaunchVoice(Pool<Event>::Iterator& itNoteOnEvent, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing) { |
732 |
midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOnEvent->Param.Note.Key]; |
midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
733 |
|
|
734 |
// allocate a new voice for the key |
// allocate a new voice for the key |
735 |
Voice* pNewVoice = pKey->pActiveVoices->alloc(); |
Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend(); |
736 |
if (pNewVoice) { |
if (itNewVoice) { |
737 |
// launch the new voice |
// launch the new voice |
738 |
if (pNewVoice->Trigger(pNoteOnEvent, this->Pitch, this->pInstrument, iLayer, ReleaseTriggerVoice) < 0) { |
if (itNewVoice->Trigger(itNoteOnEvent, this->Pitch, this->pInstrument, iLayer, ReleaseTriggerVoice, VoiceStealing) < 0) { |
739 |
dmsg(1,("Triggering new voice failed!\n")); |
dmsg(1,("Triggering new voice failed!\n")); |
740 |
pKey->pActiveVoices->free(pNewVoice); |
pKey->pActiveVoices->free(itNewVoice); |
741 |
} |
} |
742 |
else { // on success |
else { // on success |
743 |
uint** ppKeyGroup = NULL; |
uint** ppKeyGroup = NULL; |
744 |
if (pNewVoice->KeyGroup) { // if this voice / key belongs to a key group |
if (itNewVoice->KeyGroup) { // if this voice / key belongs to a key group |
745 |
ppKeyGroup = &ActiveKeyGroups[pNewVoice->KeyGroup]; |
ppKeyGroup = &ActiveKeyGroups[itNewVoice->KeyGroup]; |
746 |
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 |
747 |
midi_key_info_t* pOtherKey = &pMIDIKeyInfo[**ppKeyGroup]; |
midi_key_info_t* pOtherKey = &pMIDIKeyInfo[**ppKeyGroup]; |
748 |
// kill all voices on the (other) key |
// kill all voices on the (other) key |
749 |
Voice* pVoiceToBeKilled = pOtherKey->pActiveVoices->first(); |
RTList<Voice>::Iterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first(); |
750 |
while (pVoiceToBeKilled) { |
RTList<Voice>::Iterator end = pOtherKey->pActiveVoices->end(); |
751 |
Voice* pVoiceToBeKilledNext = pOtherKey->pActiveVoices->next(); |
for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) { |
752 |
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; |
|
753 |
} |
} |
754 |
} |
} |
755 |
} |
} |
756 |
if (!pKey->Active) { // mark as active key |
if (!pKey->Active) { // mark as active key |
757 |
pKey->Active = true; |
pKey->Active = true; |
758 |
pKey->pSelf = pActiveKeys->alloc(); |
pKey->itSelf = pActiveKeys->allocAppend(); |
759 |
*pKey->pSelf = pNoteOnEvent->Param.Note.Key; |
*pKey->itSelf = itNoteOnEvent->Param.Note.Key; |
760 |
} |
} |
761 |
if (pNewVoice->KeyGroup) { |
if (itNewVoice->KeyGroup) { |
762 |
*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 |
763 |
} |
} |
764 |
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) |
765 |
|
return itNewVoice; // success |
766 |
} |
} |
767 |
} |
} |
768 |
else std::cerr << "No free voice!" << std::endl << std::flush; |
else if (VoiceStealing) { |
769 |
|
// first, get total amount of required voices (dependant on amount of layers) |
770 |
|
::gig::Region* pRegion = pInstrument->GetRegion(itNoteOnEvent->Param.Note.Key); |
771 |
|
if (!pRegion) return Pool<Voice>::Iterator(); // nothing defined for this MIDI key, so no voice needed |
772 |
|
int voicesRequired = pRegion->Layers; |
773 |
|
|
774 |
|
// now steal the (remaining) amount of voices |
775 |
|
for (int i = iLayer; i < voicesRequired; i++) |
776 |
|
StealVoice(itNoteOnEvent); |
777 |
|
|
778 |
|
// put note-on event into voice-stealing queue, so it will be reprocessed after killed voice died |
779 |
|
RTList<Event>::Iterator itStealEvent = pVoiceStealingQueue->allocAppend(); |
780 |
|
if (itStealEvent) { |
781 |
|
*itStealEvent = *itNoteOnEvent; // copy event |
782 |
|
itStealEvent->Param.Note.Layer = iLayer; |
783 |
|
itStealEvent->Param.Note.ReleaseTrigger = ReleaseTriggerVoice; |
784 |
|
} |
785 |
|
else dmsg(1,("Voice stealing queue full!\n")); |
786 |
|
} |
787 |
|
|
788 |
|
return Pool<Voice>::Iterator(); // no free voice or error |
789 |
} |
} |
790 |
|
|
791 |
/** |
/** |
792 |
* Immediately kills the voice given with pVoice (no matter if sustain is |
* Will be called by LaunchVoice() method in case there are no free |
793 |
* pressed or not) and removes it from the MIDI key's list of active voice. |
* voices left. This method will select and kill one old voice for |
794 |
* This method will e.g. be called if a voice went inactive by itself. |
* voice stealing and postpone the note-on event until the selected |
795 |
|
* voice actually died. |
796 |
* |
* |
797 |
* @param pVoice - points to the voice to be killed |
* @param itNoteOnEvent - key, velocity and time stamp of the event |
798 |
*/ |
*/ |
799 |
void Engine::KillVoiceImmediately(Voice* pVoice) { |
void Engine::StealVoice(Pool<Event>::Iterator& itNoteOnEvent) { |
800 |
if (pVoice) { |
if (!pEventPool->poolIsEmpty()) { |
801 |
if (pVoice->IsActive()) pVoice->KillImmediately(); |
|
802 |
|
RTList<uint>::Iterator iuiOldestKey; |
803 |
|
RTList<Voice>::Iterator itOldestVoice; |
804 |
|
|
805 |
|
// Select one voice for voice stealing |
806 |
|
switch (VOICE_STEAL_ALGORITHM) { |
807 |
|
|
808 |
|
// try to pick the oldest voice on the key where the new |
809 |
|
// voice should be spawned, if there is no voice on that |
810 |
|
// key, or no voice left to kill there, then procceed with |
811 |
|
// 'oldestkey' algorithm |
812 |
|
case voice_steal_algo_keymask: { |
813 |
|
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
814 |
|
if (itLastStolenVoice) { |
815 |
|
itOldestVoice = itLastStolenVoice; |
816 |
|
++itOldestVoice; |
817 |
|
} |
818 |
|
else { // no voice stolen in this audio fragment cycle yet |
819 |
|
itOldestVoice = pOldestKey->pActiveVoices->first(); |
820 |
|
} |
821 |
|
if (itOldestVoice) { |
822 |
|
iuiOldestKey = pOldestKey->itSelf; |
823 |
|
break; // selection succeeded |
824 |
|
} |
825 |
|
} // no break - intentional ! |
826 |
|
|
827 |
midi_key_info_t* pKey = &pMIDIKeyInfo[pVoice->MIDIKey]; |
// try to pick the oldest voice on the oldest active key |
828 |
|
// (caution: must stay after 'keymask' algorithm !) |
829 |
|
case voice_steal_algo_oldestkey: { |
830 |
|
if (itLastStolenVoice) { |
831 |
|
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiLastStolenKey]; |
832 |
|
itOldestVoice = itLastStolenVoice; |
833 |
|
++itOldestVoice; |
834 |
|
if (!itOldestVoice) { |
835 |
|
iuiOldestKey = iuiLastStolenKey; |
836 |
|
++iuiOldestKey; |
837 |
|
if (iuiOldestKey) { |
838 |
|
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiOldestKey]; |
839 |
|
itOldestVoice = pOldestKey->pActiveVoices->first(); |
840 |
|
} |
841 |
|
else { |
842 |
|
dmsg(1,("gig::Engine: Warning, too less voices, even for voice stealing! - Better recompile with higher MAX_AUDIO_VOICES.\n")); |
843 |
|
return; |
844 |
|
} |
845 |
|
} |
846 |
|
else iuiOldestKey = iuiLastStolenKey; |
847 |
|
} |
848 |
|
else { // no voice stolen in this audio fragment cycle yet |
849 |
|
iuiOldestKey = pActiveKeys->first(); |
850 |
|
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiOldestKey]; |
851 |
|
itOldestVoice = pOldestKey->pActiveVoices->first(); |
852 |
|
} |
853 |
|
break; |
854 |
|
} |
855 |
|
|
856 |
|
// don't steal anything |
857 |
|
case voice_steal_algo_none: |
858 |
|
default: { |
859 |
|
dmsg(1,("No free voice (voice stealing disabled)!\n")); |
860 |
|
return; |
861 |
|
} |
862 |
|
} |
863 |
|
|
864 |
|
//FIXME: can be removed, just a sanity check for debugging |
865 |
|
if (!itOldestVoice->IsActive()) dmsg(1,("gig::Engine: ERROR, tried to steal a voice which was not active !!!\n")); |
866 |
|
|
867 |
|
// now kill the selected voice |
868 |
|
itOldestVoice->Kill(itNoteOnEvent); |
869 |
|
// remember which voice on which key we stole, so we can simply proceed for the next voice stealing |
870 |
|
this->itLastStolenVoice = itOldestVoice; |
871 |
|
this->iuiLastStolenKey = iuiOldestKey; |
872 |
|
} |
873 |
|
else dmsg(1,("Event pool emtpy!\n")); |
874 |
|
} |
875 |
|
|
876 |
|
/** |
877 |
|
* Removes the given voice from the MIDI key's list of active voices. |
878 |
|
* This method will be called when a voice went inactive, e.g. because |
879 |
|
* it finished to playback its sample, finished its release stage or |
880 |
|
* just was killed. |
881 |
|
* |
882 |
|
* @param itVoice - points to the voice to be freed |
883 |
|
*/ |
884 |
|
void Engine::FreeVoice(Pool<Voice>::Iterator& itVoice) { |
885 |
|
if (itVoice) { |
886 |
|
midi_key_info_t* pKey = &pMIDIKeyInfo[itVoice->MIDIKey]; |
887 |
|
|
888 |
|
uint keygroup = itVoice->KeyGroup; |
889 |
|
|
890 |
// free the voice object |
// free the voice object |
891 |
pVoicePool->free(pVoice); |
pVoicePool->free(itVoice); |
892 |
|
|
893 |
// 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 |
894 |
if (pKey->pActiveVoices->is_empty()) { |
if (pKey->pActiveVoices->isEmpty() && keygroup) { |
895 |
if (pVoice->KeyGroup) { // if voice / key belongs to a key group |
uint** ppKeyGroup = &ActiveKeyGroups[keygroup]; |
896 |
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; |
|
|
dmsg(3,("Key has no more voices now\n")); |
|
897 |
} |
} |
898 |
} |
} |
899 |
else std::cerr << "Couldn't release voice! (pVoice == NULL)\n" << std::flush; |
else std::cerr << "Couldn't release voice! (!itVoice)\n" << std::flush; |
900 |
|
} |
901 |
|
|
902 |
|
/** |
903 |
|
* Called when there's no more voice left on a key, this call will |
904 |
|
* update the key info respectively. |
905 |
|
* |
906 |
|
* @param pKey - key which is now inactive |
907 |
|
*/ |
908 |
|
void Engine::FreeKey(midi_key_info_t* pKey) { |
909 |
|
if (pKey->pActiveVoices->isEmpty()) { |
910 |
|
pKey->Active = false; |
911 |
|
pActiveKeys->free(pKey->itSelf); // remove key from list of active keys |
912 |
|
pKey->itSelf = RTList<uint>::Iterator(); |
913 |
|
pKey->ReleaseTrigger = false; |
914 |
|
pKey->pEvents->clear(); |
915 |
|
dmsg(3,("Key has no more voices now\n")); |
916 |
|
} |
917 |
|
else dmsg(1,("gig::Engine: Oops, tried to free a key which contains voices.\n")); |
918 |
} |
} |
919 |
|
|
920 |
/** |
/** |
921 |
* Reacts on supported control change commands (e.g. pitch bend wheel, |
* Reacts on supported control change commands (e.g. pitch bend wheel, |
922 |
* modulation wheel, aftertouch). |
* modulation wheel, aftertouch). |
923 |
* |
* |
924 |
* @param pControlChangeEvent - controller, value and time stamp of the event |
* @param itControlChangeEvent - controller, value and time stamp of the event |
925 |
*/ |
*/ |
926 |
void Engine::ProcessControlChange(Event* pControlChangeEvent) { |
void Engine::ProcessControlChange(Pool<Event>::Iterator& itControlChangeEvent) { |
927 |
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)); |
928 |
|
|
929 |
switch (pControlChangeEvent->Param.CC.Controller) { |
switch (itControlChangeEvent->Param.CC.Controller) { |
930 |
case 64: { |
case 64: { |
931 |
if (pControlChangeEvent->Param.CC.Value >= 64 && !SustainPedal) { |
if (itControlChangeEvent->Param.CC.Value >= 64 && !SustainPedal) { |
932 |
dmsg(4,("PEDAL DOWN\n")); |
dmsg(4,("PEDAL DOWN\n")); |
933 |
SustainPedal = true; |
SustainPedal = true; |
934 |
|
|
935 |
// cancel release process of voices if necessary |
// cancel release process of voices if necessary |
936 |
uint* piKey = pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
937 |
if (piKey) { |
if (iuiKey) { |
938 |
pControlChangeEvent->Type = Event::type_cancel_release; // transform event type |
itControlChangeEvent->Type = Event::type_cancel_release; // transform event type |
939 |
while (piKey) { |
while (iuiKey) { |
940 |
midi_key_info_t* pKey = &pMIDIKeyInfo[*piKey]; |
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
941 |
pActiveKeys->set_current(piKey); |
++iuiKey; |
|
piKey = pActiveKeys->next(); |
|
942 |
if (!pKey->KeyPressed) { |
if (!pKey->KeyPressed) { |
943 |
Event* pNewEvent = pKey->pEvents->alloc(); |
RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend(); |
944 |
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 |
945 |
else dmsg(1,("Event pool emtpy!\n")); |
else dmsg(1,("Event pool emtpy!\n")); |
946 |
} |
} |
947 |
} |
} |
948 |
} |
} |
949 |
} |
} |
950 |
if (pControlChangeEvent->Param.CC.Value < 64 && SustainPedal) { |
if (itControlChangeEvent->Param.CC.Value < 64 && SustainPedal) { |
951 |
dmsg(4,("PEDAL UP\n")); |
dmsg(4,("PEDAL UP\n")); |
952 |
SustainPedal = false; |
SustainPedal = false; |
953 |
|
|
954 |
// release voices if their respective key is not pressed |
// release voices if their respective key is not pressed |
955 |
uint* piKey = pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
956 |
if (piKey) { |
if (iuiKey) { |
957 |
pControlChangeEvent->Type = Event::type_release; // transform event type |
itControlChangeEvent->Type = Event::type_release; // transform event type |
958 |
while (piKey) { |
while (iuiKey) { |
959 |
midi_key_info_t* pKey = &pMIDIKeyInfo[*piKey]; |
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
960 |
pActiveKeys->set_current(piKey); |
++iuiKey; |
|
piKey = pActiveKeys->next(); |
|
961 |
if (!pKey->KeyPressed) { |
if (!pKey->KeyPressed) { |
962 |
Event* pNewEvent = pKey->pEvents->alloc(); |
RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend(); |
963 |
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 |
964 |
else dmsg(1,("Event pool emtpy!\n")); |
else dmsg(1,("Event pool emtpy!\n")); |
965 |
} |
} |
966 |
} |
} |
971 |
} |
} |
972 |
|
|
973 |
// update controller value in the engine's controller table |
// update controller value in the engine's controller table |
974 |
ControllerTable[pControlChangeEvent->Param.CC.Controller] = pControlChangeEvent->Param.CC.Value; |
ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value; |
975 |
|
|
976 |
// 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 |
977 |
pEvents->move(pControlChangeEvent, pCCEvents); |
itControlChangeEvent.moveToEndOf(pCCEvents); |
978 |
} |
} |
979 |
|
|
980 |
/** |
/** |
981 |
* Reacts on MIDI system exclusive messages. |
* Reacts on MIDI system exclusive messages. |
982 |
* |
* |
983 |
* @param pSysexEvent - sysex data size and time stamp of the sysex event |
* @param itSysexEvent - sysex data size and time stamp of the sysex event |
984 |
*/ |
*/ |
985 |
void Engine::ProcessSysex(Event* pSysexEvent) { |
void Engine::ProcessSysex(Pool<Event>::Iterator& itSysexEvent) { |
986 |
RingBuffer<uint8_t>::NonVolatileReader reader = pSysexBuffer->get_non_volatile_reader(); |
RingBuffer<uint8_t>::NonVolatileReader reader = pSysexBuffer->get_non_volatile_reader(); |
987 |
|
|
988 |
uint8_t exclusive_status, id; |
uint8_t exclusive_status, id; |
1030 |
} |
} |
1031 |
|
|
1032 |
free_sysex_data: // finally free sysex data |
free_sysex_data: // finally free sysex data |
1033 |
pSysexBuffer->increment_read_ptr(pSysexEvent->Param.Sysex.Size); |
pSysexBuffer->increment_read_ptr(itSysexEvent->Param.Sysex.Size); |
1034 |
} |
} |
1035 |
|
|
1036 |
/** |
/** |
1164 |
} |
} |
1165 |
|
|
1166 |
String Engine::Version() { |
String Engine::Version() { |
1167 |
String s = "$Revision: 1.13 $"; |
String s = "$Revision: 1.18 $"; |
1168 |
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 |
1169 |
} |
} |
1170 |
|
|