3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005, 2006 Christian Schoenebeck * |
* Copyright (C) 2005 - 2009 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
9 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
23 |
|
|
24 |
#include "EngineChannel.h" |
#include "EngineChannel.h" |
25 |
|
|
26 |
|
#include "../../common/global_private.h" |
27 |
|
#include "../../Sampler.h" |
28 |
|
|
29 |
namespace LinuxSampler { namespace gig { |
namespace LinuxSampler { namespace gig { |
30 |
|
|
31 |
EngineChannel::EngineChannel() { |
EngineChannel::EngineChannel() : |
32 |
|
InstrumentChangeCommandReader(InstrumentChangeCommand), |
33 |
|
virtualMidiDevicesReader_AudioThread(virtualMidiDevices), |
34 |
|
virtualMidiDevicesReader_MidiThread(virtualMidiDevices) |
35 |
|
{ |
36 |
pMIDIKeyInfo = new midi_key_info_t[128]; |
pMIDIKeyInfo = new midi_key_info_t[128]; |
37 |
pEngine = NULL; |
pEngine = NULL; |
38 |
pInstrument = NULL; |
pInstrument = NULL; |
50 |
} |
} |
51 |
InstrumentIdx = -1; |
InstrumentIdx = -1; |
52 |
InstrumentStat = -1; |
InstrumentStat = -1; |
53 |
|
pChannelLeft = NULL; |
54 |
|
pChannelRight = NULL; |
55 |
AudioDeviceChannelLeft = -1; |
AudioDeviceChannelLeft = -1; |
56 |
AudioDeviceChannelRight = -1; |
AudioDeviceChannelRight = -1; |
57 |
pMidiInputPort = NULL; |
pMidiInputPort = NULL; |
60 |
SoloMode = false; |
SoloMode = false; |
61 |
PortamentoMode = false; |
PortamentoMode = false; |
62 |
PortamentoTime = CONFIG_PORTAMENTO_TIME_DEFAULT; |
PortamentoTime = CONFIG_PORTAMENTO_TIME_DEFAULT; |
63 |
|
|
64 |
|
// reset the instrument change command struct (need to be done |
65 |
|
// twice, as it is double buffered) |
66 |
|
{ |
67 |
|
instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate(); |
68 |
|
cmd.pDimRegionsInUse = NULL; |
69 |
|
cmd.pInstrument = NULL; |
70 |
|
cmd.bChangeInstrument = false; |
71 |
|
} |
72 |
|
{ |
73 |
|
instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig(); |
74 |
|
cmd.pDimRegionsInUse = NULL; |
75 |
|
cmd.pInstrument = NULL; |
76 |
|
cmd.bChangeInstrument = false; |
77 |
|
} |
78 |
} |
} |
79 |
|
|
80 |
EngineChannel::~EngineChannel() { |
EngineChannel::~EngineChannel() { |
81 |
DisconnectAudioOutputDevice(); |
DisconnectAudioOutputDevice(); |
82 |
if (pInstrument) Engine::instruments.HandBack(pInstrument, this); |
|
83 |
|
// In case the channel was removed before the instrument was |
84 |
|
// fully loaded, try to give back instrument again (see bug #113) |
85 |
|
instrument_change_command_t& cmd = ChangeInstrument(NULL); |
86 |
|
if (cmd.pInstrument) { |
87 |
|
Engine::instruments.HandBack(cmd.pInstrument, this); |
88 |
|
} |
89 |
|
/////// |
90 |
|
|
91 |
if (pEventQueue) delete pEventQueue; |
if (pEventQueue) delete pEventQueue; |
92 |
if (pActiveKeys) delete pActiveKeys; |
if (pActiveKeys) delete pActiveKeys; |
93 |
if (pMIDIKeyInfo) delete[] pMIDIKeyInfo; |
if (pMIDIKeyInfo) delete[] pMIDIKeyInfo; |
94 |
|
RemoveAllFxSends(); |
95 |
} |
} |
96 |
|
|
97 |
/** |
/** |
189 |
* This method will then actually start to load the instrument and block |
* This method will then actually start to load the instrument and block |
190 |
* the calling thread until loading was completed. |
* the calling thread until loading was completed. |
191 |
* |
* |
|
* @returns detailed description of the method call result |
|
192 |
* @see PrepareLoadInstrument() |
* @see PrepareLoadInstrument() |
193 |
*/ |
*/ |
194 |
void EngineChannel::LoadInstrument() { |
void EngineChannel::LoadInstrument() { |
195 |
|
// make sure we don't trigger any new notes with an old |
196 |
if (pEngine) pEngine->DisableAndLock(); |
// instrument |
197 |
|
instrument_change_command_t& cmd = ChangeInstrument(0); |
198 |
ResetInternal(); |
if (cmd.pInstrument) { |
199 |
|
// give old instrument back to instrument manager, but |
200 |
// free old instrument |
// keep the dimension regions and samples that are in use |
201 |
if (pInstrument) { |
Engine::instruments.HandBackInstrument(cmd.pInstrument, this, cmd.pDimRegionsInUse); |
|
// give old instrument back to instrument manager |
|
|
Engine::instruments.HandBack(pInstrument, this); |
|
202 |
} |
} |
203 |
|
cmd.pDimRegionsInUse->clear(); |
204 |
|
|
205 |
// delete all key groups |
// delete all key groups |
206 |
ActiveKeyGroups.clear(); |
ActiveKeyGroups.clear(); |
207 |
|
|
208 |
// request gig instrument from instrument manager |
// request gig instrument from instrument manager |
209 |
|
::gig::Instrument* newInstrument; |
210 |
try { |
try { |
211 |
InstrumentManager::instrument_id_t instrid; |
InstrumentManager::instrument_id_t instrid; |
212 |
instrid.FileName = InstrumentFile; |
instrid.FileName = InstrumentFile; |
213 |
instrid.Index = InstrumentIdx; |
instrid.Index = InstrumentIdx; |
214 |
pInstrument = Engine::instruments.Borrow(instrid, this); |
newInstrument = Engine::instruments.Borrow(instrid, this); |
215 |
if (!pInstrument) { |
if (!newInstrument) { |
216 |
InstrumentStat = -1; |
throw InstrumentManagerException("resource was not created"); |
|
dmsg(1,("no instrument loaded!!!\n")); |
|
|
exit(EXIT_FAILURE); |
|
217 |
} |
} |
218 |
} |
} |
219 |
catch (RIFF::Exception e) { |
catch (RIFF::Exception e) { |
220 |
InstrumentStat = -2; |
InstrumentStat = -2; |
221 |
|
StatusChanged(true); |
222 |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message; |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message; |
223 |
throw Exception(msg); |
throw Exception(msg); |
224 |
} |
} |
225 |
catch (InstrumentResourceManagerException e) { |
catch (InstrumentManagerException e) { |
226 |
InstrumentStat = -3; |
InstrumentStat = -3; |
227 |
|
StatusChanged(true); |
228 |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message(); |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message(); |
229 |
throw Exception(msg); |
throw Exception(msg); |
230 |
} |
} |
231 |
catch (...) { |
catch (...) { |
232 |
InstrumentStat = -4; |
InstrumentStat = -4; |
233 |
|
StatusChanged(true); |
234 |
throw Exception("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file."); |
throw Exception("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file."); |
235 |
} |
} |
236 |
|
|
237 |
// rebuild ActiveKeyGroups map with key groups of current instrument |
// rebuild ActiveKeyGroups map with key groups of current instrument |
238 |
for (::gig::Region* pRegion = pInstrument->GetFirstRegion(); pRegion; pRegion = pInstrument->GetNextRegion()) |
for (::gig::Region* pRegion = newInstrument->GetFirstRegion(); pRegion; pRegion = newInstrument->GetNextRegion()) |
239 |
if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL; |
if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL; |
240 |
|
|
241 |
InstrumentIdxName = pInstrument->pInfo->Name; |
InstrumentIdxName = newInstrument->pInfo->Name; |
242 |
InstrumentStat = 100; |
InstrumentStat = 100; |
243 |
|
|
244 |
// inform audio driver for the need of two channels |
ChangeInstrument(newInstrument); |
|
try { |
|
|
if (pEngine && pEngine->pAudioOutputDevice) |
|
|
pEngine->pAudioOutputDevice->AcquireChannels(2); // gig Engine only stereo |
|
|
} |
|
|
catch (AudioOutputException e) { |
|
|
String msg = "Audio output device unable to provide 2 audio channels, cause: " + e.Message(); |
|
|
throw Exception(msg); |
|
|
} |
|
245 |
|
|
246 |
if (pEngine) pEngine->Enable(); |
StatusChanged(true); |
247 |
|
} |
248 |
|
|
249 |
|
|
250 |
|
/** |
251 |
|
* Changes the instrument for an engine channel. |
252 |
|
* |
253 |
|
* @param pInstrument - new instrument |
254 |
|
* @returns the resulting instrument change command after the |
255 |
|
* command switch, containing the old instrument and |
256 |
|
* the dimregions it is using |
257 |
|
*/ |
258 |
|
EngineChannel::instrument_change_command_t& EngineChannel::ChangeInstrument(::gig::Instrument* pInstrument) { |
259 |
|
instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate(); |
260 |
|
cmd.pInstrument = pInstrument; |
261 |
|
cmd.bChangeInstrument = true; |
262 |
|
|
263 |
|
return InstrumentChangeCommand.SwitchConfig(); |
264 |
} |
} |
265 |
|
|
266 |
/** |
/** |
305 |
pEngine = Engine::AcquireEngine(this, pAudioOut); |
pEngine = Engine::AcquireEngine(this, pAudioOut); |
306 |
ResetInternal(); |
ResetInternal(); |
307 |
pEvents = new RTList<Event>(pEngine->pEventPool); |
pEvents = new RTList<Event>(pEngine->pEventPool); |
308 |
|
|
309 |
|
// reset the instrument change command struct (need to be done |
310 |
|
// twice, as it is double buffered) |
311 |
|
{ |
312 |
|
instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate(); |
313 |
|
cmd.pDimRegionsInUse = new RTList< ::gig::DimensionRegion*>(pEngine->pDimRegionPool[0]); |
314 |
|
cmd.pInstrument = 0; |
315 |
|
cmd.bChangeInstrument = false; |
316 |
|
} |
317 |
|
{ |
318 |
|
instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig(); |
319 |
|
cmd.pDimRegionsInUse = new RTList< ::gig::DimensionRegion*>(pEngine->pDimRegionPool[1]); |
320 |
|
cmd.pInstrument = 0; |
321 |
|
cmd.bChangeInstrument = false; |
322 |
|
} |
323 |
|
|
324 |
|
if (pInstrument != NULL) { |
325 |
|
pInstrument = NULL; |
326 |
|
InstrumentStat = -1; |
327 |
|
InstrumentIdx = -1; |
328 |
|
InstrumentIdxName = ""; |
329 |
|
InstrumentFile = ""; |
330 |
|
bStatusChanged = true; |
331 |
|
} |
332 |
|
|
333 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
334 |
pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool); |
pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool); |
335 |
pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEngine->pEventPool); |
pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEngine->pEventPool); |
336 |
} |
} |
337 |
AudioDeviceChannelLeft = 0; |
AudioDeviceChannelLeft = 0; |
338 |
AudioDeviceChannelRight = 1; |
AudioDeviceChannelRight = 1; |
339 |
pOutputLeft = pAudioOut->Channel(0)->Buffer(); |
if (fxSends.empty()) { // render directly into the AudioDevice's output buffers |
340 |
pOutputRight = pAudioOut->Channel(1)->Buffer(); |
pChannelLeft = pAudioOut->Channel(AudioDeviceChannelLeft); |
341 |
|
pChannelRight = pAudioOut->Channel(AudioDeviceChannelRight); |
342 |
|
} else { // use local buffers for rendering and copy later |
343 |
|
// ensure the local buffers have the correct size |
344 |
|
if (pChannelLeft) delete pChannelLeft; |
345 |
|
if (pChannelRight) delete pChannelRight; |
346 |
|
pChannelLeft = new AudioChannel(0, pAudioOut->MaxSamplesPerCycle()); |
347 |
|
pChannelRight = new AudioChannel(1, pAudioOut->MaxSamplesPerCycle()); |
348 |
|
} |
349 |
|
if (pEngine->EngineDisabled.GetUnsafe()) pEngine->Enable(); |
350 |
MidiInputPort::AddSysexListener(pEngine); |
MidiInputPort::AddSysexListener(pEngine); |
351 |
} |
} |
352 |
|
|
353 |
void EngineChannel::DisconnectAudioOutputDevice() { |
void EngineChannel::DisconnectAudioOutputDevice() { |
354 |
if (pEngine) { // if clause to prevent disconnect loops |
if (pEngine) { // if clause to prevent disconnect loops |
355 |
|
|
356 |
ResetInternal(); |
ResetInternal(); |
357 |
|
|
358 |
|
// delete the structures used for instrument change |
359 |
|
RTList< ::gig::DimensionRegion*>* d = InstrumentChangeCommand.GetConfigForUpdate().pDimRegionsInUse; |
360 |
|
if (d) delete d; |
361 |
|
EngineChannel::instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig(); |
362 |
|
d = cmd.pDimRegionsInUse; |
363 |
|
if (d) delete d; |
364 |
|
|
365 |
|
if (cmd.pInstrument) { |
366 |
|
// release the currently loaded instrument |
367 |
|
Engine::instruments.HandBack(cmd.pInstrument, this); |
368 |
|
} |
369 |
|
|
370 |
if (pEvents) { |
if (pEvents) { |
371 |
delete pEvents; |
delete pEvents; |
372 |
pEvents = NULL; |
pEvents = NULL; |
381 |
pMIDIKeyInfo[i].pEvents = NULL; |
pMIDIKeyInfo[i].pEvents = NULL; |
382 |
} |
} |
383 |
} |
} |
|
Engine* oldEngine = pEngine; |
|
384 |
AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice; |
AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice; |
385 |
pEngine = NULL; |
pEngine = NULL; |
386 |
Engine::FreeEngine(this, oldAudioDevice); |
Engine::FreeEngine(this, oldAudioDevice); |
387 |
AudioDeviceChannelLeft = -1; |
AudioDeviceChannelLeft = -1; |
388 |
AudioDeviceChannelRight = -1; |
AudioDeviceChannelRight = -1; |
389 |
|
if (!fxSends.empty()) { // free the local rendering buffers |
390 |
|
if (pChannelLeft) delete pChannelLeft; |
391 |
|
if (pChannelRight) delete pChannelRight; |
392 |
|
} |
393 |
|
pChannelLeft = NULL; |
394 |
|
pChannelRight = NULL; |
395 |
} |
} |
396 |
} |
} |
397 |
|
|
398 |
|
AudioOutputDevice* EngineChannel::GetAudioOutputDevice() { |
399 |
|
return (pEngine) ? pEngine->pAudioOutputDevice : NULL; |
400 |
|
} |
401 |
|
|
402 |
void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) { |
void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) { |
403 |
if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet."); |
if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet."); |
404 |
|
|
406 |
if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel)); |
if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel)); |
407 |
switch (EngineAudioChannel) { |
switch (EngineAudioChannel) { |
408 |
case 0: // left output channel |
case 0: // left output channel |
409 |
pOutputLeft = pChannel->Buffer(); |
if (fxSends.empty()) pChannelLeft = pChannel; |
410 |
AudioDeviceChannelLeft = AudioDeviceChannel; |
AudioDeviceChannelLeft = AudioDeviceChannel; |
411 |
break; |
break; |
412 |
case 1: // right output channel |
case 1: // right output channel |
413 |
pOutputRight = pChannel->Buffer(); |
if (fxSends.empty()) pChannelRight = pChannel; |
414 |
AudioDeviceChannelRight = AudioDeviceChannel; |
AudioDeviceChannelRight = AudioDeviceChannel; |
415 |
break; |
break; |
416 |
default: |
default: |
417 |
throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel)); |
throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel)); |
418 |
} |
} |
419 |
|
|
420 |
|
bStatusChanged = true; |
421 |
} |
} |
422 |
|
|
423 |
int EngineChannel::OutputChannel(uint EngineAudioChannel) { |
int EngineChannel::OutputChannel(uint EngineAudioChannel) { |
453 |
return midiChannel; |
return midiChannel; |
454 |
} |
} |
455 |
|
|
456 |
|
FxSend* EngineChannel::AddFxSend(uint8_t MidiCtrl, String Name) throw (Exception) { |
457 |
|
if (pEngine) pEngine->DisableAndLock(); |
458 |
|
FxSend* pFxSend = new FxSend(this, MidiCtrl, Name); |
459 |
|
if (fxSends.empty()) { |
460 |
|
if (pEngine && pEngine->pAudioOutputDevice) { |
461 |
|
AudioOutputDevice* pDevice = pEngine->pAudioOutputDevice; |
462 |
|
// create local render buffers |
463 |
|
pChannelLeft = new AudioChannel(0, pDevice->MaxSamplesPerCycle()); |
464 |
|
pChannelRight = new AudioChannel(1, pDevice->MaxSamplesPerCycle()); |
465 |
|
} else { |
466 |
|
// postpone local render buffer creation until audio device is assigned |
467 |
|
pChannelLeft = NULL; |
468 |
|
pChannelRight = NULL; |
469 |
|
} |
470 |
|
} |
471 |
|
fxSends.push_back(pFxSend); |
472 |
|
if (pEngine) pEngine->Enable(); |
473 |
|
fireFxSendCountChanged(GetSamplerChannel()->Index(), GetFxSendCount()); |
474 |
|
|
475 |
|
return pFxSend; |
476 |
|
} |
477 |
|
|
478 |
|
FxSend* EngineChannel::GetFxSend(uint FxSendIndex) { |
479 |
|
return (FxSendIndex < fxSends.size()) ? fxSends[FxSendIndex] : NULL; |
480 |
|
} |
481 |
|
|
482 |
|
uint EngineChannel::GetFxSendCount() { |
483 |
|
return fxSends.size(); |
484 |
|
} |
485 |
|
|
486 |
|
void EngineChannel::RemoveFxSend(FxSend* pFxSend) { |
487 |
|
if (pEngine) pEngine->DisableAndLock(); |
488 |
|
for ( |
489 |
|
std::vector<FxSend*>::iterator iter = fxSends.begin(); |
490 |
|
iter != fxSends.end(); iter++ |
491 |
|
) { |
492 |
|
if (*iter == pFxSend) { |
493 |
|
delete pFxSend; |
494 |
|
fxSends.erase(iter); |
495 |
|
if (fxSends.empty()) { |
496 |
|
// destroy local render buffers |
497 |
|
if (pChannelLeft) delete pChannelLeft; |
498 |
|
if (pChannelRight) delete pChannelRight; |
499 |
|
// fallback to render directly into AudioOutputDevice's buffers |
500 |
|
if (pEngine && pEngine->pAudioOutputDevice) { |
501 |
|
pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft); |
502 |
|
pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight); |
503 |
|
} else { // we update the pointers later |
504 |
|
pChannelLeft = NULL; |
505 |
|
pChannelRight = NULL; |
506 |
|
} |
507 |
|
} |
508 |
|
break; |
509 |
|
} |
510 |
|
} |
511 |
|
if (pEngine) pEngine->Enable(); |
512 |
|
fireFxSendCountChanged(GetSamplerChannel()->Index(), GetFxSendCount()); |
513 |
|
} |
514 |
|
|
515 |
/** |
/** |
516 |
* Will be called by the MIDIIn Thread to let the audio thread trigger a new |
* Will be called by the MIDIIn Thread to let the audio thread trigger a new |
517 |
* voice for the given key. This method is meant for real time rendering, |
* voice for the given key. This method is meant for real time rendering, |
530 |
event.pEngineChannel = this; |
event.pEngineChannel = this; |
531 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
532 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
else dmsg(1,("EngineChannel: Input event queue full!")); |
533 |
|
// inform connected virtual MIDI devices if any ... |
534 |
|
// (e.g. virtual MIDI keyboard in instrument editor(s)) |
535 |
|
ArrayList<VirtualMidiDevice*>& devices = |
536 |
|
const_cast<ArrayList<VirtualMidiDevice*>&>( |
537 |
|
virtualMidiDevicesReader_MidiThread.Lock() |
538 |
|
); |
539 |
|
for (int i = 0; i < devices.size(); i++) { |
540 |
|
devices[i]->SendNoteOnToDevice(Key, Velocity); |
541 |
|
} |
542 |
|
virtualMidiDevicesReader_MidiThread.Unlock(); |
543 |
} |
} |
544 |
} |
} |
545 |
|
|
566 |
event.pEngineChannel = this; |
event.pEngineChannel = this; |
567 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
568 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
else dmsg(1,("EngineChannel: Input event queue full!")); |
569 |
|
// inform connected virtual MIDI devices if any ... |
570 |
|
// (e.g. virtual MIDI keyboard in instrument editor(s)) |
571 |
|
ArrayList<VirtualMidiDevice*>& devices = |
572 |
|
const_cast<ArrayList<VirtualMidiDevice*>&>( |
573 |
|
virtualMidiDevicesReader_MidiThread.Lock() |
574 |
|
); |
575 |
|
for (int i = 0; i < devices.size(); i++) { |
576 |
|
devices[i]->SendNoteOnToDevice(Key, Velocity); |
577 |
|
} |
578 |
|
virtualMidiDevicesReader_MidiThread.Unlock(); |
579 |
} |
} |
580 |
} |
} |
581 |
|
|
597 |
event.pEngineChannel = this; |
event.pEngineChannel = this; |
598 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
599 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
else dmsg(1,("EngineChannel: Input event queue full!")); |
600 |
|
// inform connected virtual MIDI devices if any ... |
601 |
|
// (e.g. virtual MIDI keyboard in instrument editor(s)) |
602 |
|
ArrayList<VirtualMidiDevice*>& devices = |
603 |
|
const_cast<ArrayList<VirtualMidiDevice*>&>( |
604 |
|
virtualMidiDevicesReader_MidiThread.Lock() |
605 |
|
); |
606 |
|
for (int i = 0; i < devices.size(); i++) { |
607 |
|
devices[i]->SendNoteOffToDevice(Key, Velocity); |
608 |
|
} |
609 |
|
virtualMidiDevicesReader_MidiThread.Unlock(); |
610 |
} |
} |
611 |
} |
} |
612 |
|
|
633 |
event.pEngineChannel = this; |
event.pEngineChannel = this; |
634 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
635 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
else dmsg(1,("EngineChannel: Input event queue full!")); |
636 |
|
// inform connected virtual MIDI devices if any ... |
637 |
|
// (e.g. virtual MIDI keyboard in instrument editor(s)) |
638 |
|
ArrayList<VirtualMidiDevice*>& devices = |
639 |
|
const_cast<ArrayList<VirtualMidiDevice*>&>( |
640 |
|
virtualMidiDevicesReader_MidiThread.Lock() |
641 |
|
); |
642 |
|
for (int i = 0; i < devices.size(); i++) { |
643 |
|
devices[i]->SendNoteOffToDevice(Key, Velocity); |
644 |
|
} |
645 |
|
virtualMidiDevicesReader_MidiThread.Unlock(); |
646 |
} |
} |
647 |
} |
} |
648 |
|
|
736 |
} |
} |
737 |
} |
} |
738 |
|
|
739 |
|
/** |
740 |
|
* Will be called by the MIDIIn Thread to signal that a program |
741 |
|
* change should be performed. As a program change isn't |
742 |
|
* real-time safe, the actual change is performed by the disk |
743 |
|
* thread. |
744 |
|
* |
745 |
|
* @param Program - MIDI program change number |
746 |
|
*/ |
747 |
|
void EngineChannel::SendProgramChange(uint8_t Program) { |
748 |
|
if (pEngine) { |
749 |
|
pEngine->pDiskThread->OrderProgramChange(Program, this); |
750 |
|
} |
751 |
|
} |
752 |
|
|
753 |
void EngineChannel::ClearEventLists() { |
void EngineChannel::ClearEventLists() { |
754 |
pEvents->clear(); |
pEvents->clear(); |
755 |
// empty MIDI key specific event lists |
// empty MIDI key specific event lists |
766 |
Pitch = 0; |
Pitch = 0; |
767 |
SustainPedal = false; |
SustainPedal = false; |
768 |
SostenutoPedal = false; |
SostenutoPedal = false; |
769 |
GlobalVolume = CONFIG_GLOBAL_ATTENUATION; |
GlobalVolume = 1.0f; |
770 |
MidiVolume = 1.0; |
MidiVolume = 1.0; |
771 |
GlobalPanLeft = 1.0f; |
GlobalPanLeft = 1.0f; |
772 |
GlobalPanRight = 1.0f; |
GlobalPanRight = 1.0f; |
773 |
|
iLastPanRequest = 64; |
774 |
|
GlobalTranspose = 0; |
775 |
// set all MIDI controller values to zero |
// set all MIDI controller values to zero |
776 |
memset(ControllerTable, 0x00, 129); |
memset(ControllerTable, 0x00, 129); |
777 |
|
// reset all FX Send levels |
778 |
|
for ( |
779 |
|
std::vector<FxSend*>::iterator iter = fxSends.begin(); |
780 |
|
iter != fxSends.end(); iter++ |
781 |
|
) { |
782 |
|
(*iter)->Reset(); |
783 |
|
} |
784 |
} |
} |
785 |
|
|
786 |
/** |
/** |
797 |
* current audio cycle |
* current audio cycle |
798 |
*/ |
*/ |
799 |
void EngineChannel::ImportEvents(uint Samples) { |
void EngineChannel::ImportEvents(uint Samples) { |
800 |
|
// import events from pure software MIDI "devices" |
801 |
|
// (e.g. virtual keyboard in instrument editor) |
802 |
|
{ |
803 |
|
const int FragmentPos = 0; // randomly chosen, we don't care about jitter for virtual MIDI devices |
804 |
|
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
805 |
|
VirtualMidiDevice::event_t devEvent; // the event format we get from the virtual MIDI device |
806 |
|
// as we're going to (carefully) write some status to the |
807 |
|
// synchronized struct, we cast away the const |
808 |
|
ArrayList<VirtualMidiDevice*>& devices = |
809 |
|
const_cast<ArrayList<VirtualMidiDevice*>&>(virtualMidiDevicesReader_AudioThread.Lock()); |
810 |
|
// iterate through all virtual MIDI devices |
811 |
|
for (int i = 0; i < devices.size(); i++) { |
812 |
|
VirtualMidiDevice* pDev = devices[i]; |
813 |
|
// I think we can simply flush the whole FIFO(s), the user shouldn't be so fast ;-) |
814 |
|
while (pDev->GetMidiEventFromDevice(devEvent)) { |
815 |
|
event.Type = |
816 |
|
(devEvent.Type == VirtualMidiDevice::EVENT_TYPE_NOTEON) ? |
817 |
|
Event::type_note_on : Event::type_note_off; |
818 |
|
event.Param.Note.Key = devEvent.Key; |
819 |
|
event.Param.Note.Velocity = devEvent.Velocity; |
820 |
|
event.pEngineChannel = this; |
821 |
|
// copy event to internal event list |
822 |
|
if (pEvents->poolIsEmpty()) { |
823 |
|
dmsg(1,("Event pool emtpy!\n")); |
824 |
|
goto exitVirtualDevicesLoop; |
825 |
|
} |
826 |
|
*pEvents->allocAppend() = event; |
827 |
|
} |
828 |
|
} |
829 |
|
} |
830 |
|
exitVirtualDevicesLoop: |
831 |
|
virtualMidiDevicesReader_AudioThread.Unlock(); |
832 |
|
|
833 |
|
// import events from the regular MIDI devices |
834 |
RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
835 |
Event* pEvent; |
Event* pEvent; |
836 |
while (true) { |
while (true) { |
853 |
eventQueueReader.free(); // free all copied events from input queue |
eventQueueReader.free(); // free all copied events from input queue |
854 |
} |
} |
855 |
|
|
856 |
|
void EngineChannel::RemoveAllFxSends() { |
857 |
|
if (pEngine) pEngine->DisableAndLock(); |
858 |
|
if (!fxSends.empty()) { // free local render buffers |
859 |
|
if (pChannelLeft) { |
860 |
|
delete pChannelLeft; |
861 |
|
if (pEngine && pEngine->pAudioOutputDevice) { |
862 |
|
// fallback to render directly to the AudioOutputDevice's buffer |
863 |
|
pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft); |
864 |
|
} else pChannelLeft = NULL; |
865 |
|
} |
866 |
|
if (pChannelRight) { |
867 |
|
delete pChannelRight; |
868 |
|
if (pEngine && pEngine->pAudioOutputDevice) { |
869 |
|
// fallback to render directly to the AudioOutputDevice's buffer |
870 |
|
pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight); |
871 |
|
} else pChannelRight = NULL; |
872 |
|
} |
873 |
|
} |
874 |
|
for (int i = 0; i < fxSends.size(); i++) delete fxSends[i]; |
875 |
|
fxSends.clear(); |
876 |
|
if (pEngine) pEngine->Enable(); |
877 |
|
} |
878 |
|
|
879 |
|
void EngineChannel::Connect(VirtualMidiDevice* pDevice) { |
880 |
|
// double buffer ... double work ... |
881 |
|
{ |
882 |
|
ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.GetConfigForUpdate(); |
883 |
|
devices.add(pDevice); |
884 |
|
} |
885 |
|
{ |
886 |
|
ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.SwitchConfig(); |
887 |
|
devices.add(pDevice); |
888 |
|
} |
889 |
|
} |
890 |
|
|
891 |
|
void EngineChannel::Disconnect(VirtualMidiDevice* pDevice) { |
892 |
|
// double buffer ... double work ... |
893 |
|
{ |
894 |
|
ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.GetConfigForUpdate(); |
895 |
|
devices.remove(pDevice); |
896 |
|
} |
897 |
|
{ |
898 |
|
ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.SwitchConfig(); |
899 |
|
devices.remove(pDevice); |
900 |
|
} |
901 |
|
} |
902 |
|
|
903 |
float EngineChannel::Volume() { |
float EngineChannel::Volume() { |
904 |
return GlobalVolume; |
return GlobalVolume; |
905 |
} |
} |
909 |
bStatusChanged = true; // status of engine channel has changed, so set notify flag |
bStatusChanged = true; // status of engine channel has changed, so set notify flag |
910 |
} |
} |
911 |
|
|
912 |
|
float EngineChannel::Pan() { |
913 |
|
return float(iLastPanRequest - 64) / 64.0f; |
914 |
|
} |
915 |
|
|
916 |
|
void EngineChannel::Pan(float f) { |
917 |
|
int iMidiPan = int(f * 64.0f) + 64; |
918 |
|
if (iMidiPan > 127) iMidiPan = 127; |
919 |
|
else if (iMidiPan < 0) iMidiPan = 0; |
920 |
|
GlobalPanLeft = Engine::PanCurve[128 - iMidiPan]; |
921 |
|
GlobalPanRight = Engine::PanCurve[iMidiPan]; |
922 |
|
iLastPanRequest = iMidiPan; |
923 |
|
} |
924 |
|
|
925 |
uint EngineChannel::Channels() { |
uint EngineChannel::Channels() { |
926 |
return 2; |
return 2; |
927 |
} |
} |
946 |
return LS_GIG_ENGINE_NAME; |
return LS_GIG_ENGINE_NAME; |
947 |
} |
} |
948 |
|
|
949 |
|
void EngineChannel::ClearDimRegionsInUse() { |
950 |
|
{ |
951 |
|
instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate(); |
952 |
|
if(cmd.pDimRegionsInUse != NULL) cmd.pDimRegionsInUse->clear(); |
953 |
|
} |
954 |
|
{ |
955 |
|
instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig(); |
956 |
|
if(cmd.pDimRegionsInUse != NULL) cmd.pDimRegionsInUse->clear(); |
957 |
|
} |
958 |
|
} |
959 |
|
|
960 |
|
void EngineChannel::ResetDimRegionsInUse() { |
961 |
|
{ |
962 |
|
instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate(); |
963 |
|
if(cmd.pDimRegionsInUse != NULL) { |
964 |
|
delete cmd.pDimRegionsInUse; |
965 |
|
cmd.pDimRegionsInUse = new RTList< ::gig::DimensionRegion*>(pEngine->pDimRegionPool[0]); |
966 |
|
} |
967 |
|
} |
968 |
|
{ |
969 |
|
instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig(); |
970 |
|
if(cmd.pDimRegionsInUse != NULL) { |
971 |
|
delete cmd.pDimRegionsInUse; |
972 |
|
cmd.pDimRegionsInUse = new RTList< ::gig::DimensionRegion*>(pEngine->pDimRegionPool[1]); |
973 |
|
} |
974 |
|
} |
975 |
|
} |
976 |
|
|
977 |
}} // namespace LinuxSampler::gig |
}} // namespace LinuxSampler::gig |