1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 - 2016 Christian Schoenebeck * |
7 |
* * |
8 |
* 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 * |
10 |
* the Free Software Foundation; either version 2 of the License, or * |
11 |
* (at your option) any later version. * |
12 |
* * |
13 |
* This program is distributed in the hope that it will be useful, * |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
* GNU General Public License for more details. * |
17 |
* * |
18 |
* You should have received a copy of the GNU General Public License * |
19 |
* along with this program; if not, write to the Free Software * |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
23 |
|
24 |
#include <sstream> |
25 |
|
26 |
#include "InstrumentResourceManager.h" |
27 |
#include "EngineChannel.h" |
28 |
#include "Engine.h" |
29 |
|
30 |
#include "../../common/global_private.h" |
31 |
#include "../../plugins/InstrumentEditorFactory.h" |
32 |
|
33 |
namespace LinuxSampler { namespace gig { |
34 |
|
35 |
// some data needed for the libgig callback function |
36 |
struct progress_callback_arg_t { |
37 |
InstrumentResourceManager* pManager; |
38 |
InstrumentManager::instrument_id_t* pInstrumentKey; |
39 |
}; |
40 |
|
41 |
// we use this to react on events concerning an instrument on behalf of an instrument editor |
42 |
class InstrumentEditorProxy : public InstrumentConsumer { |
43 |
public: |
44 |
virtual void ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) { |
45 |
//TODO: inform the instrument editor about the pending update |
46 |
} |
47 |
|
48 |
virtual void ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) { |
49 |
//TODO:: inform the instrument editor about finished update |
50 |
} |
51 |
|
52 |
virtual void OnResourceProgress(float fProgress) { |
53 |
//TODO: inform the instrument editor about the progress of an update |
54 |
} |
55 |
|
56 |
// the instrument we borrowed on behalf of the editor |
57 |
::gig::Instrument* pInstrument; |
58 |
// the instrument editor we work on behalf |
59 |
InstrumentEditor* pEditor; |
60 |
}; |
61 |
|
62 |
/** |
63 |
* Callback function which will be called by libgig during loading of |
64 |
* instruments to inform about the current progress. Or to be more |
65 |
* specific; it will be called during the GetInstrument() call. |
66 |
* |
67 |
* @param pProgress - contains current progress value, pointer to the |
68 |
* InstrumentResourceManager instance and |
69 |
* instrument ID |
70 |
*/ |
71 |
void InstrumentResourceManager::OnInstrumentLoadingProgress(::gig::progress_t* pProgress) { |
72 |
dmsg(7,("gig::InstrumentResourceManager: progress %f%%", pProgress->factor)); |
73 |
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 |
75 |
const float localProgress = 0.9f * pProgress->factor; |
76 |
pArg->pManager->DispatchResourceProgressEvent(*pArg->pInstrumentKey, localProgress); |
77 |
} |
78 |
|
79 |
String InstrumentResourceManager::GetInstrumentName(instrument_id_t ID) { |
80 |
Lock(); |
81 |
::gig::Instrument* pInstrument = Resource(ID, false); |
82 |
String res = (pInstrument) ? pInstrument->pInfo->Name : ""; |
83 |
Unlock(); |
84 |
return res; |
85 |
} |
86 |
|
87 |
String InstrumentResourceManager::GetInstrumentDataStructureName(instrument_id_t ID) { |
88 |
return ::gig::libraryName(); |
89 |
} |
90 |
|
91 |
String InstrumentResourceManager::GetInstrumentDataStructureVersion(instrument_id_t ID) { |
92 |
return ::gig::libraryVersion(); |
93 |
} |
94 |
|
95 |
std::vector<InstrumentResourceManager::instrument_id_t> InstrumentResourceManager::GetInstrumentFileContent(String File) throw (InstrumentManagerException) { |
96 |
::RIFF::File* riff = NULL; |
97 |
::gig::File* gig = NULL; |
98 |
try { |
99 |
std::vector<instrument_id_t> result; |
100 |
riff = new ::RIFF::File(File); |
101 |
gig = new ::gig::File(riff); |
102 |
gig->SetAutoLoad(false); // avoid time consuming samples scanning |
103 |
for (int i = 0; gig->GetInstrument(i); i++) { |
104 |
instrument_id_t id; |
105 |
id.FileName = File; |
106 |
id.Index = i; |
107 |
result.push_back(id); |
108 |
} |
109 |
if (gig) delete gig; |
110 |
if (riff) delete riff; |
111 |
return result; |
112 |
} catch (::RIFF::Exception e) { |
113 |
if (gig) delete gig; |
114 |
if (riff) delete riff; |
115 |
throw InstrumentManagerException(e.Message); |
116 |
} catch (...) { |
117 |
if (gig) delete gig; |
118 |
if (riff) delete riff; |
119 |
throw InstrumentManagerException("Unknown exception while trying to parse '" + File + "'"); |
120 |
} |
121 |
} |
122 |
|
123 |
InstrumentResourceManager::instrument_info_t InstrumentResourceManager::GetInstrumentInfo(instrument_id_t ID) throw (InstrumentManagerException) { |
124 |
Lock(); |
125 |
::gig::Instrument* pInstrument = Resource(ID, false); |
126 |
bool loaded = (pInstrument != NULL); |
127 |
if (!loaded) Unlock(); |
128 |
|
129 |
::RIFF::File* riff = NULL; |
130 |
::gig::File* gig = NULL; |
131 |
try { |
132 |
if (!loaded) { |
133 |
riff = new ::RIFF::File(ID.FileName); |
134 |
gig = new ::gig::File(riff); |
135 |
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); |
140 |
|
141 |
instrument_info_t info; |
142 |
for (int i = 0; i < 128; i++) { info.KeyBindings[i] = info.KeySwitchBindings[i] = 0; } |
143 |
|
144 |
::gig::File* pFile = (::gig::File*) pInstrument->GetParent(); |
145 |
|
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; |
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; |
196 |
if (riff) delete riff; |
197 |
return info; |
198 |
} catch (::RIFF::Exception e) { |
199 |
if (loaded) Unlock(); |
200 |
if (gig) delete gig; |
201 |
if (riff) delete riff; |
202 |
throw InstrumentManagerException(e.Message); |
203 |
} catch (...) { |
204 |
if (loaded) Unlock(); |
205 |
if (gig) delete gig; |
206 |
if (riff) delete riff; |
207 |
throw InstrumentManagerException("Unknown exception while trying to parse '" + ID.FileName + "'"); |
208 |
} |
209 |
} |
210 |
|
211 |
InstrumentEditor* InstrumentResourceManager::LaunchInstrumentEditor(LinuxSampler::EngineChannel* pEngineChannel, instrument_id_t ID, void* pUserData) throw (InstrumentManagerException) { |
212 |
const String sDataType = GetInstrumentDataStructureName(ID); |
213 |
const String sDataVersion = GetInstrumentDataStructureVersion(ID); |
214 |
// find instrument editors capable to handle given instrument |
215 |
std::vector<String> vEditors = |
216 |
InstrumentEditorFactory::MatchingEditors(sDataType, sDataVersion); |
217 |
if (!vEditors.size()) |
218 |
throw InstrumentManagerException( |
219 |
"There is no instrument editor capable to handle this instrument" |
220 |
); |
221 |
// simply use the first editor in the result set |
222 |
dmsg(1,("Found matching editor '%s' for instrument ('%s', %d) having data structure ('%s','%s')\n", |
223 |
vEditors[0].c_str(), ID.FileName.c_str(), ID.Index, sDataType.c_str(), sDataVersion.c_str())); |
224 |
InstrumentEditor* pEditor = InstrumentEditorFactory::Create(vEditors[0]); |
225 |
// register for receiving notifications from the instrument editor |
226 |
pEditor->AddListener(this); |
227 |
// create a proxy that reacts on notification on behalf of the editor |
228 |
InstrumentEditorProxy* pProxy = new InstrumentEditorProxy; |
229 |
// borrow the instrument on behalf of the instrument editor |
230 |
::gig::Instrument* pInstrument = Borrow(ID, pProxy); |
231 |
// remember the proxy and instrument for this instrument editor |
232 |
pProxy->pInstrument = pInstrument; |
233 |
pProxy->pEditor = pEditor; |
234 |
InstrumentEditorProxiesMutex.Lock(); |
235 |
InstrumentEditorProxies.add(pProxy); |
236 |
InstrumentEditorProxiesMutex.Unlock(); |
237 |
// launch the instrument editor for the given instrument |
238 |
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 |
/** |
260 |
* Will be called by the respective instrument editor once it left its |
261 |
* Main() loop. That way we can handle cleanup before its thread finally |
262 |
* dies. |
263 |
* |
264 |
* @param pSender - instrument editor that stops execution |
265 |
*/ |
266 |
void InstrumentResourceManager::OnInstrumentEditorQuit(InstrumentEditor* pSender) { |
267 |
dmsg(1,("InstrumentResourceManager: instrument editor quit, doing cleanup\n")); |
268 |
|
269 |
::gig::Instrument* pInstrument = NULL; |
270 |
InstrumentEditorProxy* pProxy = NULL; |
271 |
int iProxyIndex = -1; |
272 |
|
273 |
// first find the editor proxy entry for this editor |
274 |
{ |
275 |
LockGuard lock(InstrumentEditorProxiesMutex); |
276 |
for (int i = 0; i < InstrumentEditorProxies.size(); i++) { |
277 |
InstrumentEditorProxy* pCurProxy = |
278 |
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 { |
308 |
std::cerr << "Could not unregister editor as not longer acting as " |
309 |
"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 |
325 |
// derives from Thread, it will delete itself when the thread |
326 |
// 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) { |
374 |
if (Samples.empty()) { |
375 |
std::cerr << "gig::InstrumentResourceManager: WARNING, " |
376 |
"OnSamplesToBeRemoved() called with empty list, this " |
377 |
"is a bug!\n" << std::flush; |
378 |
return; |
379 |
} |
380 |
// TODO: ATM we assume here that all samples are from the same file |
381 |
::gig::Sample* pFirstSample = (::gig::Sample*) *Samples.begin(); |
382 |
::gig::File* pCriticalFile = dynamic_cast< ::gig::File*>(pFirstSample->GetParent()); |
383 |
// completely suspend all engines that use that same file |
384 |
SuspendEnginesUsing(pCriticalFile); |
385 |
} |
386 |
|
387 |
void InstrumentResourceManager::OnSamplesRemoved(InstrumentEditor* pSender) { |
388 |
// resume all previously, completely suspended engines |
389 |
// (we don't have to un-cache the removed samples here, since that is |
390 |
// automatically done by the gig::Sample destructor) |
391 |
ResumeAllEngines(); |
392 |
} |
393 |
|
394 |
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 |
397 |
if (sStructType == "gig::File") { |
398 |
// completely suspend all engines that use that file |
399 |
::gig::File* pFile = (::gig::File*) pStruct; |
400 |
SuspendEnginesUsing(pFile); |
401 |
} else if (sStructType == "gig::Instrument") { |
402 |
// completely suspend all engines that use that instrument |
403 |
::gig::Instrument* pInstrument = (::gig::Instrument*) pStruct; |
404 |
SuspendEnginesUsing(pInstrument); |
405 |
} else if (sStructType == "gig::Region") { |
406 |
// only advice the engines to suspend the given region, so they'll |
407 |
// only ignore that region (and probably already other suspended |
408 |
// ones), but beside that continue normal playback |
409 |
::gig::Region* pRegion = (::gig::Region*) pStruct; |
410 |
::gig::Instrument* pInstrument = |
411 |
(::gig::Instrument*) pRegion->GetParent(); |
412 |
Lock(); |
413 |
std::set<Engine*> engines = |
414 |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
415 |
std::set<Engine*>::iterator iter = engines.begin(); |
416 |
std::set<Engine*>::iterator end = engines.end(); |
417 |
for (; iter != end; ++iter) (*iter)->Suspend(pRegion); |
418 |
Unlock(); |
419 |
} else if (sStructType == "gig::DimensionRegion") { |
420 |
// only advice the engines to suspend the given DimensionRegions's |
421 |
// parent region, so they'll only ignore that region (and probably |
422 |
// already other suspended ones), but beside that continue normal |
423 |
// playback |
424 |
::gig::DimensionRegion* pDimReg = |
425 |
(::gig::DimensionRegion*) pStruct; |
426 |
::gig::Region* pRegion = pDimReg->GetParent(); |
427 |
::gig::Instrument* pInstrument = |
428 |
(::gig::Instrument*) pRegion->GetParent(); |
429 |
Lock(); |
430 |
std::set<Engine*> engines = |
431 |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
432 |
std::set<Engine*>::iterator iter = engines.begin(); |
433 |
std::set<Engine*>::iterator end = engines.end(); |
434 |
for (; iter != end; ++iter) (*iter)->Suspend(pRegion); |
435 |
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 { |
447 |
std::cerr << "gig::InstrumentResourceManager: ERROR, unknown data " |
448 |
"structure '" << sStructType << "' requested to be " |
449 |
"suspended by instrument editor. This is a bug!\n" |
450 |
<< std::flush; |
451 |
//TODO: we should inform the instrument editor that something seriously went wrong |
452 |
} |
453 |
} |
454 |
|
455 |
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 |
458 |
if (sStructType == "gig::File") { |
459 |
// resume all previously suspended engines |
460 |
ResumeAllEngines(); |
461 |
} else if (sStructType == "gig::Instrument") { |
462 |
// resume all previously suspended engines |
463 |
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") { |
487 |
// advice the engines to resume the given region, that is to |
488 |
// using it for playback again |
489 |
::gig::Region* pRegion = (::gig::Region*) pStruct; |
490 |
::gig::Instrument* pInstrument = |
491 |
(::gig::Instrument*) pRegion->GetParent(); |
492 |
Lock(); |
493 |
std::set<Engine*> engines = |
494 |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
495 |
std::set<Engine*>::iterator iter = engines.begin(); |
496 |
std::set<Engine*>::iterator end = engines.end(); |
497 |
for (; iter != end; ++iter) (*iter)->Resume(pRegion); |
498 |
Unlock(); |
499 |
} else if (sStructType == "gig::DimensionRegion") { |
500 |
// advice the engines to resume the given DimensionRegion's parent |
501 |
// region, that is to using it for playback again |
502 |
::gig::DimensionRegion* pDimReg = |
503 |
(::gig::DimensionRegion*) pStruct; |
504 |
::gig::Region* pRegion = pDimReg->GetParent(); |
505 |
::gig::Instrument* pInstrument = |
506 |
(::gig::Instrument*) pRegion->GetParent(); |
507 |
Lock(); |
508 |
std::set<Engine*> engines = |
509 |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
510 |
std::set<Engine*>::iterator iter = engines.begin(); |
511 |
std::set<Engine*>::iterator end = engines.end(); |
512 |
for (; iter != end; ++iter) (*iter)->Resume(pRegion); |
513 |
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 { |
529 |
std::cerr << "gig::InstrumentResourceManager: ERROR, unknown data " |
530 |
"structure '" << sStructType << "' requested to be " |
531 |
"resumed by instrument editor. This is a bug!\n" |
532 |
<< std::flush; |
533 |
//TODO: we should inform the instrument editor that something seriously went wrong |
534 |
} |
535 |
} |
536 |
|
537 |
void InstrumentResourceManager::OnSampleReferenceChanged(void* pOldSample, void* pNewSample, InstrumentEditor* pSender) { |
538 |
// uncache old sample in case it's not used by anybody anymore |
539 |
if (pOldSample) { |
540 |
Lock(); |
541 |
::gig::Sample* pSample = (::gig::Sample*) pOldSample; |
542 |
::gig::File* pFile = (::gig::File*) pSample->GetParent(); |
543 |
bool bSampleStillInUse = false; |
544 |
std::vector< ::gig::Instrument*> instruments = |
545 |
GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/); |
546 |
for (int i = 0; i < instruments.size(); i++) { |
547 |
if (SampleReferencedByInstrument(pSample, instruments[i])) { |
548 |
bSampleStillInUse = true; |
549 |
break; |
550 |
} |
551 |
} |
552 |
if (!bSampleStillInUse) UncacheInitialSamples(pSample); |
553 |
Unlock(); |
554 |
} |
555 |
// make sure new sample reference is cached |
556 |
if (pNewSample) { |
557 |
Lock(); |
558 |
::gig::Sample* pSample = (::gig::Sample*) pNewSample; |
559 |
::gig::File* pFile = (::gig::File*) pSample->GetParent(); |
560 |
// get all engines that use that same gig::File |
561 |
std::set<Engine*> engines = GetEnginesUsing(pFile, false/*don't lock again*/); |
562 |
std::set<Engine*>::iterator iter = engines.begin(); |
563 |
std::set<Engine*>::iterator end = engines.end(); |
564 |
for (; iter != end; ++iter) |
565 |
CacheInitialSamples(pSample, *iter); |
566 |
Unlock(); |
567 |
} |
568 |
} |
569 |
|
570 |
::gig::Instrument* InstrumentResourceManager::Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg) { |
571 |
// get gig file from internal gig file manager |
572 |
::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 |
575 |
progress_callback_arg_t callbackArg; |
576 |
callbackArg.pManager = this; |
577 |
callbackArg.pInstrumentKey = &Key; |
578 |
|
579 |
::gig::progress_t progress; |
580 |
progress.callback = OnInstrumentLoadingProgress; |
581 |
progress.custom = &callbackArg; |
582 |
|
583 |
dmsg(1,("Loading gig instrument ('%s',%d)...",Key.FileName.c_str(),Key.Index)); |
584 |
::gig::Instrument* pInstrument = pGig->GetInstrument(Key.Index, &progress); |
585 |
if (!pInstrument) { |
586 |
std::stringstream msg; |
587 |
msg << "There's no instrument with index " << Key.Index << "."; |
588 |
throw InstrumentManagerException(msg.str()); |
589 |
} |
590 |
pGig->GetFirstSample(); // just to force complete instrument loading |
591 |
dmsg(1,("OK\n")); |
592 |
|
593 |
uint maxSamplesPerCycle = GetMaxSamplesPerCycle(pConsumer); |
594 |
|
595 |
// cache initial samples points (for actually needed samples) |
596 |
dmsg(1,("Caching initial samples...")); |
597 |
uint iRegion = 0; // just for progress calculation |
598 |
::gig::Region* pRgn = pInstrument->GetFirstRegion(); |
599 |
while (pRgn) { |
600 |
// we randomly schedule 90% for the .gig file loading and the remaining 10% now for sample caching |
601 |
const float localProgress = 0.9f + 0.1f * (float) iRegion / (float) pInstrument->Regions; |
602 |
DispatchResourceProgressEvent(Key, localProgress); |
603 |
|
604 |
if (pRgn->GetSample() && !pRgn->GetSample()->GetCache().Size) { |
605 |
dmsg(2,("C")); |
606 |
CacheInitialSamples(pRgn->GetSample(), maxSamplesPerCycle); |
607 |
} |
608 |
for (uint i = 0; i < pRgn->DimensionRegions; i++) { |
609 |
CacheInitialSamples(pRgn->pDimensionRegions[i]->pSample, maxSamplesPerCycle); |
610 |
} |
611 |
|
612 |
pRgn = pInstrument->GetNextRegion(); |
613 |
iRegion++; |
614 |
} |
615 |
dmsg(1,("OK\n")); |
616 |
DispatchResourceProgressEvent(Key, 1.0f); // done; notify all consumers about progress 100% |
617 |
|
618 |
// we need the following for destruction later |
619 |
instr_entry_t* pEntry = new instr_entry_t; |
620 |
pEntry->ID.FileName = Key.FileName; |
621 |
pEntry->ID.Index = Key.Index; |
622 |
pEntry->pFile = pGig; |
623 |
|
624 |
// and we save this to check if we need to reallocate for an engine with higher value of 'MaxSamplesPerSecond' |
625 |
pEntry->MaxSamplesPerCycle = maxSamplesPerCycle; |
626 |
|
627 |
pArg = pEntry; |
628 |
|
629 |
return pInstrument; |
630 |
} |
631 |
|
632 |
void InstrumentResourceManager::Destroy(::gig::Instrument* pResource, void* pArg) { |
633 |
instr_entry_t* pEntry = (instr_entry_t*) pArg; |
634 |
// we don't need the .gig file here anymore |
635 |
Gigs.HandBack(pEntry->pFile, reinterpret_cast<GigConsumer*>(pEntry->ID.Index)); // conversion kinda hackish :/ |
636 |
delete pEntry; |
637 |
} |
638 |
|
639 |
void InstrumentResourceManager::DeleteRegionIfNotUsed(::gig::DimensionRegion* pRegion, region_info_t* pRegInfo) { |
640 |
// TODO: we could delete Region and Instrument here if they have become unused |
641 |
} |
642 |
|
643 |
void InstrumentResourceManager::DeleteSampleIfNotUsed(::gig::Sample* pSample, region_info_t* pRegInfo) { |
644 |
::gig::File* gig = pRegInfo->file; |
645 |
::RIFF::File* riff = static_cast< ::RIFF::File*>(pRegInfo->pArg); |
646 |
if (gig) { |
647 |
gig->DeleteSample(pSample); |
648 |
if (!gig->GetFirstSample()) { |
649 |
dmsg(2,("No more samples in use - freeing gig\n")); |
650 |
delete gig; |
651 |
delete riff; |
652 |
} |
653 |
} |
654 |
} |
655 |
|
656 |
/** |
657 |
* Just a wrapper around the other @c CacheInitialSamples() method. |
658 |
* |
659 |
* @param pSample - points to the sample to be cached |
660 |
* @param pEngine - pointer to Gig Engine Channel which caused this call |
661 |
* (may be NULL, in this case default amount of samples |
662 |
* will be cached) |
663 |
*/ |
664 |
void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, EngineChannel* pEngineChannel) { |
665 |
Engine* pEngine = |
666 |
(pEngineChannel && pEngineChannel->GetEngine()) ? |
667 |
dynamic_cast<Engine*>(pEngineChannel->GetEngine()) : NULL; |
668 |
CacheInitialSamples(pSample, pEngine); |
669 |
} |
670 |
|
671 |
/** |
672 |
* Caches a certain size at the beginning of the given sample in RAM. If the |
673 |
* sample is very short, the whole sample will be loaded into RAM and thus |
674 |
* no disk streaming is needed for this sample. Caching an initial part of |
675 |
* samples is needed to compensate disk reading latency. |
676 |
* |
677 |
* @param pSample - points to the sample to be cached |
678 |
* @param pEngine - pointer to Gig Engine which caused this call |
679 |
* (may be NULL, in this case default amount of samples |
680 |
* will be cached) |
681 |
*/ |
682 |
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) { |
691 |
dmsg(4,("gig::InstrumentResourceManager: Skipping sample (pSample == NULL)\n")); |
692 |
return; |
693 |
} |
694 |
if (!pSample->SamplesTotal) return; // skip zero size samples |
695 |
|
696 |
if (pSample->SamplesTotal <= CONFIG_PRELOAD_SAMPLES) { |
697 |
// Sample is too short for disk streaming, so we load the whole |
698 |
// sample into RAM and place 'pAudioIO->FragmentSize << CONFIG_MAX_PITCH' |
699 |
// 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 |
701 |
// the sample. |
702 |
const uint neededSilenceSamples = (maxSamplesPerCycle << CONFIG_MAX_PITCH) + 3; |
703 |
const uint currentlyCachedSilenceSamples = pSample->GetCache().NullExtensionSize / pSample->FrameSize; |
704 |
if (currentlyCachedSilenceSamples < neededSilenceSamples) { |
705 |
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); |
707 |
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 |
711 |
if (!pSample->GetCache().Size) pSample->LoadSampleData(CONFIG_PRELOAD_SAMPLES); |
712 |
} |
713 |
|
714 |
if (!pSample->GetCache().Size) std::cerr << "Unable to cache sample - maybe memory full!" << std::endl << std::flush; |
715 |
} |
716 |
|
717 |
void InstrumentResourceManager::UncacheInitialSamples(::gig::Sample* pSample) { |
718 |
dmsg(1,("Uncaching sample %p\n",(void*)pSample)); |
719 |
if (pSample->GetCache().Size) pSample->ReleaseSampleData(); |
720 |
} |
721 |
|
722 |
/** |
723 |
* Returns a list with all instruments currently in use, that are part of |
724 |
* the given file. |
725 |
* |
726 |
* @param pFile - search criteria |
727 |
* @param bLock - whether we should lock (mutex) the instrument manager |
728 |
* during this call and unlock at the end of this call |
729 |
*/ |
730 |
std::vector< ::gig::Instrument*> InstrumentResourceManager::GetInstrumentsCurrentlyUsedOf(::gig::File* pFile, bool bLock) { |
731 |
if (bLock) Lock(); |
732 |
std::vector< ::gig::Instrument*> result; |
733 |
std::vector< ::gig::Instrument*> allInstruments = Resources(false/*don't lock again*/); |
734 |
for (int i = 0; i < allInstruments.size(); i++) |
735 |
if ( |
736 |
(::gig::File*) allInstruments[i]->GetParent() |
737 |
== pFile |
738 |
) result.push_back(allInstruments[i]); |
739 |
if (bLock) Unlock(); |
740 |
return result; |
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 |
791 |
* instrument. |
792 |
* |
793 |
* @param pInstrument - search criteria |
794 |
* @param bLock - whether we should lock (mutex) the instrument manager |
795 |
* during this call and unlock at the end of this call |
796 |
*/ |
797 |
std::set<Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::Instrument* pInstrument, bool bLock) { |
798 |
if (bLock) Lock(); |
799 |
std::set<Engine*> result; |
800 |
std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument); |
801 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin(); |
802 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end = consumers.end(); |
803 |
for (; iter != end; ++iter) { |
804 |
EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(*iter); |
805 |
if (!pEngineChannel) continue; |
806 |
Engine* pEngine = dynamic_cast<Engine*>(pEngineChannel->GetEngine()); |
807 |
if (!pEngine) continue; |
808 |
result.insert(pEngine); |
809 |
} |
810 |
if (bLock) Unlock(); |
811 |
return result; |
812 |
} |
813 |
|
814 |
/** |
815 |
* Returns a list with all gig Engines that are currently using an |
816 |
* instrument that is part of the given instrument file. |
817 |
* |
818 |
* @param pFile - search criteria |
819 |
* @param bLock - whether we should lock (mutex) the instrument manager |
820 |
* during this call and unlock at the end of this call |
821 |
*/ |
822 |
std::set<Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::File* pFile, bool bLock) { |
823 |
if (bLock) Lock(); |
824 |
// get all instruments (currently in usage) that use that same gig::File |
825 |
std::vector< ::gig::Instrument*> instrumentsOfInterest = |
826 |
GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/); |
827 |
|
828 |
// get all engines that use that same gig::File |
829 |
std::set<Engine*> result; |
830 |
{ |
831 |
for (int i = 0; i < instrumentsOfInterest.size(); i++) { |
832 |
std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(instrumentsOfInterest[i]); |
833 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin(); |
834 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end = consumers.end(); |
835 |
for (; iter != end; ++iter) { |
836 |
EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(*iter); |
837 |
if (!pEngineChannel) continue; |
838 |
Engine* pEngine = dynamic_cast<Engine*>(pEngineChannel->GetEngine()); |
839 |
if (!pEngine) continue; |
840 |
// the unique, sorted container std::set makes |
841 |
// sure we won't have duplicates |
842 |
result.insert(pEngine); |
843 |
} |
844 |
} |
845 |
} |
846 |
if (bLock) Unlock(); |
847 |
return result; |
848 |
} |
849 |
|
850 |
/** |
851 |
* Returns @c true in case the given sample is referenced somewhere by the |
852 |
* given instrument, @c false otherwise. |
853 |
* |
854 |
* @param pSample - sample reference |
855 |
* @param pInstrument - instrument that might use that sample |
856 |
*/ |
857 |
bool InstrumentResourceManager::SampleReferencedByInstrument(::gig::Sample* pSample, ::gig::Instrument* pInstrument) { |
858 |
for ( |
859 |
::gig::Region* pRegion = pInstrument->GetFirstRegion(); |
860 |
pRegion; pRegion = pInstrument->GetNextRegion() |
861 |
) { |
862 |
for ( |
863 |
int i = 0; i < pRegion->DimensionRegions && |
864 |
pRegion->pDimensionRegions[i]; i++ |
865 |
) { |
866 |
if (pRegion->pDimensionRegions[i]->pSample == pSample) |
867 |
return true; |
868 |
} |
869 |
} |
870 |
return false; |
871 |
} |
872 |
|
873 |
/** |
874 |
* Suspend all gig engines that use the given instrument. This means |
875 |
* completely stopping playback on those engines and killing all their |
876 |
* voices and disk streams. This method will block until all voices AND |
877 |
* their disk streams are finally deleted and the engine turned into a |
878 |
* complete idle loop. |
879 |
* |
880 |
* All @c SuspendEnginesUsing() methods only serve one thread by one and |
881 |
* block all other threads until the current active thread called |
882 |
* @c ResumeAllEngines() . |
883 |
* |
884 |
* @param pInstrument - search criteria |
885 |
*/ |
886 |
void InstrumentResourceManager::SuspendEnginesUsing(::gig::Instrument* pInstrument) { |
887 |
// make sure no other thread suspends whole engines at the same time |
888 |
suspendedEnginesMutex.Lock(); |
889 |
// get all engines that use that same gig::Instrument |
890 |
suspendedEngines = GetEnginesUsing(pInstrument, true/*lock*/); |
891 |
// finally, completely suspend all engines that use that same gig::Instrument |
892 |
std::set<Engine*>::iterator iter = suspendedEngines.begin(); |
893 |
std::set<Engine*>::iterator end = suspendedEngines.end(); |
894 |
for (; iter != end; ++iter) (*iter)->SuspendAll(); |
895 |
} |
896 |
|
897 |
/** |
898 |
* Suspend all gig engines that use the given instrument file. This means |
899 |
* completely stopping playback on those engines and killing all their |
900 |
* voices and disk streams. This method will block until all voices AND |
901 |
* their disk streams are finally deleted and the engine turned into a |
902 |
* complete idle loop. |
903 |
* |
904 |
* All @c SuspendEnginesUsing() methods only serve one thread by one and |
905 |
* block all other threads until the current active thread called |
906 |
* @c ResumeAllEngines() . |
907 |
* |
908 |
* @param pFile - search criteria |
909 |
*/ |
910 |
void InstrumentResourceManager::SuspendEnginesUsing(::gig::File* pFile) { |
911 |
// make sure no other thread suspends whole engines at the same time |
912 |
suspendedEnginesMutex.Lock(); |
913 |
// get all engines that use that same gig::File |
914 |
suspendedEngines = GetEnginesUsing(pFile, true/*lock*/); |
915 |
// finally, completely suspend all engines that use that same gig::File |
916 |
std::set<Engine*>::iterator iter = suspendedEngines.begin(); |
917 |
std::set<Engine*>::iterator end = suspendedEngines.end(); |
918 |
for (; iter != end; ++iter) (*iter)->SuspendAll(); |
919 |
} |
920 |
|
921 |
/** |
922 |
* MUST be called after one called one of the @c SuspendEnginesUsing() |
923 |
* methods, to resume normal playback on all previously suspended engines. |
924 |
* As it's only possible for one thread to suspend whole engines at the |
925 |
* same time, this method doesn't take any arguments. |
926 |
*/ |
927 |
void InstrumentResourceManager::ResumeAllEngines() { |
928 |
// resume all previously completely suspended engines |
929 |
std::set<Engine*>::iterator iter = suspendedEngines.begin(); |
930 |
std::set<Engine*>::iterator end = suspendedEngines.end(); |
931 |
for (; iter != end; ++iter) (*iter)->ResumeAll(); |
932 |
// no more suspended engines ... |
933 |
suspendedEngines.clear(); |
934 |
// allow another thread to suspend whole engines |
935 |
suspendedEnginesMutex.Unlock(); |
936 |
} |
937 |
|
938 |
|
939 |
|
940 |
// internal gig file manager |
941 |
|
942 |
::gig::File* InstrumentResourceManager::GigResourceManager::Create(String Key, GigConsumer* pConsumer, void*& pArg) { |
943 |
dmsg(1,("Loading gig file \'%s\'...", Key.c_str())); |
944 |
::RIFF::File* pRIFF = new ::RIFF::File(Key); |
945 |
::gig::File* pGig = new ::gig::File(pRIFF); |
946 |
pArg = pRIFF; |
947 |
dmsg(1,("OK\n")); |
948 |
return pGig; |
949 |
} |
950 |
|
951 |
void InstrumentResourceManager::GigResourceManager::Destroy(::gig::File* pResource, void* pArg) { |
952 |
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 |
955 |
// dimension regions and samples may still be in use - these |
956 |
// will be deleted later by the HandBackDimReg function. |
957 |
bool deleteFile = true; |
958 |
::gig::Instrument* nextInstrument; |
959 |
for (::gig::Instrument* instrument = pResource->GetFirstInstrument() ; |
960 |
instrument ; |
961 |
instrument = nextInstrument) { |
962 |
nextInstrument = pResource->GetNextInstrument(); |
963 |
bool deleteInstrument = true; |
964 |
::gig::Region* nextRegion; |
965 |
for (::gig::Region *region = instrument->GetFirstRegion() ; |
966 |
region ; |
967 |
region = nextRegion) { |
968 |
nextRegion = instrument->GetNextRegion(); |
969 |
bool deleteRegion = true; |
970 |
for (int i = 0 ; i < region->DimensionRegions ; i++) |
971 |
{ |
972 |
::gig::DimensionRegion *d = region->pDimensionRegions[i]; |
973 |
std::map< ::gig::DimensionRegion*, region_info_t>::iterator iter = parent->RegionInfo.find(d); |
974 |
if (iter != parent->RegionInfo.end()) { |
975 |
region_info_t& dimRegInfo = (*iter).second; |
976 |
dimRegInfo.file = pResource; |
977 |
dimRegInfo.pArg = (::RIFF::File*)pArg; |
978 |
deleteFile = deleteInstrument = deleteRegion = false; |
979 |
} |
980 |
} |
981 |
if (deleteRegion) instrument->DeleteRegion(region); |
982 |
} |
983 |
if (deleteInstrument) pResource->DeleteInstrument(instrument); |
984 |
} |
985 |
if (deleteFile) { |
986 |
delete pResource; |
987 |
delete (::RIFF::File*) pArg; |
988 |
} else { |
989 |
dmsg(2,("keeping some samples that are in use...")); |
990 |
::gig::Sample* nextSample; |
991 |
for (::gig::Sample* sample = pResource->GetFirstSample() ; |
992 |
sample ; |
993 |
sample = nextSample) { |
994 |
nextSample = pResource->GetNextSample(); |
995 |
if (parent->SampleRefCount.find(sample) == parent->SampleRefCount.end()) { |
996 |
pResource->DeleteSample(sample); |
997 |
} |
998 |
} |
999 |
} |
1000 |
dmsg(1,("OK\n")); |
1001 |
} |
1002 |
|
1003 |
}} // namespace LinuxSampler::gig |