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-2008 Christian Schoenebeck * |
* Copyright (C) 2005-2008 Christian Schoenebeck * |
7 |
* Copyright (C) 2009-2012 Christian Schoenebeck and Grigor Iliev * |
* Copyright (C) 2009-2013 Christian Schoenebeck and 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 * |
39 |
bool bChangeInstrument; ///< Set to true by the loader when the channel should change instrument. |
bool bChangeInstrument; ///< Set to true by the loader when the channel should change instrument. |
40 |
I* pInstrument; ///< The new instrument. Also used by the loader to read the previously loaded instrument. |
I* pInstrument; ///< The new instrument. Also used by the loader to read the previously loaded instrument. |
41 |
RTList<R*>* pRegionsInUse; ///< List of dimension regions in use by the currently loaded instrument. Continuously updated by the audio thread. |
RTList<R*>* pRegionsInUse; ///< List of dimension regions in use by the currently loaded instrument. Continuously updated by the audio thread. |
42 |
|
InstrumentScript* pScript; ///< Instrument script to be executed for this instrument. This is never NULL, it is always a valid InstrumentScript pointer. Use InstrumentScript::bHasValidScript whether it reflects a valid instrument script to be executed. |
43 |
}; |
}; |
44 |
|
|
45 |
template<class R> |
template<class R> |
60 |
typedef typename RTList<R*>::Iterator RTListRegionIterator; |
typedef typename RTList<R*>::Iterator RTListRegionIterator; |
61 |
typedef typename MidiKeyboardManager<V>::MidiKey MidiKey; |
typedef typename MidiKeyboardManager<V>::MidiKey MidiKey; |
62 |
|
|
63 |
|
virtual MidiKeyboardManagerBase* GetMidiKeyboardManager() OVERRIDE { |
64 |
|
return this; |
65 |
|
} |
66 |
|
|
67 |
virtual void HandBack(I* Instrument) { |
virtual void HandBack(I* Instrument) { |
68 |
ResourceManager<InstrumentManager::instrument_id_t, I>* mgr = |
ResourceManager<InstrumentManager::instrument_id_t, I>* mgr = |
69 |
dynamic_cast<ResourceManager<InstrumentManager::instrument_id_t, I>*>(pEngine->GetInstrumentManager()); |
dynamic_cast<ResourceManager<InstrumentManager::instrument_id_t, I>*>(pEngine->GetInstrumentManager()); |
89 |
} |
} |
90 |
|
|
91 |
virtual void DeleteRegionsInUse() { |
virtual void DeleteRegionsInUse() { |
92 |
|
RTList<R*>* previous = NULL; // prevent double free |
93 |
{ |
{ |
94 |
InstrumentChangeCmd<R, I>& cmd = InstrumentChangeCommand.GetConfigForUpdate(); |
InstrumentChangeCmd<R, I>& cmd = InstrumentChangeCommand.GetConfigForUpdate(); |
95 |
if (cmd.pRegionsInUse) { |
if (cmd.pRegionsInUse) { |
96 |
|
previous = cmd.pRegionsInUse; |
97 |
delete cmd.pRegionsInUse; |
delete cmd.pRegionsInUse; |
98 |
cmd.pRegionsInUse = NULL; |
cmd.pRegionsInUse = NULL; |
99 |
} |
} |
102 |
{ |
{ |
103 |
InstrumentChangeCmd<R, I>& cmd = InstrumentChangeCommand.SwitchConfig(); |
InstrumentChangeCmd<R, I>& cmd = InstrumentChangeCommand.SwitchConfig(); |
104 |
if (cmd.pRegionsInUse) { |
if (cmd.pRegionsInUse) { |
105 |
delete cmd.pRegionsInUse; |
if (cmd.pRegionsInUse != previous) |
106 |
|
delete cmd.pRegionsInUse; |
107 |
cmd.pRegionsInUse = NULL; |
cmd.pRegionsInUse = NULL; |
108 |
} |
} |
109 |
cmd.bChangeInstrument = false; |
cmd.bChangeInstrument = false; |
129 |
DisconnectAudioOutputDevice(); |
DisconnectAudioOutputDevice(); |
130 |
} |
} |
131 |
AbstractEngine* newEngine = AbstractEngine::AcquireEngine(this, pAudioOut); |
AbstractEngine* newEngine = AbstractEngine::AcquireEngine(this, pAudioOut); |
132 |
EngineMutex.Lock(); |
{ |
133 |
pEngine = newEngine; |
LockGuard lock(EngineMutex); |
134 |
EngineMutex.Unlock(); |
pEngine = newEngine; |
135 |
|
} |
136 |
ResetInternal(); |
ResetInternal(); |
137 |
pEvents = new RTList<Event>(pEngine->pEventPool); |
pEvents = new RTList<Event>(pEngine->pEventPool); |
138 |
|
|
187 |
ResetInternal(); |
ResetInternal(); |
188 |
|
|
189 |
DeleteRegionsInUse(); |
DeleteRegionsInUse(); |
190 |
|
UnloadScriptInUse(); |
191 |
|
|
192 |
InstrumentChangeCmd<R, I>& cmd = InstrumentChangeCommand.GetConfigForUpdate(); |
InstrumentChangeCmd<R, I>& cmd = InstrumentChangeCommand.GetConfigForUpdate(); |
193 |
if (cmd.pInstrument) { |
if (cmd.pInstrument) { |
205 |
DeleteGroupEventLists(); |
DeleteGroupEventLists(); |
206 |
|
|
207 |
AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice; |
AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice; |
208 |
EngineMutex.Lock(); |
{ |
209 |
pEngine = NULL; |
LockGuard lock(EngineMutex); |
210 |
EngineMutex.Unlock(); |
pEngine = NULL; |
211 |
|
} |
212 |
AbstractEngine::FreeEngine(this, oldAudioDevice); |
AbstractEngine::FreeEngine(this, oldAudioDevice); |
213 |
AudioDeviceChannelLeft = -1; |
AudioDeviceChannelLeft = -1; |
214 |
AudioDeviceChannelRight = -1; |
AudioDeviceChannelRight = -1; |
246 |
* we are currently using on this EngineChannel is going to be updated, |
* we are currently using on this EngineChannel is going to be updated, |
247 |
* so we can stop playback before that happens. |
* so we can stop playback before that happens. |
248 |
*/ |
*/ |
249 |
virtual void ResourceToBeUpdated(I* pResource, void*& pUpdateArg) { |
virtual void ResourceToBeUpdated(I* pResource, void*& pUpdateArg) OVERRIDE { |
250 |
dmsg(3,("EngineChannelBase: Received instrument update message.\n")); |
dmsg(3,("EngineChannelBase: Received instrument update message.\n")); |
251 |
if (pEngine) pEngine->DisableAndLock(); |
if (pEngine) pEngine->DisableAndLock(); |
252 |
ResetInternal(); |
ResetInternal(); |
257 |
* Will be called by the InstrumentResourceManager when the instrument |
* Will be called by the InstrumentResourceManager when the instrument |
258 |
* update process was completed, so we can continue with playback. |
* update process was completed, so we can continue with playback. |
259 |
*/ |
*/ |
260 |
virtual void ResourceUpdated(I* pOldResource, I* pNewResource, void* pUpdateArg) { |
virtual void ResourceUpdated(I* pOldResource, I* pNewResource, void* pUpdateArg) OVERRIDE { |
261 |
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()) |
262 |
if (pEngine) pEngine->Enable(); |
if (pEngine) pEngine->Enable(); |
263 |
bStatusChanged = true; // status of engine has changed, so set notify flag |
bStatusChanged = true; // status of engine has changed, so set notify flag |
269 |
* |
* |
270 |
* @param fProgress - current progress as value between 0.0 and 1.0 |
* @param fProgress - current progress as value between 0.0 and 1.0 |
271 |
*/ |
*/ |
272 |
virtual void OnResourceProgress(float fProgress) { |
virtual void OnResourceProgress(float fProgress) OVERRIDE { |
273 |
this->InstrumentStat = int(fProgress * 100.0f); |
this->InstrumentStat = int(fProgress * 100.0f); |
274 |
dmsg(7,("EngineChannelBase: progress %d%", InstrumentStat)); |
dmsg(7,("EngineChannelBase: progress %d%", InstrumentStat)); |
275 |
bStatusChanged = true; // status of engine has changed, so set notify flag |
bStatusChanged = true; // status of engine has changed, so set notify flag |
289 |
template<class TV, class TRR, class TR, class TD, class TIM, class TI> friend class EngineBase; |
template<class TV, class TRR, class TR, class TD, class TIM, class TI> friend class EngineBase; |
290 |
|
|
291 |
protected: |
protected: |
292 |
EngineChannelBase() : InstrumentChangeCommandReader(InstrumentChangeCommand) { |
EngineChannelBase() : |
293 |
|
MidiKeyboardManager<V>(this), |
294 |
|
InstrumentChangeCommandReader(InstrumentChangeCommand) |
295 |
|
{ |
296 |
pInstrument = NULL; |
pInstrument = NULL; |
297 |
|
|
298 |
// reset the instrument change command struct (need to be done |
// reset the instrument change command struct (need to be done |
301 |
InstrumentChangeCmd<R, I>& cmd = InstrumentChangeCommand.GetConfigForUpdate(); |
InstrumentChangeCmd<R, I>& cmd = InstrumentChangeCommand.GetConfigForUpdate(); |
302 |
cmd.pRegionsInUse = NULL; |
cmd.pRegionsInUse = NULL; |
303 |
cmd.pInstrument = NULL; |
cmd.pInstrument = NULL; |
304 |
|
cmd.pScript = new InstrumentScript(this); |
305 |
cmd.bChangeInstrument = false; |
cmd.bChangeInstrument = false; |
306 |
} |
} |
307 |
{ |
{ |
308 |
InstrumentChangeCmd<R, I>& cmd = InstrumentChangeCommand.SwitchConfig(); |
InstrumentChangeCmd<R, I>& cmd = InstrumentChangeCommand.SwitchConfig(); |
309 |
cmd.pRegionsInUse = NULL; |
cmd.pRegionsInUse = NULL; |
310 |
cmd.pInstrument = NULL; |
cmd.pInstrument = NULL; |
311 |
|
cmd.pScript = new InstrumentScript(this); |
312 |
cmd.bChangeInstrument = false; |
cmd.bChangeInstrument = false; |
313 |
} |
} |
314 |
} |
} |
315 |
|
|
316 |
virtual ~EngineChannelBase() { } |
virtual ~EngineChannelBase() { |
317 |
|
InstrumentScript* previous = NULL; // prevent double free |
318 |
|
{ |
319 |
|
InstrumentChangeCmd<R, I>& cmd = InstrumentChangeCommand.GetConfigForUpdate(); |
320 |
|
if (cmd.pScript) { |
321 |
|
previous = cmd.pScript; |
322 |
|
delete cmd.pScript; |
323 |
|
cmd.pScript = NULL; |
324 |
|
} |
325 |
|
} |
326 |
|
{ |
327 |
|
InstrumentChangeCmd<R, I>& cmd = InstrumentChangeCommand.SwitchConfig(); |
328 |
|
if (cmd.pScript) { |
329 |
|
if (previous != cmd.pScript) |
330 |
|
delete cmd.pScript; |
331 |
|
cmd.pScript = NULL; |
332 |
|
} |
333 |
|
} |
334 |
|
} |
335 |
|
|
336 |
typedef typename RTList<V>::Iterator RTListVoiceIterator; |
typedef typename RTList<V>::Iterator RTListVoiceIterator; |
337 |
|
|
384 |
} |
} |
385 |
|
|
386 |
/** |
/** |
387 |
|
* Unload the currently used and loaded real-time instrument script. |
388 |
|
* The source code of the script is retained, so that it can still |
389 |
|
* be reloaded. |
390 |
|
*/ |
391 |
|
void UnloadScriptInUse() { |
392 |
|
{ |
393 |
|
InstrumentChangeCmd<R, I>& cmd = InstrumentChangeCommand.GetConfigForUpdate(); |
394 |
|
if (cmd.pScript) cmd.pScript->unload(); |
395 |
|
} |
396 |
|
{ |
397 |
|
InstrumentChangeCmd<R, I>& cmd = InstrumentChangeCommand.SwitchConfig(); |
398 |
|
if (cmd.pScript) cmd.pScript->unload(); |
399 |
|
} |
400 |
|
InstrumentChangeCommand.SwitchConfig(); // switch back to original one |
401 |
|
} |
402 |
|
|
403 |
|
/** |
404 |
|
* Load real-time instrument script and all its resources required |
405 |
|
* for the upcoming instrument change. |
406 |
|
* |
407 |
|
* @param text - source code of script |
408 |
|
*/ |
409 |
|
void LoadInstrumentScript(const String& text) { |
410 |
|
InstrumentChangeCmd<R, I>& cmd = InstrumentChangeCommand.GetConfigForUpdate(); |
411 |
|
// load the new script |
412 |
|
cmd.pScript->load(text); |
413 |
|
} |
414 |
|
|
415 |
|
/** |
416 |
* Changes the instrument for an engine channel. |
* Changes the instrument for an engine channel. |
417 |
* |
* |
418 |
* @param pInstrument - new instrument |
* @param pInstrument - new instrument |