27 |
|
|
28 |
#include "Engine.h" |
#include "Engine.h" |
29 |
|
|
30 |
|
#if defined(__APPLE__) |
31 |
|
# include <stdlib.h> |
32 |
|
#else |
33 |
|
# include <malloc.h> |
34 |
|
#endif |
35 |
|
|
36 |
namespace LinuxSampler { namespace gig { |
namespace LinuxSampler { namespace gig { |
37 |
|
|
38 |
InstrumentResourceManager Engine::Instruments; |
InstrumentResourceManager Engine::Instruments; |
82 |
|
|
83 |
Engine::~Engine() { |
Engine::~Engine() { |
84 |
if (pDiskThread) { |
if (pDiskThread) { |
85 |
|
dmsg(1,("Stopping disk thread...")); |
86 |
pDiskThread->StopThread(); |
pDiskThread->StopThread(); |
87 |
delete pDiskThread; |
delete pDiskThread; |
88 |
|
dmsg(1,("OK\n")); |
89 |
} |
} |
90 |
|
|
91 |
|
if (pInstrument) Instruments.HandBack(pInstrument, this); |
92 |
|
|
93 |
if (pGig) delete pGig; |
if (pGig) delete pGig; |
94 |
if (pRIFF) delete pRIFF; |
if (pRIFF) delete pRIFF; |
95 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
99 |
for (uint i = 0; i < Event::destination_count; i++) { |
for (uint i = 0; i < Event::destination_count; i++) { |
100 |
if (pSynthesisEvents[i]) delete pSynthesisEvents[i]; |
if (pSynthesisEvents[i]) delete pSynthesisEvents[i]; |
101 |
} |
} |
|
delete[] pSynthesisEvents; |
|
102 |
if (pEvents) delete pEvents; |
if (pEvents) delete pEvents; |
103 |
if (pCCEvents) delete pCCEvents; |
if (pCCEvents) delete pCCEvents; |
104 |
if (pEventQueue) delete pEventQueue; |
if (pEventQueue) delete pEventQueue; |
105 |
if (pEventPool) delete pEventPool; |
if (pEventPool) delete pEventPool; |
106 |
if (pVoicePool) delete pVoicePool; |
if (pVoicePool) { |
107 |
|
pVoicePool->clear(); |
108 |
|
delete pVoicePool; |
109 |
|
} |
110 |
if (pActiveKeys) delete pActiveKeys; |
if (pActiveKeys) delete pActiveKeys; |
111 |
if (pSysexBuffer) delete pSysexBuffer; |
if (pSysexBuffer) delete pSysexBuffer; |
112 |
if (pEventGenerator) delete pEventGenerator; |
if (pEventGenerator) delete pEventGenerator; |
113 |
if (pMainFilterParameters) delete[] pMainFilterParameters; |
if (pMainFilterParameters) delete[] pMainFilterParameters; |
114 |
if (pBasicFilterParameters) delete[] pBasicFilterParameters; |
if (pBasicFilterParameters) delete[] pBasicFilterParameters; |
115 |
if (pSynthesisParameters[0]) delete[] pSynthesisParameters[0]; |
if (pSynthesisParameters[0]) free(pSynthesisParameters[0]); |
116 |
if (pVoiceStealingQueue) delete pVoiceStealingQueue; |
if (pVoiceStealingQueue) delete pVoiceStealingQueue; |
117 |
} |
} |
118 |
|
|
171 |
ActiveVoiceCount = 0; |
ActiveVoiceCount = 0; |
172 |
ActiveVoiceCountMax = 0; |
ActiveVoiceCountMax = 0; |
173 |
GlobalVolume = 1.0; |
GlobalVolume = 1.0; |
174 |
|
CurrentKeyDimension = 0; |
175 |
|
|
176 |
// reset voice stealing parameters |
// reset voice stealing parameters |
177 |
itLastStolenVoice = RTList<Voice>::Iterator(); |
itLastStolenVoice = RTList<Voice>::Iterator(); |
335 |
|
|
336 |
// (re)create disk thread |
// (re)create disk thread |
337 |
if (this->pDiskThread) { |
if (this->pDiskThread) { |
338 |
|
dmsg(1,("Stopping disk thread...")); |
339 |
this->pDiskThread->StopThread(); |
this->pDiskThread->StopThread(); |
340 |
delete this->pDiskThread; |
delete this->pDiskThread; |
341 |
|
dmsg(1,("OK\n")); |
342 |
} |
} |
343 |
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 |
344 |
if (!pDiskThread) { |
if (!pDiskThread) { |
357 |
pEventGenerator = new EventGenerator(pAudioOut->SampleRate()); |
pEventGenerator = new EventGenerator(pAudioOut->SampleRate()); |
358 |
|
|
359 |
// (re)allocate synthesis parameter matrix |
// (re)allocate synthesis parameter matrix |
360 |
if (pSynthesisParameters[0]) delete[] pSynthesisParameters[0]; |
if (pSynthesisParameters[0]) free(pSynthesisParameters[0]); |
361 |
pSynthesisParameters[0] = new float[Event::destination_count * pAudioOut->MaxSamplesPerCycle()]; |
|
362 |
|
#if defined(__APPLE__) |
363 |
|
pSynthesisParameters[0] = (float *) malloc(Event::destination_count * sizeof(float) * pAudioOut->MaxSamplesPerCycle()); |
364 |
|
#else |
365 |
|
pSynthesisParameters[0] = (float *) memalign(16,(Event::destination_count * sizeof(float) * pAudioOut->MaxSamplesPerCycle())); |
366 |
|
#endif |
367 |
for (int dst = 1; dst < Event::destination_count; dst++) |
for (int dst = 1; dst < Event::destination_count; dst++) |
368 |
pSynthesisParameters[dst] = pSynthesisParameters[dst - 1] + pAudioOut->MaxSamplesPerCycle(); |
pSynthesisParameters[dst] = pSynthesisParameters[dst - 1] + pAudioOut->MaxSamplesPerCycle(); |
369 |
|
|
419 |
} |
} |
420 |
|
|
421 |
|
|
422 |
|
// update time of start and end of this audio fragment (as events' time stamps relate to this) |
423 |
|
pEventGenerator->UpdateFragmentTime(Samples); |
424 |
|
|
425 |
|
|
426 |
// empty the event lists for the new fragment |
// empty the event lists for the new fragment |
427 |
pEvents->clear(); |
pEvents->clear(); |
428 |
pCCEvents->clear(); |
pCCEvents->clear(); |
437 |
} |
} |
438 |
} |
} |
439 |
|
|
|
// read and copy events from input queue |
|
|
Event event = pEventGenerator->CreateEvent(); |
|
|
while (true) { |
|
|
if (!pEventQueue->pop(&event) || pEvents->poolIsEmpty()) break; |
|
|
*pEvents->allocAppend() = event; |
|
|
} |
|
|
|
|
440 |
|
|
441 |
// 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 |
442 |
pEventGenerator->UpdateFragmentTime(Samples); |
{ |
443 |
|
RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
444 |
|
Event* pEvent; |
445 |
|
while (true) { |
446 |
|
// get next event from input event queue |
447 |
|
if (!(pEvent = eventQueueReader.pop())) break; |
448 |
|
// if younger event reached, ignore that and all subsequent ones for now |
449 |
|
if (pEvent->FragmentPos() >= Samples) { |
450 |
|
eventQueueReader--; |
451 |
|
dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples)); |
452 |
|
pEvent->ResetFragmentPos(); |
453 |
|
break; |
454 |
|
} |
455 |
|
// copy event to internal event list |
456 |
|
if (pEvents->poolIsEmpty()) { |
457 |
|
dmsg(1,("Event pool emtpy!\n")); |
458 |
|
break; |
459 |
|
} |
460 |
|
*pEvents->allocAppend() = *pEvent; |
461 |
|
} |
462 |
|
eventQueueReader.free(); // free all copied events from input queue |
463 |
|
} |
464 |
|
|
465 |
|
|
466 |
// process events |
// process events |
533 |
} |
} |
534 |
} |
} |
535 |
} |
} |
536 |
else dmsg(1,("Ouch, voice stealing didn't work out!\n")); |
else dmsg(1,("gig::Engine: ERROR, voice stealing didn't work out!\n")); |
537 |
} |
} |
538 |
} |
} |
539 |
// reset voice stealing for the new fragment |
// reset voice stealing for the new fragment |
542 |
iuiLastStolenKey = RTList<uint>::Iterator(); |
iuiLastStolenKey = RTList<uint>::Iterator(); |
543 |
|
|
544 |
|
|
545 |
|
// free all keys which have no active voices left |
546 |
|
{ |
547 |
|
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
548 |
|
RTList<uint>::Iterator end = pActiveKeys->end(); |
549 |
|
while (iuiKey != end) { // iterate through all active keys |
550 |
|
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
551 |
|
++iuiKey; |
552 |
|
if (pKey->pActiveVoices->isEmpty()) FreeKey(pKey); |
553 |
|
#if DEVMODE |
554 |
|
else { // FIXME: should be removed before the final release (purpose: just a sanity check for debugging) |
555 |
|
RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first(); |
556 |
|
RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end(); |
557 |
|
for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key |
558 |
|
if (itVoice->itKillEvent) { |
559 |
|
dmsg(1,("gig::Engine: ERROR, killed voice survived !!!\n")); |
560 |
|
} |
561 |
|
} |
562 |
|
} |
563 |
|
#endif // DEVMODE |
564 |
|
} |
565 |
|
} |
566 |
|
|
567 |
|
|
568 |
// 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 |
569 |
// on the console for debugging purposes |
// on the console for debugging purposes |
570 |
ActiveVoiceCount = active_voices; |
ActiveVoiceCount = active_voices; |
673 |
* @param itNoteOnEvent - key, velocity and time stamp of the event |
* @param itNoteOnEvent - key, velocity and time stamp of the event |
674 |
*/ |
*/ |
675 |
void Engine::ProcessNoteOn(Pool<Event>::Iterator& itNoteOnEvent) { |
void Engine::ProcessNoteOn(Pool<Event>::Iterator& itNoteOnEvent) { |
676 |
midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
|
677 |
|
const int key = itNoteOnEvent->Param.Note.Key; |
678 |
|
|
679 |
|
// Change key dimension value if key is in keyswitching area |
680 |
|
if (key >= pInstrument->DimensionKeyRange.low && key <= pInstrument->DimensionKeyRange.high) |
681 |
|
CurrentKeyDimension = ((key - pInstrument->DimensionKeyRange.low) * 128) / |
682 |
|
(pInstrument->DimensionKeyRange.high - pInstrument->DimensionKeyRange.low + 1); |
683 |
|
|
684 |
|
midi_key_info_t* pKey = &pMIDIKeyInfo[key]; |
685 |
|
|
686 |
pKey->KeyPressed = true; // the MIDI key was now pressed down |
pKey->KeyPressed = true; // the MIDI key was now pressed down |
687 |
|
|
699 |
RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents); |
RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents); |
700 |
|
|
701 |
// allocate and trigger a new voice for the key |
// allocate and trigger a new voice for the key |
702 |
LaunchVoice(itNoteOnEventOnKeyList); |
LaunchVoice(itNoteOnEventOnKeyList, 0, false, true); |
703 |
} |
} |
704 |
|
|
705 |
/** |
/** |
725 |
|
|
726 |
// spawn release triggered voice(s) if needed |
// spawn release triggered voice(s) if needed |
727 |
if (pKey->ReleaseTrigger) { |
if (pKey->ReleaseTrigger) { |
728 |
LaunchVoice(itNoteOffEventOnKeyList, 0, true); |
LaunchVoice(itNoteOffEventOnKeyList, 0, true, false); //FIXME: for the moment we don't perform voice stealing for release triggered samples |
729 |
pKey->ReleaseTrigger = false; |
pKey->ReleaseTrigger = false; |
730 |
} |
} |
731 |
} |
} |
755 |
* when there is no free voice |
* when there is no free voice |
756 |
* (optional, default = true) |
* (optional, default = true) |
757 |
* @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 |
758 |
* if an error occured while trying to trigger the new voice |
* if the voice wasn't triggered (for example when no region is |
759 |
|
* defined for the given key). |
760 |
*/ |
*/ |
761 |
Pool<Voice>::Iterator Engine::LaunchVoice(Pool<Event>::Iterator& itNoteOnEvent, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing) { |
Pool<Voice>::Iterator Engine::LaunchVoice(Pool<Event>::Iterator& itNoteOnEvent, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing) { |
762 |
midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
765 |
Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend(); |
Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend(); |
766 |
if (itNewVoice) { |
if (itNewVoice) { |
767 |
// launch the new voice |
// launch the new voice |
768 |
if (itNewVoice->Trigger(itNoteOnEvent, this->Pitch, this->pInstrument, iLayer, ReleaseTriggerVoice) < 0) { |
if (itNewVoice->Trigger(itNoteOnEvent, this->Pitch, this->pInstrument, iLayer, ReleaseTriggerVoice, VoiceStealing) < 0) { |
769 |
dmsg(1,("Triggering new voice failed!\n")); |
dmsg(4,("Voice not triggered\n")); |
770 |
pKey->pActiveVoices->free(itNewVoice); |
pKey->pActiveVoices->free(itNewVoice); |
771 |
} |
} |
772 |
else { // on success |
else { // on success |
891 |
} |
} |
892 |
} |
} |
893 |
|
|
894 |
|
//FIXME: can be removed, just a sanity check for debugging |
895 |
|
if (!itOldestVoice->IsActive()) dmsg(1,("gig::Engine: ERROR, tried to steal a voice which was not active !!!\n")); |
896 |
|
|
897 |
// now kill the selected voice |
// now kill the selected voice |
898 |
itOldestVoice->Kill(itNoteOnEvent); |
itOldestVoice->Kill(itNoteOnEvent); |
899 |
// 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 |
920 |
// free the voice object |
// free the voice object |
921 |
pVoicePool->free(itVoice); |
pVoicePool->free(itVoice); |
922 |
|
|
923 |
// 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 |
924 |
if (pKey->pActiveVoices->isEmpty()) { |
if (pKey->pActiveVoices->isEmpty() && keygroup) { |
925 |
if (keygroup) { // if voice / key belongs to a key group |
uint** ppKeyGroup = &ActiveKeyGroups[keygroup]; |
926 |
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")); |
|
927 |
} |
} |
928 |
} |
} |
929 |
else std::cerr << "Couldn't release voice! (!itVoice)\n" << std::flush; |
else std::cerr << "Couldn't release voice! (!itVoice)\n" << std::flush; |
930 |
} |
} |
931 |
|
|
932 |
/** |
/** |
933 |
|
* Called when there's no more voice left on a key, this call will |
934 |
|
* update the key info respectively. |
935 |
|
* |
936 |
|
* @param pKey - key which is now inactive |
937 |
|
*/ |
938 |
|
void Engine::FreeKey(midi_key_info_t* pKey) { |
939 |
|
if (pKey->pActiveVoices->isEmpty()) { |
940 |
|
pKey->Active = false; |
941 |
|
pActiveKeys->free(pKey->itSelf); // remove key from list of active keys |
942 |
|
pKey->itSelf = RTList<uint>::Iterator(); |
943 |
|
pKey->ReleaseTrigger = false; |
944 |
|
pKey->pEvents->clear(); |
945 |
|
dmsg(3,("Key has no more voices now\n")); |
946 |
|
} |
947 |
|
else dmsg(1,("gig::Engine: Oops, tried to free a key which contains voices.\n")); |
948 |
|
} |
949 |
|
|
950 |
|
/** |
951 |
* Reacts on supported control change commands (e.g. pitch bend wheel, |
* Reacts on supported control change commands (e.g. pitch bend wheel, |
952 |
* modulation wheel, aftertouch). |
* modulation wheel, aftertouch). |
953 |
* |
* |
1194 |
} |
} |
1195 |
|
|
1196 |
String Engine::Version() { |
String Engine::Version() { |
1197 |
String s = "$Revision: 1.16 $"; |
String s = "$Revision: 1.23 $"; |
1198 |
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 |
1199 |
} |
} |
1200 |
|
|