29 |
pMIDIKeyInfo = new midi_key_info_t[128]; |
pMIDIKeyInfo = new midi_key_info_t[128]; |
30 |
pEngine = NULL; |
pEngine = NULL; |
31 |
pInstrument = NULL; |
pInstrument = NULL; |
32 |
pEventQueue = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0); |
pEvents = NULL; // we allocate when we retrieve the right Engine object |
33 |
|
pCCEvents = NULL; // we allocate when we retrieve the right Engine object |
34 |
|
pEventQueue = new RingBuffer<Event>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0); |
35 |
pActiveKeys = new Pool<uint>(128); |
pActiveKeys = new Pool<uint>(128); |
36 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
37 |
pMIDIKeyInfo[i].pActiveVoices = NULL; // we allocate when we retrieve the right Engine object |
pMIDIKeyInfo[i].pActiveVoices = NULL; // we allocate when we retrieve the right Engine object |
39 |
pMIDIKeyInfo[i].Active = false; |
pMIDIKeyInfo[i].Active = false; |
40 |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
41 |
pMIDIKeyInfo[i].pEvents = NULL; // we allocate when we retrieve the right Engine object |
pMIDIKeyInfo[i].pEvents = NULL; // we allocate when we retrieve the right Engine object |
42 |
|
pMIDIKeyInfo[i].VoiceTheftsQueued = 0; |
43 |
pMIDIKeyInfo[i].RoundRobinIndex = 0; |
pMIDIKeyInfo[i].RoundRobinIndex = 0; |
44 |
} |
} |
45 |
|
for (uint i = 0; i < Event::destination_count; i++) { |
46 |
|
pSynthesisEvents[i] = NULL; // we allocate when we retrieve the right Engine object |
47 |
|
} |
48 |
InstrumentIdx = -1; |
InstrumentIdx = -1; |
49 |
InstrumentStat = -1; |
InstrumentStat = -1; |
50 |
AudioDeviceChannelLeft = -1; |
AudioDeviceChannelLeft = -1; |
52 |
} |
} |
53 |
|
|
54 |
EngineChannel::~EngineChannel() { |
EngineChannel::~EngineChannel() { |
55 |
|
DisconnectAudioOutputDevice(); |
56 |
if (pInstrument) Engine::instruments.HandBack(pInstrument, this); |
if (pInstrument) Engine::instruments.HandBack(pInstrument, this); |
|
for (uint i = 0; i < 128; i++) { |
|
|
if (pMIDIKeyInfo[i].pActiveVoices) { |
|
|
pMIDIKeyInfo[i].pActiveVoices->clear(); |
|
|
delete pMIDIKeyInfo[i].pActiveVoices; |
|
|
} |
|
|
if (pMIDIKeyInfo[i].pEvents) { |
|
|
pMIDIKeyInfo[i].pEvents->clear(); |
|
|
delete pMIDIKeyInfo[i].pEvents; |
|
|
} |
|
|
} |
|
57 |
if (pEventQueue) delete pEventQueue; |
if (pEventQueue) delete pEventQueue; |
58 |
if (pActiveKeys) delete pActiveKeys; |
if (pActiveKeys) delete pActiveKeys; |
59 |
if (pMIDIKeyInfo) delete[] pMIDIKeyInfo; |
if (pMIDIKeyInfo) delete[] pMIDIKeyInfo; |
60 |
} |
} |
61 |
|
|
62 |
/** |
/** |
63 |
|
* Implementation of virtual method from abstract EngineChannel interface. |
64 |
|
* This method will periodically be polled (e.g. by the LSCP server) to |
65 |
|
* check if some engine channel parameter has changed since the last |
66 |
|
* StatusChanged() call. |
67 |
|
* |
68 |
|
* TODO: This "poll method" is just a lazy solution and might be |
69 |
|
* replaced in future. |
70 |
|
* |
71 |
|
* @returns true if engine channel status has changed since last |
72 |
|
* StatusChanged() call |
73 |
|
*/ |
74 |
|
bool EngineChannel::StatusChanged() { |
75 |
|
bool b = bStatusChanged; |
76 |
|
bStatusChanged = false; |
77 |
|
return b; |
78 |
|
} |
79 |
|
|
80 |
|
/** |
81 |
* This method is not thread safe! |
* This method is not thread safe! |
82 |
*/ |
*/ |
83 |
void EngineChannel::ResetInternal() { |
void EngineChannel::ResetInternal() { |
88 |
GlobalPanRight = 1.0f; |
GlobalPanRight = 1.0f; |
89 |
CurrentKeyDimension = 0; |
CurrentKeyDimension = 0; |
90 |
|
|
91 |
// set all MIDI controller values to zero |
ResetControllers(); |
|
memset(ControllerTable, 0x00, 128); |
|
|
|
|
|
// reset voice stealing parameters |
|
|
itLastStolenVoice = RTList<Voice>::Iterator(); |
|
|
iuiLastStolenKey = RTList<uint>::Iterator(); |
|
92 |
|
|
93 |
// reset key info |
// reset key info |
94 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
100 |
pMIDIKeyInfo[i].Active = false; |
pMIDIKeyInfo[i].Active = false; |
101 |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
102 |
pMIDIKeyInfo[i].itSelf = Pool<uint>::Iterator(); |
pMIDIKeyInfo[i].itSelf = Pool<uint>::Iterator(); |
103 |
|
pMIDIKeyInfo[i].VoiceTheftsQueued = 0; |
104 |
} |
} |
105 |
|
|
106 |
// reset all key groups |
// reset all key groups |
114 |
pEventQueue->init(); |
pEventQueue->init(); |
115 |
|
|
116 |
if (pEngine) pEngine->ResetInternal(); |
if (pEngine) pEngine->ResetInternal(); |
117 |
|
|
118 |
|
// status of engine channel has changed, so set notify flag |
119 |
|
bStatusChanged = true; |
120 |
} |
} |
121 |
|
|
122 |
LinuxSampler::Engine* EngineChannel::GetEngine() { |
LinuxSampler::Engine* EngineChannel::GetEngine() { |
212 |
|
|
213 |
/** |
/** |
214 |
* Will be called by the InstrumentResourceManager when the instrument |
* Will be called by the InstrumentResourceManager when the instrument |
215 |
* we are currently using in this engine is going to be updated, so we |
* we are currently using on this EngineChannel is going to be updated, |
216 |
* can stop playback before that happens. |
* so we can stop playback before that happens. |
217 |
*/ |
*/ |
218 |
void EngineChannel::ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) { |
void EngineChannel::ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) { |
219 |
dmsg(3,("gig::Engine: Received instrument update message.\n")); |
dmsg(3,("gig::Engine: Received instrument update message.\n")); |
229 |
void EngineChannel::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) { |
void EngineChannel::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) { |
230 |
this->pInstrument = pNewResource; //TODO: there are couple of engine parameters we should update here as well if the instrument was updated (see LoadInstrument()) |
this->pInstrument = pNewResource; //TODO: there are couple of engine parameters we should update here as well if the instrument was updated (see LoadInstrument()) |
231 |
if (pEngine) pEngine->Enable(); |
if (pEngine) pEngine->Enable(); |
232 |
|
bStatusChanged = true; // status of engine has changed, so set notify flag |
233 |
|
} |
234 |
|
|
235 |
|
/** |
236 |
|
* Will be called by the InstrumentResourceManager on progress changes |
237 |
|
* while loading or realoading an instrument for this EngineChannel. |
238 |
|
* |
239 |
|
* @param fProgress - current progress as value between 0.0 and 1.0 |
240 |
|
*/ |
241 |
|
void EngineChannel::OnResourceProgress(float fProgress) { |
242 |
|
this->InstrumentStat = int(fProgress * 100.0f); |
243 |
|
dmsg(7,("gig::EngineChannel: progress %d%", InstrumentStat)); |
244 |
|
bStatusChanged = true; // status of engine has changed, so set notify flag |
245 |
} |
} |
246 |
|
|
247 |
void EngineChannel::Connect(AudioOutputDevice* pAudioOut) { |
void EngineChannel::Connect(AudioOutputDevice* pAudioOut) { |
248 |
if (pEngine && pEngine->pAudioOutputDevice != pAudioOut) { |
if (pEngine) { |
249 |
|
if (pEngine->pAudioOutputDevice == pAudioOut) return; |
250 |
DisconnectAudioOutputDevice(); |
DisconnectAudioOutputDevice(); |
251 |
} |
} |
252 |
pEngine = Engine::AcquireEngine(this, pAudioOut); |
pEngine = Engine::AcquireEngine(this, pAudioOut); |
253 |
ResetInternal(); |
ResetInternal(); |
254 |
|
pEvents = new RTList<Event>(pEngine->pEventPool); |
255 |
|
pCCEvents = new RTList<Event>(pEngine->pEventPool); |
256 |
|
for (uint i = 0; i < Event::destination_count; i++) { |
257 |
|
pSynthesisEvents[i] = new RTList<Event>(pEngine->pEventPool); |
258 |
|
} |
259 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
260 |
pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool); |
pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool); |
261 |
pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEngine->pEventPool); |
pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEngine->pEventPool); |
269 |
void EngineChannel::DisconnectAudioOutputDevice() { |
void EngineChannel::DisconnectAudioOutputDevice() { |
270 |
if (pEngine) { // if clause to prevent disconnect loops |
if (pEngine) { // if clause to prevent disconnect loops |
271 |
ResetInternal(); |
ResetInternal(); |
272 |
|
if (pEvents) { |
273 |
|
delete pEvents; |
274 |
|
pEvents = NULL; |
275 |
|
} |
276 |
|
if (pCCEvents) { |
277 |
|
delete pCCEvents; |
278 |
|
pCCEvents = NULL; |
279 |
|
} |
280 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
281 |
if (pMIDIKeyInfo[i].pActiveVoices) { |
if (pMIDIKeyInfo[i].pActiveVoices) { |
282 |
delete pMIDIKeyInfo[i].pActiveVoices; |
delete pMIDIKeyInfo[i].pActiveVoices; |
287 |
pMIDIKeyInfo[i].pEvents = NULL; |
pMIDIKeyInfo[i].pEvents = NULL; |
288 |
} |
} |
289 |
} |
} |
290 |
|
for (uint i = 0; i < Event::destination_count; i++) { |
291 |
|
if (pSynthesisEvents[i]) { |
292 |
|
delete pSynthesisEvents[i]; |
293 |
|
pSynthesisEvents[i] = NULL; |
294 |
|
} |
295 |
|
} |
296 |
Engine* oldEngine = pEngine; |
Engine* oldEngine = pEngine; |
297 |
AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice; |
AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice; |
298 |
pEngine = NULL; |
pEngine = NULL; |
406 |
} |
} |
407 |
} |
} |
408 |
|
|
409 |
|
void EngineChannel::ClearEventLists() { |
410 |
|
pEvents->clear(); |
411 |
|
pCCEvents->clear(); |
412 |
|
for (uint i = 0; i < Event::destination_count; i++) { |
413 |
|
pSynthesisEvents[i]->clear(); |
414 |
|
} |
415 |
|
// empty MIDI key specific event lists |
416 |
|
{ |
417 |
|
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
418 |
|
RTList<uint>::Iterator end = pActiveKeys->end(); |
419 |
|
for(; iuiKey != end; ++iuiKey) { |
420 |
|
pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key |
421 |
|
} |
422 |
|
} |
423 |
|
} |
424 |
|
|
425 |
|
void EngineChannel::ResetControllers() { |
426 |
|
// set all MIDI controller values to zero |
427 |
|
memset(ControllerTable, 0x00, 128); |
428 |
|
} |
429 |
|
|
430 |
|
/** |
431 |
|
* Copy all events from the engine channel's input event queue buffer to |
432 |
|
* the internal event list. This will be done at the beginning of each |
433 |
|
* audio cycle (that is each RenderAudio() call) to distinguish all |
434 |
|
* events which have to be processed in the current audio cycle. Each |
435 |
|
* EngineChannel has it's own input event queue for the common channel |
436 |
|
* specific events (like NoteOn, NoteOff and ControlChange events). |
437 |
|
* Beside that, the engine also has a input event queue for global |
438 |
|
* events (usually SysEx messages). |
439 |
|
* |
440 |
|
* @param Samples - number of sample points to be processed in the |
441 |
|
* current audio cycle |
442 |
|
*/ |
443 |
|
void EngineChannel::ImportEvents(uint Samples) { |
444 |
|
RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
445 |
|
Event* pEvent; |
446 |
|
while (true) { |
447 |
|
// get next event from input event queue |
448 |
|
if (!(pEvent = eventQueueReader.pop())) break; |
449 |
|
// if younger event reached, ignore that and all subsequent ones for now |
450 |
|
if (pEvent->FragmentPos() >= Samples) { |
451 |
|
eventQueueReader--; |
452 |
|
dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples)); |
453 |
|
pEvent->ResetFragmentPos(); |
454 |
|
break; |
455 |
|
} |
456 |
|
// copy event to internal event list |
457 |
|
if (pEvents->poolIsEmpty()) { |
458 |
|
dmsg(1,("Event pool emtpy!\n")); |
459 |
|
break; |
460 |
|
} |
461 |
|
*pEvents->allocAppend() = *pEvent; |
462 |
|
} |
463 |
|
eventQueueReader.free(); // free all copied events from input queue |
464 |
|
} |
465 |
|
|
466 |
float EngineChannel::Volume() { |
float EngineChannel::Volume() { |
467 |
return GlobalVolume; |
return GlobalVolume; |
468 |
} |
} |
469 |
|
|
470 |
void EngineChannel::Volume(float f) { |
void EngineChannel::Volume(float f) { |
471 |
GlobalVolume = f; |
GlobalVolume = f; |
472 |
|
bStatusChanged = true; // status of engine channel has changed, so set notify flag |
473 |
} |
} |
474 |
|
|
475 |
uint EngineChannel::Channels() { |
uint EngineChannel::Channels() { |
492 |
return InstrumentStat; |
return InstrumentStat; |
493 |
} |
} |
494 |
|
|
495 |
|
String EngineChannel::EngineName() { |
496 |
|
return LS_GIG_ENGINE_NAME; |
497 |
|
} |
498 |
|
|
499 |
}} // namespace LinuxSampler::gig |
}} // namespace LinuxSampler::gig |