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 |
|
|
38 |
pAudioOutputDevice = NULL; |
pAudioOutputDevice = NULL; |
39 |
pDiskThread = NULL; |
pDiskThread = NULL; |
40 |
pEventGenerator = NULL; |
pEventGenerator = NULL; |
41 |
pEventQueue = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT); |
pSysexBuffer = new RingBuffer<uint8_t>(SYSEX_BUFFER_SIZE, 0); |
42 |
pEventPool = new RTELMemoryPool<Event>(MAX_EVENTS_PER_FRAGMENT); |
pEventQueue = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0); |
43 |
pVoicePool = new RTELMemoryPool<Voice>(MAX_AUDIO_VOICES); |
pEventPool = new Pool<Event>(MAX_EVENTS_PER_FRAGMENT); |
44 |
pActiveKeys = new RTELMemoryPool<uint>(128); |
pVoicePool = new Pool<Voice>(MAX_AUDIO_VOICES); |
45 |
pEvents = new RTEList<Event>(pEventPool); |
pActiveKeys = new Pool<uint>(128); |
46 |
pCCEvents = new RTEList<Event>(pEventPool); |
pVoiceStealingQueue = new RTList<Event>(pEventPool); |
47 |
|
pEvents = new RTList<Event>(pEventPool); |
48 |
|
pCCEvents = new RTList<Event>(pEventPool); |
49 |
for (uint i = 0; i < Event::destination_count; i++) { |
for (uint i = 0; i < Event::destination_count; i++) { |
50 |
pSynthesisEvents[i] = new RTEList<Event>(pEventPool); |
pSynthesisEvents[i] = new RTList<Event>(pEventPool); |
51 |
} |
} |
52 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
53 |
pMIDIKeyInfo[i].pActiveVoices = new RTEList<Voice>(pVoicePool); |
pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pVoicePool); |
54 |
pMIDIKeyInfo[i].KeyPressed = false; |
pMIDIKeyInfo[i].KeyPressed = false; |
55 |
pMIDIKeyInfo[i].Active = false; |
pMIDIKeyInfo[i].Active = false; |
56 |
pMIDIKeyInfo[i].pSelf = NULL; |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
57 |
pMIDIKeyInfo[i].pEvents = new RTEList<Event>(pEventPool); |
pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEventPool); |
58 |
} |
} |
59 |
for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) { |
for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { |
60 |
pVoice->SetEngine(this); |
iterVoice->SetEngine(this); |
61 |
} |
} |
62 |
pVoicePool->clear(); |
pVoicePool->clear(); |
63 |
|
|
68 |
InstrumentIdx = -1; |
InstrumentIdx = -1; |
69 |
InstrumentStat = -1; |
InstrumentStat = -1; |
70 |
|
|
71 |
|
AudioDeviceChannelLeft = -1; |
72 |
|
AudioDeviceChannelRight = -1; |
73 |
|
|
74 |
ResetInternal(); |
ResetInternal(); |
75 |
} |
} |
76 |
|
|
95 |
if (pEventPool) delete pEventPool; |
if (pEventPool) delete pEventPool; |
96 |
if (pVoicePool) delete pVoicePool; |
if (pVoicePool) delete pVoicePool; |
97 |
if (pActiveKeys) delete pActiveKeys; |
if (pActiveKeys) delete pActiveKeys; |
98 |
|
if (pSysexBuffer) delete pSysexBuffer; |
99 |
if (pEventGenerator) delete pEventGenerator; |
if (pEventGenerator) delete pEventGenerator; |
100 |
if (pMainFilterParameters) delete[] pMainFilterParameters; |
if (pMainFilterParameters) delete[] pMainFilterParameters; |
101 |
if (pBasicFilterParameters) delete[] pBasicFilterParameters; |
if (pBasicFilterParameters) delete[] pBasicFilterParameters; |
102 |
if (pSynthesisParameters[0]) delete[] pSynthesisParameters[0]; |
if (pSynthesisParameters[0]) delete[] pSynthesisParameters[0]; |
103 |
|
if (pVoiceStealingQueue) delete pVoiceStealingQueue; |
104 |
} |
} |
105 |
|
|
106 |
void Engine::Enable() { |
void Engine::Enable() { |
157 |
SustainPedal = false; |
SustainPedal = false; |
158 |
ActiveVoiceCount = 0; |
ActiveVoiceCount = 0; |
159 |
ActiveVoiceCountMax = 0; |
ActiveVoiceCountMax = 0; |
160 |
|
GlobalVolume = 1.0; |
161 |
|
|
162 |
|
// reset voice stealing parameters |
163 |
|
itLastStolenVoice = RTList<Voice>::Iterator(); |
164 |
|
iuiLastStolenKey = RTList<uint>::Iterator(); |
165 |
|
pVoiceStealingQueue->clear(); |
166 |
|
|
167 |
|
// reset to normal chromatic scale (means equal temper) |
168 |
|
memset(&ScaleTuning[0], 0x00, 12); |
169 |
|
|
170 |
// set all MIDI controller values to zero |
// set all MIDI controller values to zero |
171 |
memset(ControllerTable, 0x00, 128); |
memset(ControllerTable, 0x00, 128); |
174 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
175 |
pMIDIKeyInfo[i].pActiveVoices->clear(); |
pMIDIKeyInfo[i].pActiveVoices->clear(); |
176 |
pMIDIKeyInfo[i].pEvents->clear(); |
pMIDIKeyInfo[i].pEvents->clear(); |
177 |
pMIDIKeyInfo[i].KeyPressed = false; |
pMIDIKeyInfo[i].KeyPressed = false; |
178 |
pMIDIKeyInfo[i].Active = false; |
pMIDIKeyInfo[i].Active = false; |
179 |
pMIDIKeyInfo[i].pSelf = NULL; |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
180 |
|
pMIDIKeyInfo[i].itSelf = Pool<uint>::Iterator(); |
181 |
} |
} |
182 |
|
|
183 |
|
// reset all key groups |
184 |
|
map<uint,uint*>::iterator iter = ActiveKeyGroups.begin(); |
185 |
|
for (; iter != ActiveKeyGroups.end(); iter++) iter->second = NULL; |
186 |
|
|
187 |
// reset all voices |
// reset all voices |
188 |
for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) { |
for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { |
189 |
pVoice->Reset(); |
iterVoice->Reset(); |
190 |
} |
} |
191 |
pVoicePool->clear(); |
pVoicePool->clear(); |
192 |
|
|
224 |
InstrumentIdx = Instrument; |
InstrumentIdx = Instrument; |
225 |
InstrumentStat = 0; |
InstrumentStat = 0; |
226 |
|
|
227 |
|
// delete all key groups |
228 |
|
ActiveKeyGroups.clear(); |
229 |
|
|
230 |
// request gig instrument from instrument manager |
// request gig instrument from instrument manager |
231 |
try { |
try { |
232 |
instrument_id_t instrid; |
instrument_id_t instrid; |
254 |
throw LinuxSamplerException("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file."); |
throw LinuxSamplerException("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file."); |
255 |
} |
} |
256 |
|
|
257 |
|
// rebuild ActiveKeyGroups map with key groups of current instrument |
258 |
|
for (::gig::Region* pRegion = pInstrument->GetFirstRegion(); pRegion; pRegion = pInstrument->GetNextRegion()) |
259 |
|
if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL; |
260 |
|
|
261 |
InstrumentStat = 100; |
InstrumentStat = 100; |
262 |
|
|
263 |
// inform audio driver for the need of two channels |
// inform audio driver for the need of two channels |
289 |
* update process was completed, so we can continue with playback. |
* update process was completed, so we can continue with playback. |
290 |
*/ |
*/ |
291 |
void Engine::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) { |
void Engine::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) { |
292 |
this->pInstrument = pNewResource; |
this->pInstrument = pNewResource; //TODO: there are couple of engine parameters we should update here as well if the instrument was updated (see LoadInstrument()) |
293 |
Enable(); |
Enable(); |
294 |
} |
} |
295 |
|
|
307 |
throw LinuxSamplerException(msg); |
throw LinuxSamplerException(msg); |
308 |
} |
} |
309 |
|
|
310 |
|
this->AudioDeviceChannelLeft = 0; |
311 |
|
this->AudioDeviceChannelRight = 1; |
312 |
|
this->pOutputLeft = pAudioOutputDevice->Channel(0)->Buffer(); |
313 |
|
this->pOutputRight = pAudioOutputDevice->Channel(1)->Buffer(); |
314 |
|
this->MaxSamplesPerCycle = pAudioOutputDevice->MaxSamplesPerCycle(); |
315 |
|
this->SampleRate = pAudioOutputDevice->SampleRate(); |
316 |
|
|
317 |
|
// FIXME: audio drivers with varying fragment sizes might be a problem here |
318 |
|
MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * EG_MIN_RELEASE_TIME) - 1; |
319 |
|
if (MaxFadeOutPos < 0) |
320 |
|
throw LinuxSamplerException("EG_MIN_RELEASE_TIME in EGADSR.h to big for current audio fragment size / sampling rate!"); |
321 |
|
|
322 |
// (re)create disk thread |
// (re)create disk thread |
323 |
if (this->pDiskThread) { |
if (this->pDiskThread) { |
324 |
this->pDiskThread->StopThread(); |
this->pDiskThread->StopThread(); |
330 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
331 |
} |
} |
332 |
|
|
333 |
for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) { |
for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { |
334 |
pVoice->pDiskThread = this->pDiskThread; |
iterVoice->pDiskThread = this->pDiskThread; |
|
pVoice->SetOutput(pAudioOut); |
|
335 |
dmsg(3,("d")); |
dmsg(3,("d")); |
336 |
} |
} |
337 |
pVoicePool->clear(); |
pVoicePool->clear(); |
356 |
pDiskThread->StartThread(); |
pDiskThread->StartThread(); |
357 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
358 |
|
|
359 |
for (Voice* pVoice = pVoicePool->first(); pVoice; pVoice = pVoicePool->next()) { |
for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { |
360 |
if (!pVoice->pDiskThread) { |
if (!iterVoice->pDiskThread) { |
361 |
dmsg(0,("Engine -> voice::trigger: !pDiskThread\n")); |
dmsg(0,("Engine -> voice::trigger: !pDiskThread\n")); |
362 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
363 |
} |
} |
369 |
AudioOutputDevice* olddevice = pAudioOutputDevice; |
AudioOutputDevice* olddevice = pAudioOutputDevice; |
370 |
pAudioOutputDevice = NULL; |
pAudioOutputDevice = NULL; |
371 |
olddevice->Disconnect(this); |
olddevice->Disconnect(this); |
372 |
|
AudioDeviceChannelLeft = -1; |
373 |
|
AudioDeviceChannelRight = -1; |
374 |
} |
} |
375 |
} |
} |
376 |
|
|
404 |
for (uint i = 0; i < Event::destination_count; i++) { |
for (uint i = 0; i < Event::destination_count; i++) { |
405 |
pSynthesisEvents[i]->clear(); |
pSynthesisEvents[i]->clear(); |
406 |
} |
} |
407 |
|
{ |
408 |
|
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
409 |
|
RTList<uint>::Iterator end = pActiveKeys->end(); |
410 |
|
for(; iuiKey != end; ++iuiKey) { |
411 |
|
pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key |
412 |
|
} |
413 |
|
} |
414 |
|
|
415 |
// read and copy events from input queue |
// read and copy events from input queue |
416 |
Event event = pEventGenerator->CreateEvent(); |
Event event = pEventGenerator->CreateEvent(); |
417 |
while (true) { |
while (true) { |
418 |
if (!pEventQueue->pop(&event)) break; |
if (!pEventQueue->pop(&event) || pEvents->poolIsEmpty()) break; |
419 |
pEvents->alloc_assign(event); |
*pEvents->allocAppend() = event; |
420 |
} |
} |
421 |
|
|
422 |
|
|
425 |
|
|
426 |
|
|
427 |
// process events |
// process events |
428 |
Event* pNextEvent = pEvents->first(); |
{ |
429 |
while (pNextEvent) { |
RTList<Event>::Iterator itEvent = pEvents->first(); |
430 |
Event* pEvent = pNextEvent; |
RTList<Event>::Iterator end = pEvents->end(); |
431 |
pEvents->set_current(pEvent); |
for (; itEvent != end; ++itEvent) { |
432 |
pNextEvent = pEvents->next(); |
switch (itEvent->Type) { |
433 |
switch (pEvent->Type) { |
case Event::type_note_on: |
434 |
case Event::type_note_on: |
dmsg(5,("Engine: Note on received\n")); |
435 |
dmsg(5,("Audio Thread: Note on received\n")); |
ProcessNoteOn(itEvent); |
436 |
ProcessNoteOn(pEvent); |
break; |
437 |
break; |
case Event::type_note_off: |
438 |
case Event::type_note_off: |
dmsg(5,("Engine: Note off received\n")); |
439 |
dmsg(5,("Audio Thread: Note off received\n")); |
ProcessNoteOff(itEvent); |
440 |
ProcessNoteOff(pEvent); |
break; |
441 |
break; |
case Event::type_control_change: |
442 |
case Event::type_control_change: |
dmsg(5,("Engine: MIDI CC received\n")); |
443 |
dmsg(5,("Audio Thread: MIDI CC received\n")); |
ProcessControlChange(itEvent); |
444 |
ProcessControlChange(pEvent); |
break; |
445 |
break; |
case Event::type_pitchbend: |
446 |
case Event::type_pitchbend: |
dmsg(5,("Engine: Pitchbend received\n")); |
447 |
dmsg(5,("Audio Thread: Pitchbend received\n")); |
ProcessPitchbend(itEvent); |
448 |
ProcessPitchbend(pEvent); |
break; |
449 |
break; |
case Event::type_sysex: |
450 |
|
dmsg(5,("Engine: Sysex received\n")); |
451 |
|
ProcessSysex(itEvent); |
452 |
|
break; |
453 |
|
} |
454 |
} |
} |
455 |
} |
} |
456 |
|
|
457 |
|
|
|
// render audio from all active voices |
|
458 |
int active_voices = 0; |
int active_voices = 0; |
459 |
uint* piKey = pActiveKeys->first(); |
|
460 |
while (piKey) { // iterate through all active keys |
// render audio from all active voices |
461 |
midi_key_info_t* pKey = &pMIDIKeyInfo[*piKey]; |
{ |
462 |
pActiveKeys->set_current(piKey); |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
463 |
piKey = pActiveKeys->next(); |
RTList<uint>::Iterator end = pActiveKeys->end(); |
464 |
|
while (iuiKey != end) { // iterate through all active keys |
465 |
Voice* pVoiceNext = pKey->pActiveVoices->first(); |
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
466 |
while (pVoiceNext) { // iterate through all voices on this key |
++iuiKey; |
467 |
// already get next voice on key |
|
468 |
Voice* pVoice = pVoiceNext; |
RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first(); |
469 |
pKey->pActiveVoices->set_current(pVoice); |
RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end(); |
470 |
pVoiceNext = pKey->pActiveVoices->next(); |
for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key |
471 |
|
// now render current voice |
472 |
// now render current voice |
itVoice->Render(Samples); |
473 |
pVoice->Render(Samples); |
if (itVoice->IsActive()) active_voices++; // still active |
474 |
if (pVoice->IsActive()) active_voices++; // still active |
else { // voice reached end, is now inactive |
475 |
else { // voice reached end, is now inactive |
FreeVoice(itVoice); // remove voice from the list of active voices |
476 |
KillVoice(pVoice); // remove voice from the list of active voices |
} |
477 |
} |
} |
478 |
} |
} |
479 |
pKey->pEvents->clear(); // free all events on the key |
} |
480 |
|
|
481 |
|
|
482 |
|
// now render all postponed voices from voice stealing |
483 |
|
{ |
484 |
|
RTList<Event>::Iterator itVoiceStealEvent = pVoiceStealingQueue->first(); |
485 |
|
RTList<Event>::Iterator end = pVoiceStealingQueue->end(); |
486 |
|
for (; itVoiceStealEvent != end; ++itVoiceStealEvent) { |
487 |
|
Pool<Voice>::Iterator itNewVoice = LaunchVoice(itVoiceStealEvent, itVoiceStealEvent->Param.Note.Layer, itVoiceStealEvent->Param.Note.ReleaseTrigger, false); |
488 |
|
if (itNewVoice) { |
489 |
|
for (; itNewVoice; itNewVoice = itNewVoice->itChildVoice) { |
490 |
|
itNewVoice->Render(Samples); |
491 |
|
if (itNewVoice->IsActive()) active_voices++; // still active |
492 |
|
else { // voice reached end, is now inactive |
493 |
|
FreeVoice(itNewVoice); // remove voice from the list of active voices |
494 |
|
} |
495 |
|
} |
496 |
|
} |
497 |
|
else dmsg(1,("gig::Engine: ERROR, voice stealing didn't work out!\n")); |
498 |
|
} |
499 |
|
} |
500 |
|
// reset voice stealing for the new fragment |
501 |
|
pVoiceStealingQueue->clear(); |
502 |
|
itLastStolenVoice = RTList<Voice>::Iterator(); |
503 |
|
iuiLastStolenKey = RTList<uint>::Iterator(); |
504 |
|
|
505 |
|
|
506 |
|
// free all keys which have no active voices left |
507 |
|
{ |
508 |
|
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
509 |
|
RTList<uint>::Iterator end = pActiveKeys->end(); |
510 |
|
while (iuiKey != end) { // iterate through all active keys |
511 |
|
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
512 |
|
++iuiKey; |
513 |
|
if (pKey->pActiveVoices->isEmpty()) FreeKey(pKey); |
514 |
|
#if DEVMODE |
515 |
|
else { // FIXME: should be removed before the final release (purpose: just a sanity check for debugging) |
516 |
|
RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first(); |
517 |
|
RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end(); |
518 |
|
for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key |
519 |
|
if (itVoice->itKillEvent) { |
520 |
|
dmsg(1,("gig::Engine: ERROR, killed voice survived !!!\n")); |
521 |
|
} |
522 |
|
} |
523 |
|
} |
524 |
|
#endif // DEVMODE |
525 |
|
} |
526 |
} |
} |
527 |
|
|
528 |
|
|
543 |
* @param Velocity - MIDI velocity value of the triggered key |
* @param Velocity - MIDI velocity value of the triggered key |
544 |
*/ |
*/ |
545 |
void Engine::SendNoteOn(uint8_t Key, uint8_t Velocity) { |
void Engine::SendNoteOn(uint8_t Key, uint8_t Velocity) { |
546 |
Event event = pEventGenerator->CreateEvent(); |
Event event = pEventGenerator->CreateEvent(); |
547 |
event.Type = Event::type_note_on; |
event.Type = Event::type_note_on; |
548 |
event.Key = Key; |
event.Param.Note.Key = Key; |
549 |
event.Velocity = Velocity; |
event.Param.Note.Velocity = Velocity; |
550 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
551 |
else dmsg(1,("Engine: Input event queue full!")); |
else dmsg(1,("Engine: Input event queue full!")); |
552 |
} |
} |
559 |
* @param Velocity - MIDI release velocity value of the released key |
* @param Velocity - MIDI release velocity value of the released key |
560 |
*/ |
*/ |
561 |
void Engine::SendNoteOff(uint8_t Key, uint8_t Velocity) { |
void Engine::SendNoteOff(uint8_t Key, uint8_t Velocity) { |
562 |
Event event = pEventGenerator->CreateEvent(); |
Event event = pEventGenerator->CreateEvent(); |
563 |
event.Type = Event::type_note_off; |
event.Type = Event::type_note_off; |
564 |
event.Key = Key; |
event.Param.Note.Key = Key; |
565 |
event.Velocity = Velocity; |
event.Param.Note.Velocity = Velocity; |
566 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
567 |
else dmsg(1,("Engine: Input event queue full!")); |
else dmsg(1,("Engine: Input event queue full!")); |
568 |
} |
} |
574 |
* @param Pitch - MIDI pitch value (-8192 ... +8191) |
* @param Pitch - MIDI pitch value (-8192 ... +8191) |
575 |
*/ |
*/ |
576 |
void Engine::SendPitchbend(int Pitch) { |
void Engine::SendPitchbend(int Pitch) { |
577 |
Event event = pEventGenerator->CreateEvent(); |
Event event = pEventGenerator->CreateEvent(); |
578 |
event.Type = Event::type_pitchbend; |
event.Type = Event::type_pitchbend; |
579 |
event.Pitch = Pitch; |
event.Param.Pitch.Pitch = Pitch; |
580 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
581 |
else dmsg(1,("Engine: Input event queue full!")); |
else dmsg(1,("Engine: Input event queue full!")); |
582 |
} |
} |
589 |
* @param Value - value of the control change |
* @param Value - value of the control change |
590 |
*/ |
*/ |
591 |
void Engine::SendControlChange(uint8_t Controller, uint8_t Value) { |
void Engine::SendControlChange(uint8_t Controller, uint8_t Value) { |
592 |
Event event = pEventGenerator->CreateEvent(); |
Event event = pEventGenerator->CreateEvent(); |
593 |
event.Type = Event::type_control_change; |
event.Type = Event::type_control_change; |
594 |
event.Controller = Controller; |
event.Param.CC.Controller = Controller; |
595 |
event.Value = Value; |
event.Param.CC.Value = Value; |
596 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
597 |
else dmsg(1,("Engine: Input event queue full!")); |
else dmsg(1,("Engine: Input event queue full!")); |
598 |
} |
} |
599 |
|
|
600 |
/** |
/** |
601 |
|
* Will be called by the MIDI input device whenever a MIDI system |
602 |
|
* exclusive message has arrived. |
603 |
|
* |
604 |
|
* @param pData - pointer to sysex data |
605 |
|
* @param Size - lenght of sysex data (in bytes) |
606 |
|
*/ |
607 |
|
void Engine::SendSysex(void* pData, uint Size) { |
608 |
|
Event event = pEventGenerator->CreateEvent(); |
609 |
|
event.Type = Event::type_sysex; |
610 |
|
event.Param.Sysex.Size = Size; |
611 |
|
if (pEventQueue->write_space() > 0) { |
612 |
|
if (pSysexBuffer->write_space() >= Size) { |
613 |
|
// copy sysex data to input buffer |
614 |
|
uint toWrite = Size; |
615 |
|
uint8_t* pPos = (uint8_t*) pData; |
616 |
|
while (toWrite) { |
617 |
|
const uint writeNow = RTMath::Min(toWrite, pSysexBuffer->write_space_to_end()); |
618 |
|
pSysexBuffer->write(pPos, writeNow); |
619 |
|
toWrite -= writeNow; |
620 |
|
pPos += writeNow; |
621 |
|
|
622 |
|
} |
623 |
|
// finally place sysex event into input event queue |
624 |
|
pEventQueue->push(&event); |
625 |
|
} |
626 |
|
else dmsg(1,("Engine: Sysex message too large (%d byte) for input buffer (%d byte)!",Size,SYSEX_BUFFER_SIZE)); |
627 |
|
} |
628 |
|
else dmsg(1,("Engine: Input event queue full!")); |
629 |
|
} |
630 |
|
|
631 |
|
/** |
632 |
* Assigns and triggers a new voice for the respective MIDI key. |
* Assigns and triggers a new voice for the respective MIDI key. |
633 |
* |
* |
634 |
* @param pNoteOnEvent - key, velocity and time stamp of the event |
* @param itNoteOnEvent - key, velocity and time stamp of the event |
635 |
*/ |
*/ |
636 |
void Engine::ProcessNoteOn(Event* pNoteOnEvent) { |
void Engine::ProcessNoteOn(Pool<Event>::Iterator& itNoteOnEvent) { |
637 |
midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOnEvent->Key]; |
midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
638 |
|
|
639 |
pKey->KeyPressed = true; // the MIDI key was now pressed down |
pKey->KeyPressed = true; // the MIDI key was now pressed down |
640 |
|
|
641 |
// cancel release process of voices on this key if needed |
// cancel release process of voices on this key if needed |
642 |
if (pKey->Active && !SustainPedal) { |
if (pKey->Active && !SustainPedal) { |
643 |
pNoteOnEvent->Type = Event::type_cancel_release; // transform event type |
RTList<Event>::Iterator itCancelReleaseEvent = pKey->pEvents->allocAppend(); |
644 |
pEvents->move(pNoteOnEvent, pKey->pEvents); // move event to the key's own event list |
if (itCancelReleaseEvent) { |
645 |
} |
*itCancelReleaseEvent = *itNoteOnEvent; // copy event |
646 |
|
itCancelReleaseEvent->Type = Event::type_cancel_release; // transform event type |
|
// allocate a new voice for the key |
|
|
Voice* pNewVoice = pKey->pActiveVoices->alloc(); |
|
|
if (pNewVoice) { |
|
|
// launch the new voice |
|
|
if (pNewVoice->Trigger(pNoteOnEvent, this->Pitch, this->pInstrument) < 0) { |
|
|
dmsg(1,("Triggering new voice failed!\n")); |
|
|
pKey->pActiveVoices->free(pNewVoice); |
|
|
} |
|
|
else if (!pKey->Active) { // mark as active key |
|
|
pKey->Active = true; |
|
|
pKey->pSelf = pActiveKeys->alloc(); |
|
|
*pKey->pSelf = pNoteOnEvent->Key; |
|
647 |
} |
} |
648 |
|
else dmsg(1,("Event pool emtpy!\n")); |
649 |
} |
} |
650 |
else std::cerr << "No free voice!" << std::endl << std::flush; |
|
651 |
|
// move note on event to the key's own event list |
652 |
|
RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents); |
653 |
|
|
654 |
|
// allocate and trigger a new voice for the key |
655 |
|
LaunchVoice(itNoteOnEventOnKeyList, 0, false, true); |
656 |
} |
} |
657 |
|
|
658 |
/** |
/** |
661 |
* 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. |
662 |
* due to completion of sample playback). |
* due to completion of sample playback). |
663 |
* |
* |
664 |
* @param pNoteOffEvent - key, velocity and time stamp of the event |
* @param itNoteOffEvent - key, velocity and time stamp of the event |
665 |
*/ |
*/ |
666 |
void Engine::ProcessNoteOff(Event* pNoteOffEvent) { |
void Engine::ProcessNoteOff(Pool<Event>::Iterator& itNoteOffEvent) { |
667 |
midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOffEvent->Key]; |
midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key]; |
668 |
|
|
669 |
pKey->KeyPressed = false; // the MIDI key was now released |
pKey->KeyPressed = false; // the MIDI key was now released |
670 |
|
|
671 |
// release voices on this key if needed |
// release voices on this key if needed |
672 |
if (pKey->Active && !SustainPedal) { |
if (pKey->Active && !SustainPedal) { |
673 |
pNoteOffEvent->Type = Event::type_release; // transform event type |
itNoteOffEvent->Type = Event::type_release; // transform event type |
674 |
pEvents->move(pNoteOffEvent, pKey->pEvents); // move event to the key's own event list |
} |
675 |
|
|
676 |
|
// move event to the key's own event list |
677 |
|
RTList<Event>::Iterator itNoteOffEventOnKeyList = itNoteOffEvent.moveToEndOf(pKey->pEvents); |
678 |
|
|
679 |
|
// spawn release triggered voice(s) if needed |
680 |
|
if (pKey->ReleaseTrigger) { |
681 |
|
LaunchVoice(itNoteOffEventOnKeyList, 0, true, false); //FIXME: for the moment we don't perform voice stealing for release triggered samples |
682 |
|
pKey->ReleaseTrigger = false; |
683 |
} |
} |
684 |
} |
} |
685 |
|
|
687 |
* Moves pitchbend event from the general (input) event list to the pitch |
* Moves pitchbend event from the general (input) event list to the pitch |
688 |
* event list. |
* event list. |
689 |
* |
* |
690 |
* @param pPitchbendEvent - absolute pitch value and time stamp of the event |
* @param itPitchbendEvent - absolute pitch value and time stamp of the event |
691 |
|
*/ |
692 |
|
void Engine::ProcessPitchbend(Pool<Event>::Iterator& itPitchbendEvent) { |
693 |
|
this->Pitch = itPitchbendEvent->Param.Pitch.Pitch; // store current pitch value |
694 |
|
itPitchbendEvent.moveToEndOf(pSynthesisEvents[Event::destination_vco]); |
695 |
|
} |
696 |
|
|
697 |
|
/** |
698 |
|
* Allocates and triggers a new voice. This method will usually be |
699 |
|
* called by the ProcessNoteOn() method and by the voices itself |
700 |
|
* (e.g. to spawn further voices on the same key for layered sounds). |
701 |
|
* |
702 |
|
* @param itNoteOnEvent - key, velocity and time stamp of the event |
703 |
|
* @param iLayer - layer index for the new voice (optional - only |
704 |
|
* in case of layered sounds of course) |
705 |
|
* @param ReleaseTriggerVoice - if new voice is a release triggered voice |
706 |
|
* (optional, default = false) |
707 |
|
* @param VoiceStealing - if voice stealing should be performed |
708 |
|
* when there is no free voice |
709 |
|
* (optional, default = true) |
710 |
|
* @returns pointer to new voice or NULL if there was no free voice or |
711 |
|
* if an error occured while trying to trigger the new voice |
712 |
|
*/ |
713 |
|
Pool<Voice>::Iterator Engine::LaunchVoice(Pool<Event>::Iterator& itNoteOnEvent, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing) { |
714 |
|
midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
715 |
|
|
716 |
|
// allocate a new voice for the key |
717 |
|
Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend(); |
718 |
|
if (itNewVoice) { |
719 |
|
// launch the new voice |
720 |
|
if (itNewVoice->Trigger(itNoteOnEvent, this->Pitch, this->pInstrument, iLayer, ReleaseTriggerVoice, VoiceStealing) < 0) { |
721 |
|
dmsg(1,("Triggering new voice failed!\n")); |
722 |
|
pKey->pActiveVoices->free(itNewVoice); |
723 |
|
} |
724 |
|
else { // on success |
725 |
|
uint** ppKeyGroup = NULL; |
726 |
|
if (itNewVoice->KeyGroup) { // if this voice / key belongs to a key group |
727 |
|
ppKeyGroup = &ActiveKeyGroups[itNewVoice->KeyGroup]; |
728 |
|
if (*ppKeyGroup) { // if there's already an active key in that key group |
729 |
|
midi_key_info_t* pOtherKey = &pMIDIKeyInfo[**ppKeyGroup]; |
730 |
|
// kill all voices on the (other) key |
731 |
|
RTList<Voice>::Iterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first(); |
732 |
|
RTList<Voice>::Iterator end = pOtherKey->pActiveVoices->end(); |
733 |
|
for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) { |
734 |
|
if (itVoiceToBeKilled->Type != Voice::type_release_trigger) itVoiceToBeKilled->Kill(itNoteOnEvent); |
735 |
|
} |
736 |
|
} |
737 |
|
} |
738 |
|
if (!pKey->Active) { // mark as active key |
739 |
|
pKey->Active = true; |
740 |
|
pKey->itSelf = pActiveKeys->allocAppend(); |
741 |
|
*pKey->itSelf = itNoteOnEvent->Param.Note.Key; |
742 |
|
} |
743 |
|
if (itNewVoice->KeyGroup) { |
744 |
|
*ppKeyGroup = &*pKey->itSelf; // put key as the (new) active key to its key group |
745 |
|
} |
746 |
|
if (itNewVoice->Type == Voice::type_release_trigger_required) pKey->ReleaseTrigger = true; // mark key for the need of release triggered voice(s) |
747 |
|
return itNewVoice; // success |
748 |
|
} |
749 |
|
} |
750 |
|
else if (VoiceStealing) { |
751 |
|
// first, get total amount of required voices (dependant on amount of layers) |
752 |
|
::gig::Region* pRegion = pInstrument->GetRegion(itNoteOnEvent->Param.Note.Key); |
753 |
|
if (!pRegion) return Pool<Voice>::Iterator(); // nothing defined for this MIDI key, so no voice needed |
754 |
|
int voicesRequired = pRegion->Layers; |
755 |
|
|
756 |
|
// now steal the (remaining) amount of voices |
757 |
|
for (int i = iLayer; i < voicesRequired; i++) |
758 |
|
StealVoice(itNoteOnEvent); |
759 |
|
|
760 |
|
// put note-on event into voice-stealing queue, so it will be reprocessed after killed voice died |
761 |
|
RTList<Event>::Iterator itStealEvent = pVoiceStealingQueue->allocAppend(); |
762 |
|
if (itStealEvent) { |
763 |
|
*itStealEvent = *itNoteOnEvent; // copy event |
764 |
|
itStealEvent->Param.Note.Layer = iLayer; |
765 |
|
itStealEvent->Param.Note.ReleaseTrigger = ReleaseTriggerVoice; |
766 |
|
} |
767 |
|
else dmsg(1,("Voice stealing queue full!\n")); |
768 |
|
} |
769 |
|
|
770 |
|
return Pool<Voice>::Iterator(); // no free voice or error |
771 |
|
} |
772 |
|
|
773 |
|
/** |
774 |
|
* Will be called by LaunchVoice() method in case there are no free |
775 |
|
* voices left. This method will select and kill one old voice for |
776 |
|
* voice stealing and postpone the note-on event until the selected |
777 |
|
* voice actually died. |
778 |
|
* |
779 |
|
* @param itNoteOnEvent - key, velocity and time stamp of the event |
780 |
*/ |
*/ |
781 |
void Engine::ProcessPitchbend(Event* pPitchbendEvent) { |
void Engine::StealVoice(Pool<Event>::Iterator& itNoteOnEvent) { |
782 |
this->Pitch = pPitchbendEvent->Pitch; // store current pitch value |
if (!pEventPool->poolIsEmpty()) { |
783 |
pEvents->move(pPitchbendEvent, pSynthesisEvents[Event::destination_vco]); |
|
784 |
|
RTList<uint>::Iterator iuiOldestKey; |
785 |
|
RTList<Voice>::Iterator itOldestVoice; |
786 |
|
|
787 |
|
// Select one voice for voice stealing |
788 |
|
switch (VOICE_STEAL_ALGORITHM) { |
789 |
|
|
790 |
|
// try to pick the oldest voice on the key where the new |
791 |
|
// voice should be spawned, if there is no voice on that |
792 |
|
// key, or no voice left to kill there, then procceed with |
793 |
|
// 'oldestkey' algorithm |
794 |
|
case voice_steal_algo_keymask: { |
795 |
|
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
796 |
|
if (itLastStolenVoice) { |
797 |
|
itOldestVoice = itLastStolenVoice; |
798 |
|
++itOldestVoice; |
799 |
|
} |
800 |
|
else { // no voice stolen in this audio fragment cycle yet |
801 |
|
itOldestVoice = pOldestKey->pActiveVoices->first(); |
802 |
|
} |
803 |
|
if (itOldestVoice) { |
804 |
|
iuiOldestKey = pOldestKey->itSelf; |
805 |
|
break; // selection succeeded |
806 |
|
} |
807 |
|
} // no break - intentional ! |
808 |
|
|
809 |
|
// try to pick the oldest voice on the oldest active key |
810 |
|
// (caution: must stay after 'keymask' algorithm !) |
811 |
|
case voice_steal_algo_oldestkey: { |
812 |
|
if (itLastStolenVoice) { |
813 |
|
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiLastStolenKey]; |
814 |
|
itOldestVoice = itLastStolenVoice; |
815 |
|
++itOldestVoice; |
816 |
|
if (!itOldestVoice) { |
817 |
|
iuiOldestKey = iuiLastStolenKey; |
818 |
|
++iuiOldestKey; |
819 |
|
if (iuiOldestKey) { |
820 |
|
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiOldestKey]; |
821 |
|
itOldestVoice = pOldestKey->pActiveVoices->first(); |
822 |
|
} |
823 |
|
else { |
824 |
|
dmsg(1,("gig::Engine: Warning, too less voices, even for voice stealing! - Better recompile with higher MAX_AUDIO_VOICES.\n")); |
825 |
|
return; |
826 |
|
} |
827 |
|
} |
828 |
|
else iuiOldestKey = iuiLastStolenKey; |
829 |
|
} |
830 |
|
else { // no voice stolen in this audio fragment cycle yet |
831 |
|
iuiOldestKey = pActiveKeys->first(); |
832 |
|
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiOldestKey]; |
833 |
|
itOldestVoice = pOldestKey->pActiveVoices->first(); |
834 |
|
} |
835 |
|
break; |
836 |
|
} |
837 |
|
|
838 |
|
// don't steal anything |
839 |
|
case voice_steal_algo_none: |
840 |
|
default: { |
841 |
|
dmsg(1,("No free voice (voice stealing disabled)!\n")); |
842 |
|
return; |
843 |
|
} |
844 |
|
} |
845 |
|
|
846 |
|
//FIXME: can be removed, just a sanity check for debugging |
847 |
|
if (!itOldestVoice->IsActive()) dmsg(1,("gig::Engine: ERROR, tried to steal a voice which was not active !!!\n")); |
848 |
|
|
849 |
|
// now kill the selected voice |
850 |
|
itOldestVoice->Kill(itNoteOnEvent); |
851 |
|
// remember which voice on which key we stole, so we can simply proceed for the next voice stealing |
852 |
|
this->itLastStolenVoice = itOldestVoice; |
853 |
|
this->iuiLastStolenKey = iuiOldestKey; |
854 |
|
} |
855 |
|
else dmsg(1,("Event pool emtpy!\n")); |
856 |
} |
} |
857 |
|
|
858 |
/** |
/** |
859 |
* 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. |
860 |
* 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 |
861 |
* 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 |
862 |
|
* just was killed. |
863 |
* |
* |
864 |
* @param pVoice - points to the voice to be killed |
* @param itVoice - points to the voice to be freed |
865 |
*/ |
*/ |
866 |
void Engine::KillVoice(Voice* pVoice) { |
void Engine::FreeVoice(Pool<Voice>::Iterator& itVoice) { |
867 |
if (pVoice) { |
if (itVoice) { |
868 |
if (pVoice->IsActive()) pVoice->Kill(); |
midi_key_info_t* pKey = &pMIDIKeyInfo[itVoice->MIDIKey]; |
869 |
|
|
870 |
midi_key_info_t* pKey = &pMIDIKeyInfo[pVoice->MIDIKey]; |
uint keygroup = itVoice->KeyGroup; |
871 |
|
|
872 |
// free the voice object |
// free the voice object |
873 |
pVoicePool->free(pVoice); |
pVoicePool->free(itVoice); |
874 |
|
|
875 |
// 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 |
876 |
if (pKey->pActiveVoices->is_empty()) { |
if (pKey->pActiveVoices->isEmpty() && keygroup) { |
877 |
pKey->Active = false; |
uint** ppKeyGroup = &ActiveKeyGroups[keygroup]; |
878 |
pActiveKeys->free(pKey->pSelf); // remove key from list of active keys |
if (*ppKeyGroup == &*pKey->itSelf) *ppKeyGroup = NULL; // remove key from key group |
|
pKey->pSelf = NULL; |
|
|
dmsg(3,("Key has no more voices now\n")); |
|
879 |
} |
} |
880 |
} |
} |
881 |
else std::cerr << "Couldn't release voice! (pVoice == NULL)\n" << std::flush; |
else std::cerr << "Couldn't release voice! (!itVoice)\n" << std::flush; |
882 |
|
} |
883 |
|
|
884 |
|
/** |
885 |
|
* Called when there's no more voice left on a key, this call will |
886 |
|
* update the key info respectively. |
887 |
|
* |
888 |
|
* @param pKey - key which is now inactive |
889 |
|
*/ |
890 |
|
void Engine::FreeKey(midi_key_info_t* pKey) { |
891 |
|
if (pKey->pActiveVoices->isEmpty()) { |
892 |
|
pKey->Active = false; |
893 |
|
pActiveKeys->free(pKey->itSelf); // remove key from list of active keys |
894 |
|
pKey->itSelf = RTList<uint>::Iterator(); |
895 |
|
pKey->ReleaseTrigger = false; |
896 |
|
pKey->pEvents->clear(); |
897 |
|
dmsg(3,("Key has no more voices now\n")); |
898 |
|
} |
899 |
|
else dmsg(1,("gig::Engine: Oops, tried to free a key which contains voices.\n")); |
900 |
} |
} |
901 |
|
|
902 |
/** |
/** |
903 |
* Reacts on supported control change commands (e.g. pitch bend wheel, |
* Reacts on supported control change commands (e.g. pitch bend wheel, |
904 |
* modulation wheel, aftertouch). |
* modulation wheel, aftertouch). |
905 |
* |
* |
906 |
* @param pControlChangeEvent - controller, value and time stamp of the event |
* @param itControlChangeEvent - controller, value and time stamp of the event |
907 |
*/ |
*/ |
908 |
void Engine::ProcessControlChange(Event* pControlChangeEvent) { |
void Engine::ProcessControlChange(Pool<Event>::Iterator& itControlChangeEvent) { |
909 |
dmsg(4,("Engine::ContinuousController cc=%d v=%d\n", pControlChangeEvent->Controller, pControlChangeEvent->Value)); |
dmsg(4,("Engine::ContinuousController cc=%d v=%d\n", itControlChangeEvent->Param.CC.Controller, itControlChangeEvent->Param.CC.Value)); |
910 |
|
|
911 |
switch (pControlChangeEvent->Controller) { |
switch (itControlChangeEvent->Param.CC.Controller) { |
912 |
case 64: { |
case 64: { |
913 |
if (pControlChangeEvent->Value >= 64 && !SustainPedal) { |
if (itControlChangeEvent->Param.CC.Value >= 64 && !SustainPedal) { |
914 |
dmsg(4,("PEDAL DOWN\n")); |
dmsg(4,("PEDAL DOWN\n")); |
915 |
SustainPedal = true; |
SustainPedal = true; |
916 |
|
|
917 |
// cancel release process of voices if necessary |
// cancel release process of voices if necessary |
918 |
uint* piKey = pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
919 |
if (piKey) { |
if (iuiKey) { |
920 |
pControlChangeEvent->Type = Event::type_cancel_release; // transform event type |
itControlChangeEvent->Type = Event::type_cancel_release; // transform event type |
921 |
while (piKey) { |
while (iuiKey) { |
922 |
midi_key_info_t* pKey = &pMIDIKeyInfo[*piKey]; |
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
923 |
pActiveKeys->set_current(piKey); |
++iuiKey; |
|
piKey = pActiveKeys->next(); |
|
924 |
if (!pKey->KeyPressed) { |
if (!pKey->KeyPressed) { |
925 |
Event* pNewEvent = pKey->pEvents->alloc(); |
RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend(); |
926 |
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 |
927 |
else dmsg(1,("Event pool emtpy!\n")); |
else dmsg(1,("Event pool emtpy!\n")); |
928 |
} |
} |
929 |
} |
} |
930 |
} |
} |
931 |
} |
} |
932 |
if (pControlChangeEvent->Value < 64 && SustainPedal) { |
if (itControlChangeEvent->Param.CC.Value < 64 && SustainPedal) { |
933 |
dmsg(4,("PEDAL UP\n")); |
dmsg(4,("PEDAL UP\n")); |
934 |
SustainPedal = false; |
SustainPedal = false; |
935 |
|
|
936 |
// release voices if their respective key is not pressed |
// release voices if their respective key is not pressed |
937 |
uint* piKey = pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
938 |
if (piKey) { |
if (iuiKey) { |
939 |
pControlChangeEvent->Type = Event::type_release; // transform event type |
itControlChangeEvent->Type = Event::type_release; // transform event type |
940 |
while (piKey) { |
while (iuiKey) { |
941 |
midi_key_info_t* pKey = &pMIDIKeyInfo[*piKey]; |
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
942 |
pActiveKeys->set_current(piKey); |
++iuiKey; |
|
piKey = pActiveKeys->next(); |
|
943 |
if (!pKey->KeyPressed) { |
if (!pKey->KeyPressed) { |
944 |
Event* pNewEvent = pKey->pEvents->alloc(); |
RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend(); |
945 |
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 |
946 |
else dmsg(1,("Event pool emtpy!\n")); |
else dmsg(1,("Event pool emtpy!\n")); |
947 |
} |
} |
948 |
} |
} |
953 |
} |
} |
954 |
|
|
955 |
// update controller value in the engine's controller table |
// update controller value in the engine's controller table |
956 |
ControllerTable[pControlChangeEvent->Controller] = pControlChangeEvent->Value; |
ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value; |
957 |
|
|
958 |
// 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 |
959 |
pEvents->move(pControlChangeEvent, pCCEvents); |
itControlChangeEvent.moveToEndOf(pCCEvents); |
960 |
|
} |
961 |
|
|
962 |
|
/** |
963 |
|
* Reacts on MIDI system exclusive messages. |
964 |
|
* |
965 |
|
* @param itSysexEvent - sysex data size and time stamp of the sysex event |
966 |
|
*/ |
967 |
|
void Engine::ProcessSysex(Pool<Event>::Iterator& itSysexEvent) { |
968 |
|
RingBuffer<uint8_t>::NonVolatileReader reader = pSysexBuffer->get_non_volatile_reader(); |
969 |
|
|
970 |
|
uint8_t exclusive_status, id; |
971 |
|
if (!reader.pop(&exclusive_status)) goto free_sysex_data; |
972 |
|
if (!reader.pop(&id)) goto free_sysex_data; |
973 |
|
if (exclusive_status != 0xF0) goto free_sysex_data; |
974 |
|
|
975 |
|
switch (id) { |
976 |
|
case 0x41: { // Roland |
977 |
|
uint8_t device_id, model_id, cmd_id; |
978 |
|
if (!reader.pop(&device_id)) goto free_sysex_data; |
979 |
|
if (!reader.pop(&model_id)) goto free_sysex_data; |
980 |
|
if (!reader.pop(&cmd_id)) goto free_sysex_data; |
981 |
|
if (model_id != 0x42 /*GS*/) goto free_sysex_data; |
982 |
|
if (cmd_id != 0x12 /*DT1*/) goto free_sysex_data; |
983 |
|
|
984 |
|
// command address |
985 |
|
uint8_t addr[3]; // 2 byte addr MSB, followed by 1 byte addr LSB) |
986 |
|
const RingBuffer<uint8_t>::NonVolatileReader checksum_reader = reader; // so we can calculate the check sum later |
987 |
|
if (reader.read(&addr[0], 3) != 3) goto free_sysex_data; |
988 |
|
if (addr[0] == 0x40 && addr[1] == 0x00) { // System Parameters |
989 |
|
} |
990 |
|
else if (addr[0] == 0x40 && addr[1] == 0x01) { // Common Parameters |
991 |
|
} |
992 |
|
else if (addr[0] == 0x40 && (addr[1] & 0xf0) == 0x10) { // Part Parameters (1) |
993 |
|
switch (addr[3]) { |
994 |
|
case 0x40: { // scale tuning |
995 |
|
uint8_t scale_tunes[12]; // detuning of all 12 semitones of an octave |
996 |
|
if (reader.read(&scale_tunes[0], 12) != 12) goto free_sysex_data; |
997 |
|
uint8_t checksum; |
998 |
|
if (!reader.pop(&checksum)) goto free_sysex_data; |
999 |
|
if (GSCheckSum(checksum_reader, 12) != checksum) goto free_sysex_data; |
1000 |
|
for (int i = 0; i < 12; i++) scale_tunes[i] -= 64; |
1001 |
|
AdjustScale((int8_t*) scale_tunes); |
1002 |
|
break; |
1003 |
|
} |
1004 |
|
} |
1005 |
|
} |
1006 |
|
else if (addr[0] == 0x40 && (addr[1] & 0xf0) == 0x20) { // Part Parameters (2) |
1007 |
|
} |
1008 |
|
else if (addr[0] == 0x41) { // Drum Setup Parameters |
1009 |
|
} |
1010 |
|
break; |
1011 |
|
} |
1012 |
|
} |
1013 |
|
|
1014 |
|
free_sysex_data: // finally free sysex data |
1015 |
|
pSysexBuffer->increment_read_ptr(itSysexEvent->Param.Sysex.Size); |
1016 |
|
} |
1017 |
|
|
1018 |
|
/** |
1019 |
|
* Calculates the Roland GS sysex check sum. |
1020 |
|
* |
1021 |
|
* @param AddrReader - reader which currently points to the first GS |
1022 |
|
* command address byte of the GS sysex message in |
1023 |
|
* question |
1024 |
|
* @param DataSize - size of the GS message data (in bytes) |
1025 |
|
*/ |
1026 |
|
uint8_t Engine::GSCheckSum(const RingBuffer<uint8_t>::NonVolatileReader AddrReader, uint DataSize) { |
1027 |
|
RingBuffer<uint8_t>::NonVolatileReader reader = AddrReader; |
1028 |
|
uint bytes = 3 /*addr*/ + DataSize; |
1029 |
|
uint8_t addr_and_data[bytes]; |
1030 |
|
reader.read(&addr_and_data[0], bytes); |
1031 |
|
uint8_t sum = 0; |
1032 |
|
for (uint i = 0; i < bytes; i++) sum += addr_and_data[i]; |
1033 |
|
return 128 - sum % 128; |
1034 |
|
} |
1035 |
|
|
1036 |
|
/** |
1037 |
|
* Allows to tune each of the twelve semitones of an octave. |
1038 |
|
* |
1039 |
|
* @param ScaleTunes - detuning of all twelve semitones (in cents) |
1040 |
|
*/ |
1041 |
|
void Engine::AdjustScale(int8_t ScaleTunes[12]) { |
1042 |
|
memcpy(&this->ScaleTuning[0], &ScaleTunes[0], 12); //TODO: currently not sample accurate |
1043 |
} |
} |
1044 |
|
|
1045 |
/** |
/** |
1065 |
GlobalVolume = f; |
GlobalVolume = f; |
1066 |
} |
} |
1067 |
|
|
1068 |
|
uint Engine::Channels() { |
1069 |
|
return 2; |
1070 |
|
} |
1071 |
|
|
1072 |
|
void Engine::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) { |
1073 |
|
AudioChannel* pChannel = pAudioOutputDevice->Channel(AudioDeviceChannel); |
1074 |
|
if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel)); |
1075 |
|
switch (EngineAudioChannel) { |
1076 |
|
case 0: // left output channel |
1077 |
|
pOutputLeft = pChannel->Buffer(); |
1078 |
|
AudioDeviceChannelLeft = AudioDeviceChannel; |
1079 |
|
break; |
1080 |
|
case 1: // right output channel |
1081 |
|
pOutputRight = pChannel->Buffer(); |
1082 |
|
AudioDeviceChannelRight = AudioDeviceChannel; |
1083 |
|
break; |
1084 |
|
default: |
1085 |
|
throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel)); |
1086 |
|
} |
1087 |
|
} |
1088 |
|
|
1089 |
|
int Engine::OutputChannel(uint EngineAudioChannel) { |
1090 |
|
switch (EngineAudioChannel) { |
1091 |
|
case 0: // left channel |
1092 |
|
return AudioDeviceChannelLeft; |
1093 |
|
case 1: // right channel |
1094 |
|
return AudioDeviceChannelRight; |
1095 |
|
default: |
1096 |
|
throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel)); |
1097 |
|
} |
1098 |
|
} |
1099 |
|
|
1100 |
uint Engine::VoiceCount() { |
uint Engine::VoiceCount() { |
1101 |
return ActiveVoiceCount; |
return ActiveVoiceCount; |
1102 |
} |
} |
1146 |
} |
} |
1147 |
|
|
1148 |
String Engine::Version() { |
String Engine::Version() { |
1149 |
String s = "$Revision: 1.7 $"; |
String s = "$Revision: 1.17 $"; |
1150 |
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 |
1151 |
} |
} |
1152 |
|
|