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 |
|
|
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 |
|
|
86 |
|
if (pInstrument) Instruments.HandBack(pInstrument, this); |
87 |
|
|
88 |
if (pGig) delete pGig; |
if (pGig) delete pGig; |
89 |
if (pRIFF) delete pRIFF; |
if (pRIFF) delete pRIFF; |
90 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
94 |
for (uint i = 0; i < Event::destination_count; i++) { |
for (uint i = 0; i < Event::destination_count; i++) { |
95 |
if (pSynthesisEvents[i]) delete pSynthesisEvents[i]; |
if (pSynthesisEvents[i]) delete pSynthesisEvents[i]; |
96 |
} |
} |
|
delete[] pSynthesisEvents; |
|
97 |
if (pEvents) delete pEvents; |
if (pEvents) delete pEvents; |
98 |
if (pCCEvents) delete pCCEvents; |
if (pCCEvents) delete pCCEvents; |
99 |
if (pEventQueue) delete pEventQueue; |
if (pEventQueue) delete pEventQueue; |
100 |
if (pEventPool) delete pEventPool; |
if (pEventPool) delete pEventPool; |
101 |
if (pVoicePool) delete pVoicePool; |
if (pVoicePool) { |
102 |
|
pVoicePool->clear(); |
103 |
|
delete pVoicePool; |
104 |
|
} |
105 |
if (pActiveKeys) delete pActiveKeys; |
if (pActiveKeys) delete pActiveKeys; |
106 |
if (pSysexBuffer) delete pSysexBuffer; |
if (pSysexBuffer) delete pSysexBuffer; |
107 |
if (pEventGenerator) delete pEventGenerator; |
if (pEventGenerator) delete pEventGenerator; |
108 |
if (pMainFilterParameters) delete[] pMainFilterParameters; |
if (pMainFilterParameters) delete[] pMainFilterParameters; |
109 |
if (pBasicFilterParameters) delete[] pBasicFilterParameters; |
if (pBasicFilterParameters) delete[] pBasicFilterParameters; |
110 |
if (pSynthesisParameters[0]) delete[] pSynthesisParameters[0]; |
if (pSynthesisParameters[0]) free(pSynthesisParameters[0]); |
111 |
if (pVoiceStealingQueue) delete pVoiceStealingQueue; |
if (pVoiceStealingQueue) delete pVoiceStealingQueue; |
112 |
} |
} |
113 |
|
|
322 |
this->MaxSamplesPerCycle = pAudioOutputDevice->MaxSamplesPerCycle(); |
this->MaxSamplesPerCycle = pAudioOutputDevice->MaxSamplesPerCycle(); |
323 |
this->SampleRate = pAudioOutputDevice->SampleRate(); |
this->SampleRate = pAudioOutputDevice->SampleRate(); |
324 |
|
|
325 |
|
// FIXME: audio drivers with varying fragment sizes might be a problem here |
326 |
|
MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * EG_MIN_RELEASE_TIME) - 1; |
327 |
|
if (MaxFadeOutPos < 0) |
328 |
|
throw LinuxSamplerException("EG_MIN_RELEASE_TIME in EGADSR.h to big for current audio fragment size / sampling rate!"); |
329 |
|
|
330 |
// (re)create disk thread |
// (re)create disk thread |
331 |
if (this->pDiskThread) { |
if (this->pDiskThread) { |
332 |
|
dmsg(1,("Stopping disk thread...")); |
333 |
this->pDiskThread->StopThread(); |
this->pDiskThread->StopThread(); |
334 |
delete this->pDiskThread; |
delete this->pDiskThread; |
335 |
|
dmsg(1,("OK\n")); |
336 |
} |
} |
337 |
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 |
338 |
if (!pDiskThread) { |
if (!pDiskThread) { |
351 |
pEventGenerator = new EventGenerator(pAudioOut->SampleRate()); |
pEventGenerator = new EventGenerator(pAudioOut->SampleRate()); |
352 |
|
|
353 |
// (re)allocate synthesis parameter matrix |
// (re)allocate synthesis parameter matrix |
354 |
if (pSynthesisParameters[0]) delete[] pSynthesisParameters[0]; |
if (pSynthesisParameters[0]) free(pSynthesisParameters[0]); |
355 |
pSynthesisParameters[0] = new float[Event::destination_count * pAudioOut->MaxSamplesPerCycle()]; |
pSynthesisParameters[0] = (float *) memalign(16,(Event::destination_count * sizeof(float) * pAudioOut->MaxSamplesPerCycle())); |
356 |
for (int dst = 1; dst < Event::destination_count; dst++) |
for (int dst = 1; dst < Event::destination_count; dst++) |
357 |
pSynthesisParameters[dst] = pSynthesisParameters[dst - 1] + pAudioOut->MaxSamplesPerCycle(); |
pSynthesisParameters[dst] = pSynthesisParameters[dst - 1] + pAudioOut->MaxSamplesPerCycle(); |
358 |
|
|
408 |
} |
} |
409 |
|
|
410 |
|
|
411 |
|
// update time of start and end of this audio fragment (as events' time stamps relate to this) |
412 |
|
pEventGenerator->UpdateFragmentTime(Samples); |
413 |
|
|
414 |
|
|
415 |
// empty the event lists for the new fragment |
// empty the event lists for the new fragment |
416 |
pEvents->clear(); |
pEvents->clear(); |
417 |
pCCEvents->clear(); |
pCCEvents->clear(); |
426 |
} |
} |
427 |
} |
} |
428 |
|
|
|
// read and copy events from input queue |
|
|
Event event = pEventGenerator->CreateEvent(); |
|
|
while (true) { |
|
|
if (!pEventQueue->pop(&event) || pEvents->poolIsEmpty()) break; |
|
|
*pEvents->allocAppend() = event; |
|
|
} |
|
|
|
|
429 |
|
|
430 |
// 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 |
431 |
pEventGenerator->UpdateFragmentTime(Samples); |
{ |
432 |
|
RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
433 |
|
Event* pEvent; |
434 |
|
while (true) { |
435 |
|
// get next event from input event queue |
436 |
|
if (!(pEvent = eventQueueReader.pop())) break; |
437 |
|
// if younger event reached, ignore that and all subsequent ones for now |
438 |
|
if (pEvent->FragmentPos() >= Samples) { |
439 |
|
eventQueueReader--; |
440 |
|
dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples)); |
441 |
|
pEvent->ResetFragmentPos(); |
442 |
|
break; |
443 |
|
} |
444 |
|
// copy event to internal event list |
445 |
|
if (pEvents->poolIsEmpty()) { |
446 |
|
dmsg(1,("Event pool emtpy!\n")); |
447 |
|
break; |
448 |
|
} |
449 |
|
*pEvents->allocAppend() = *pEvent; |
450 |
|
} |
451 |
|
eventQueueReader.free(); // free all copied events from input queue |
452 |
|
} |
453 |
|
|
454 |
|
|
455 |
// process events |
// process events |
500 |
itVoice->Render(Samples); |
itVoice->Render(Samples); |
501 |
if (itVoice->IsActive()) active_voices++; // still active |
if (itVoice->IsActive()) active_voices++; // still active |
502 |
else { // voice reached end, is now inactive |
else { // voice reached end, is now inactive |
503 |
KillVoiceImmediately(itVoice); // remove voice from the list of active voices |
FreeVoice(itVoice); // remove voice from the list of active voices |
504 |
} |
} |
505 |
} |
} |
506 |
} |
} |
514 |
for (; itVoiceStealEvent != end; ++itVoiceStealEvent) { |
for (; itVoiceStealEvent != end; ++itVoiceStealEvent) { |
515 |
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); |
516 |
if (itNewVoice) { |
if (itNewVoice) { |
517 |
itNewVoice->Render(Samples); |
for (; itNewVoice; itNewVoice = itNewVoice->itChildVoice) { |
518 |
if (itNewVoice->IsActive()) active_voices++; // still active |
itNewVoice->Render(Samples); |
519 |
else { // voice reached end, is now inactive |
if (itNewVoice->IsActive()) active_voices++; // still active |
520 |
KillVoiceImmediately(itNewVoice); // remove voice from the list of active voices |
else { // voice reached end, is now inactive |
521 |
|
FreeVoice(itNewVoice); // remove voice from the list of active voices |
522 |
|
} |
523 |
} |
} |
524 |
} |
} |
525 |
else dmsg(1,("Ouch, voice stealing didn't work out!\n")); |
else dmsg(1,("gig::Engine: ERROR, voice stealing didn't work out!\n")); |
526 |
} |
} |
527 |
} |
} |
528 |
// reset voice stealing for the new fragment |
// reset voice stealing for the new fragment |
531 |
iuiLastStolenKey = RTList<uint>::Iterator(); |
iuiLastStolenKey = RTList<uint>::Iterator(); |
532 |
|
|
533 |
|
|
534 |
|
// free all keys which have no active voices left |
535 |
|
{ |
536 |
|
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
537 |
|
RTList<uint>::Iterator end = pActiveKeys->end(); |
538 |
|
while (iuiKey != end) { // iterate through all active keys |
539 |
|
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
540 |
|
++iuiKey; |
541 |
|
if (pKey->pActiveVoices->isEmpty()) FreeKey(pKey); |
542 |
|
#if DEVMODE |
543 |
|
else { // FIXME: should be removed before the final release (purpose: just a sanity check for debugging) |
544 |
|
RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first(); |
545 |
|
RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end(); |
546 |
|
for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key |
547 |
|
if (itVoice->itKillEvent) { |
548 |
|
dmsg(1,("gig::Engine: ERROR, killed voice survived !!!\n")); |
549 |
|
} |
550 |
|
} |
551 |
|
} |
552 |
|
#endif // DEVMODE |
553 |
|
} |
554 |
|
} |
555 |
|
|
556 |
|
|
557 |
// 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 |
558 |
// on the console for debugging purposes |
// on the console for debugging purposes |
559 |
ActiveVoiceCount = active_voices; |
ActiveVoiceCount = active_voices; |
680 |
RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents); |
RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents); |
681 |
|
|
682 |
// allocate and trigger a new voice for the key |
// allocate and trigger a new voice for the key |
683 |
LaunchVoice(itNoteOnEventOnKeyList); |
LaunchVoice(itNoteOnEventOnKeyList, 0, false, true); |
684 |
} |
} |
685 |
|
|
686 |
/** |
/** |
706 |
|
|
707 |
// spawn release triggered voice(s) if needed |
// spawn release triggered voice(s) if needed |
708 |
if (pKey->ReleaseTrigger) { |
if (pKey->ReleaseTrigger) { |
709 |
LaunchVoice(itNoteOffEventOnKeyList, 0, true); |
LaunchVoice(itNoteOffEventOnKeyList, 0, true, false); //FIXME: for the moment we don't perform voice stealing for release triggered samples |
710 |
pKey->ReleaseTrigger = false; |
pKey->ReleaseTrigger = false; |
711 |
} |
} |
712 |
} |
} |
745 |
Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend(); |
Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend(); |
746 |
if (itNewVoice) { |
if (itNewVoice) { |
747 |
// launch the new voice |
// launch the new voice |
748 |
if (itNewVoice->Trigger(itNoteOnEvent, this->Pitch, this->pInstrument, iLayer, ReleaseTriggerVoice) < 0) { |
if (itNewVoice->Trigger(itNoteOnEvent, this->Pitch, this->pInstrument, iLayer, ReleaseTriggerVoice, VoiceStealing) < 0) { |
749 |
dmsg(1,("Triggering new voice failed!\n")); |
dmsg(1,("Triggering new voice failed!\n")); |
750 |
pKey->pActiveVoices->free(itNewVoice); |
pKey->pActiveVoices->free(itNewVoice); |
751 |
} |
} |
775 |
return itNewVoice; // success |
return itNewVoice; // success |
776 |
} |
} |
777 |
} |
} |
778 |
else if (VoiceStealing) StealVoice(itNoteOnEvent, iLayer, ReleaseTriggerVoice); // no free voice left, so steal one |
else if (VoiceStealing) { |
779 |
|
// first, get total amount of required voices (dependant on amount of layers) |
780 |
|
::gig::Region* pRegion = pInstrument->GetRegion(itNoteOnEvent->Param.Note.Key); |
781 |
|
if (!pRegion) return Pool<Voice>::Iterator(); // nothing defined for this MIDI key, so no voice needed |
782 |
|
int voicesRequired = pRegion->Layers; |
783 |
|
|
784 |
|
// now steal the (remaining) amount of voices |
785 |
|
for (int i = iLayer; i < voicesRequired; i++) |
786 |
|
StealVoice(itNoteOnEvent); |
787 |
|
|
788 |
|
// put note-on event into voice-stealing queue, so it will be reprocessed after killed voice died |
789 |
|
RTList<Event>::Iterator itStealEvent = pVoiceStealingQueue->allocAppend(); |
790 |
|
if (itStealEvent) { |
791 |
|
*itStealEvent = *itNoteOnEvent; // copy event |
792 |
|
itStealEvent->Param.Note.Layer = iLayer; |
793 |
|
itStealEvent->Param.Note.ReleaseTrigger = ReleaseTriggerVoice; |
794 |
|
} |
795 |
|
else dmsg(1,("Voice stealing queue full!\n")); |
796 |
|
} |
797 |
|
|
798 |
return Pool<Voice>::Iterator(); // no free voice or error |
return Pool<Voice>::Iterator(); // no free voice or error |
799 |
} |
} |
804 |
* voice stealing and postpone the note-on event until the selected |
* voice stealing and postpone the note-on event until the selected |
805 |
* voice actually died. |
* voice actually died. |
806 |
* |
* |
807 |
* @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 |
|
808 |
*/ |
*/ |
809 |
void Engine::StealVoice(Pool<Event>::Iterator& itNoteOnEvent, int iLayer, bool ReleaseTriggerVoice) { |
void Engine::StealVoice(Pool<Event>::Iterator& itNoteOnEvent) { |
810 |
if (!pEventPool->poolIsEmpty()) { |
if (!pEventPool->poolIsEmpty()) { |
811 |
|
|
812 |
RTList<uint>::Iterator iuiOldestKey; |
RTList<uint>::Iterator iuiOldestKey; |
848 |
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiOldestKey]; |
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiOldestKey]; |
849 |
itOldestVoice = pOldestKey->pActiveVoices->first(); |
itOldestVoice = pOldestKey->pActiveVoices->first(); |
850 |
} |
} |
851 |
else { // too less voices, even for voice stealing |
else { |
852 |
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")); |
853 |
return; |
return; |
854 |
} |
} |
855 |
} |
} |
871 |
} |
} |
872 |
} |
} |
873 |
|
|
874 |
|
//FIXME: can be removed, just a sanity check for debugging |
875 |
|
if (!itOldestVoice->IsActive()) dmsg(1,("gig::Engine: ERROR, tried to steal a voice which was not active !!!\n")); |
876 |
|
|
877 |
// now kill the selected voice |
// now kill the selected voice |
878 |
itOldestVoice->Kill(itNoteOnEvent); |
itOldestVoice->Kill(itNoteOnEvent); |
879 |
// 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 |
880 |
this->itLastStolenVoice = itOldestVoice; |
this->itLastStolenVoice = itOldestVoice; |
881 |
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")); |
|
882 |
} |
} |
883 |
else dmsg(1,("Event pool emtpy!\n")); |
else dmsg(1,("Event pool emtpy!\n")); |
884 |
} |
} |
885 |
|
|
886 |
/** |
/** |
887 |
* 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. |
888 |
* 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 |
889 |
* 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 |
890 |
|
* just was killed. |
891 |
* |
* |
892 |
* @param itVoice - points to the voice to be killed |
* @param itVoice - points to the voice to be freed |
893 |
*/ |
*/ |
894 |
void Engine::KillVoiceImmediately(Pool<Voice>::Iterator& itVoice) { |
void Engine::FreeVoice(Pool<Voice>::Iterator& itVoice) { |
895 |
if (itVoice) { |
if (itVoice) { |
|
if (itVoice->IsActive()) itVoice->KillImmediately(); |
|
|
|
|
896 |
midi_key_info_t* pKey = &pMIDIKeyInfo[itVoice->MIDIKey]; |
midi_key_info_t* pKey = &pMIDIKeyInfo[itVoice->MIDIKey]; |
897 |
|
|
898 |
uint keygroup = itVoice->KeyGroup; |
uint keygroup = itVoice->KeyGroup; |
900 |
// free the voice object |
// free the voice object |
901 |
pVoicePool->free(itVoice); |
pVoicePool->free(itVoice); |
902 |
|
|
903 |
// 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 |
904 |
if (pKey->pActiveVoices->isEmpty()) { |
if (pKey->pActiveVoices->isEmpty() && keygroup) { |
905 |
if (keygroup) { // if voice / key belongs to a key group |
uint** ppKeyGroup = &ActiveKeyGroups[keygroup]; |
906 |
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")); |
|
907 |
} |
} |
908 |
} |
} |
909 |
else std::cerr << "Couldn't release voice! (pVoice == NULL)\n" << std::flush; |
else std::cerr << "Couldn't release voice! (!itVoice)\n" << std::flush; |
910 |
|
} |
911 |
|
|
912 |
|
/** |
913 |
|
* Called when there's no more voice left on a key, this call will |
914 |
|
* update the key info respectively. |
915 |
|
* |
916 |
|
* @param pKey - key which is now inactive |
917 |
|
*/ |
918 |
|
void Engine::FreeKey(midi_key_info_t* pKey) { |
919 |
|
if (pKey->pActiveVoices->isEmpty()) { |
920 |
|
pKey->Active = false; |
921 |
|
pActiveKeys->free(pKey->itSelf); // remove key from list of active keys |
922 |
|
pKey->itSelf = RTList<uint>::Iterator(); |
923 |
|
pKey->ReleaseTrigger = false; |
924 |
|
pKey->pEvents->clear(); |
925 |
|
dmsg(3,("Key has no more voices now\n")); |
926 |
|
} |
927 |
|
else dmsg(1,("gig::Engine: Oops, tried to free a key which contains voices.\n")); |
928 |
} |
} |
929 |
|
|
930 |
/** |
/** |
1174 |
} |
} |
1175 |
|
|
1176 |
String Engine::Version() { |
String Engine::Version() { |
1177 |
String s = "$Revision: 1.15 $"; |
String s = "$Revision: 1.21 $"; |
1178 |
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 |
1179 |
} |
} |
1180 |
|
|