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 - 2008 Christian Schoenebeck * |
* Copyright (C) 2005 - 2016 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 * |
24 |
#include <sstream> |
#include <sstream> |
25 |
|
|
26 |
#include "InstrumentResourceManager.h" |
#include "InstrumentResourceManager.h" |
27 |
|
#include "EngineChannel.h" |
28 |
|
#include "Engine.h" |
29 |
|
|
30 |
#include "../../common/global_private.h" |
#include "../../common/global_private.h" |
31 |
#include "../../plugins/InstrumentEditorFactory.h" |
#include "../../plugins/InstrumentEditorFactory.h" |
32 |
|
|
|
// We need to know the maximum number of sample points which are going to |
|
|
// be processed for each render cycle of the audio output driver, to know |
|
|
// how much initial sample points we need to cache into RAM. If the given |
|
|
// sampler channel does not have an audio output device assigned yet |
|
|
// though, we simply use this default value. |
|
|
#define GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE 128 |
|
|
|
|
33 |
namespace LinuxSampler { namespace gig { |
namespace LinuxSampler { namespace gig { |
34 |
|
|
|
// data stored as long as an instrument resource exists |
|
|
struct instr_entry_t { |
|
|
InstrumentManager::instrument_id_t ID; |
|
|
::gig::File* pGig; |
|
|
uint MaxSamplesPerCycle; ///< if some engine requests an already allocated instrument with a higher value, we have to reallocate the instrument |
|
|
}; |
|
|
|
|
35 |
// some data needed for the libgig callback function |
// some data needed for the libgig callback function |
36 |
struct progress_callback_arg_t { |
struct progress_callback_arg_t { |
37 |
InstrumentResourceManager* pManager; |
InstrumentResourceManager* pManager; |
55 |
|
|
56 |
// the instrument we borrowed on behalf of the editor |
// the instrument we borrowed on behalf of the editor |
57 |
::gig::Instrument* pInstrument; |
::gig::Instrument* pInstrument; |
58 |
|
// the instrument editor we work on behalf |
59 |
|
InstrumentEditor* pEditor; |
60 |
}; |
}; |
61 |
|
|
62 |
/** |
/** |
69 |
* instrument ID |
* instrument ID |
70 |
*/ |
*/ |
71 |
void InstrumentResourceManager::OnInstrumentLoadingProgress(::gig::progress_t* pProgress) { |
void InstrumentResourceManager::OnInstrumentLoadingProgress(::gig::progress_t* pProgress) { |
72 |
dmsg(7,("gig::InstrumentResourceManager: progress %f%", pProgress->factor)); |
dmsg(7,("gig::InstrumentResourceManager: progress %f%%", pProgress->factor)); |
73 |
progress_callback_arg_t* pArg = static_cast<progress_callback_arg_t*>(pProgress->custom); |
progress_callback_arg_t* pArg = static_cast<progress_callback_arg_t*>(pProgress->custom); |
74 |
// we randomly schedule 90% for the .gig file loading and the remaining 10% later for sample caching |
// we randomly schedule 90% for the .gig file loading and the remaining 10% later for sample caching |
75 |
const float localProgress = 0.9f * pProgress->factor; |
const float localProgress = 0.9f * pProgress->factor; |
76 |
pArg->pManager->DispatchResourceProgressEvent(*pArg->pInstrumentKey, localProgress); |
pArg->pManager->DispatchResourceProgressEvent(*pArg->pInstrumentKey, localProgress); |
77 |
} |
} |
78 |
|
|
|
std::vector<InstrumentResourceManager::instrument_id_t> InstrumentResourceManager::Instruments() { |
|
|
return Entries(); |
|
|
} |
|
|
|
|
|
InstrumentManager::mode_t InstrumentResourceManager::GetMode(const instrument_id_t& ID) { |
|
|
return static_cast<InstrumentManager::mode_t>(AvailabilityMode(ID)); |
|
|
} |
|
|
|
|
|
void InstrumentResourceManager::SetMode(const instrument_id_t& ID, InstrumentManager::mode_t Mode) { |
|
|
dmsg(2,("gig::InstrumentResourceManager: setting mode for %s (Index=%d) to %d\n",ID.FileName.c_str(),ID.Index,Mode)); |
|
|
SetAvailabilityMode(ID, static_cast<ResourceManager<InstrumentManager::instrument_id_t, ::gig::Instrument>::mode_t>(Mode)); |
|
|
} |
|
|
|
|
79 |
String InstrumentResourceManager::GetInstrumentName(instrument_id_t ID) { |
String InstrumentResourceManager::GetInstrumentName(instrument_id_t ID) { |
80 |
Lock(); |
Lock(); |
81 |
::gig::Instrument* pInstrument = Resource(ID, false); |
::gig::Instrument* pInstrument = Resource(ID, false); |
121 |
} |
} |
122 |
|
|
123 |
InstrumentResourceManager::instrument_info_t InstrumentResourceManager::GetInstrumentInfo(instrument_id_t ID) throw (InstrumentManagerException) { |
InstrumentResourceManager::instrument_info_t InstrumentResourceManager::GetInstrumentInfo(instrument_id_t ID) throw (InstrumentManagerException) { |
124 |
std::vector<instrument_id_t> result; |
Lock(); |
125 |
|
::gig::Instrument* pInstrument = Resource(ID, false); |
126 |
|
bool loaded = (pInstrument != NULL); |
127 |
|
if (!loaded) Unlock(); |
128 |
|
|
129 |
::RIFF::File* riff = NULL; |
::RIFF::File* riff = NULL; |
130 |
::gig::File* gig = NULL; |
::gig::File* gig = NULL; |
131 |
try { |
try { |
132 |
riff = new ::RIFF::File(ID.FileName); |
if (!loaded) { |
133 |
gig = new ::gig::File(riff); |
riff = new ::RIFF::File(ID.FileName); |
134 |
gig->SetAutoLoad(false); // avoid time consuming samples scanning |
gig = new ::gig::File(riff); |
135 |
::gig::Instrument* pInstrument = gig->GetInstrument(ID.Index); |
gig->SetAutoLoad(false); // avoid time consuming samples scanning |
136 |
|
pInstrument = gig->GetInstrument(ID.Index); |
137 |
|
} |
138 |
|
|
139 |
if (!pInstrument) throw InstrumentManagerException("There is no instrument " + ToString(ID.Index) + " in " + ID.FileName); |
if (!pInstrument) throw InstrumentManagerException("There is no instrument " + ToString(ID.Index) + " in " + ID.FileName); |
140 |
|
|
141 |
instrument_info_t info; |
instrument_info_t info; |
142 |
if (gig->pVersion) { |
for (int i = 0; i < 128; i++) { info.KeyBindings[i] = info.KeySwitchBindings[i] = 0; } |
143 |
info.FormatVersion = ToString(gig->pVersion->major); |
|
144 |
info.Product = gig->pInfo->Product; |
::gig::File* pFile = (::gig::File*) pInstrument->GetParent(); |
145 |
info.Artists = gig->pInfo->Artists; |
|
146 |
|
if (pFile->pVersion) { |
147 |
|
info.FormatVersion = ToString(pFile->pVersion->major); |
148 |
|
info.Product = pFile->pInfo->Product; |
149 |
|
info.Artists = pFile->pInfo->Artists; |
150 |
} |
} |
151 |
|
|
152 |
info.InstrumentName = pInstrument->pInfo->Name; |
info.InstrumentName = pInstrument->pInfo->Name; |
153 |
|
|
154 |
|
::gig::Region* pRegion = pInstrument->GetFirstRegion(); |
155 |
|
while (pRegion) { |
156 |
|
int low = pRegion->KeyRange.low; |
157 |
|
int high = pRegion->KeyRange.high; |
158 |
|
if (low < 0 || low > 127 || high < 0 || high > 127 || low > high) { |
159 |
|
std::cerr << "Invalid key range: " << low << " - " << high << std::endl; |
160 |
|
} else { |
161 |
|
for (int i = low; i <= high; i++) info.KeyBindings[i] = 1; |
162 |
|
} |
163 |
|
|
164 |
|
pRegion = pInstrument->GetNextRegion(); |
165 |
|
} |
166 |
|
|
167 |
|
if (loaded) { // retrieve keyswitching only if the instrument is fully loaded. |
168 |
|
|
169 |
|
// only return keyswitch range if keyswitching is used |
170 |
|
bool hasKeyswitches = false; |
171 |
|
for (::gig::Region* pRegion = pInstrument->GetFirstRegion() ; |
172 |
|
pRegion && !hasKeyswitches ; |
173 |
|
pRegion = pInstrument->GetNextRegion()) { |
174 |
|
for (int i = 0 ; i < pRegion->Dimensions ; i++) { |
175 |
|
if (pRegion->pDimensionDefinitions[i].dimension == ::gig::dimension_keyboard) { |
176 |
|
hasKeyswitches = true; |
177 |
|
break; |
178 |
|
} |
179 |
|
} |
180 |
|
} |
181 |
|
|
182 |
|
if (hasKeyswitches) { |
183 |
|
int low = pInstrument->DimensionKeyRange.low; |
184 |
|
int high = pInstrument->DimensionKeyRange.high; |
185 |
|
if (low < 0 || low > 127 || high < 0 || high > 127 || low > high) { |
186 |
|
std::cerr << "Invalid keyswitch range: " << low << " - " << high << std::endl; |
187 |
|
} else { |
188 |
|
for (int i = low; i <= high; i++) info.KeySwitchBindings[i] = 1; |
189 |
|
} |
190 |
|
} |
191 |
|
} |
192 |
|
|
193 |
|
if (loaded) Unlock(); |
194 |
|
|
195 |
if (gig) delete gig; |
if (gig) delete gig; |
196 |
if (riff) delete riff; |
if (riff) delete riff; |
197 |
return info; |
return info; |
198 |
} catch (::RIFF::Exception e) { |
} catch (::RIFF::Exception e) { |
199 |
|
if (loaded) Unlock(); |
200 |
if (gig) delete gig; |
if (gig) delete gig; |
201 |
if (riff) delete riff; |
if (riff) delete riff; |
202 |
throw InstrumentManagerException(e.Message); |
throw InstrumentManagerException(e.Message); |
203 |
} catch (...) { |
} catch (...) { |
204 |
|
if (loaded) Unlock(); |
205 |
if (gig) delete gig; |
if (gig) delete gig; |
206 |
if (riff) delete riff; |
if (riff) delete riff; |
207 |
throw InstrumentManagerException("Unknown exception while trying to parse '" + ID.FileName + "'"); |
throw InstrumentManagerException("Unknown exception while trying to parse '" + ID.FileName + "'"); |
208 |
} |
} |
209 |
} |
} |
210 |
|
|
211 |
void InstrumentResourceManager::LaunchInstrumentEditor(instrument_id_t ID) throw (InstrumentManagerException) { |
InstrumentEditor* InstrumentResourceManager::LaunchInstrumentEditor(LinuxSampler::EngineChannel* pEngineChannel, instrument_id_t ID, void* pUserData) throw (InstrumentManagerException) { |
212 |
const String sDataType = GetInstrumentDataStructureName(ID); |
const String sDataType = GetInstrumentDataStructureName(ID); |
213 |
const String sDataVersion = GetInstrumentDataStructureVersion(ID); |
const String sDataVersion = GetInstrumentDataStructureVersion(ID); |
214 |
// find instrument editors capable to handle given instrument |
// find instrument editors capable to handle given instrument |
230 |
::gig::Instrument* pInstrument = Borrow(ID, pProxy); |
::gig::Instrument* pInstrument = Borrow(ID, pProxy); |
231 |
// remember the proxy and instrument for this instrument editor |
// remember the proxy and instrument for this instrument editor |
232 |
pProxy->pInstrument = pInstrument; |
pProxy->pInstrument = pInstrument; |
233 |
|
pProxy->pEditor = pEditor; |
234 |
InstrumentEditorProxiesMutex.Lock(); |
InstrumentEditorProxiesMutex.Lock(); |
235 |
InstrumentEditorProxies[pEditor] = pProxy; |
InstrumentEditorProxies.add(pProxy); |
236 |
InstrumentEditorProxiesMutex.Unlock(); |
InstrumentEditorProxiesMutex.Unlock(); |
237 |
// launch the instrument editor for the given instrument |
// launch the instrument editor for the given instrument |
238 |
pEditor->Launch(pInstrument, sDataType, sDataVersion); |
pEditor->Launch(pEngineChannel, pInstrument, sDataType, sDataVersion, pUserData); |
239 |
|
|
240 |
|
// register the instrument editor as virtual MIDI device as well ... |
241 |
|
VirtualMidiDevice* pVirtualMidiDevice = |
242 |
|
dynamic_cast<VirtualMidiDevice*>(pEditor); |
243 |
|
if (!pVirtualMidiDevice) { |
244 |
|
std::cerr << "Instrument editor not a virtual MIDI device\n" << std::flush; |
245 |
|
return pEditor; |
246 |
|
} |
247 |
|
// NOTE: for now connect the virtual MIDI keyboard of the instrument editor (if any) with all engine channels that have the same instrument as the editor was opened for ( other ideas ? ) |
248 |
|
Lock(); |
249 |
|
std::set<EngineChannel*> engineChannels = |
250 |
|
GetEngineChannelsUsing(pInstrument, false/*don't lock again*/); |
251 |
|
std::set<EngineChannel*>::iterator iter = engineChannels.begin(); |
252 |
|
std::set<EngineChannel*>::iterator end = engineChannels.end(); |
253 |
|
for (; iter != end; ++iter) (static_cast<AbstractEngineChannel*>(*iter))->Connect(pVirtualMidiDevice); |
254 |
|
Unlock(); |
255 |
|
|
256 |
|
return pEditor; |
257 |
} |
} |
258 |
|
|
259 |
/** |
/** |
265 |
*/ |
*/ |
266 |
void InstrumentResourceManager::OnInstrumentEditorQuit(InstrumentEditor* pSender) { |
void InstrumentResourceManager::OnInstrumentEditorQuit(InstrumentEditor* pSender) { |
267 |
dmsg(1,("InstrumentResourceManager: instrument editor quit, doing cleanup\n")); |
dmsg(1,("InstrumentResourceManager: instrument editor quit, doing cleanup\n")); |
268 |
// hand back instrument and free proxy |
|
269 |
InstrumentEditorProxiesMutex.Lock(); |
::gig::Instrument* pInstrument = NULL; |
270 |
if (InstrumentEditorProxies.count(pSender)) { |
InstrumentEditorProxy* pProxy = NULL; |
271 |
InstrumentEditorProxy* pProxy = |
int iProxyIndex = -1; |
272 |
dynamic_cast<InstrumentEditorProxy*>( |
|
273 |
InstrumentEditorProxies[pSender] |
// first find the editor proxy entry for this editor |
274 |
); |
{ |
275 |
InstrumentEditorProxies.erase(pSender); |
LockGuard lock(InstrumentEditorProxiesMutex); |
276 |
InstrumentEditorProxiesMutex.Unlock(); |
for (int i = 0; i < InstrumentEditorProxies.size(); i++) { |
277 |
HandBack(pProxy->pInstrument, pProxy); |
InstrumentEditorProxy* pCurProxy = |
278 |
if (pProxy) delete pProxy; |
dynamic_cast<InstrumentEditorProxy*>( |
279 |
|
InstrumentEditorProxies[i] |
280 |
|
); |
281 |
|
if (pCurProxy->pEditor == pSender) { |
282 |
|
pProxy = pCurProxy; |
283 |
|
iProxyIndex = i; |
284 |
|
pInstrument = pCurProxy->pInstrument; |
285 |
|
} |
286 |
|
} |
287 |
|
} |
288 |
|
|
289 |
|
if (!pProxy) { |
290 |
|
std::cerr << "Eeeek, could not find instrument editor proxy, " |
291 |
|
"this is a bug!\n" << std::flush; |
292 |
|
return; |
293 |
|
} |
294 |
|
|
295 |
|
// now unregister editor as not being available as a virtual MIDI device anymore |
296 |
|
VirtualMidiDevice* pVirtualMidiDevice = |
297 |
|
dynamic_cast<VirtualMidiDevice*>(pSender); |
298 |
|
if (pVirtualMidiDevice) { |
299 |
|
Lock(); |
300 |
|
// NOTE: see note in LaunchInstrumentEditor() |
301 |
|
std::set<EngineChannel*> engineChannels = |
302 |
|
GetEngineChannelsUsing(pInstrument, false/*don't lock again*/); |
303 |
|
std::set<EngineChannel*>::iterator iter = engineChannels.begin(); |
304 |
|
std::set<EngineChannel*>::iterator end = engineChannels.end(); |
305 |
|
for (; iter != end; ++iter) (*iter)->Disconnect(pVirtualMidiDevice); |
306 |
|
Unlock(); |
307 |
} else { |
} else { |
308 |
InstrumentEditorProxiesMutex.Unlock(); |
std::cerr << "Could not unregister editor as not longer acting as " |
309 |
std::cerr << "Eeeek, could not find instrument editor proxy, this is a bug!\n" << std::flush; |
"virtual MIDI device. Wasn't it registered?\n" |
310 |
|
<< std::flush; |
311 |
|
} |
312 |
|
|
313 |
|
// finally delete proxy entry and hand back instrument |
314 |
|
if (pInstrument) { |
315 |
|
{ |
316 |
|
LockGuard lock(InstrumentEditorProxiesMutex); |
317 |
|
InstrumentEditorProxies.remove(iProxyIndex); |
318 |
|
} |
319 |
|
|
320 |
|
HandBack(pInstrument, pProxy); |
321 |
|
delete pProxy; |
322 |
} |
} |
323 |
|
|
324 |
// Note that we don't need to free the editor here. As it |
// Note that we don't need to free the editor here. As it |
326 |
// dies. |
// dies. |
327 |
} |
} |
328 |
|
|
329 |
|
#if 0 // currently unused : |
330 |
|
/** |
331 |
|
* Try to inform the respective instrument editor(s), that a note on |
332 |
|
* event just occured. This method is called by the MIDI thread. If any |
333 |
|
* obstacles are in the way (e.g. if a wait for an unlock would be |
334 |
|
* required) we give up immediately, since the RT safeness of the MIDI |
335 |
|
* thread has absolute priority. |
336 |
|
*/ |
337 |
|
void InstrumentResourceManager::TrySendNoteOnToEditors(uint8_t Key, uint8_t Velocity, ::gig::Instrument* pInstrument) { |
338 |
|
const bool bGotLock = InstrumentEditorProxiesMutex.Trylock(); // naively assumes RT safe implementation |
339 |
|
if (!bGotLock) return; // hell, forget it, not worth the hassle |
340 |
|
for (int i = 0; i < InstrumentEditorProxies.size(); i++) { |
341 |
|
InstrumentEditorProxy* pProxy = |
342 |
|
dynamic_cast<InstrumentEditorProxy*>( |
343 |
|
InstrumentEditorProxies[i] |
344 |
|
); |
345 |
|
if (pProxy->pInstrument == pInstrument) |
346 |
|
pProxy->pEditor->SendNoteOnToDevice(Key, Velocity); |
347 |
|
} |
348 |
|
InstrumentEditorProxiesMutex.Unlock(); // naively assumes RT safe implementation |
349 |
|
} |
350 |
|
|
351 |
|
/** |
352 |
|
* Try to inform the respective instrument editor(s), that a note off |
353 |
|
* event just occured. This method is called by the MIDI thread. If any |
354 |
|
* obstacles are in the way (e.g. if a wait for an unlock would be |
355 |
|
* required) we give up immediately, since the RT safeness of the MIDI |
356 |
|
* thread has absolute priority. |
357 |
|
*/ |
358 |
|
void InstrumentResourceManager::TrySendNoteOffToEditors(uint8_t Key, uint8_t Velocity, ::gig::Instrument* pInstrument) { |
359 |
|
const bool bGotLock = InstrumentEditorProxiesMutex.Trylock(); // naively assumes RT safe implementation |
360 |
|
if (!bGotLock) return; // hell, forget it, not worth the hassle |
361 |
|
for (int i = 0; i < InstrumentEditorProxies.size(); i++) { |
362 |
|
InstrumentEditorProxy* pProxy = |
363 |
|
dynamic_cast<InstrumentEditorProxy*>( |
364 |
|
InstrumentEditorProxies[i] |
365 |
|
); |
366 |
|
if (pProxy->pInstrument == pInstrument) |
367 |
|
pProxy->pEditor->SendNoteOffToDevice(Key, Velocity); |
368 |
|
} |
369 |
|
InstrumentEditorProxiesMutex.Unlock(); // naively assumes RT safe implementation |
370 |
|
} |
371 |
|
#endif // unused |
372 |
|
|
373 |
void InstrumentResourceManager::OnSamplesToBeRemoved(std::set<void*> Samples, InstrumentEditor* pSender) { |
void InstrumentResourceManager::OnSamplesToBeRemoved(std::set<void*> Samples, InstrumentEditor* pSender) { |
374 |
if (Samples.empty()) { |
if (Samples.empty()) { |
375 |
std::cerr << "gig::InstrumentResourceManager: WARNING, " |
std::cerr << "gig::InstrumentResourceManager: WARNING, " |
392 |
} |
} |
393 |
|
|
394 |
void InstrumentResourceManager::OnDataStructureToBeChanged(void* pStruct, String sStructType, InstrumentEditor* pSender) { |
void InstrumentResourceManager::OnDataStructureToBeChanged(void* pStruct, String sStructType, InstrumentEditor* pSender) { |
395 |
|
dmsg(5,("gig::InstrumentResourceManager::OnDataStructureToBeChanged(%s)\n", sStructType.c_str())); |
396 |
//TODO: remove code duplication |
//TODO: remove code duplication |
397 |
if (sStructType == "gig::File") { |
if (sStructType == "gig::File") { |
398 |
// completely suspend all engines that use that file |
// completely suspend all engines that use that file |
410 |
::gig::Instrument* pInstrument = |
::gig::Instrument* pInstrument = |
411 |
(::gig::Instrument*) pRegion->GetParent(); |
(::gig::Instrument*) pRegion->GetParent(); |
412 |
Lock(); |
Lock(); |
413 |
std::set<gig::Engine*> engines = |
std::set<Engine*> engines = |
414 |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
415 |
std::set<gig::Engine*>::iterator iter = engines.begin(); |
std::set<Engine*>::iterator iter = engines.begin(); |
416 |
std::set<gig::Engine*>::iterator end = engines.end(); |
std::set<Engine*>::iterator end = engines.end(); |
417 |
for (; iter != end; ++iter) (*iter)->Suspend(pRegion); |
for (; iter != end; ++iter) (*iter)->Suspend(pRegion); |
418 |
Unlock(); |
Unlock(); |
419 |
} else if (sStructType == "gig::DimensionRegion") { |
} else if (sStructType == "gig::DimensionRegion") { |
427 |
::gig::Instrument* pInstrument = |
::gig::Instrument* pInstrument = |
428 |
(::gig::Instrument*) pRegion->GetParent(); |
(::gig::Instrument*) pRegion->GetParent(); |
429 |
Lock(); |
Lock(); |
430 |
std::set<gig::Engine*> engines = |
std::set<Engine*> engines = |
431 |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
432 |
std::set<gig::Engine*>::iterator iter = engines.begin(); |
std::set<Engine*>::iterator iter = engines.begin(); |
433 |
std::set<gig::Engine*>::iterator end = engines.end(); |
std::set<Engine*>::iterator end = engines.end(); |
434 |
for (; iter != end; ++iter) (*iter)->Suspend(pRegion); |
for (; iter != end; ++iter) (*iter)->Suspend(pRegion); |
435 |
Unlock(); |
Unlock(); |
436 |
|
} else if (sStructType == "gig::Script") { |
437 |
|
// no need to suspend anything here, since the sampler is |
438 |
|
// processing a translated VM representation of the original script |
439 |
|
// source code, not accessing the source code itself during playback |
440 |
|
::gig::Script* pScript = (::gig::Script*) pStruct; |
441 |
|
// remember the original source code of the script, since the script |
442 |
|
// resource manager uses the source code as key |
443 |
|
pendingScriptUpdatesMutex.Lock(); |
444 |
|
pendingScriptUpdates[pScript] = pScript->GetScriptAsText(); |
445 |
|
pendingScriptUpdatesMutex.Unlock(); |
446 |
} else { |
} else { |
447 |
std::cerr << "gig::InstrumentResourceManager: ERROR, unknown data " |
std::cerr << "gig::InstrumentResourceManager: ERROR, unknown data " |
448 |
"structure '" << sStructType << "' requested to be " |
"structure '" << sStructType << "' requested to be " |
453 |
} |
} |
454 |
|
|
455 |
void InstrumentResourceManager::OnDataStructureChanged(void* pStruct, String sStructType, InstrumentEditor* pSender) { |
void InstrumentResourceManager::OnDataStructureChanged(void* pStruct, String sStructType, InstrumentEditor* pSender) { |
456 |
|
dmsg(5,("gig::InstrumentResourceManager::OnDataStructureChanged(%s)\n", sStructType.c_str())); |
457 |
//TODO: remove code duplication |
//TODO: remove code duplication |
458 |
if (sStructType == "gig::File") { |
if (sStructType == "gig::File") { |
459 |
// resume all previously suspended engines |
// resume all previously suspended engines |
461 |
} else if (sStructType == "gig::Instrument") { |
} else if (sStructType == "gig::Instrument") { |
462 |
// resume all previously suspended engines |
// resume all previously suspended engines |
463 |
ResumeAllEngines(); |
ResumeAllEngines(); |
464 |
|
} else if (sStructType == "gig::Sample") { |
465 |
|
// we're assuming here, that OnDataStructureToBeChanged() with |
466 |
|
// "gig::File" was called previously, so we won't resume anything |
467 |
|
// here, but just re-cache the given sample |
468 |
|
Lock(); |
469 |
|
::gig::Sample* pSample = (::gig::Sample*) pStruct; |
470 |
|
::gig::File* pFile = (::gig::File*) pSample->GetParent(); |
471 |
|
UncacheInitialSamples(pSample); |
472 |
|
// now re-cache ... |
473 |
|
std::vector< ::gig::Instrument*> instruments = |
474 |
|
GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/); |
475 |
|
for (int i = 0; i < instruments.size(); i++) { |
476 |
|
if (SampleReferencedByInstrument(pSample, instruments[i])) { |
477 |
|
std::set<EngineChannel*> engineChannels = |
478 |
|
GetEngineChannelsUsing(instruments[i], false/*don't lock again*/); |
479 |
|
std::set<EngineChannel*>::iterator iter = engineChannels.begin(); |
480 |
|
std::set<EngineChannel*>::iterator end = engineChannels.end(); |
481 |
|
for (; iter != end; ++iter) |
482 |
|
CacheInitialSamples(pSample, *iter); |
483 |
|
} |
484 |
|
} |
485 |
|
Unlock(); |
486 |
} else if (sStructType == "gig::Region") { |
} else if (sStructType == "gig::Region") { |
487 |
// advice the engines to resume the given region, that is to |
// advice the engines to resume the given region, that is to |
488 |
// using it for playback again |
// using it for playback again |
490 |
::gig::Instrument* pInstrument = |
::gig::Instrument* pInstrument = |
491 |
(::gig::Instrument*) pRegion->GetParent(); |
(::gig::Instrument*) pRegion->GetParent(); |
492 |
Lock(); |
Lock(); |
493 |
std::set<gig::Engine*> engines = |
std::set<Engine*> engines = |
494 |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
495 |
std::set<gig::Engine*>::iterator iter = engines.begin(); |
std::set<Engine*>::iterator iter = engines.begin(); |
496 |
std::set<gig::Engine*>::iterator end = engines.end(); |
std::set<Engine*>::iterator end = engines.end(); |
497 |
for (; iter != end; ++iter) (*iter)->Resume(pRegion); |
for (; iter != end; ++iter) (*iter)->Resume(pRegion); |
498 |
Unlock(); |
Unlock(); |
499 |
} else if (sStructType == "gig::DimensionRegion") { |
} else if (sStructType == "gig::DimensionRegion") { |
505 |
::gig::Instrument* pInstrument = |
::gig::Instrument* pInstrument = |
506 |
(::gig::Instrument*) pRegion->GetParent(); |
(::gig::Instrument*) pRegion->GetParent(); |
507 |
Lock(); |
Lock(); |
508 |
std::set<gig::Engine*> engines = |
std::set<Engine*> engines = |
509 |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
510 |
std::set<gig::Engine*>::iterator iter = engines.begin(); |
std::set<Engine*>::iterator iter = engines.begin(); |
511 |
std::set<gig::Engine*>::iterator end = engines.end(); |
std::set<Engine*>::iterator end = engines.end(); |
512 |
for (; iter != end; ++iter) (*iter)->Resume(pRegion); |
for (; iter != end; ++iter) (*iter)->Resume(pRegion); |
513 |
Unlock(); |
Unlock(); |
514 |
|
} else if (sStructType == "gig::Script") { |
515 |
|
// inform all engine channels which are using this script, that |
516 |
|
// they need to reload (parse) the script's source code text |
517 |
|
::gig::Script* pScript = (::gig::Script*) pStruct; |
518 |
|
pendingScriptUpdatesMutex.Lock(); |
519 |
|
if (pendingScriptUpdates.count(pScript)) { |
520 |
|
const String& code = pendingScriptUpdates[pScript]; |
521 |
|
std::set<EngineChannel*> channels = GetEngineChannelsUsingScriptSourceCode(code, true/*lock*/); |
522 |
|
pendingScriptUpdates.erase(pScript); |
523 |
|
std::set<EngineChannel*>::iterator iter = channels.begin(); |
524 |
|
std::set<EngineChannel*>::iterator end = channels.end(); |
525 |
|
for (; iter != end; ++iter) (*iter)->reloadScript(pScript); |
526 |
|
} |
527 |
|
pendingScriptUpdatesMutex.Unlock(); |
528 |
} else { |
} else { |
529 |
std::cerr << "gig::InstrumentResourceManager: ERROR, unknown data " |
std::cerr << "gig::InstrumentResourceManager: ERROR, unknown data " |
530 |
"structure '" << sStructType << "' requested to be " |
"structure '" << sStructType << "' requested to be " |
540 |
Lock(); |
Lock(); |
541 |
::gig::Sample* pSample = (::gig::Sample*) pOldSample; |
::gig::Sample* pSample = (::gig::Sample*) pOldSample; |
542 |
::gig::File* pFile = (::gig::File*) pSample->GetParent(); |
::gig::File* pFile = (::gig::File*) pSample->GetParent(); |
543 |
|
bool bSampleStillInUse = false; |
544 |
std::vector< ::gig::Instrument*> instruments = |
std::vector< ::gig::Instrument*> instruments = |
545 |
GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/); |
GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/); |
546 |
for (int i = 0; i < instruments.size(); i++) |
for (int i = 0; i < instruments.size(); i++) { |
547 |
if (!SampleReferencedByInstrument(pSample, instruments[i])) |
if (SampleReferencedByInstrument(pSample, instruments[i])) { |
548 |
UncacheInitialSamples(pSample); |
bSampleStillInUse = true; |
549 |
|
break; |
550 |
|
} |
551 |
|
} |
552 |
|
if (!bSampleStillInUse) UncacheInitialSamples(pSample); |
553 |
Unlock(); |
Unlock(); |
554 |
} |
} |
555 |
// make sure new sample reference is cached |
// make sure new sample reference is cached |
558 |
::gig::Sample* pSample = (::gig::Sample*) pNewSample; |
::gig::Sample* pSample = (::gig::Sample*) pNewSample; |
559 |
::gig::File* pFile = (::gig::File*) pSample->GetParent(); |
::gig::File* pFile = (::gig::File*) pSample->GetParent(); |
560 |
// get all engines that use that same gig::File |
// get all engines that use that same gig::File |
561 |
std::set<gig::Engine*> engines = GetEnginesUsing(pFile, false/*don't lock again*/); |
std::set<Engine*> engines = GetEnginesUsing(pFile, false/*don't lock again*/); |
562 |
std::set<gig::Engine*>::iterator iter = engines.begin(); |
std::set<Engine*>::iterator iter = engines.begin(); |
563 |
std::set<gig::Engine*>::iterator end = engines.end(); |
std::set<Engine*>::iterator end = engines.end(); |
564 |
for (; iter != end; ++iter) |
for (; iter != end; ++iter) |
565 |
CacheInitialSamples(pSample, *iter); |
CacheInitialSamples(pSample, *iter); |
566 |
Unlock(); |
Unlock(); |
569 |
|
|
570 |
::gig::Instrument* InstrumentResourceManager::Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg) { |
::gig::Instrument* InstrumentResourceManager::Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg) { |
571 |
// get gig file from internal gig file manager |
// get gig file from internal gig file manager |
572 |
::gig::File* pGig = Gigs.Borrow(Key.FileName, (GigConsumer*) Key.Index); // conversion kinda hackish :/ |
::gig::File* pGig = Gigs.Borrow(Key.FileName, reinterpret_cast<GigConsumer*>(Key.Index)); // conversion kinda hackish :/ |
573 |
|
|
574 |
// we pass this to the progress callback mechanism of libgig |
// we pass this to the progress callback mechanism of libgig |
575 |
progress_callback_arg_t callbackArg; |
progress_callback_arg_t callbackArg; |
590 |
pGig->GetFirstSample(); // just to force complete instrument loading |
pGig->GetFirstSample(); // just to force complete instrument loading |
591 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
592 |
|
|
593 |
|
uint maxSamplesPerCycle = GetMaxSamplesPerCycle(pConsumer); |
594 |
|
|
595 |
// cache initial samples points (for actually needed samples) |
// cache initial samples points (for actually needed samples) |
596 |
dmsg(1,("Caching initial samples...")); |
dmsg(1,("Caching initial samples...")); |
597 |
uint iRegion = 0; // just for progress calculation |
uint iRegion = 0; // just for progress calculation |
603 |
|
|
604 |
if (pRgn->GetSample() && !pRgn->GetSample()->GetCache().Size) { |
if (pRgn->GetSample() && !pRgn->GetSample()->GetCache().Size) { |
605 |
dmsg(2,("C")); |
dmsg(2,("C")); |
606 |
CacheInitialSamples(pRgn->GetSample(), (gig::EngineChannel*) pConsumer); |
CacheInitialSamples(pRgn->GetSample(), maxSamplesPerCycle); |
607 |
} |
} |
608 |
for (uint i = 0; i < pRgn->DimensionRegions; i++) { |
for (uint i = 0; i < pRgn->DimensionRegions; i++) { |
609 |
CacheInitialSamples(pRgn->pDimensionRegions[i]->pSample, (gig::EngineChannel*) pConsumer); |
CacheInitialSamples(pRgn->pDimensionRegions[i]->pSample, maxSamplesPerCycle); |
610 |
} |
} |
611 |
|
|
612 |
pRgn = pInstrument->GetNextRegion(); |
pRgn = pInstrument->GetNextRegion(); |
619 |
instr_entry_t* pEntry = new instr_entry_t; |
instr_entry_t* pEntry = new instr_entry_t; |
620 |
pEntry->ID.FileName = Key.FileName; |
pEntry->ID.FileName = Key.FileName; |
621 |
pEntry->ID.Index = Key.Index; |
pEntry->ID.Index = Key.Index; |
622 |
pEntry->pGig = pGig; |
pEntry->pFile = pGig; |
623 |
|
|
624 |
gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(pConsumer); |
// and we save this to check if we need to reallocate for an engine with higher value of 'MaxSamplesPerSecond' |
625 |
// and we save this to check if we need to reallocate for a engine with higher value of 'MaxSamplesPerSecond' |
pEntry->MaxSamplesPerCycle = maxSamplesPerCycle; |
626 |
pEntry->MaxSamplesPerCycle = |
|
|
(!pEngineChannel) ? 0 /* don't care for instrument editors */ : |
|
|
(pEngineChannel->GetEngine()) ? |
|
|
dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle() |
|
|
: GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE; |
|
627 |
pArg = pEntry; |
pArg = pEntry; |
628 |
|
|
629 |
return pInstrument; |
return pInstrument; |
630 |
} |
} |
631 |
|
|
632 |
void InstrumentResourceManager::Destroy( ::gig::Instrument* pResource, void* pArg) { |
void InstrumentResourceManager::Destroy(::gig::Instrument* pResource, void* pArg) { |
633 |
instr_entry_t* pEntry = (instr_entry_t*) pArg; |
instr_entry_t* pEntry = (instr_entry_t*) pArg; |
634 |
// we don't need the .gig file here anymore |
// we don't need the .gig file here anymore |
635 |
Gigs.HandBack(pEntry->pGig, (GigConsumer*) pEntry->ID.Index); // conversion kinda hackish :/ |
Gigs.HandBack(pEntry->pFile, reinterpret_cast<GigConsumer*>(pEntry->ID.Index)); // conversion kinda hackish :/ |
636 |
delete pEntry; |
delete pEntry; |
637 |
} |
} |
638 |
|
|
639 |
void InstrumentResourceManager::OnBorrow(::gig::Instrument* pResource, InstrumentConsumer* pConsumer, void*& pArg) { |
void InstrumentResourceManager::DeleteRegionIfNotUsed(::gig::DimensionRegion* pRegion, region_info_t* pRegInfo) { |
640 |
instr_entry_t* pEntry = (instr_entry_t*) pArg; |
// TODO: we could delete Region and Instrument here if they have become unused |
|
gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(pConsumer); |
|
|
uint maxSamplesPerCycle = |
|
|
(pEngineChannel && pEngineChannel->GetEngine()) ? dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle() |
|
|
: GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE; |
|
|
if (pEntry->MaxSamplesPerCycle < maxSamplesPerCycle) { |
|
|
Update(pResource, pConsumer); |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* Give back an instrument. This should be used instead of |
|
|
* HandBack if there are some dimension regions that are still in |
|
|
* use. (When an instrument is changed, the voices currently |
|
|
* playing are allowed to keep playing with the old instrument |
|
|
* until note off arrives. New notes will use the new instrument.) |
|
|
*/ |
|
|
void InstrumentResourceManager::HandBackInstrument(::gig::Instrument* pResource, InstrumentConsumer* pConsumer, |
|
|
RTList< ::gig::DimensionRegion*>* pDimRegionsInUse) { |
|
|
DimRegInfoMutex.Lock(); |
|
|
for (RTList< ::gig::DimensionRegion*>::Iterator i = pDimRegionsInUse->first() ; i != pDimRegionsInUse->end() ; i++) { |
|
|
DimRegInfo[*i].refCount++; |
|
|
SampleRefCount[(*i)->pSample]++; |
|
|
} |
|
|
HandBack(pResource, pConsumer, true); |
|
|
DimRegInfoMutex.Unlock(); |
|
641 |
} |
} |
642 |
|
|
643 |
/** |
void InstrumentResourceManager::DeleteSampleIfNotUsed(::gig::Sample* pSample, region_info_t* pRegInfo) { |
644 |
* Give back a dimension region that belongs to an instrument that |
::gig::File* gig = pRegInfo->file; |
645 |
* was previously handed back. |
::RIFF::File* riff = static_cast< ::RIFF::File*>(pRegInfo->pArg); |
646 |
*/ |
if (gig) { |
647 |
void InstrumentResourceManager::HandBackDimReg(::gig::DimensionRegion* pDimReg) { |
gig->DeleteSample(pSample); |
648 |
DimRegInfoMutex.Lock(); |
if (!gig->GetFirstSample()) { |
649 |
dimreg_info_t& dimRegInfo = DimRegInfo[pDimReg]; |
dmsg(2,("No more samples in use - freeing gig\n")); |
650 |
int dimRegRefCount = --dimRegInfo.refCount; |
delete gig; |
651 |
int sampleRefCount = --SampleRefCount[pDimReg->pSample]; |
delete riff; |
|
if (dimRegRefCount == 0) { |
|
|
::gig::File* gig = dimRegInfo.file; |
|
|
::RIFF::File* riff = dimRegInfo.riff; |
|
|
DimRegInfo.erase(pDimReg); |
|
|
// TODO: we could delete Region and Instrument here if |
|
|
// they have become unused |
|
|
|
|
|
if (sampleRefCount == 0) { |
|
|
SampleRefCount.erase(pDimReg->pSample); |
|
|
|
|
|
if (gig) { |
|
|
gig->DeleteSample(pDimReg->pSample); |
|
|
if (!gig->GetFirstSample()) { |
|
|
dmsg(2,("No more samples in use - freeing gig\n")); |
|
|
delete gig; |
|
|
delete riff; |
|
|
} |
|
|
} |
|
652 |
} |
} |
653 |
} |
} |
|
DimRegInfoMutex.Unlock(); |
|
654 |
} |
} |
655 |
|
|
656 |
/** |
/** |
661 |
* (may be NULL, in this case default amount of samples |
* (may be NULL, in this case default amount of samples |
662 |
* will be cached) |
* will be cached) |
663 |
*/ |
*/ |
664 |
void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, gig::EngineChannel* pEngineChannel) { |
void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, EngineChannel* pEngineChannel) { |
665 |
gig::Engine* pEngine = |
Engine* pEngine = |
666 |
(pEngineChannel && pEngineChannel->GetEngine()) ? |
(pEngineChannel && pEngineChannel->GetEngine()) ? |
667 |
dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine()) : NULL; |
dynamic_cast<Engine*>(pEngineChannel->GetEngine()) : NULL; |
668 |
CacheInitialSamples(pSample, pEngine); |
CacheInitialSamples(pSample, pEngine); |
669 |
} |
} |
670 |
|
|
679 |
* (may be NULL, in this case default amount of samples |
* (may be NULL, in this case default amount of samples |
680 |
* will be cached) |
* will be cached) |
681 |
*/ |
*/ |
682 |
void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, gig::Engine* pEngine) { |
void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, AbstractEngine* pEngine) { |
683 |
|
uint maxSamplesPerCycle = |
684 |
|
(pEngine) ? pEngine->pAudioOutputDevice->MaxSamplesPerCycle() : |
685 |
|
DefaultMaxSamplesPerCycle(); |
686 |
|
CacheInitialSamples(pSample, maxSamplesPerCycle); |
687 |
|
} |
688 |
|
|
689 |
|
void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, uint maxSamplesPerCycle) { |
690 |
if (!pSample) { |
if (!pSample) { |
691 |
dmsg(4,("gig::InstrumentResourceManager: Skipping sample (pSample == NULL)\n")); |
dmsg(4,("gig::InstrumentResourceManager: Skipping sample (pSample == NULL)\n")); |
692 |
return; |
return; |
699 |
// number of '0' samples (silence samples) behind the official buffer |
// number of '0' samples (silence samples) behind the official buffer |
700 |
// border, to allow the interpolator do it's work even at the end of |
// border, to allow the interpolator do it's work even at the end of |
701 |
// the sample. |
// the sample. |
|
const uint maxSamplesPerCycle = |
|
|
(pEngine) ? pEngine->pAudioOutputDevice->MaxSamplesPerCycle() |
|
|
: GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE; |
|
702 |
const uint neededSilenceSamples = (maxSamplesPerCycle << CONFIG_MAX_PITCH) + 3; |
const uint neededSilenceSamples = (maxSamplesPerCycle << CONFIG_MAX_PITCH) + 3; |
703 |
const uint currentlyCachedSilenceSamples = pSample->GetCache().NullExtensionSize / pSample->FrameSize; |
const uint currentlyCachedSilenceSamples = pSample->GetCache().NullExtensionSize / pSample->FrameSize; |
704 |
if (currentlyCachedSilenceSamples < neededSilenceSamples) { |
if (currentlyCachedSilenceSamples < neededSilenceSamples) { |
705 |
dmsg(3,("Caching whole sample (sample name: \"%s\", sample size: %d)\n", pSample->pInfo->Name.c_str(), pSample->SamplesTotal)); |
dmsg(3,("Caching whole sample (sample name: \"%s\", sample size: %lu)\n", pSample->pInfo->Name.c_str(), pSample->SamplesTotal)); |
706 |
::gig::buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension(neededSilenceSamples); |
::gig::buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension(neededSilenceSamples); |
707 |
dmsg(4,("Cached %d Bytes, %d silence bytes.\n", buf.Size, buf.NullExtensionSize)); |
dmsg(4,("Cached %lu Bytes, %lu silence bytes.\n", buf.Size, buf.NullExtensionSize)); |
708 |
} |
} |
709 |
} |
} |
710 |
else { // we only cache CONFIG_PRELOAD_SAMPLES and stream the other sample points from disk |
else { // we only cache CONFIG_PRELOAD_SAMPLES and stream the other sample points from disk |
715 |
} |
} |
716 |
|
|
717 |
void InstrumentResourceManager::UncacheInitialSamples(::gig::Sample* pSample) { |
void InstrumentResourceManager::UncacheInitialSamples(::gig::Sample* pSample) { |
718 |
dmsg(1,("Uncaching sample %x\n",pSample)); |
dmsg(1,("Uncaching sample %p\n",(void*)pSample)); |
719 |
if (pSample->GetCache().Size) pSample->ReleaseSampleData(); |
if (pSample->GetCache().Size) pSample->ReleaseSampleData(); |
720 |
} |
} |
721 |
|
|
741 |
} |
} |
742 |
|
|
743 |
/** |
/** |
744 |
|
* Returns a list with all gig engine channels that are currently using |
745 |
|
* the given real-time instrument script (provided as source code). |
746 |
|
* |
747 |
|
* @param pScript - search criteria |
748 |
|
* @param bLock - whether we should lock (mutex) the instrument manager |
749 |
|
* during this call and unlock at the end of this call |
750 |
|
*/ |
751 |
|
std::set<EngineChannel*> InstrumentResourceManager::GetEngineChannelsUsingScriptSourceCode(const String& code, bool bLock) { |
752 |
|
if (bLock) Lock(); |
753 |
|
std::set<EngineChannel*> result; |
754 |
|
std::set<InstrumentScriptConsumer*> consumers = scripts.ConsumersOf(code); |
755 |
|
std::set<InstrumentScriptConsumer*>::iterator iter = consumers.begin(); |
756 |
|
std::set<InstrumentScriptConsumer*>::iterator end = consumers.end(); |
757 |
|
for (; iter != end; ++iter) { |
758 |
|
EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(*iter); |
759 |
|
if (!pEngineChannel) continue; |
760 |
|
result.insert(pEngineChannel); |
761 |
|
} |
762 |
|
if (bLock) Unlock(); |
763 |
|
return result; |
764 |
|
} |
765 |
|
|
766 |
|
/** |
767 |
|
* Returns a list with all gig engine channels that are currently using |
768 |
|
* the given instrument. |
769 |
|
* |
770 |
|
* @param pInstrument - search criteria |
771 |
|
* @param bLock - whether we should lock (mutex) the instrument manager |
772 |
|
* during this call and unlock at the end of this call |
773 |
|
*/ |
774 |
|
std::set<EngineChannel*> InstrumentResourceManager::GetEngineChannelsUsing(::gig::Instrument* pInstrument, bool bLock) { |
775 |
|
if (bLock) Lock(); |
776 |
|
std::set<EngineChannel*> result; |
777 |
|
std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument); |
778 |
|
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin(); |
779 |
|
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end = consumers.end(); |
780 |
|
for (; iter != end; ++iter) { |
781 |
|
EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(*iter); |
782 |
|
if (!pEngineChannel) continue; |
783 |
|
result.insert(pEngineChannel); |
784 |
|
} |
785 |
|
if (bLock) Unlock(); |
786 |
|
return result; |
787 |
|
} |
788 |
|
|
789 |
|
/** |
790 |
* Returns a list with all gig Engines that are currently using the given |
* Returns a list with all gig Engines that are currently using the given |
791 |
* instrument. |
* instrument. |
792 |
* |
* |
794 |
* @param bLock - whether we should lock (mutex) the instrument manager |
* @param bLock - whether we should lock (mutex) the instrument manager |
795 |
* during this call and unlock at the end of this call |
* during this call and unlock at the end of this call |
796 |
*/ |
*/ |
797 |
std::set<gig::Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::Instrument* pInstrument, bool bLock) { |
std::set<Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::Instrument* pInstrument, bool bLock) { |
798 |
if (bLock) Lock(); |
if (bLock) Lock(); |
799 |
std::set<gig::Engine*> result; |
std::set<Engine*> result; |
800 |
std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument); |
std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument); |
801 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin(); |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin(); |
802 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end = consumers.end(); |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end = consumers.end(); |
803 |
for (; iter != end; ++iter) { |
for (; iter != end; ++iter) { |
804 |
gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(*iter); |
EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(*iter); |
805 |
if (!pEngineChannel) continue; |
if (!pEngineChannel) continue; |
806 |
gig::Engine* pEngine = dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine()); |
Engine* pEngine = dynamic_cast<Engine*>(pEngineChannel->GetEngine()); |
807 |
if (!pEngine) continue; |
if (!pEngine) continue; |
808 |
result.insert(pEngine); |
result.insert(pEngine); |
809 |
} |
} |
819 |
* @param bLock - whether we should lock (mutex) the instrument manager |
* @param bLock - whether we should lock (mutex) the instrument manager |
820 |
* during this call and unlock at the end of this call |
* during this call and unlock at the end of this call |
821 |
*/ |
*/ |
822 |
std::set<gig::Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::File* pFile, bool bLock) { |
std::set<Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::File* pFile, bool bLock) { |
823 |
if (bLock) Lock(); |
if (bLock) Lock(); |
824 |
// get all instruments (currently in usage) that use that same gig::File |
// get all instruments (currently in usage) that use that same gig::File |
825 |
std::vector< ::gig::Instrument*> instrumentsOfInterest = |
std::vector< ::gig::Instrument*> instrumentsOfInterest = |
826 |
GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/); |
GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/); |
827 |
|
|
828 |
// get all engines that use that same gig::File |
// get all engines that use that same gig::File |
829 |
std::set<gig::Engine*> result; |
std::set<Engine*> result; |
830 |
{ |
{ |
831 |
for (int i = 0; i < instrumentsOfInterest.size(); i++) { |
for (int i = 0; i < instrumentsOfInterest.size(); i++) { |
832 |
std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(instrumentsOfInterest[i]); |
std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(instrumentsOfInterest[i]); |
833 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin(); |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin(); |
834 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end = consumers.end(); |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end = consumers.end(); |
835 |
for (; iter != end; ++iter) { |
for (; iter != end; ++iter) { |
836 |
gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(*iter); |
EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(*iter); |
837 |
if (!pEngineChannel) continue; |
if (!pEngineChannel) continue; |
838 |
gig::Engine* pEngine = dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine()); |
Engine* pEngine = dynamic_cast<Engine*>(pEngineChannel->GetEngine()); |
839 |
if (!pEngine) continue; |
if (!pEngine) continue; |
840 |
// the unique, sorted container std::set makes |
// the unique, sorted container std::set makes |
841 |
// sure we won't have duplicates |
// sure we won't have duplicates |
889 |
// get all engines that use that same gig::Instrument |
// get all engines that use that same gig::Instrument |
890 |
suspendedEngines = GetEnginesUsing(pInstrument, true/*lock*/); |
suspendedEngines = GetEnginesUsing(pInstrument, true/*lock*/); |
891 |
// finally, completely suspend all engines that use that same gig::Instrument |
// finally, completely suspend all engines that use that same gig::Instrument |
892 |
std::set<gig::Engine*>::iterator iter = suspendedEngines.begin(); |
std::set<Engine*>::iterator iter = suspendedEngines.begin(); |
893 |
std::set<gig::Engine*>::iterator end = suspendedEngines.end(); |
std::set<Engine*>::iterator end = suspendedEngines.end(); |
894 |
for (; iter != end; ++iter) (*iter)->SuspendAll(); |
for (; iter != end; ++iter) (*iter)->SuspendAll(); |
895 |
} |
} |
896 |
|
|
913 |
// get all engines that use that same gig::File |
// get all engines that use that same gig::File |
914 |
suspendedEngines = GetEnginesUsing(pFile, true/*lock*/); |
suspendedEngines = GetEnginesUsing(pFile, true/*lock*/); |
915 |
// finally, completely suspend all engines that use that same gig::File |
// finally, completely suspend all engines that use that same gig::File |
916 |
std::set<gig::Engine*>::iterator iter = suspendedEngines.begin(); |
std::set<Engine*>::iterator iter = suspendedEngines.begin(); |
917 |
std::set<gig::Engine*>::iterator end = suspendedEngines.end(); |
std::set<Engine*>::iterator end = suspendedEngines.end(); |
918 |
for (; iter != end; ++iter) (*iter)->SuspendAll(); |
for (; iter != end; ++iter) (*iter)->SuspendAll(); |
919 |
} |
} |
920 |
|
|
949 |
} |
} |
950 |
|
|
951 |
void InstrumentResourceManager::GigResourceManager::Destroy(::gig::File* pResource, void* pArg) { |
void InstrumentResourceManager::GigResourceManager::Destroy(::gig::File* pResource, void* pArg) { |
952 |
dmsg(1,("Freeing gig file from memory...")); |
dmsg(1,("Freeing gig file '%s' from memory ...", pResource->GetFileName().c_str())); |
953 |
|
|
954 |
// Delete as much as possible of the gig file. Some of the |
// Delete as much as possible of the gig file. Some of the |
955 |
// dimension regions and samples may still be in use - these |
// dimension regions and samples may still be in use - these |
970 |
for (int i = 0 ; i < region->DimensionRegions ; i++) |
for (int i = 0 ; i < region->DimensionRegions ; i++) |
971 |
{ |
{ |
972 |
::gig::DimensionRegion *d = region->pDimensionRegions[i]; |
::gig::DimensionRegion *d = region->pDimensionRegions[i]; |
973 |
std::map< ::gig::DimensionRegion*, dimreg_info_t>::iterator iter = parent->DimRegInfo.find(d); |
std::map< ::gig::DimensionRegion*, region_info_t>::iterator iter = parent->RegionInfo.find(d); |
974 |
if (iter != parent->DimRegInfo.end()) { |
if (iter != parent->RegionInfo.end()) { |
975 |
dimreg_info_t& dimRegInfo = (*iter).second; |
region_info_t& dimRegInfo = (*iter).second; |
976 |
dimRegInfo.file = pResource; |
dimRegInfo.file = pResource; |
977 |
dimRegInfo.riff = (::RIFF::File*)pArg; |
dimRegInfo.pArg = (::RIFF::File*)pArg; |
978 |
deleteFile = deleteInstrument = deleteRegion = false; |
deleteFile = deleteInstrument = deleteRegion = false; |
979 |
} |
} |
980 |
} |
} |