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, 2006 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 * |
30 |
pEngine = NULL; |
pEngine = NULL; |
31 |
pInstrument = NULL; |
pInstrument = NULL; |
32 |
pEvents = NULL; // we allocate when we retrieve the right Engine object |
pEvents = NULL; // we allocate when we retrieve the right Engine object |
33 |
pCCEvents = NULL; // we allocate when we retrieve the right Engine object |
pEventQueue = new RingBuffer<Event>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0); |
|
pEventQueue = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0); |
|
34 |
pActiveKeys = new Pool<uint>(128); |
pActiveKeys = new Pool<uint>(128); |
35 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
36 |
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 |
41 |
pMIDIKeyInfo[i].VoiceTheftsQueued = 0; |
pMIDIKeyInfo[i].VoiceTheftsQueued = 0; |
42 |
pMIDIKeyInfo[i].RoundRobinIndex = 0; |
pMIDIKeyInfo[i].RoundRobinIndex = 0; |
43 |
} |
} |
|
for (uint i = 0; i < Event::destination_count; i++) { |
|
|
pSynthesisEvents[i] = NULL; // we allocate when we retrieve the right Engine object |
|
|
} |
|
44 |
InstrumentIdx = -1; |
InstrumentIdx = -1; |
45 |
InstrumentStat = -1; |
InstrumentStat = -1; |
46 |
AudioDeviceChannelLeft = -1; |
AudioDeviceChannelLeft = -1; |
47 |
AudioDeviceChannelRight = -1; |
AudioDeviceChannelRight = -1; |
48 |
|
pMidiInputPort = NULL; |
49 |
|
midiChannel = midi_chan_all; |
50 |
|
ResetControllers(); |
51 |
|
SoloMode = false; |
52 |
|
PortamentoMode = false; |
53 |
|
PortamentoTime = CONFIG_PORTAMENTO_TIME_DEFAULT; |
54 |
} |
} |
55 |
|
|
56 |
EngineChannel::~EngineChannel() { |
EngineChannel::~EngineChannel() { |
62 |
} |
} |
63 |
|
|
64 |
/** |
/** |
65 |
|
* Implementation of virtual method from abstract EngineChannel interface. |
66 |
|
* This method will periodically be polled (e.g. by the LSCP server) to |
67 |
|
* check if some engine channel parameter has changed since the last |
68 |
|
* StatusChanged() call. |
69 |
|
* |
70 |
|
* This method can also be used to mark the engine channel as changed |
71 |
|
* from outside, e.g. by a MIDI input device. The optional argument |
72 |
|
* \a nNewStatus can be used for this. |
73 |
|
* |
74 |
|
* TODO: This "poll method" is just a lazy solution and might be |
75 |
|
* replaced in future. |
76 |
|
* @param bNewStatus - (optional, default: false) sets the new status flag |
77 |
|
* @returns true if engine channel status has changed since last |
78 |
|
* StatusChanged() call |
79 |
|
*/ |
80 |
|
bool EngineChannel::StatusChanged(bool bNewStatus) { |
81 |
|
bool b = bStatusChanged; |
82 |
|
bStatusChanged = bNewStatus; |
83 |
|
return b; |
84 |
|
} |
85 |
|
|
86 |
|
void EngineChannel::Reset() { |
87 |
|
if (pEngine) pEngine->DisableAndLock(); |
88 |
|
ResetInternal(); |
89 |
|
ResetControllers(); |
90 |
|
if (pEngine) { |
91 |
|
pEngine->Enable(); |
92 |
|
pEngine->Reset(); |
93 |
|
} |
94 |
|
} |
95 |
|
|
96 |
|
/** |
97 |
* This method is not thread safe! |
* This method is not thread safe! |
98 |
*/ |
*/ |
99 |
void EngineChannel::ResetInternal() { |
void EngineChannel::ResetInternal() { |
|
Pitch = 0; |
|
|
SustainPedal = false; |
|
|
GlobalVolume = 1.0; |
|
|
GlobalPanLeft = 1.0f; |
|
|
GlobalPanRight = 1.0f; |
|
100 |
CurrentKeyDimension = 0; |
CurrentKeyDimension = 0; |
101 |
|
|
|
ResetControllers(); |
|
|
|
|
102 |
// reset key info |
// reset key info |
103 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
104 |
if (pMIDIKeyInfo[i].pActiveVoices) |
if (pMIDIKeyInfo[i].pActiveVoices) |
111 |
pMIDIKeyInfo[i].itSelf = Pool<uint>::Iterator(); |
pMIDIKeyInfo[i].itSelf = Pool<uint>::Iterator(); |
112 |
pMIDIKeyInfo[i].VoiceTheftsQueued = 0; |
pMIDIKeyInfo[i].VoiceTheftsQueued = 0; |
113 |
} |
} |
114 |
|
SoloKey = -1; // no solo key active yet |
115 |
|
PortamentoPos = -1.0f; // no portamento active yet |
116 |
|
|
117 |
// reset all key groups |
// reset all key groups |
118 |
std::map<uint,uint*>::iterator iter = ActiveKeyGroups.begin(); |
std::map<uint,uint*>::iterator iter = ActiveKeyGroups.begin(); |
125 |
pEventQueue->init(); |
pEventQueue->init(); |
126 |
|
|
127 |
if (pEngine) pEngine->ResetInternal(); |
if (pEngine) pEngine->ResetInternal(); |
128 |
|
|
129 |
|
// status of engine channel has changed, so set notify flag |
130 |
|
bStatusChanged = true; |
131 |
} |
} |
132 |
|
|
133 |
LinuxSampler::Engine* EngineChannel::GetEngine() { |
LinuxSampler::Engine* EngineChannel::GetEngine() { |
189 |
catch (RIFF::Exception e) { |
catch (RIFF::Exception e) { |
190 |
InstrumentStat = -2; |
InstrumentStat = -2; |
191 |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message; |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message; |
192 |
throw LinuxSamplerException(msg); |
throw Exception(msg); |
193 |
} |
} |
194 |
catch (InstrumentResourceManagerException e) { |
catch (InstrumentResourceManagerException e) { |
195 |
InstrumentStat = -3; |
InstrumentStat = -3; |
196 |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message(); |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message(); |
197 |
throw LinuxSamplerException(msg); |
throw Exception(msg); |
198 |
} |
} |
199 |
catch (...) { |
catch (...) { |
200 |
InstrumentStat = -4; |
InstrumentStat = -4; |
201 |
throw LinuxSamplerException("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."); |
202 |
} |
} |
203 |
|
|
204 |
// rebuild ActiveKeyGroups map with key groups of current instrument |
// rebuild ActiveKeyGroups map with key groups of current instrument |
215 |
} |
} |
216 |
catch (AudioOutputException e) { |
catch (AudioOutputException e) { |
217 |
String msg = "Audio output device unable to provide 2 audio channels, cause: " + e.Message(); |
String msg = "Audio output device unable to provide 2 audio channels, cause: " + e.Message(); |
218 |
throw LinuxSamplerException(msg); |
throw Exception(msg); |
219 |
} |
} |
220 |
|
|
221 |
if (pEngine) pEngine->Enable(); |
if (pEngine) pEngine->Enable(); |
223 |
|
|
224 |
/** |
/** |
225 |
* Will be called by the InstrumentResourceManager when the instrument |
* Will be called by the InstrumentResourceManager when the instrument |
226 |
* 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, |
227 |
* can stop playback before that happens. |
* so we can stop playback before that happens. |
228 |
*/ |
*/ |
229 |
void EngineChannel::ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) { |
void EngineChannel::ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) { |
230 |
dmsg(3,("gig::Engine: Received instrument update message.\n")); |
dmsg(3,("gig::Engine: Received instrument update message.\n")); |
240 |
void EngineChannel::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) { |
void EngineChannel::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) { |
241 |
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()) |
242 |
if (pEngine) pEngine->Enable(); |
if (pEngine) pEngine->Enable(); |
243 |
|
bStatusChanged = true; // status of engine has changed, so set notify flag |
244 |
|
} |
245 |
|
|
246 |
|
/** |
247 |
|
* Will be called by the InstrumentResourceManager on progress changes |
248 |
|
* while loading or realoading an instrument for this EngineChannel. |
249 |
|
* |
250 |
|
* @param fProgress - current progress as value between 0.0 and 1.0 |
251 |
|
*/ |
252 |
|
void EngineChannel::OnResourceProgress(float fProgress) { |
253 |
|
this->InstrumentStat = int(fProgress * 100.0f); |
254 |
|
dmsg(7,("gig::EngineChannel: progress %d%", InstrumentStat)); |
255 |
|
bStatusChanged = true; // status of engine has changed, so set notify flag |
256 |
} |
} |
257 |
|
|
258 |
void EngineChannel::Connect(AudioOutputDevice* pAudioOut) { |
void EngineChannel::Connect(AudioOutputDevice* pAudioOut) { |
262 |
} |
} |
263 |
pEngine = Engine::AcquireEngine(this, pAudioOut); |
pEngine = Engine::AcquireEngine(this, pAudioOut); |
264 |
ResetInternal(); |
ResetInternal(); |
265 |
pEvents = new RTList<Event>(pEngine->pEventPool); |
pEvents = new RTList<Event>(pEngine->pEventPool); |
|
pCCEvents = new RTList<Event>(pEngine->pEventPool); |
|
|
for (uint i = 0; i < Event::destination_count; i++) { |
|
|
pSynthesisEvents[i] = new RTList<Event>(pEngine->pEventPool); |
|
|
} |
|
266 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
267 |
pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool); |
pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool); |
268 |
pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEngine->pEventPool); |
pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEngine->pEventPool); |
271 |
AudioDeviceChannelRight = 1; |
AudioDeviceChannelRight = 1; |
272 |
pOutputLeft = pAudioOut->Channel(0)->Buffer(); |
pOutputLeft = pAudioOut->Channel(0)->Buffer(); |
273 |
pOutputRight = pAudioOut->Channel(1)->Buffer(); |
pOutputRight = pAudioOut->Channel(1)->Buffer(); |
274 |
|
MidiInputPort::AddSysexListener(pEngine); |
275 |
} |
} |
276 |
|
|
277 |
void EngineChannel::DisconnectAudioOutputDevice() { |
void EngineChannel::DisconnectAudioOutputDevice() { |
281 |
delete pEvents; |
delete pEvents; |
282 |
pEvents = NULL; |
pEvents = NULL; |
283 |
} |
} |
|
if (pCCEvents) { |
|
|
delete pCCEvents; |
|
|
pCCEvents = NULL; |
|
|
} |
|
284 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
285 |
if (pMIDIKeyInfo[i].pActiveVoices) { |
if (pMIDIKeyInfo[i].pActiveVoices) { |
286 |
delete pMIDIKeyInfo[i].pActiveVoices; |
delete pMIDIKeyInfo[i].pActiveVoices; |
291 |
pMIDIKeyInfo[i].pEvents = NULL; |
pMIDIKeyInfo[i].pEvents = NULL; |
292 |
} |
} |
293 |
} |
} |
|
for (uint i = 0; i < Event::destination_count; i++) { |
|
|
if (pSynthesisEvents[i]) { |
|
|
delete pSynthesisEvents[i]; |
|
|
pSynthesisEvents[i] = NULL; |
|
|
} |
|
|
} |
|
294 |
Engine* oldEngine = pEngine; |
Engine* oldEngine = pEngine; |
295 |
AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice; |
AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice; |
296 |
pEngine = NULL; |
pEngine = NULL; |
330 |
} |
} |
331 |
} |
} |
332 |
|
|
333 |
|
void EngineChannel::Connect(MidiInputPort* pMidiPort, midi_chan_t MidiChannel) { |
334 |
|
if (!pMidiPort || pMidiPort == this->pMidiInputPort) return; |
335 |
|
DisconnectMidiInputPort(); |
336 |
|
this->pMidiInputPort = pMidiPort; |
337 |
|
this->midiChannel = MidiChannel; |
338 |
|
pMidiPort->Connect(this, MidiChannel); |
339 |
|
} |
340 |
|
|
341 |
|
void EngineChannel::DisconnectMidiInputPort() { |
342 |
|
MidiInputPort* pOldPort = this->pMidiInputPort; |
343 |
|
this->pMidiInputPort = NULL; |
344 |
|
if (pOldPort) pOldPort->Disconnect(this); |
345 |
|
} |
346 |
|
|
347 |
|
MidiInputPort* EngineChannel::GetMidiInputPort() { |
348 |
|
return pMidiInputPort; |
349 |
|
} |
350 |
|
|
351 |
|
midi_chan_t EngineChannel::MidiChannel() { |
352 |
|
return midiChannel; |
353 |
|
} |
354 |
|
|
355 |
/** |
/** |
356 |
* 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 |
357 |
* voice for the given key. |
* voice for the given key. |
428 |
|
|
429 |
void EngineChannel::ClearEventLists() { |
void EngineChannel::ClearEventLists() { |
430 |
pEvents->clear(); |
pEvents->clear(); |
|
pCCEvents->clear(); |
|
|
for (uint i = 0; i < Event::destination_count; i++) { |
|
|
pSynthesisEvents[i]->clear(); |
|
|
} |
|
431 |
// empty MIDI key specific event lists |
// empty MIDI key specific event lists |
432 |
{ |
{ |
433 |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
439 |
} |
} |
440 |
|
|
441 |
void EngineChannel::ResetControllers() { |
void EngineChannel::ResetControllers() { |
442 |
|
Pitch = 0; |
443 |
|
SustainPedal = false; |
444 |
|
SostenutoPedal = false; |
445 |
|
GlobalVolume = CONFIG_GLOBAL_ATTENUATION; |
446 |
|
GlobalPanLeft = 1.0f; |
447 |
|
GlobalPanRight = 1.0f; |
448 |
// set all MIDI controller values to zero |
// set all MIDI controller values to zero |
449 |
memset(ControllerTable, 0x00, 128); |
memset(ControllerTable, 0x00, 129); |
450 |
} |
} |
451 |
|
|
452 |
/** |
/** |
491 |
|
|
492 |
void EngineChannel::Volume(float f) { |
void EngineChannel::Volume(float f) { |
493 |
GlobalVolume = f; |
GlobalVolume = f; |
494 |
|
bStatusChanged = true; // status of engine channel has changed, so set notify flag |
495 |
} |
} |
496 |
|
|
497 |
uint EngineChannel::Channels() { |
uint EngineChannel::Channels() { |
514 |
return InstrumentStat; |
return InstrumentStat; |
515 |
} |
} |
516 |
|
|
517 |
|
String EngineChannel::EngineName() { |
518 |
|
return LS_GIG_ENGINE_NAME; |
519 |
|
} |
520 |
|
|
521 |
}} // namespace LinuxSampler::gig |
}} // namespace LinuxSampler::gig |