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