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 |
|
|
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; |
46 |
pEventGenerator = NULL; |
pEventGenerator = NULL; |
47 |
pSysexBuffer = new RingBuffer<uint8_t>(SYSEX_BUFFER_SIZE, 0); |
pSysexBuffer = new RingBuffer<uint8_t>(SYSEX_BUFFER_SIZE, 0); |
48 |
pEventQueue = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0); |
pEventQueue = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0); |
49 |
pEventPool = new RTELMemoryPool<Event>(MAX_EVENTS_PER_FRAGMENT); |
pEventPool = new Pool<Event>(MAX_EVENTS_PER_FRAGMENT); |
50 |
pVoicePool = new RTELMemoryPool<Voice>(MAX_AUDIO_VOICES); |
pVoicePool = new Pool<Voice>(MAX_AUDIO_VOICES); |
51 |
pActiveKeys = new RTELMemoryPool<uint>(128); |
pActiveKeys = new Pool<uint>(128); |
52 |
pEvents = new RTEList<Event>(pEventPool); |
pVoiceStealingQueue = new RTList<Event>(pEventPool); |
53 |
pCCEvents = new RTEList<Event>(pEventPool); |
pEvents = new RTList<Event>(pEventPool); |
54 |
|
pCCEvents = new RTList<Event>(pEventPool); |
55 |
for (uint i = 0; i < Event::destination_count; i++) { |
for (uint i = 0; i < Event::destination_count; i++) { |
56 |
pSynthesisEvents[i] = new RTEList<Event>(pEventPool); |
pSynthesisEvents[i] = new RTList<Event>(pEventPool); |
57 |
} |
} |
58 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
59 |
pMIDIKeyInfo[i].pActiveVoices = new RTEList<Voice>(pVoicePool); |
pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pVoicePool); |
60 |
pMIDIKeyInfo[i].KeyPressed = false; |
pMIDIKeyInfo[i].KeyPressed = false; |
61 |
pMIDIKeyInfo[i].Active = false; |
pMIDIKeyInfo[i].Active = false; |
62 |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
63 |
pMIDIKeyInfo[i].pSelf = NULL; |
pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEventPool); |
|
pMIDIKeyInfo[i].pEvents = new RTEList<Event>(pEventPool); |
|
64 |
} |
} |
65 |
for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) { |
for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { |
66 |
pVoice->SetEngine(this); |
iterVoice->SetEngine(this); |
67 |
} |
} |
68 |
pVoicePool->clear(); |
pVoicePool->clear(); |
69 |
|
|
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; |
117 |
} |
} |
118 |
|
|
119 |
void Engine::Enable() { |
void Engine::Enable() { |
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 |
177 |
|
itLastStolenVoice = RTList<Voice>::Iterator(); |
178 |
|
iuiLastStolenKey = RTList<uint>::Iterator(); |
179 |
|
pVoiceStealingQueue->clear(); |
180 |
|
|
181 |
// reset to normal chromatic scale (means equal temper) |
// reset to normal chromatic scale (means equal temper) |
182 |
memset(&ScaleTuning[0], 0x00, 12); |
memset(&ScaleTuning[0], 0x00, 12); |
191 |
pMIDIKeyInfo[i].KeyPressed = false; |
pMIDIKeyInfo[i].KeyPressed = false; |
192 |
pMIDIKeyInfo[i].Active = false; |
pMIDIKeyInfo[i].Active = false; |
193 |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
194 |
pMIDIKeyInfo[i].pSelf = NULL; |
pMIDIKeyInfo[i].itSelf = Pool<uint>::Iterator(); |
195 |
} |
} |
196 |
|
|
197 |
// reset all key groups |
// reset all key groups |
199 |
for (; iter != ActiveKeyGroups.end(); iter++) iter->second = NULL; |
for (; iter != ActiveKeyGroups.end(); iter++) iter->second = NULL; |
200 |
|
|
201 |
// reset all voices |
// reset all voices |
202 |
for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) { |
for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { |
203 |
pVoice->Reset(); |
iterVoice->Reset(); |
204 |
} |
} |
205 |
pVoicePool->clear(); |
pVoicePool->clear(); |
206 |
|
|
215 |
} |
} |
216 |
|
|
217 |
/** |
/** |
218 |
* Load an instrument from a .gig file. |
* More or less a workaround to set the instrument name, index and load |
219 |
|
* status variable to zero percent immediately, that is without blocking |
220 |
|
* the calling thread. It might be used in future for other preparations |
221 |
|
* as well though. |
222 |
|
* |
223 |
|
* @param FileName - file name of the Gigasampler instrument file |
224 |
|
* @param Instrument - index of the instrument in the .gig file |
225 |
|
* @see LoadInstrument() |
226 |
|
*/ |
227 |
|
void Engine::PrepareLoadInstrument(const char* FileName, uint Instrument) { |
228 |
|
InstrumentFile = FileName; |
229 |
|
InstrumentIdx = Instrument; |
230 |
|
InstrumentStat = 0; |
231 |
|
} |
232 |
|
|
233 |
|
/** |
234 |
|
* Load an instrument from a .gig file. PrepareLoadInstrument() has to |
235 |
|
* be called first to provide the information which instrument to load. |
236 |
|
* This method will then actually start to load the instrument and block |
237 |
|
* the calling thread until loading was completed. |
238 |
* |
* |
239 |
* @param FileName - file name of the Gigasampler instrument file |
* @returns detailed description of the method call result |
240 |
* @param Instrument - index of the instrument in the .gig file |
* @see PrepareLoadInstrument() |
|
* @throws LinuxSamplerException on error |
|
|
* @returns detailed description of the method call result |
|
241 |
*/ |
*/ |
242 |
void Engine::LoadInstrument(const char* FileName, uint Instrument) { |
void Engine::LoadInstrument() { |
243 |
|
|
244 |
DisableAndLock(); |
DisableAndLock(); |
245 |
|
|
251 |
Instruments.HandBack(pInstrument, this); |
Instruments.HandBack(pInstrument, this); |
252 |
} |
} |
253 |
|
|
|
InstrumentFile = FileName; |
|
|
InstrumentIdx = Instrument; |
|
|
InstrumentStat = 0; |
|
|
|
|
254 |
// delete all key groups |
// delete all key groups |
255 |
ActiveKeyGroups.clear(); |
ActiveKeyGroups.clear(); |
256 |
|
|
257 |
// request gig instrument from instrument manager |
// request gig instrument from instrument manager |
258 |
try { |
try { |
259 |
instrument_id_t instrid; |
instrument_id_t instrid; |
260 |
instrid.FileName = FileName; |
instrid.FileName = InstrumentFile; |
261 |
instrid.iInstrument = Instrument; |
instrid.iInstrument = InstrumentIdx; |
262 |
pInstrument = Instruments.Borrow(instrid, this); |
pInstrument = Instruments.Borrow(instrid, this); |
263 |
if (!pInstrument) { |
if (!pInstrument) { |
264 |
InstrumentStat = -1; |
InstrumentStat = -1; |
285 |
for (::gig::Region* pRegion = pInstrument->GetFirstRegion(); pRegion; pRegion = pInstrument->GetNextRegion()) |
for (::gig::Region* pRegion = pInstrument->GetFirstRegion(); pRegion; pRegion = pInstrument->GetNextRegion()) |
286 |
if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL; |
if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL; |
287 |
|
|
288 |
|
InstrumentIdxName = pInstrument->pInfo->Name; |
289 |
InstrumentStat = 100; |
InstrumentStat = 100; |
290 |
|
|
291 |
// inform audio driver for the need of two channels |
// inform audio driver for the need of two channels |
342 |
this->MaxSamplesPerCycle = pAudioOutputDevice->MaxSamplesPerCycle(); |
this->MaxSamplesPerCycle = pAudioOutputDevice->MaxSamplesPerCycle(); |
343 |
this->SampleRate = pAudioOutputDevice->SampleRate(); |
this->SampleRate = pAudioOutputDevice->SampleRate(); |
344 |
|
|
345 |
|
// FIXME: audio drivers with varying fragment sizes might be a problem here |
346 |
|
MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * EG_MIN_RELEASE_TIME) - 1; |
347 |
|
if (MaxFadeOutPos < 0) |
348 |
|
throw LinuxSamplerException("EG_MIN_RELEASE_TIME in EGADSR.h to big for current audio fragment size / sampling rate!"); |
349 |
|
|
350 |
// (re)create disk thread |
// (re)create disk thread |
351 |
if (this->pDiskThread) { |
if (this->pDiskThread) { |
352 |
|
dmsg(1,("Stopping disk thread...")); |
353 |
this->pDiskThread->StopThread(); |
this->pDiskThread->StopThread(); |
354 |
delete this->pDiskThread; |
delete this->pDiskThread; |
355 |
|
dmsg(1,("OK\n")); |
356 |
} |
} |
357 |
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 |
358 |
if (!pDiskThread) { |
if (!pDiskThread) { |
360 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
361 |
} |
} |
362 |
|
|
363 |
for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) { |
for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { |
364 |
pVoice->pDiskThread = this->pDiskThread; |
iterVoice->pDiskThread = this->pDiskThread; |
365 |
dmsg(3,("d")); |
dmsg(3,("d")); |
366 |
} |
} |
367 |
pVoicePool->clear(); |
pVoicePool->clear(); |
371 |
pEventGenerator = new EventGenerator(pAudioOut->SampleRate()); |
pEventGenerator = new EventGenerator(pAudioOut->SampleRate()); |
372 |
|
|
373 |
// (re)allocate synthesis parameter matrix |
// (re)allocate synthesis parameter matrix |
374 |
if (pSynthesisParameters[0]) delete[] pSynthesisParameters[0]; |
if (pSynthesisParameters[0]) free(pSynthesisParameters[0]); |
375 |
pSynthesisParameters[0] = new float[Event::destination_count * pAudioOut->MaxSamplesPerCycle()]; |
|
376 |
|
#if defined(__APPLE__) |
377 |
|
pSynthesisParameters[0] = (float *) malloc(Event::destination_count * sizeof(float) * pAudioOut->MaxSamplesPerCycle()); |
378 |
|
#else |
379 |
|
pSynthesisParameters[0] = (float *) memalign(16,(Event::destination_count * sizeof(float) * pAudioOut->MaxSamplesPerCycle())); |
380 |
|
#endif |
381 |
for (int dst = 1; dst < Event::destination_count; dst++) |
for (int dst = 1; dst < Event::destination_count; dst++) |
382 |
pSynthesisParameters[dst] = pSynthesisParameters[dst - 1] + pAudioOut->MaxSamplesPerCycle(); |
pSynthesisParameters[dst] = pSynthesisParameters[dst - 1] + pAudioOut->MaxSamplesPerCycle(); |
383 |
|
|
391 |
pDiskThread->StartThread(); |
pDiskThread->StartThread(); |
392 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
393 |
|
|
394 |
for (Voice* pVoice = pVoicePool->first(); pVoice; pVoice = pVoicePool->next()) { |
for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { |
395 |
if (!pVoice->pDiskThread) { |
if (!iterVoice->pDiskThread) { |
396 |
dmsg(0,("Engine -> voice::trigger: !pDiskThread\n")); |
dmsg(0,("Engine -> voice::trigger: !pDiskThread\n")); |
397 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
398 |
} |
} |
433 |
} |
} |
434 |
|
|
435 |
|
|
436 |
|
// update time of start and end of this audio fragment (as events' time stamps relate to this) |
437 |
|
pEventGenerator->UpdateFragmentTime(Samples); |
438 |
|
|
439 |
|
|
440 |
// empty the event lists for the new fragment |
// empty the event lists for the new fragment |
441 |
pEvents->clear(); |
pEvents->clear(); |
442 |
pCCEvents->clear(); |
pCCEvents->clear(); |
443 |
for (uint i = 0; i < Event::destination_count; i++) { |
for (uint i = 0; i < Event::destination_count; i++) { |
444 |
pSynthesisEvents[i]->clear(); |
pSynthesisEvents[i]->clear(); |
445 |
} |
} |
446 |
|
{ |
447 |
// read and copy events from input queue |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
448 |
Event event = pEventGenerator->CreateEvent(); |
RTList<uint>::Iterator end = pActiveKeys->end(); |
449 |
while (true) { |
for(; iuiKey != end; ++iuiKey) { |
450 |
if (!pEventQueue->pop(&event)) break; |
pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key |
451 |
pEvents->alloc_assign(event); |
} |
452 |
} |
} |
453 |
|
|
454 |
|
|
455 |
// 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 |
456 |
pEventGenerator->UpdateFragmentTime(Samples); |
{ |
457 |
|
RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
458 |
|
Event* pEvent; |
459 |
|
while (true) { |
460 |
|
// get next event from input event queue |
461 |
|
if (!(pEvent = eventQueueReader.pop())) break; |
462 |
|
// if younger event reached, ignore that and all subsequent ones for now |
463 |
|
if (pEvent->FragmentPos() >= Samples) { |
464 |
|
eventQueueReader--; |
465 |
|
dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples)); |
466 |
|
pEvent->ResetFragmentPos(); |
467 |
|
break; |
468 |
|
} |
469 |
|
// copy event to internal event list |
470 |
|
if (pEvents->poolIsEmpty()) { |
471 |
|
dmsg(1,("Event pool emtpy!\n")); |
472 |
|
break; |
473 |
|
} |
474 |
|
*pEvents->allocAppend() = *pEvent; |
475 |
|
} |
476 |
|
eventQueueReader.free(); // free all copied events from input queue |
477 |
|
} |
478 |
|
|
479 |
|
|
480 |
// process events |
// process events |
481 |
Event* pNextEvent = pEvents->first(); |
{ |
482 |
while (pNextEvent) { |
RTList<Event>::Iterator itEvent = pEvents->first(); |
483 |
Event* pEvent = pNextEvent; |
RTList<Event>::Iterator end = pEvents->end(); |
484 |
pEvents->set_current(pEvent); |
for (; itEvent != end; ++itEvent) { |
485 |
pNextEvent = pEvents->next(); |
switch (itEvent->Type) { |
486 |
switch (pEvent->Type) { |
case Event::type_note_on: |
487 |
case Event::type_note_on: |
dmsg(5,("Engine: Note on received\n")); |
488 |
dmsg(5,("Engine: Note on received\n")); |
ProcessNoteOn(itEvent); |
489 |
ProcessNoteOn(pEvent); |
break; |
490 |
break; |
case Event::type_note_off: |
491 |
case Event::type_note_off: |
dmsg(5,("Engine: Note off received\n")); |
492 |
dmsg(5,("Engine: Note off received\n")); |
ProcessNoteOff(itEvent); |
493 |
ProcessNoteOff(pEvent); |
break; |
494 |
break; |
case Event::type_control_change: |
495 |
case Event::type_control_change: |
dmsg(5,("Engine: MIDI CC received\n")); |
496 |
dmsg(5,("Engine: MIDI CC received\n")); |
ProcessControlChange(itEvent); |
497 |
ProcessControlChange(pEvent); |
break; |
498 |
break; |
case Event::type_pitchbend: |
499 |
case Event::type_pitchbend: |
dmsg(5,("Engine: Pitchbend received\n")); |
500 |
dmsg(5,("Engine: Pitchbend received\n")); |
ProcessPitchbend(itEvent); |
501 |
ProcessPitchbend(pEvent); |
break; |
502 |
break; |
case Event::type_sysex: |
503 |
case Event::type_sysex: |
dmsg(5,("Engine: Sysex received\n")); |
504 |
dmsg(5,("Engine: Sysex received\n")); |
ProcessSysex(itEvent); |
505 |
ProcessSysex(pEvent); |
break; |
506 |
break; |
} |
507 |
} |
} |
508 |
} |
} |
509 |
|
|
510 |
|
|
|
// render audio from all active voices |
|
511 |
int active_voices = 0; |
int active_voices = 0; |
512 |
uint* piKey = pActiveKeys->first(); |
|
513 |
while (piKey) { // iterate through all active keys |
// render audio from all active voices |
514 |
midi_key_info_t* pKey = &pMIDIKeyInfo[*piKey]; |
{ |
515 |
pActiveKeys->set_current(piKey); |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
516 |
piKey = pActiveKeys->next(); |
RTList<uint>::Iterator end = pActiveKeys->end(); |
517 |
|
while (iuiKey != end) { // iterate through all active keys |
518 |
Voice* pVoiceNext = pKey->pActiveVoices->first(); |
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
519 |
while (pVoiceNext) { // iterate through all voices on this key |
++iuiKey; |
520 |
// already get next voice on key |
|
521 |
Voice* pVoice = pVoiceNext; |
RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first(); |
522 |
pKey->pActiveVoices->set_current(pVoice); |
RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end(); |
523 |
pVoiceNext = pKey->pActiveVoices->next(); |
for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key |
524 |
|
// now render current voice |
525 |
// now render current voice |
itVoice->Render(Samples); |
526 |
pVoice->Render(Samples); |
if (itVoice->IsActive()) active_voices++; // still active |
527 |
if (pVoice->IsActive()) active_voices++; // still active |
else { // voice reached end, is now inactive |
528 |
else { // voice reached end, is now inactive |
FreeVoice(itVoice); // remove voice from the list of active voices |
529 |
KillVoiceImmediately(pVoice); // remove voice from the list of active voices |
} |
530 |
} |
} |
531 |
} |
} |
532 |
pKey->pEvents->clear(); // free all events on the key |
} |
533 |
|
|
534 |
|
|
535 |
|
// now render all postponed voices from voice stealing |
536 |
|
{ |
537 |
|
RTList<Event>::Iterator itVoiceStealEvent = pVoiceStealingQueue->first(); |
538 |
|
RTList<Event>::Iterator end = pVoiceStealingQueue->end(); |
539 |
|
for (; itVoiceStealEvent != end; ++itVoiceStealEvent) { |
540 |
|
Pool<Voice>::Iterator itNewVoice = LaunchVoice(itVoiceStealEvent, itVoiceStealEvent->Param.Note.Layer, itVoiceStealEvent->Param.Note.ReleaseTrigger, false); |
541 |
|
if (itNewVoice) { |
542 |
|
for (; itNewVoice; itNewVoice = itNewVoice->itChildVoice) { |
543 |
|
itNewVoice->Render(Samples); |
544 |
|
if (itNewVoice->IsActive()) active_voices++; // still active |
545 |
|
else { // voice reached end, is now inactive |
546 |
|
FreeVoice(itNewVoice); // remove voice from the list of active voices |
547 |
|
} |
548 |
|
} |
549 |
|
} |
550 |
|
else dmsg(1,("gig::Engine: ERROR, voice stealing didn't work out!\n")); |
551 |
|
} |
552 |
|
} |
553 |
|
// reset voice stealing for the new fragment |
554 |
|
pVoiceStealingQueue->clear(); |
555 |
|
itLastStolenVoice = RTList<Voice>::Iterator(); |
556 |
|
iuiLastStolenKey = RTList<uint>::Iterator(); |
557 |
|
|
558 |
|
|
559 |
|
// free all keys which have no active voices left |
560 |
|
{ |
561 |
|
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
562 |
|
RTList<uint>::Iterator end = pActiveKeys->end(); |
563 |
|
while (iuiKey != end) { // iterate through all active keys |
564 |
|
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
565 |
|
++iuiKey; |
566 |
|
if (pKey->pActiveVoices->isEmpty()) FreeKey(pKey); |
567 |
|
#if DEVMODE |
568 |
|
else { // FIXME: should be removed before the final release (purpose: just a sanity check for debugging) |
569 |
|
RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first(); |
570 |
|
RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end(); |
571 |
|
for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key |
572 |
|
if (itVoice->itKillEvent) { |
573 |
|
dmsg(1,("gig::Engine: ERROR, killed voice survived !!!\n")); |
574 |
|
} |
575 |
|
} |
576 |
|
} |
577 |
|
#endif // DEVMODE |
578 |
|
} |
579 |
} |
} |
580 |
|
|
581 |
|
|
684 |
/** |
/** |
685 |
* Assigns and triggers a new voice for the respective MIDI key. |
* Assigns and triggers a new voice for the respective MIDI key. |
686 |
* |
* |
687 |
* @param pNoteOnEvent - key, velocity and time stamp of the event |
* @param itNoteOnEvent - key, velocity and time stamp of the event |
688 |
*/ |
*/ |
689 |
void Engine::ProcessNoteOn(Event* pNoteOnEvent) { |
void Engine::ProcessNoteOn(Pool<Event>::Iterator& itNoteOnEvent) { |
690 |
midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOnEvent->Param.Note.Key]; |
|
691 |
|
const int key = itNoteOnEvent->Param.Note.Key; |
692 |
|
|
693 |
|
// Change key dimension value if key is in keyswitching area |
694 |
|
if (key >= pInstrument->DimensionKeyRange.low && key <= pInstrument->DimensionKeyRange.high) |
695 |
|
CurrentKeyDimension = ((key - pInstrument->DimensionKeyRange.low) * 128) / |
696 |
|
(pInstrument->DimensionKeyRange.high - pInstrument->DimensionKeyRange.low + 1); |
697 |
|
|
698 |
|
midi_key_info_t* pKey = &pMIDIKeyInfo[key]; |
699 |
|
|
700 |
pKey->KeyPressed = true; // the MIDI key was now pressed down |
pKey->KeyPressed = true; // the MIDI key was now pressed down |
701 |
|
|
702 |
// cancel release process of voices on this key if needed |
// cancel release process of voices on this key if needed |
703 |
if (pKey->Active && !SustainPedal) { |
if (pKey->Active && !SustainPedal) { |
704 |
Event* pCancelReleaseEvent = pKey->pEvents->alloc(); |
RTList<Event>::Iterator itCancelReleaseEvent = pKey->pEvents->allocAppend(); |
705 |
if (pCancelReleaseEvent) { |
if (itCancelReleaseEvent) { |
706 |
*pCancelReleaseEvent = *pNoteOnEvent; |
*itCancelReleaseEvent = *itNoteOnEvent; // copy event |
707 |
pCancelReleaseEvent->Type = Event::type_cancel_release; // transform event type |
itCancelReleaseEvent->Type = Event::type_cancel_release; // transform event type |
708 |
} |
} |
709 |
else dmsg(1,("Event pool emtpy!\n")); |
else dmsg(1,("Event pool emtpy!\n")); |
710 |
} |
} |
711 |
|
|
712 |
// allocate and trigger a new voice for the key |
// move note on event to the key's own event list |
713 |
LaunchVoice(pNoteOnEvent); |
RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents); |
714 |
|
|
715 |
// finally move note on event to the key's own event list |
// allocate and trigger a new voice for the key |
716 |
pEvents->move(pNoteOnEvent, pKey->pEvents); |
LaunchVoice(itNoteOnEventOnKeyList, 0, false, true); |
717 |
} |
} |
718 |
|
|
719 |
/** |
/** |
722 |
* 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. |
723 |
* due to completion of sample playback). |
* due to completion of sample playback). |
724 |
* |
* |
725 |
* @param pNoteOffEvent - key, velocity and time stamp of the event |
* @param itNoteOffEvent - key, velocity and time stamp of the event |
726 |
*/ |
*/ |
727 |
void Engine::ProcessNoteOff(Event* pNoteOffEvent) { |
void Engine::ProcessNoteOff(Pool<Event>::Iterator& itNoteOffEvent) { |
728 |
midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOffEvent->Param.Note.Key]; |
midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key]; |
729 |
|
|
730 |
pKey->KeyPressed = false; // the MIDI key was now released |
pKey->KeyPressed = false; // the MIDI key was now released |
731 |
|
|
732 |
// release voices on this key if needed |
// release voices on this key if needed |
733 |
if (pKey->Active && !SustainPedal) { |
if (pKey->Active && !SustainPedal) { |
734 |
pNoteOffEvent->Type = Event::type_release; // transform event type |
itNoteOffEvent->Type = Event::type_release; // transform event type |
735 |
} |
} |
736 |
|
|
737 |
|
// move event to the key's own event list |
738 |
|
RTList<Event>::Iterator itNoteOffEventOnKeyList = itNoteOffEvent.moveToEndOf(pKey->pEvents); |
739 |
|
|
740 |
// spawn release triggered voice(s) if needed |
// spawn release triggered voice(s) if needed |
741 |
if (pKey->ReleaseTrigger) { |
if (pKey->ReleaseTrigger) { |
742 |
LaunchVoice(pNoteOffEvent, 0, true); |
LaunchVoice(itNoteOffEventOnKeyList, 0, true, false); //FIXME: for the moment we don't perform voice stealing for release triggered samples |
743 |
pKey->ReleaseTrigger = false; |
pKey->ReleaseTrigger = false; |
744 |
} |
} |
|
|
|
|
// move event to the key's own event list |
|
|
pEvents->move(pNoteOffEvent, pKey->pEvents); |
|
745 |
} |
} |
746 |
|
|
747 |
/** |
/** |
748 |
* Moves pitchbend event from the general (input) event list to the pitch |
* Moves pitchbend event from the general (input) event list to the pitch |
749 |
* event list. |
* event list. |
750 |
* |
* |
751 |
* @param pPitchbendEvent - absolute pitch value and time stamp of the event |
* @param itPitchbendEvent - absolute pitch value and time stamp of the event |
752 |
*/ |
*/ |
753 |
void Engine::ProcessPitchbend(Event* pPitchbendEvent) { |
void Engine::ProcessPitchbend(Pool<Event>::Iterator& itPitchbendEvent) { |
754 |
this->Pitch = pPitchbendEvent->Param.Pitch.Pitch; // store current pitch value |
this->Pitch = itPitchbendEvent->Param.Pitch.Pitch; // store current pitch value |
755 |
pEvents->move(pPitchbendEvent, pSynthesisEvents[Event::destination_vco]); |
itPitchbendEvent.moveToEndOf(pSynthesisEvents[Event::destination_vco]); |
756 |
} |
} |
757 |
|
|
758 |
/** |
/** |
760 |
* called by the ProcessNoteOn() method and by the voices itself |
* called by the ProcessNoteOn() method and by the voices itself |
761 |
* (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). |
762 |
* |
* |
763 |
* @param pNoteOnEvent - key, velocity and time stamp of the event |
* @param itNoteOnEvent - key, velocity and time stamp of the event |
764 |
* @param iLayer - layer index for the new voice (optional - only |
* @param iLayer - layer index for the new voice (optional - only |
765 |
* in case of layered sounds of course) |
* in case of layered sounds of course) |
766 |
* @param ReleaseTriggerVoice - if new voice is a release triggered voice |
* @param ReleaseTriggerVoice - if new voice is a release triggered voice |
767 |
* (optional, default = false) |
* (optional, default = false) |
768 |
|
* @param VoiceStealing - if voice stealing should be performed |
769 |
|
* when there is no free voice |
770 |
|
* (optional, default = true) |
771 |
|
* @returns pointer to new voice or NULL if there was no free voice or |
772 |
|
* if the voice wasn't triggered (for example when no region is |
773 |
|
* defined for the given key). |
774 |
*/ |
*/ |
775 |
void Engine::LaunchVoice(Event* pNoteOnEvent, int iLayer, bool ReleaseTriggerVoice) { |
Pool<Voice>::Iterator Engine::LaunchVoice(Pool<Event>::Iterator& itNoteOnEvent, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing) { |
776 |
midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOnEvent->Param.Note.Key]; |
midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
777 |
|
|
778 |
// allocate a new voice for the key |
// allocate a new voice for the key |
779 |
Voice* pNewVoice = pKey->pActiveVoices->alloc(); |
Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend(); |
780 |
if (pNewVoice) { |
if (itNewVoice) { |
781 |
// launch the new voice |
// launch the new voice |
782 |
if (pNewVoice->Trigger(pNoteOnEvent, this->Pitch, this->pInstrument, iLayer, ReleaseTriggerVoice) < 0) { |
if (itNewVoice->Trigger(itNoteOnEvent, this->Pitch, this->pInstrument, iLayer, ReleaseTriggerVoice, VoiceStealing) < 0) { |
783 |
dmsg(1,("Triggering new voice failed!\n")); |
dmsg(4,("Voice not triggered\n")); |
784 |
pKey->pActiveVoices->free(pNewVoice); |
pKey->pActiveVoices->free(itNewVoice); |
785 |
} |
} |
786 |
else { // on success |
else { // on success |
787 |
uint** ppKeyGroup = NULL; |
uint** ppKeyGroup = NULL; |
788 |
if (pNewVoice->KeyGroup) { // if this voice / key belongs to a key group |
if (itNewVoice->KeyGroup) { // if this voice / key belongs to a key group |
789 |
ppKeyGroup = &ActiveKeyGroups[pNewVoice->KeyGroup]; |
ppKeyGroup = &ActiveKeyGroups[itNewVoice->KeyGroup]; |
790 |
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 |
791 |
midi_key_info_t* pOtherKey = &pMIDIKeyInfo[**ppKeyGroup]; |
midi_key_info_t* pOtherKey = &pMIDIKeyInfo[**ppKeyGroup]; |
792 |
// kill all voices on the (other) key |
// kill all voices on the (other) key |
793 |
Voice* pVoiceToBeKilled = pOtherKey->pActiveVoices->first(); |
RTList<Voice>::Iterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first(); |
794 |
while (pVoiceToBeKilled) { |
RTList<Voice>::Iterator end = pOtherKey->pActiveVoices->end(); |
795 |
Voice* pVoiceToBeKilledNext = pOtherKey->pActiveVoices->next(); |
for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) { |
796 |
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; |
|
797 |
} |
} |
798 |
} |
} |
799 |
} |
} |
800 |
if (!pKey->Active) { // mark as active key |
if (!pKey->Active) { // mark as active key |
801 |
pKey->Active = true; |
pKey->Active = true; |
802 |
pKey->pSelf = pActiveKeys->alloc(); |
pKey->itSelf = pActiveKeys->allocAppend(); |
803 |
*pKey->pSelf = pNoteOnEvent->Param.Note.Key; |
*pKey->itSelf = itNoteOnEvent->Param.Note.Key; |
804 |
|
} |
805 |
|
if (itNewVoice->KeyGroup) { |
806 |
|
*ppKeyGroup = &*pKey->itSelf; // put key as the (new) active key to its key group |
807 |
|
} |
808 |
|
if (itNewVoice->Type == Voice::type_release_trigger_required) pKey->ReleaseTrigger = true; // mark key for the need of release triggered voice(s) |
809 |
|
return itNewVoice; // success |
810 |
|
} |
811 |
|
} |
812 |
|
else if (VoiceStealing) { |
813 |
|
// first, get total amount of required voices (dependant on amount of layers) |
814 |
|
::gig::Region* pRegion = pInstrument->GetRegion(itNoteOnEvent->Param.Note.Key); |
815 |
|
if (!pRegion) return Pool<Voice>::Iterator(); // nothing defined for this MIDI key, so no voice needed |
816 |
|
int voicesRequired = pRegion->Layers; |
817 |
|
|
818 |
|
// now steal the (remaining) amount of voices |
819 |
|
for (int i = iLayer; i < voicesRequired; i++) |
820 |
|
StealVoice(itNoteOnEvent); |
821 |
|
|
822 |
|
// put note-on event into voice-stealing queue, so it will be reprocessed after killed voice died |
823 |
|
RTList<Event>::Iterator itStealEvent = pVoiceStealingQueue->allocAppend(); |
824 |
|
if (itStealEvent) { |
825 |
|
*itStealEvent = *itNoteOnEvent; // copy event |
826 |
|
itStealEvent->Param.Note.Layer = iLayer; |
827 |
|
itStealEvent->Param.Note.ReleaseTrigger = ReleaseTriggerVoice; |
828 |
|
} |
829 |
|
else dmsg(1,("Voice stealing queue full!\n")); |
830 |
|
} |
831 |
|
|
832 |
|
return Pool<Voice>::Iterator(); // no free voice or error |
833 |
|
} |
834 |
|
|
835 |
|
/** |
836 |
|
* Will be called by LaunchVoice() method in case there are no free |
837 |
|
* voices left. This method will select and kill one old voice for |
838 |
|
* voice stealing and postpone the note-on event until the selected |
839 |
|
* voice actually died. |
840 |
|
* |
841 |
|
* @param itNoteOnEvent - key, velocity and time stamp of the event |
842 |
|
*/ |
843 |
|
void Engine::StealVoice(Pool<Event>::Iterator& itNoteOnEvent) { |
844 |
|
if (!pEventPool->poolIsEmpty()) { |
845 |
|
|
846 |
|
RTList<uint>::Iterator iuiOldestKey; |
847 |
|
RTList<Voice>::Iterator itOldestVoice; |
848 |
|
|
849 |
|
// Select one voice for voice stealing |
850 |
|
switch (VOICE_STEAL_ALGORITHM) { |
851 |
|
|
852 |
|
// try to pick the oldest voice on the key where the new |
853 |
|
// voice should be spawned, if there is no voice on that |
854 |
|
// key, or no voice left to kill there, then procceed with |
855 |
|
// 'oldestkey' algorithm |
856 |
|
case voice_steal_algo_keymask: { |
857 |
|
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
858 |
|
if (itLastStolenVoice) { |
859 |
|
itOldestVoice = itLastStolenVoice; |
860 |
|
++itOldestVoice; |
861 |
|
} |
862 |
|
else { // no voice stolen in this audio fragment cycle yet |
863 |
|
itOldestVoice = pOldestKey->pActiveVoices->first(); |
864 |
|
} |
865 |
|
if (itOldestVoice) { |
866 |
|
iuiOldestKey = pOldestKey->itSelf; |
867 |
|
break; // selection succeeded |
868 |
|
} |
869 |
|
} // no break - intentional ! |
870 |
|
|
871 |
|
// try to pick the oldest voice on the oldest active key |
872 |
|
// (caution: must stay after 'keymask' algorithm !) |
873 |
|
case voice_steal_algo_oldestkey: { |
874 |
|
if (itLastStolenVoice) { |
875 |
|
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiLastStolenKey]; |
876 |
|
itOldestVoice = itLastStolenVoice; |
877 |
|
++itOldestVoice; |
878 |
|
if (!itOldestVoice) { |
879 |
|
iuiOldestKey = iuiLastStolenKey; |
880 |
|
++iuiOldestKey; |
881 |
|
if (iuiOldestKey) { |
882 |
|
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiOldestKey]; |
883 |
|
itOldestVoice = pOldestKey->pActiveVoices->first(); |
884 |
|
} |
885 |
|
else { |
886 |
|
dmsg(1,("gig::Engine: Warning, too less voices, even for voice stealing! - Better recompile with higher MAX_AUDIO_VOICES.\n")); |
887 |
|
return; |
888 |
|
} |
889 |
|
} |
890 |
|
else iuiOldestKey = iuiLastStolenKey; |
891 |
|
} |
892 |
|
else { // no voice stolen in this audio fragment cycle yet |
893 |
|
iuiOldestKey = pActiveKeys->first(); |
894 |
|
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiOldestKey]; |
895 |
|
itOldestVoice = pOldestKey->pActiveVoices->first(); |
896 |
|
} |
897 |
|
break; |
898 |
} |
} |
899 |
if (pNewVoice->KeyGroup) { |
|
900 |
*ppKeyGroup = pKey->pSelf; // put key as the (new) active key to its key group |
// don't steal anything |
901 |
|
case voice_steal_algo_none: |
902 |
|
default: { |
903 |
|
dmsg(1,("No free voice (voice stealing disabled)!\n")); |
904 |
|
return; |
905 |
} |
} |
|
if (pNewVoice->Type == Voice::type_release_trigger_required) pKey->ReleaseTrigger = true; // mark key for the need of release triggered voice(s) |
|
906 |
} |
} |
907 |
|
|
908 |
|
//FIXME: can be removed, just a sanity check for debugging |
909 |
|
if (!itOldestVoice->IsActive()) dmsg(1,("gig::Engine: ERROR, tried to steal a voice which was not active !!!\n")); |
910 |
|
|
911 |
|
// now kill the selected voice |
912 |
|
itOldestVoice->Kill(itNoteOnEvent); |
913 |
|
// remember which voice on which key we stole, so we can simply proceed for the next voice stealing |
914 |
|
this->itLastStolenVoice = itOldestVoice; |
915 |
|
this->iuiLastStolenKey = iuiOldestKey; |
916 |
} |
} |
917 |
else std::cerr << "No free voice!" << std::endl << std::flush; |
else dmsg(1,("Event pool emtpy!\n")); |
918 |
} |
} |
919 |
|
|
920 |
/** |
/** |
921 |
* 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. |
922 |
* 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 |
923 |
* 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 |
924 |
|
* just was killed. |
925 |
* |
* |
926 |
* @param pVoice - points to the voice to be killed |
* @param itVoice - points to the voice to be freed |
927 |
*/ |
*/ |
928 |
void Engine::KillVoiceImmediately(Voice* pVoice) { |
void Engine::FreeVoice(Pool<Voice>::Iterator& itVoice) { |
929 |
if (pVoice) { |
if (itVoice) { |
930 |
if (pVoice->IsActive()) pVoice->KillImmediately(); |
midi_key_info_t* pKey = &pMIDIKeyInfo[itVoice->MIDIKey]; |
931 |
|
|
932 |
midi_key_info_t* pKey = &pMIDIKeyInfo[pVoice->MIDIKey]; |
uint keygroup = itVoice->KeyGroup; |
933 |
|
|
934 |
// free the voice object |
// free the voice object |
935 |
pVoicePool->free(pVoice); |
pVoicePool->free(itVoice); |
936 |
|
|
937 |
// 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 |
938 |
if (pKey->pActiveVoices->is_empty()) { |
if (pKey->pActiveVoices->isEmpty() && keygroup) { |
939 |
if (pVoice->KeyGroup) { // if voice / key belongs to a key group |
uint** ppKeyGroup = &ActiveKeyGroups[keygroup]; |
940 |
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")); |
|
941 |
} |
} |
942 |
} |
} |
943 |
else std::cerr << "Couldn't release voice! (pVoice == NULL)\n" << std::flush; |
else std::cerr << "Couldn't release voice! (!itVoice)\n" << std::flush; |
944 |
|
} |
945 |
|
|
946 |
|
/** |
947 |
|
* Called when there's no more voice left on a key, this call will |
948 |
|
* update the key info respectively. |
949 |
|
* |
950 |
|
* @param pKey - key which is now inactive |
951 |
|
*/ |
952 |
|
void Engine::FreeKey(midi_key_info_t* pKey) { |
953 |
|
if (pKey->pActiveVoices->isEmpty()) { |
954 |
|
pKey->Active = false; |
955 |
|
pActiveKeys->free(pKey->itSelf); // remove key from list of active keys |
956 |
|
pKey->itSelf = RTList<uint>::Iterator(); |
957 |
|
pKey->ReleaseTrigger = false; |
958 |
|
pKey->pEvents->clear(); |
959 |
|
dmsg(3,("Key has no more voices now\n")); |
960 |
|
} |
961 |
|
else dmsg(1,("gig::Engine: Oops, tried to free a key which contains voices.\n")); |
962 |
} |
} |
963 |
|
|
964 |
/** |
/** |
965 |
* Reacts on supported control change commands (e.g. pitch bend wheel, |
* Reacts on supported control change commands (e.g. pitch bend wheel, |
966 |
* modulation wheel, aftertouch). |
* modulation wheel, aftertouch). |
967 |
* |
* |
968 |
* @param pControlChangeEvent - controller, value and time stamp of the event |
* @param itControlChangeEvent - controller, value and time stamp of the event |
969 |
*/ |
*/ |
970 |
void Engine::ProcessControlChange(Event* pControlChangeEvent) { |
void Engine::ProcessControlChange(Pool<Event>::Iterator& itControlChangeEvent) { |
971 |
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)); |
972 |
|
|
973 |
switch (pControlChangeEvent->Param.CC.Controller) { |
switch (itControlChangeEvent->Param.CC.Controller) { |
974 |
case 64: { |
case 64: { |
975 |
if (pControlChangeEvent->Param.CC.Value >= 64 && !SustainPedal) { |
if (itControlChangeEvent->Param.CC.Value >= 64 && !SustainPedal) { |
976 |
dmsg(4,("PEDAL DOWN\n")); |
dmsg(4,("PEDAL DOWN\n")); |
977 |
SustainPedal = true; |
SustainPedal = true; |
978 |
|
|
979 |
// cancel release process of voices if necessary |
// cancel release process of voices if necessary |
980 |
uint* piKey = pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
981 |
if (piKey) { |
if (iuiKey) { |
982 |
pControlChangeEvent->Type = Event::type_cancel_release; // transform event type |
itControlChangeEvent->Type = Event::type_cancel_release; // transform event type |
983 |
while (piKey) { |
while (iuiKey) { |
984 |
midi_key_info_t* pKey = &pMIDIKeyInfo[*piKey]; |
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
985 |
pActiveKeys->set_current(piKey); |
++iuiKey; |
|
piKey = pActiveKeys->next(); |
|
986 |
if (!pKey->KeyPressed) { |
if (!pKey->KeyPressed) { |
987 |
Event* pNewEvent = pKey->pEvents->alloc(); |
RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend(); |
988 |
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 |
989 |
else dmsg(1,("Event pool emtpy!\n")); |
else dmsg(1,("Event pool emtpy!\n")); |
990 |
} |
} |
991 |
} |
} |
992 |
} |
} |
993 |
} |
} |
994 |
if (pControlChangeEvent->Param.CC.Value < 64 && SustainPedal) { |
if (itControlChangeEvent->Param.CC.Value < 64 && SustainPedal) { |
995 |
dmsg(4,("PEDAL UP\n")); |
dmsg(4,("PEDAL UP\n")); |
996 |
SustainPedal = false; |
SustainPedal = false; |
997 |
|
|
998 |
// release voices if their respective key is not pressed |
// release voices if their respective key is not pressed |
999 |
uint* piKey = pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
1000 |
if (piKey) { |
if (iuiKey) { |
1001 |
pControlChangeEvent->Type = Event::type_release; // transform event type |
itControlChangeEvent->Type = Event::type_release; // transform event type |
1002 |
while (piKey) { |
while (iuiKey) { |
1003 |
midi_key_info_t* pKey = &pMIDIKeyInfo[*piKey]; |
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
1004 |
pActiveKeys->set_current(piKey); |
++iuiKey; |
|
piKey = pActiveKeys->next(); |
|
1005 |
if (!pKey->KeyPressed) { |
if (!pKey->KeyPressed) { |
1006 |
Event* pNewEvent = pKey->pEvents->alloc(); |
RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend(); |
1007 |
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 |
1008 |
else dmsg(1,("Event pool emtpy!\n")); |
else dmsg(1,("Event pool emtpy!\n")); |
1009 |
} |
} |
1010 |
} |
} |
1015 |
} |
} |
1016 |
|
|
1017 |
// update controller value in the engine's controller table |
// update controller value in the engine's controller table |
1018 |
ControllerTable[pControlChangeEvent->Param.CC.Controller] = pControlChangeEvent->Param.CC.Value; |
ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value; |
1019 |
|
|
1020 |
// 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 |
1021 |
pEvents->move(pControlChangeEvent, pCCEvents); |
itControlChangeEvent.moveToEndOf(pCCEvents); |
1022 |
} |
} |
1023 |
|
|
1024 |
/** |
/** |
1025 |
* Reacts on MIDI system exclusive messages. |
* Reacts on MIDI system exclusive messages. |
1026 |
* |
* |
1027 |
* @param pSysexEvent - sysex data size and time stamp of the sysex event |
* @param itSysexEvent - sysex data size and time stamp of the sysex event |
1028 |
*/ |
*/ |
1029 |
void Engine::ProcessSysex(Event* pSysexEvent) { |
void Engine::ProcessSysex(Pool<Event>::Iterator& itSysexEvent) { |
1030 |
RingBuffer<uint8_t>::NonVolatileReader reader = pSysexBuffer->get_non_volatile_reader(); |
RingBuffer<uint8_t>::NonVolatileReader reader = pSysexBuffer->get_non_volatile_reader(); |
1031 |
|
|
1032 |
uint8_t exclusive_status, id; |
uint8_t exclusive_status, id; |
1074 |
} |
} |
1075 |
|
|
1076 |
free_sysex_data: // finally free sysex data |
free_sysex_data: // finally free sysex data |
1077 |
pSysexBuffer->increment_read_ptr(pSysexEvent->Param.Sysex.Size); |
pSysexBuffer->increment_read_ptr(itSysexEvent->Param.Sysex.Size); |
1078 |
} |
} |
1079 |
|
|
1080 |
/** |
/** |
1195 |
return InstrumentFile; |
return InstrumentFile; |
1196 |
} |
} |
1197 |
|
|
1198 |
|
String Engine::InstrumentName() { |
1199 |
|
return InstrumentIdxName; |
1200 |
|
} |
1201 |
|
|
1202 |
int Engine::InstrumentIndex() { |
int Engine::InstrumentIndex() { |
1203 |
return InstrumentIdx; |
return InstrumentIdx; |
1204 |
} |
} |
1212 |
} |
} |
1213 |
|
|
1214 |
String Engine::Version() { |
String Engine::Version() { |
1215 |
String s = "$Revision: 1.13 $"; |
String s = "$Revision: 1.25 $"; |
1216 |
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 |
1217 |
} |
} |
1218 |
|
|