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