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 |
|
|
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(); |
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(); |
416 |
} |
} |
417 |
} |
} |
418 |
|
|
|
// read and copy events from input queue |
|
|
Event event = pEventGenerator->CreateEvent(); |
|
|
while (true) { |
|
|
if (!pEventQueue->pop(&event) || pEvents->poolIsEmpty()) break; |
|
|
*pEvents->allocAppend() = event; |
|
|
} |
|
|
|
|
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 |
490 |
itVoice->Render(Samples); |
itVoice->Render(Samples); |
491 |
if (itVoice->IsActive()) active_voices++; // still active |
if (itVoice->IsActive()) active_voices++; // still active |
492 |
else { // voice reached end, is now inactive |
else { // voice reached end, is now inactive |
493 |
KillVoiceImmediately(itVoice); // remove voice from the list of active voices |
FreeVoice(itVoice); // remove voice from the list of active voices |
494 |
} |
} |
495 |
} |
} |
496 |
} |
} |
504 |
for (; itVoiceStealEvent != end; ++itVoiceStealEvent) { |
for (; itVoiceStealEvent != end; ++itVoiceStealEvent) { |
505 |
Pool<Voice>::Iterator itNewVoice = LaunchVoice(itVoiceStealEvent, itVoiceStealEvent->Param.Note.Layer, itVoiceStealEvent->Param.Note.ReleaseTrigger, false); |
Pool<Voice>::Iterator itNewVoice = LaunchVoice(itVoiceStealEvent, itVoiceStealEvent->Param.Note.Layer, itVoiceStealEvent->Param.Note.ReleaseTrigger, false); |
506 |
if (itNewVoice) { |
if (itNewVoice) { |
507 |
itNewVoice->Render(Samples); |
for (; itNewVoice; itNewVoice = itNewVoice->itChildVoice) { |
508 |
if (itNewVoice->IsActive()) active_voices++; // still active |
itNewVoice->Render(Samples); |
509 |
else { // voice reached end, is now inactive |
if (itNewVoice->IsActive()) active_voices++; // still active |
510 |
KillVoiceImmediately(itNewVoice); // remove voice from the list of active voices |
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,("Ouch, voice stealing didn't work out!\n")); |
else dmsg(1,("gig::Engine: ERROR, voice stealing didn't work out!\n")); |
516 |
} |
} |
517 |
} |
} |
518 |
// reset voice stealing for the new fragment |
// reset voice stealing for the new fragment |
521 |
iuiLastStolenKey = RTList<uint>::Iterator(); |
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 |
|
|
547 |
// 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 |
548 |
// on the console for debugging purposes |
// on the console for debugging purposes |
549 |
ActiveVoiceCount = active_voices; |
ActiveVoiceCount = active_voices; |
670 |
RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents); |
RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents); |
671 |
|
|
672 |
// allocate and trigger a new voice for the key |
// allocate and trigger a new voice for the key |
673 |
LaunchVoice(itNoteOnEventOnKeyList); |
LaunchVoice(itNoteOnEventOnKeyList, 0, false, true); |
674 |
} |
} |
675 |
|
|
676 |
/** |
/** |
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(itNoteOffEventOnKeyList, 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 |
} |
} |
702 |
} |
} |
735 |
Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend(); |
Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend(); |
736 |
if (itNewVoice) { |
if (itNewVoice) { |
737 |
// launch the new voice |
// launch the new voice |
738 |
if (itNewVoice->Trigger(itNoteOnEvent, 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(itNewVoice); |
pKey->pActiveVoices->free(itNewVoice); |
741 |
} |
} |
765 |
return itNewVoice; // success |
return itNewVoice; // success |
766 |
} |
} |
767 |
} |
} |
768 |
else if (VoiceStealing) StealVoice(itNoteOnEvent, iLayer, ReleaseTriggerVoice); // no free voice left, so steal one |
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 |
return Pool<Voice>::Iterator(); // no free voice or error |
789 |
} |
} |
794 |
* voice stealing and postpone the note-on event until the selected |
* voice stealing and postpone the note-on event until the selected |
795 |
* voice actually died. |
* voice actually died. |
796 |
* |
* |
797 |
* @param itNoteOnEvent - 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 |
|
798 |
*/ |
*/ |
799 |
void Engine::StealVoice(Pool<Event>::Iterator& itNoteOnEvent, int iLayer, bool ReleaseTriggerVoice) { |
void Engine::StealVoice(Pool<Event>::Iterator& itNoteOnEvent) { |
800 |
if (!pEventPool->poolIsEmpty()) { |
if (!pEventPool->poolIsEmpty()) { |
801 |
|
|
802 |
RTList<uint>::Iterator iuiOldestKey; |
RTList<uint>::Iterator iuiOldestKey; |
838 |
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiOldestKey]; |
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiOldestKey]; |
839 |
itOldestVoice = pOldestKey->pActiveVoices->first(); |
itOldestVoice = pOldestKey->pActiveVoices->first(); |
840 |
} |
} |
841 |
else { // too less voices, even for voice stealing |
else { |
842 |
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")); |
843 |
return; |
return; |
844 |
} |
} |
845 |
} |
} |
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 |
// now kill the selected voice |
868 |
itOldestVoice->Kill(itNoteOnEvent); |
itOldestVoice->Kill(itNoteOnEvent); |
869 |
// 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 |
870 |
this->itLastStolenVoice = itOldestVoice; |
this->itLastStolenVoice = itOldestVoice; |
871 |
this->iuiLastStolenKey = iuiOldestKey; |
this->iuiLastStolenKey = iuiOldestKey; |
|
// put note-on event into voice-stealing queue, so it will be reprocessed after killed voice died |
|
|
RTList<Event>::Iterator itStealEvent = pVoiceStealingQueue->allocAppend(); |
|
|
if (itStealEvent) { |
|
|
*itStealEvent = *itNoteOnEvent; // copy event |
|
|
itStealEvent->Param.Note.Layer = iLayer; |
|
|
itStealEvent->Param.Note.ReleaseTrigger = ReleaseTriggerVoice; |
|
|
} |
|
|
else dmsg(1,("Voice stealing queue full!\n")); |
|
872 |
} |
} |
873 |
else dmsg(1,("Event pool emtpy!\n")); |
else dmsg(1,("Event pool emtpy!\n")); |
874 |
} |
} |
875 |
|
|
876 |
/** |
/** |
877 |
* 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. |
878 |
* 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 |
879 |
* 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 |
880 |
|
* just was killed. |
881 |
* |
* |
882 |
* @param itVoice - points to the voice to be killed |
* @param itVoice - points to the voice to be freed |
883 |
*/ |
*/ |
884 |
void Engine::KillVoiceImmediately(Pool<Voice>::Iterator& itVoice) { |
void Engine::FreeVoice(Pool<Voice>::Iterator& itVoice) { |
885 |
if (itVoice) { |
if (itVoice) { |
|
if (itVoice->IsActive()) itVoice->KillImmediately(); |
|
|
|
|
886 |
midi_key_info_t* pKey = &pMIDIKeyInfo[itVoice->MIDIKey]; |
midi_key_info_t* pKey = &pMIDIKeyInfo[itVoice->MIDIKey]; |
887 |
|
|
888 |
uint keygroup = itVoice->KeyGroup; |
uint keygroup = itVoice->KeyGroup; |
890 |
// free the voice object |
// free the voice object |
891 |
pVoicePool->free(itVoice); |
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->isEmpty()) { |
if (pKey->pActiveVoices->isEmpty() && keygroup) { |
895 |
if (keygroup) { // if voice / key belongs to a key group |
uint** ppKeyGroup = &ActiveKeyGroups[keygroup]; |
896 |
uint** ppKeyGroup = &ActiveKeyGroups[keygroup]; |
if (*ppKeyGroup == &*pKey->itSelf) *ppKeyGroup = NULL; // remove key from key group |
|
if (*ppKeyGroup == &*pKey->itSelf) *ppKeyGroup = NULL; // remove key from key group |
|
|
} |
|
|
pKey->Active = false; |
|
|
pActiveKeys->free(pKey->itSelf); // remove key from list of active keys |
|
|
pKey->itSelf = RTList<uint>::Iterator(); |
|
|
pKey->ReleaseTrigger = false; |
|
|
pKey->pEvents->clear(); |
|
|
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 |
/** |
/** |
1164 |
} |
} |
1165 |
|
|
1166 |
String Engine::Version() { |
String Engine::Version() { |
1167 |
String s = "$Revision: 1.15 $"; |
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 |
|
|