2 |
* * |
* * |
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 Christian Schoenebeck * |
* Copyright (C) 2005-2009 Christian Schoenebeck * |
7 |
|
* Copyright (C) 2009 Grigor Iliev * |
8 |
* * |
* * |
9 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
10 |
* 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 |
|
|
25 |
#include "EngineChannel.h" |
#include "EngineChannel.h" |
26 |
|
#include "Engine.h" |
27 |
|
|
28 |
namespace LinuxSampler { namespace gig { |
namespace LinuxSampler { namespace gig { |
|
|
|
29 |
EngineChannel::EngineChannel() { |
EngineChannel::EngineChannel() { |
30 |
pMIDIKeyInfo = new midi_key_info_t[128]; |
|
|
pEngine = NULL; |
|
|
pInstrument = NULL; |
|
|
pEventQueue = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0); |
|
|
pActiveKeys = new Pool<uint>(128); |
|
|
for (uint i = 0; i < 128; i++) { |
|
|
pMIDIKeyInfo[i].pActiveVoices = NULL; // we allocate when we retrieve the right Engine object |
|
|
pMIDIKeyInfo[i].KeyPressed = false; |
|
|
pMIDIKeyInfo[i].Active = false; |
|
|
pMIDIKeyInfo[i].ReleaseTrigger = false; |
|
|
pMIDIKeyInfo[i].pEvents = NULL; // we allocate when we retrieve the right Engine object |
|
|
} |
|
|
InstrumentIdx = -1; |
|
|
InstrumentStat = -1; |
|
|
AudioDeviceChannelLeft = -1; |
|
|
AudioDeviceChannelRight = -1; |
|
31 |
} |
} |
32 |
|
|
33 |
EngineChannel::~EngineChannel() { |
EngineChannel::~EngineChannel() { |
34 |
if (pInstrument) Engine::instruments.HandBack(pInstrument, this); |
DisconnectAudioOutputDevice(); |
35 |
for (uint i = 0; i < 128; i++) { |
// In case the channel was removed before the instrument was |
36 |
if (pMIDIKeyInfo[i].pActiveVoices) { |
// fully loaded, try to give back instrument again (see bug #113) |
37 |
pMIDIKeyInfo[i].pActiveVoices->clear(); |
InstrumentChangeCmd< ::gig::DimensionRegion, ::gig::Instrument>& cmd = ChangeInstrument(NULL); |
38 |
delete pMIDIKeyInfo[i].pActiveVoices; |
if (cmd.pInstrument) { |
39 |
} |
InstrumentResourceManager* instrs = dynamic_cast<InstrumentResourceManager*>(pEngine->GetInstrumentManager()); |
40 |
if (pMIDIKeyInfo[i].pEvents) { |
instrs->HandBack(cmd.pInstrument, this); |
|
pMIDIKeyInfo[i].pEvents->clear(); |
|
|
delete pMIDIKeyInfo[i].pEvents; |
|
|
} |
|
41 |
} |
} |
42 |
if (pEventQueue) delete pEventQueue; |
/////// |
|
if (pActiveKeys) delete pActiveKeys; |
|
|
if (pMIDIKeyInfo) delete[] pMIDIKeyInfo; |
|
43 |
} |
} |
44 |
|
|
45 |
/** |
AbstractEngine::Format EngineChannel::GetEngineFormat() { return AbstractEngine::GIG; } |
46 |
* This method is not thread safe! |
|
47 |
*/ |
/** This method is not thread safe! */ |
48 |
void EngineChannel::ResetInternal() { |
void EngineChannel::ResetInternal() { |
|
Pitch = 0; |
|
|
SustainPedal = false; |
|
|
GlobalVolume = 1.0; |
|
|
GlobalPanLeft = 1.0f; |
|
|
GlobalPanRight = 1.0f; |
|
49 |
CurrentKeyDimension = 0; |
CurrentKeyDimension = 0; |
50 |
|
EngineChannelBase<Voice, ::gig::DimensionRegion, ::gig::Instrument>::ResetInternal(); |
|
// set all MIDI controller values to zero |
|
|
memset(ControllerTable, 0x00, 128); |
|
|
|
|
|
// reset voice stealing parameters |
|
|
itLastStolenVoice = RTList<Voice>::Iterator(); |
|
|
iuiLastStolenKey = RTList<uint>::Iterator(); |
|
|
|
|
|
// reset key info |
|
|
for (uint i = 0; i < 128; i++) { |
|
|
if (pMIDIKeyInfo[i].pActiveVoices) |
|
|
pMIDIKeyInfo[i].pActiveVoices->clear(); |
|
|
if (pMIDIKeyInfo[i].pEvents) |
|
|
pMIDIKeyInfo[i].pEvents->clear(); |
|
|
pMIDIKeyInfo[i].KeyPressed = false; |
|
|
pMIDIKeyInfo[i].Active = false; |
|
|
pMIDIKeyInfo[i].ReleaseTrigger = false; |
|
|
pMIDIKeyInfo[i].itSelf = Pool<uint>::Iterator(); |
|
|
} |
|
|
|
|
|
// reset all key groups |
|
|
std::map<uint,uint*>::iterator iter = ActiveKeyGroups.begin(); |
|
|
for (; iter != ActiveKeyGroups.end(); iter++) iter->second = NULL; |
|
|
|
|
|
// free all active keys |
|
|
pActiveKeys->clear(); |
|
|
|
|
|
// delete all input events |
|
|
pEventQueue->init(); |
|
|
|
|
|
if (pEngine) pEngine->ResetInternal(); |
|
|
} |
|
|
|
|
|
LinuxSampler::Engine* EngineChannel::GetEngine() { |
|
|
return pEngine; |
|
51 |
} |
} |
52 |
|
|
53 |
/** |
/** |
54 |
* More or less a workaround to set the instrument name, index and load |
* Will be called by the MIDIIn Thread to signal that a program |
55 |
* status variable to zero percent immediately, that is without blocking |
* change should be performed. As a program change isn't |
56 |
* the calling thread. It might be used in future for other preparations |
* real-time safe, the actual change is performed by the disk |
57 |
* as well though. |
* thread. |
58 |
* |
* |
59 |
* @param FileName - file name of the Gigasampler instrument file |
* @param Program - MIDI program change number |
|
* @param Instrument - index of the instrument in the .gig file |
|
|
* @see LoadInstrument() |
|
60 |
*/ |
*/ |
61 |
void EngineChannel::PrepareLoadInstrument(const char* FileName, uint Instrument) { |
void EngineChannel::SendProgramChange(uint8_t Program) { |
62 |
InstrumentFile = FileName; |
Engine* engine = dynamic_cast<Engine*>(pEngine); |
63 |
InstrumentIdx = Instrument; |
if(engine == NULL) return; |
64 |
InstrumentStat = 0; |
|
65 |
|
if(engine->GetDiskThread()) { |
66 |
|
engine->GetDiskThread()->OrderProgramChange(Program, this); |
67 |
|
} else { |
68 |
|
// TODO: |
69 |
|
} |
70 |
} |
} |
71 |
|
|
72 |
/** |
/** |
75 |
* This method will then actually start to load the instrument and block |
* This method will then actually start to load the instrument and block |
76 |
* the calling thread until loading was completed. |
* the calling thread until loading was completed. |
77 |
* |
* |
|
* @returns detailed description of the method call result |
|
78 |
* @see PrepareLoadInstrument() |
* @see PrepareLoadInstrument() |
79 |
*/ |
*/ |
80 |
void EngineChannel::LoadInstrument() { |
void EngineChannel::LoadInstrument() { |
81 |
|
InstrumentResourceManager* pInstrumentManager = dynamic_cast<InstrumentResourceManager*>(pEngine->GetInstrumentManager()); |
82 |
|
|
83 |
if (pEngine) pEngine->DisableAndLock(); |
// make sure we don't trigger any new notes with an old |
84 |
|
// instrument |
85 |
ResetInternal(); |
InstrumentChangeCmd< ::gig::DimensionRegion, ::gig::Instrument>& cmd = ChangeInstrument(0); |
86 |
|
if (cmd.pInstrument) { |
87 |
// free old instrument |
// give old instrument back to instrument manager, but |
88 |
if (pInstrument) { |
// keep the dimension regions and samples that are in use |
89 |
// give old instrument back to instrument manager |
pInstrumentManager->HandBackInstrument(cmd.pInstrument, this, cmd.pRegionsInUse); |
|
Engine::instruments.HandBack(pInstrument, this); |
|
90 |
} |
} |
91 |
|
cmd.pRegionsInUse->clear(); |
92 |
|
|
93 |
// delete all key groups |
// delete all key groups |
94 |
ActiveKeyGroups.clear(); |
ActiveKeyGroups.clear(); |
95 |
|
|
96 |
// request gig instrument from instrument manager |
// request gig instrument from instrument manager |
97 |
|
::gig::Instrument* newInstrument; |
98 |
try { |
try { |
99 |
instrument_id_t instrid; |
InstrumentManager::instrument_id_t instrid; |
100 |
instrid.FileName = InstrumentFile; |
instrid.FileName = InstrumentFile; |
101 |
instrid.iInstrument = InstrumentIdx; |
instrid.Index = InstrumentIdx; |
102 |
pInstrument = Engine::instruments.Borrow(instrid, this); |
|
103 |
if (!pInstrument) { |
newInstrument = pInstrumentManager->Borrow(instrid, this); |
104 |
InstrumentStat = -1; |
if (!newInstrument) { |
105 |
dmsg(1,("no instrument loaded!!!\n")); |
throw InstrumentManagerException("resource was not created"); |
|
exit(EXIT_FAILURE); |
|
106 |
} |
} |
107 |
} |
} |
108 |
catch (RIFF::Exception e) { |
catch (RIFF::Exception e) { |
109 |
InstrumentStat = -2; |
InstrumentStat = -2; |
110 |
|
StatusChanged(true); |
111 |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message; |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message; |
112 |
throw LinuxSamplerException(msg); |
throw Exception(msg); |
113 |
} |
} |
114 |
catch (InstrumentResourceManagerException e) { |
catch (InstrumentManagerException e) { |
115 |
InstrumentStat = -3; |
InstrumentStat = -3; |
116 |
|
StatusChanged(true); |
117 |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message(); |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message(); |
118 |
throw LinuxSamplerException(msg); |
throw Exception(msg); |
119 |
} |
} |
120 |
catch (...) { |
catch (...) { |
121 |
InstrumentStat = -4; |
InstrumentStat = -4; |
122 |
throw LinuxSamplerException("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file."); |
StatusChanged(true); |
123 |
|
throw Exception("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file."); |
124 |
} |
} |
125 |
|
|
126 |
// rebuild ActiveKeyGroups map with key groups of current instrument |
// rebuild ActiveKeyGroups map with key groups of current instrument |
127 |
for (::gig::Region* pRegion = pInstrument->GetFirstRegion(); pRegion; pRegion = pInstrument->GetNextRegion()) |
for (::gig::Region* pRegion = newInstrument->GetFirstRegion(); pRegion; pRegion = newInstrument->GetNextRegion()) |
128 |
if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL; |
if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL; |
129 |
|
|
130 |
InstrumentIdxName = pInstrument->pInfo->Name; |
InstrumentIdxName = newInstrument->pInfo->Name; |
131 |
InstrumentStat = 100; |
InstrumentStat = 100; |
132 |
|
|
133 |
// 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 LinuxSamplerException(msg); |
|
|
} |
|
|
|
|
|
if (pEngine) pEngine->Enable(); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Will be called by the InstrumentResourceManager when the instrument |
|
|
* we are currently using in this engine is going to be updated, so we |
|
|
* can stop playback before that happens. |
|
|
*/ |
|
|
void EngineChannel::ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) { |
|
|
dmsg(3,("gig::Engine: Received instrument update message.\n")); |
|
|
if (pEngine) pEngine->DisableAndLock(); |
|
|
ResetInternal(); |
|
|
this->pInstrument = NULL; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Will be called by the InstrumentResourceManager when the instrument |
|
|
* update process was completed, so we can continue with playback. |
|
|
*/ |
|
|
void EngineChannel::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) { |
|
|
this->pInstrument = pNewResource; //TODO: there are couple of engine parameters we should update here as well if the instrument was updated (see LoadInstrument()) |
|
|
if (pEngine) pEngine->Enable(); |
|
|
} |
|
|
|
|
|
void EngineChannel::Connect(AudioOutputDevice* pAudioOut) { |
|
|
if (pEngine && pEngine->pAudioOutputDevice != pAudioOut) { |
|
|
DisconnectAudioOutputDevice(); |
|
|
} |
|
|
pEngine = Engine::AcquireEngine(this, pAudioOut); |
|
|
ResetInternal(); |
|
|
for (uint i = 0; i < 128; i++) { |
|
|
pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool); |
|
|
pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEngine->pEventPool); |
|
|
} |
|
|
AudioDeviceChannelLeft = 0; |
|
|
AudioDeviceChannelRight = 1; |
|
|
pOutputLeft = pAudioOut->Channel(0)->Buffer(); |
|
|
pOutputRight = pAudioOut->Channel(1)->Buffer(); |
|
|
} |
|
134 |
|
|
135 |
void EngineChannel::DisconnectAudioOutputDevice() { |
StatusChanged(true); |
|
if (pEngine) { // if clause to prevent disconnect loops |
|
|
ResetInternal(); |
|
|
for (uint i = 0; i < 128; i++) { |
|
|
if (pMIDIKeyInfo[i].pActiveVoices) { |
|
|
delete pMIDIKeyInfo[i].pActiveVoices; |
|
|
pMIDIKeyInfo[i].pActiveVoices = NULL; |
|
|
} |
|
|
if (pMIDIKeyInfo[i].pEvents) { |
|
|
delete pMIDIKeyInfo[i].pEvents; |
|
|
pMIDIKeyInfo[i].pEvents = NULL; |
|
|
} |
|
|
} |
|
|
Engine* oldEngine = pEngine; |
|
|
AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice; |
|
|
pEngine = NULL; |
|
|
Engine::FreeEngine(this, oldAudioDevice); |
|
|
AudioDeviceChannelLeft = -1; |
|
|
AudioDeviceChannelRight = -1; |
|
|
} |
|
136 |
} |
} |
137 |
|
|
138 |
void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) { |
void EngineChannel::ProcessKeySwitchChange(int key) { |
139 |
if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet."); |
// Change key dimension value if key is in keyswitching area |
140 |
|
{ |
141 |
AudioChannel* pChannel = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannel); |
if (key >= pInstrument->DimensionKeyRange.low && key <= pInstrument->DimensionKeyRange.high) |
142 |
if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel)); |
CurrentKeyDimension = float(key - pInstrument->DimensionKeyRange.low) / |
143 |
switch (EngineAudioChannel) { |
(pInstrument->DimensionKeyRange.high - pInstrument->DimensionKeyRange.low + 1); |
|
case 0: // left output channel |
|
|
pOutputLeft = pChannel->Buffer(); |
|
|
AudioDeviceChannelLeft = AudioDeviceChannel; |
|
|
break; |
|
|
case 1: // right output channel |
|
|
pOutputRight = pChannel->Buffer(); |
|
|
AudioDeviceChannelRight = AudioDeviceChannel; |
|
|
break; |
|
|
default: |
|
|
throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel)); |
|
144 |
} |
} |
145 |
} |
} |
146 |
|
|
|
int EngineChannel::OutputChannel(uint EngineAudioChannel) { |
|
|
switch (EngineAudioChannel) { |
|
|
case 0: // left channel |
|
|
return AudioDeviceChannelLeft; |
|
|
case 1: // right channel |
|
|
return AudioDeviceChannelRight; |
|
|
default: |
|
|
throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel)); |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* Will be called by the MIDIIn Thread to let the audio thread trigger a new |
|
|
* voice for the given key. |
|
|
* |
|
|
* @param Key - MIDI key number of the triggered key |
|
|
* @param Velocity - MIDI velocity value of the triggered key |
|
|
*/ |
|
|
void EngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity) { |
|
|
if (pEngine) { |
|
|
Event event = pEngine->pEventGenerator->CreateEvent(); |
|
|
event.Type = Event::type_note_on; |
|
|
event.Param.Note.Key = Key; |
|
|
event.Param.Note.Velocity = Velocity; |
|
|
event.pEngineChannel = this; |
|
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
|
|
else dmsg(1,("EngineChannel: Input event queue full!")); |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* Will be called by the MIDIIn Thread to signal the audio thread to release |
|
|
* voice(s) on the given key. |
|
|
* |
|
|
* @param Key - MIDI key number of the released key |
|
|
* @param Velocity - MIDI release velocity value of the released key |
|
|
*/ |
|
|
void EngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity) { |
|
|
if (pEngine) { |
|
|
Event event = pEngine->pEventGenerator->CreateEvent(); |
|
|
event.Type = Event::type_note_off; |
|
|
event.Param.Note.Key = Key; |
|
|
event.Param.Note.Velocity = Velocity; |
|
|
event.pEngineChannel = this; |
|
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
|
|
else dmsg(1,("EngineChannel: Input event queue full!")); |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* Will be called by the MIDIIn Thread to signal the audio thread to change |
|
|
* the pitch value for all voices. |
|
|
* |
|
|
* @param Pitch - MIDI pitch value (-8192 ... +8191) |
|
|
*/ |
|
|
void EngineChannel::SendPitchbend(int Pitch) { |
|
|
if (pEngine) { |
|
|
Event event = pEngine->pEventGenerator->CreateEvent(); |
|
|
event.Type = Event::type_pitchbend; |
|
|
event.Param.Pitch.Pitch = Pitch; |
|
|
event.pEngineChannel = this; |
|
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
|
|
else dmsg(1,("EngineChannel: Input event queue full!")); |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* Will be called by the MIDIIn Thread to signal the audio thread that a |
|
|
* continuous controller value has changed. |
|
|
* |
|
|
* @param Controller - MIDI controller number of the occured control change |
|
|
* @param Value - value of the control change |
|
|
*/ |
|
|
void EngineChannel::SendControlChange(uint8_t Controller, uint8_t Value) { |
|
|
if (pEngine) { |
|
|
Event event = pEngine->pEventGenerator->CreateEvent(); |
|
|
event.Type = Event::type_control_change; |
|
|
event.Param.CC.Controller = Controller; |
|
|
event.Param.CC.Value = Value; |
|
|
event.pEngineChannel = this; |
|
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
|
|
else dmsg(1,("EngineChannel: Input event queue full!")); |
|
|
} |
|
|
} |
|
|
|
|
|
float EngineChannel::Volume() { |
|
|
return GlobalVolume; |
|
|
} |
|
|
|
|
|
void EngineChannel::Volume(float f) { |
|
|
GlobalVolume = f; |
|
|
} |
|
|
|
|
|
uint EngineChannel::Channels() { |
|
|
return 2; |
|
|
} |
|
|
|
|
|
String EngineChannel::InstrumentFileName() { |
|
|
return InstrumentFile; |
|
|
} |
|
|
|
|
|
String EngineChannel::InstrumentName() { |
|
|
return InstrumentIdxName; |
|
|
} |
|
|
|
|
|
int EngineChannel::InstrumentIndex() { |
|
|
return InstrumentIdx; |
|
|
} |
|
|
|
|
|
int EngineChannel::InstrumentStatus() { |
|
|
return InstrumentStat; |
|
|
} |
|
|
|
|
147 |
}} // namespace LinuxSampler::gig |
}} // namespace LinuxSampler::gig |