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 - 2008 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; |
64 |
|
|
65 |
EngineChannel::~EngineChannel() { |
EngineChannel::~EngineChannel() { |
66 |
DisconnectAudioOutputDevice(); |
DisconnectAudioOutputDevice(); |
|
if (pInstrument) Engine::instruments.HandBack(pInstrument, this); |
|
67 |
if (pEventQueue) delete pEventQueue; |
if (pEventQueue) delete pEventQueue; |
68 |
if (pActiveKeys) delete pActiveKeys; |
if (pActiveKeys) delete pActiveKeys; |
69 |
if (pMIDIKeyInfo) delete[] pMIDIKeyInfo; |
if (pMIDIKeyInfo) delete[] pMIDIKeyInfo; |
165 |
* This method will then actually start to load the instrument and block |
* This method will then actually start to load the instrument and block |
166 |
* the calling thread until loading was completed. |
* the calling thread until loading was completed. |
167 |
* |
* |
|
* @returns detailed description of the method call result |
|
168 |
* @see PrepareLoadInstrument() |
* @see PrepareLoadInstrument() |
169 |
*/ |
*/ |
170 |
void EngineChannel::LoadInstrument() { |
void EngineChannel::LoadInstrument() { |
171 |
::gig::Instrument* oldInstrument = pInstrument; |
// make sure we don't trigger any new notes with an old |
172 |
|
// instrument |
173 |
// free old instrument |
instrument_change_command_t& cmd = ChangeInstrument(0); |
174 |
if (oldInstrument) { |
if (cmd.pInstrument) { |
175 |
if (pEngine) { |
// give old instrument back to instrument manager, but |
176 |
// make sure we don't trigger any new notes with the |
// keep the dimension regions and samples that are in use |
177 |
// old instrument |
Engine::instruments.HandBackInstrument(cmd.pInstrument, this, cmd.pDimRegionsInUse); |
|
::gig::DimensionRegion** dimRegionsInUse = pEngine->ChangeInstrument(this, 0); |
|
|
|
|
|
// give old instrument back to instrument manager, but |
|
|
// keep the dimension regions and samples that are in |
|
|
// use |
|
|
Engine::instruments.HandBackInstrument(oldInstrument, this, dimRegionsInUse); |
|
|
} else { |
|
|
Engine::instruments.HandBack(oldInstrument, this); |
|
|
} |
|
178 |
} |
} |
179 |
|
cmd.pDimRegionsInUse->clear(); |
180 |
|
|
181 |
// delete all key groups |
// delete all key groups |
182 |
ActiveKeyGroups.clear(); |
ActiveKeyGroups.clear(); |
189 |
instrid.Index = InstrumentIdx; |
instrid.Index = InstrumentIdx; |
190 |
newInstrument = Engine::instruments.Borrow(instrid, this); |
newInstrument = Engine::instruments.Borrow(instrid, this); |
191 |
if (!newInstrument) { |
if (!newInstrument) { |
192 |
InstrumentStat = -1; |
throw InstrumentManagerException("resource was not created"); |
|
dmsg(1,("no instrument loaded!!!\n")); |
|
|
exit(EXIT_FAILURE); |
|
193 |
} |
} |
194 |
} |
} |
195 |
catch (RIFF::Exception e) { |
catch (RIFF::Exception e) { |
196 |
InstrumentStat = -2; |
InstrumentStat = -2; |
197 |
|
StatusChanged(true); |
198 |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message; |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message; |
199 |
throw Exception(msg); |
throw Exception(msg); |
200 |
} |
} |
201 |
catch (InstrumentResourceManagerException e) { |
catch (InstrumentManagerException e) { |
202 |
InstrumentStat = -3; |
InstrumentStat = -3; |
203 |
|
StatusChanged(true); |
204 |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message(); |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message(); |
205 |
throw Exception(msg); |
throw Exception(msg); |
206 |
} |
} |
207 |
catch (...) { |
catch (...) { |
208 |
InstrumentStat = -4; |
InstrumentStat = -4; |
209 |
|
StatusChanged(true); |
210 |
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."); |
211 |
} |
} |
212 |
|
|
217 |
InstrumentIdxName = newInstrument->pInfo->Name; |
InstrumentIdxName = newInstrument->pInfo->Name; |
218 |
InstrumentStat = 100; |
InstrumentStat = 100; |
219 |
|
|
220 |
if (pEngine) pEngine->ChangeInstrument(this, newInstrument); |
ChangeInstrument(newInstrument); |
221 |
else pInstrument = newInstrument; |
|
222 |
|
StatusChanged(true); |
223 |
|
} |
224 |
|
|
225 |
|
|
226 |
|
/** |
227 |
|
* Changes the instrument for an engine channel. |
228 |
|
* |
229 |
|
* @param pInstrument - new instrument |
230 |
|
* @returns the resulting instrument change command after the |
231 |
|
* command switch, containing the old instrument and |
232 |
|
* the dimregions it is using |
233 |
|
*/ |
234 |
|
EngineChannel::instrument_change_command_t& EngineChannel::ChangeInstrument(::gig::Instrument* pInstrument) { |
235 |
|
instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate(); |
236 |
|
cmd.pInstrument = pInstrument; |
237 |
|
cmd.bChangeInstrument = true; |
238 |
|
|
239 |
|
return InstrumentChangeCommand.SwitchConfig(); |
240 |
} |
} |
241 |
|
|
242 |
/** |
/** |
281 |
pEngine = Engine::AcquireEngine(this, pAudioOut); |
pEngine = Engine::AcquireEngine(this, pAudioOut); |
282 |
ResetInternal(); |
ResetInternal(); |
283 |
pEvents = new RTList<Event>(pEngine->pEventPool); |
pEvents = new RTList<Event>(pEngine->pEventPool); |
284 |
|
|
285 |
|
// reset the instrument change command struct (need to be done |
286 |
|
// twice, as it is double buffered) |
287 |
|
{ |
288 |
|
instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate(); |
289 |
|
cmd.pDimRegionsInUse = new RTList< ::gig::DimensionRegion*>(pEngine->pDimRegionPool[0]); |
290 |
|
cmd.pInstrument = 0; |
291 |
|
cmd.bChangeInstrument = false; |
292 |
|
} |
293 |
|
{ |
294 |
|
instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig(); |
295 |
|
cmd.pDimRegionsInUse = new RTList< ::gig::DimensionRegion*>(pEngine->pDimRegionPool[1]); |
296 |
|
cmd.pInstrument = 0; |
297 |
|
cmd.bChangeInstrument = false; |
298 |
|
} |
299 |
|
|
300 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
301 |
pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool); |
pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool); |
302 |
pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEngine->pEventPool); |
pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEngine->pEventPool); |
319 |
|
|
320 |
void EngineChannel::DisconnectAudioOutputDevice() { |
void EngineChannel::DisconnectAudioOutputDevice() { |
321 |
if (pEngine) { // if clause to prevent disconnect loops |
if (pEngine) { // if clause to prevent disconnect loops |
322 |
|
|
323 |
|
// delete the structures used for instrument change |
324 |
|
RTList< ::gig::DimensionRegion*>* d = InstrumentChangeCommand.GetConfigForUpdate().pDimRegionsInUse; |
325 |
|
if (d) delete d; |
326 |
|
EngineChannel::instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig(); |
327 |
|
d = cmd.pDimRegionsInUse; |
328 |
|
|
329 |
|
if (cmd.pInstrument) { |
330 |
|
// release the currently loaded instrument |
331 |
|
Engine::instruments.HandBackInstrument(cmd.pInstrument, this, d); |
332 |
|
} |
333 |
|
|
334 |
|
if (d) delete d; |
335 |
|
|
336 |
|
// release all active dimension regions to resource |
337 |
|
// manager |
338 |
|
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
339 |
|
RTList<uint>::Iterator end = pActiveKeys->end(); |
340 |
|
while (iuiKey != end) { // iterate through all active keys |
341 |
|
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
342 |
|
++iuiKey; |
343 |
|
|
344 |
|
RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first(); |
345 |
|
RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end(); |
346 |
|
for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key |
347 |
|
Engine::instruments.HandBackDimReg(itVoice->pDimRgn); |
348 |
|
} |
349 |
|
} |
350 |
|
|
351 |
ResetInternal(); |
ResetInternal(); |
352 |
if (pEvents) { |
if (pEvents) { |
353 |
delete pEvents; |
delete pEvents; |
399 |
default: |
default: |
400 |
throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel)); |
throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel)); |
401 |
} |
} |
402 |
|
|
403 |
|
bStatusChanged = true; |
404 |
} |
} |
405 |
|
|
406 |
int EngineChannel::OutputChannel(uint EngineAudioChannel) { |
int EngineChannel::OutputChannel(uint EngineAudioChannel) { |
453 |
} |
} |
454 |
fxSends.push_back(pFxSend); |
fxSends.push_back(pFxSend); |
455 |
if (pEngine) pEngine->Enable(); |
if (pEngine) pEngine->Enable(); |
456 |
|
fireFxSendCountChanged(GetSamplerChannel()->Index(), GetFxSendCount()); |
457 |
|
|
458 |
return pFxSend; |
return pFxSend; |
459 |
} |
} |
460 |
|
|
492 |
} |
} |
493 |
} |
} |
494 |
if (pEngine) pEngine->Enable(); |
if (pEngine) pEngine->Enable(); |
495 |
|
fireFxSendCountChanged(GetSamplerChannel()->Index(), GetFxSendCount()); |
496 |
} |
} |
497 |
|
|
498 |
/** |
/** |
513 |
event.pEngineChannel = this; |
event.pEngineChannel = this; |
514 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
515 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
else dmsg(1,("EngineChannel: Input event queue full!")); |
516 |
|
// inform connected virtual MIDI devices if any ... |
517 |
|
// (e.g. virtual MIDI keyboard in instrument editor(s)) |
518 |
|
ArrayList<VirtualMidiDevice*>& devices = |
519 |
|
const_cast<ArrayList<VirtualMidiDevice*>&>( |
520 |
|
virtualMidiDevicesReader_MidiThread.Lock() |
521 |
|
); |
522 |
|
for (int i = 0; i < devices.size(); i++) { |
523 |
|
devices[i]->SendNoteOnToDevice(Key, Velocity); |
524 |
|
} |
525 |
|
virtualMidiDevicesReader_MidiThread.Unlock(); |
526 |
} |
} |
527 |
} |
} |
528 |
|
|
549 |
event.pEngineChannel = this; |
event.pEngineChannel = this; |
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,("EngineChannel: Input event queue full!")); |
else dmsg(1,("EngineChannel: Input event queue full!")); |
552 |
|
// inform connected virtual MIDI devices if any ... |
553 |
|
// (e.g. virtual MIDI keyboard in instrument editor(s)) |
554 |
|
ArrayList<VirtualMidiDevice*>& devices = |
555 |
|
const_cast<ArrayList<VirtualMidiDevice*>&>( |
556 |
|
virtualMidiDevicesReader_MidiThread.Lock() |
557 |
|
); |
558 |
|
for (int i = 0; i < devices.size(); i++) { |
559 |
|
devices[i]->SendNoteOnToDevice(Key, Velocity); |
560 |
|
} |
561 |
|
virtualMidiDevicesReader_MidiThread.Unlock(); |
562 |
} |
} |
563 |
} |
} |
564 |
|
|
580 |
event.pEngineChannel = this; |
event.pEngineChannel = this; |
581 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
582 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
else dmsg(1,("EngineChannel: Input event queue full!")); |
583 |
|
// inform connected virtual MIDI devices if any ... |
584 |
|
// (e.g. virtual MIDI keyboard in instrument editor(s)) |
585 |
|
ArrayList<VirtualMidiDevice*>& devices = |
586 |
|
const_cast<ArrayList<VirtualMidiDevice*>&>( |
587 |
|
virtualMidiDevicesReader_MidiThread.Lock() |
588 |
|
); |
589 |
|
for (int i = 0; i < devices.size(); i++) { |
590 |
|
devices[i]->SendNoteOffToDevice(Key, Velocity); |
591 |
|
} |
592 |
|
virtualMidiDevicesReader_MidiThread.Unlock(); |
593 |
} |
} |
594 |
} |
} |
595 |
|
|
616 |
event.pEngineChannel = this; |
event.pEngineChannel = this; |
617 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
618 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
else dmsg(1,("EngineChannel: Input event queue full!")); |
619 |
|
// inform connected virtual MIDI devices if any ... |
620 |
|
// (e.g. virtual MIDI keyboard in instrument editor(s)) |
621 |
|
ArrayList<VirtualMidiDevice*>& devices = |
622 |
|
const_cast<ArrayList<VirtualMidiDevice*>&>( |
623 |
|
virtualMidiDevicesReader_MidiThread.Lock() |
624 |
|
); |
625 |
|
for (int i = 0; i < devices.size(); i++) { |
626 |
|
devices[i]->SendNoteOffToDevice(Key, Velocity); |
627 |
|
} |
628 |
|
virtualMidiDevicesReader_MidiThread.Unlock(); |
629 |
} |
} |
630 |
} |
} |
631 |
|
|
739 |
MidiVolume = 1.0; |
MidiVolume = 1.0; |
740 |
GlobalPanLeft = 1.0f; |
GlobalPanLeft = 1.0f; |
741 |
GlobalPanRight = 1.0f; |
GlobalPanRight = 1.0f; |
742 |
|
iLastPanRequest = 64; |
743 |
|
GlobalTranspose = 0; |
744 |
// set all MIDI controller values to zero |
// set all MIDI controller values to zero |
745 |
memset(ControllerTable, 0x00, 129); |
memset(ControllerTable, 0x00, 129); |
746 |
|
// reset all FX Send levels |
747 |
|
for ( |
748 |
|
std::vector<FxSend*>::iterator iter = fxSends.begin(); |
749 |
|
iter != fxSends.end(); iter++ |
750 |
|
) { |
751 |
|
(*iter)->Reset(); |
752 |
|
} |
753 |
} |
} |
754 |
|
|
755 |
/** |
/** |
766 |
* current audio cycle |
* current audio cycle |
767 |
*/ |
*/ |
768 |
void EngineChannel::ImportEvents(uint Samples) { |
void EngineChannel::ImportEvents(uint Samples) { |
769 |
|
// import events from pure software MIDI "devices" |
770 |
|
// (e.g. virtual keyboard in instrument editor) |
771 |
|
{ |
772 |
|
const int FragmentPos = 0; // randomly chosen, we don't care about jitter for virtual MIDI devices |
773 |
|
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
774 |
|
VirtualMidiDevice::event_t devEvent; // the event format we get from the virtual MIDI device |
775 |
|
// as we're going to (carefully) write some status to the |
776 |
|
// synchronized struct, we cast away the const |
777 |
|
ArrayList<VirtualMidiDevice*>& devices = |
778 |
|
const_cast<ArrayList<VirtualMidiDevice*>&>(virtualMidiDevicesReader_AudioThread.Lock()); |
779 |
|
// iterate through all virtual MIDI devices |
780 |
|
for (int i = 0; i < devices.size(); i++) { |
781 |
|
VirtualMidiDevice* pDev = devices[i]; |
782 |
|
// I think we can simply flush the whole FIFO(s), the user shouldn't be so fast ;-) |
783 |
|
while (pDev->GetMidiEventFromDevice(devEvent)) { |
784 |
|
event.Type = |
785 |
|
(devEvent.Type == VirtualMidiDevice::EVENT_TYPE_NOTEON) ? |
786 |
|
Event::type_note_on : Event::type_note_off; |
787 |
|
event.Param.Note.Key = devEvent.Key; |
788 |
|
event.Param.Note.Velocity = devEvent.Velocity; |
789 |
|
event.pEngineChannel = this; |
790 |
|
// copy event to internal event list |
791 |
|
if (pEvents->poolIsEmpty()) { |
792 |
|
dmsg(1,("Event pool emtpy!\n")); |
793 |
|
goto exitVirtualDevicesLoop; |
794 |
|
} |
795 |
|
*pEvents->allocAppend() = event; |
796 |
|
} |
797 |
|
} |
798 |
|
} |
799 |
|
exitVirtualDevicesLoop: |
800 |
|
virtualMidiDevicesReader_AudioThread.Unlock(); |
801 |
|
|
802 |
|
// import events from the regular MIDI devices |
803 |
RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
804 |
Event* pEvent; |
Event* pEvent; |
805 |
while (true) { |
while (true) { |
837 |
if (pEngine && pEngine->pAudioOutputDevice) { |
if (pEngine && pEngine->pAudioOutputDevice) { |
838 |
// fallback to render directly to the AudioOutputDevice's buffer |
// fallback to render directly to the AudioOutputDevice's buffer |
839 |
pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight); |
pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight); |
840 |
} else pChannelRight = NULL; |
} else pChannelRight = NULL; |
841 |
} |
} |
842 |
} |
} |
843 |
for (int i = 0; i < fxSends.size(); i++) delete fxSends[i]; |
for (int i = 0; i < fxSends.size(); i++) delete fxSends[i]; |
845 |
if (pEngine) pEngine->Enable(); |
if (pEngine) pEngine->Enable(); |
846 |
} |
} |
847 |
|
|
848 |
|
void EngineChannel::Connect(VirtualMidiDevice* pDevice) { |
849 |
|
// double buffer ... double work ... |
850 |
|
{ |
851 |
|
ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.GetConfigForUpdate(); |
852 |
|
devices.add(pDevice); |
853 |
|
} |
854 |
|
{ |
855 |
|
ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.SwitchConfig(); |
856 |
|
devices.add(pDevice); |
857 |
|
} |
858 |
|
} |
859 |
|
|
860 |
|
void EngineChannel::Disconnect(VirtualMidiDevice* pDevice) { |
861 |
|
// double buffer ... double work ... |
862 |
|
{ |
863 |
|
ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.GetConfigForUpdate(); |
864 |
|
devices.remove(pDevice); |
865 |
|
} |
866 |
|
{ |
867 |
|
ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.SwitchConfig(); |
868 |
|
devices.remove(pDevice); |
869 |
|
} |
870 |
|
} |
871 |
|
|
872 |
float EngineChannel::Volume() { |
float EngineChannel::Volume() { |
873 |
return GlobalVolume; |
return GlobalVolume; |
874 |
} |
} |
878 |
bStatusChanged = true; // status of engine channel has changed, so set notify flag |
bStatusChanged = true; // status of engine channel has changed, so set notify flag |
879 |
} |
} |
880 |
|
|
881 |
|
float EngineChannel::Pan() { |
882 |
|
return float(iLastPanRequest - 64) / 64.0f; |
883 |
|
} |
884 |
|
|
885 |
|
void EngineChannel::Pan(float f) { |
886 |
|
int iMidiPan = int(f * 64.0f) + 64; |
887 |
|
if (iMidiPan > 127) iMidiPan = 127; |
888 |
|
else if (iMidiPan < 0) iMidiPan = 0; |
889 |
|
GlobalPanLeft = Engine::PanCurve[128 - iMidiPan]; |
890 |
|
GlobalPanRight = Engine::PanCurve[iMidiPan]; |
891 |
|
iLastPanRequest = iMidiPan; |
892 |
|
} |
893 |
|
|
894 |
uint EngineChannel::Channels() { |
uint EngineChannel::Channels() { |
895 |
return 2; |
return 2; |
896 |
} |
} |