1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 - 2008 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 |
|
28 |
#include "../../common/global_private.h" |
29 |
#include "../../plugins/InstrumentEditorFactory.h" |
30 |
|
31 |
// We need to know the maximum number of sample points which are going to |
32 |
// be processed for each render cycle of the audio output driver, to know |
33 |
// how much initial sample points we need to cache into RAM. If the given |
34 |
// sampler channel does not have an audio output device assigned yet |
35 |
// though, we simply use this default value. |
36 |
#define GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE 128 |
37 |
|
38 |
namespace LinuxSampler { namespace gig { |
39 |
|
40 |
// data stored as long as an instrument resource exists |
41 |
struct instr_entry_t { |
42 |
InstrumentManager::instrument_id_t ID; |
43 |
::gig::File* pGig; |
44 |
uint MaxSamplesPerCycle; ///< if some engine requests an already allocated instrument with a higher value, we have to reallocate the instrument |
45 |
}; |
46 |
|
47 |
// some data needed for the libgig callback function |
48 |
struct progress_callback_arg_t { |
49 |
InstrumentResourceManager* pManager; |
50 |
InstrumentManager::instrument_id_t* pInstrumentKey; |
51 |
}; |
52 |
|
53 |
// we use this to react on events concerning an instrument on behalf of an instrument editor |
54 |
class InstrumentEditorProxy : public InstrumentConsumer { |
55 |
public: |
56 |
virtual void ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) { |
57 |
//TODO: inform the instrument editor about the pending update |
58 |
} |
59 |
|
60 |
virtual void ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) { |
61 |
//TODO:: inform the instrument editor about finished update |
62 |
} |
63 |
|
64 |
virtual void OnResourceProgress(float fProgress) { |
65 |
//TODO: inform the instrument editor about the progress of an update |
66 |
} |
67 |
|
68 |
// the instrument we borrowed on behalf of the editor |
69 |
::gig::Instrument* pInstrument; |
70 |
// the instrument editor we work on behalf |
71 |
InstrumentEditor* pEditor; |
72 |
}; |
73 |
|
74 |
/** |
75 |
* Callback function which will be called by libgig during loading of |
76 |
* instruments to inform about the current progress. Or to be more |
77 |
* specific; it will be called during the GetInstrument() call. |
78 |
* |
79 |
* @param pProgress - contains current progress value, pointer to the |
80 |
* InstrumentResourceManager instance and |
81 |
* instrument ID |
82 |
*/ |
83 |
void InstrumentResourceManager::OnInstrumentLoadingProgress(::gig::progress_t* pProgress) { |
84 |
dmsg(7,("gig::InstrumentResourceManager: progress %f%", pProgress->factor)); |
85 |
progress_callback_arg_t* pArg = static_cast<progress_callback_arg_t*>(pProgress->custom); |
86 |
// we randomly schedule 90% for the .gig file loading and the remaining 10% later for sample caching |
87 |
const float localProgress = 0.9f * pProgress->factor; |
88 |
pArg->pManager->DispatchResourceProgressEvent(*pArg->pInstrumentKey, localProgress); |
89 |
} |
90 |
|
91 |
std::vector<InstrumentResourceManager::instrument_id_t> InstrumentResourceManager::Instruments() { |
92 |
return Entries(); |
93 |
} |
94 |
|
95 |
InstrumentManager::mode_t InstrumentResourceManager::GetMode(const instrument_id_t& ID) { |
96 |
return static_cast<InstrumentManager::mode_t>(AvailabilityMode(ID)); |
97 |
} |
98 |
|
99 |
void InstrumentResourceManager::SetMode(const instrument_id_t& ID, InstrumentManager::mode_t Mode) { |
100 |
dmsg(2,("gig::InstrumentResourceManager: setting mode for %s (Index=%d) to %d\n",ID.FileName.c_str(),ID.Index,Mode)); |
101 |
SetAvailabilityMode(ID, static_cast<ResourceManager<InstrumentManager::instrument_id_t, ::gig::Instrument>::mode_t>(Mode)); |
102 |
} |
103 |
|
104 |
String InstrumentResourceManager::GetInstrumentName(instrument_id_t ID) { |
105 |
Lock(); |
106 |
::gig::Instrument* pInstrument = Resource(ID, false); |
107 |
String res = (pInstrument) ? pInstrument->pInfo->Name : ""; |
108 |
Unlock(); |
109 |
return res; |
110 |
} |
111 |
|
112 |
String InstrumentResourceManager::GetInstrumentDataStructureName(instrument_id_t ID) { |
113 |
return ::gig::libraryName(); |
114 |
} |
115 |
|
116 |
String InstrumentResourceManager::GetInstrumentDataStructureVersion(instrument_id_t ID) { |
117 |
return ::gig::libraryVersion(); |
118 |
} |
119 |
|
120 |
std::vector<InstrumentResourceManager::instrument_id_t> InstrumentResourceManager::GetInstrumentFileContent(String File) throw (InstrumentManagerException) { |
121 |
::RIFF::File* riff = NULL; |
122 |
::gig::File* gig = NULL; |
123 |
try { |
124 |
std::vector<instrument_id_t> result; |
125 |
riff = new ::RIFF::File(File); |
126 |
gig = new ::gig::File(riff); |
127 |
gig->SetAutoLoad(false); // avoid time consuming samples scanning |
128 |
for (int i = 0; gig->GetInstrument(i); i++) { |
129 |
instrument_id_t id; |
130 |
id.FileName = File; |
131 |
id.Index = i; |
132 |
result.push_back(id); |
133 |
} |
134 |
if (gig) delete gig; |
135 |
if (riff) delete riff; |
136 |
return result; |
137 |
} catch (::RIFF::Exception e) { |
138 |
if (gig) delete gig; |
139 |
if (riff) delete riff; |
140 |
throw InstrumentManagerException(e.Message); |
141 |
} catch (...) { |
142 |
if (gig) delete gig; |
143 |
if (riff) delete riff; |
144 |
throw InstrumentManagerException("Unknown exception while trying to parse '" + File + "'"); |
145 |
} |
146 |
} |
147 |
|
148 |
InstrumentResourceManager::instrument_info_t InstrumentResourceManager::GetInstrumentInfo(instrument_id_t ID) throw (InstrumentManagerException) { |
149 |
Lock(); |
150 |
::gig::Instrument* pInstrument = Resource(ID, false); |
151 |
bool loaded = (pInstrument != NULL); |
152 |
if (!loaded) Unlock(); |
153 |
|
154 |
::RIFF::File* riff = NULL; |
155 |
::gig::File* gig = NULL; |
156 |
try { |
157 |
if(!loaded) { |
158 |
riff = new ::RIFF::File(ID.FileName); |
159 |
gig = new ::gig::File(riff); |
160 |
gig->SetAutoLoad(false); // avoid time consuming samples scanning |
161 |
pInstrument = gig->GetInstrument(ID.Index); |
162 |
} |
163 |
|
164 |
if (!pInstrument) throw InstrumentManagerException("There is no instrument " + ToString(ID.Index) + " in " + ID.FileName); |
165 |
|
166 |
instrument_info_t info; |
167 |
for (int i = 0; i < 128; i++) { info.KeyBindings[i] = info.KeySwitchBindings[i] = 0; } |
168 |
|
169 |
::gig::File* pFile = (::gig::File*) pInstrument->GetParent(); |
170 |
|
171 |
if (pFile->pVersion) { |
172 |
info.FormatVersion = ToString(pFile->pVersion->major); |
173 |
info.Product = pFile->pInfo->Product; |
174 |
info.Artists = pFile->pInfo->Artists; |
175 |
} |
176 |
|
177 |
info.InstrumentName = pInstrument->pInfo->Name; |
178 |
|
179 |
::gig::Region* pRegion = pInstrument->GetFirstRegion(); |
180 |
while (pRegion) { |
181 |
int low = pRegion->KeyRange.low; |
182 |
int high = pRegion->KeyRange.high; |
183 |
if (low < 0 || low > 127 || high < 0 || high > 127 || low > high) { |
184 |
std::cerr << "Invalid key range: " << low << " - " << high << std::endl; |
185 |
} else { |
186 |
for (int i = low; i <= high; i++) info.KeyBindings[i] = 1; |
187 |
} |
188 |
|
189 |
pRegion = pInstrument->GetNextRegion(); |
190 |
} |
191 |
|
192 |
if (loaded) { // retrieve keyswitching only if the instrument is fully loaded. |
193 |
|
194 |
// only return keyswitch range if keyswitching is used |
195 |
bool hasKeyswitches = false; |
196 |
for (::gig::Region* pRegion = pInstrument->GetFirstRegion() ; |
197 |
pRegion && !hasKeyswitches ; |
198 |
pRegion = pInstrument->GetNextRegion()) { |
199 |
for (int i = 0 ; i < pRegion->Dimensions ; i++) { |
200 |
if (pRegion->pDimensionDefinitions[i].dimension == ::gig::dimension_keyboard) { |
201 |
hasKeyswitches = true; |
202 |
break; |
203 |
} |
204 |
} |
205 |
} |
206 |
|
207 |
if (hasKeyswitches) { |
208 |
int low = pInstrument->DimensionKeyRange.low; |
209 |
int high = pInstrument->DimensionKeyRange.high; |
210 |
if (low < 0 || low > 127 || high < 0 || high > 127 || low > high) { |
211 |
std::cerr << "Invalid keyswitch range: " << low << " - " << high << std::endl; |
212 |
} else { |
213 |
for (int i = low; i <= high; i++) info.KeySwitchBindings[i] = 1; |
214 |
} |
215 |
} |
216 |
} |
217 |
|
218 |
if (loaded) Unlock(); |
219 |
|
220 |
if (gig) delete gig; |
221 |
if (riff) delete riff; |
222 |
return info; |
223 |
} catch (::RIFF::Exception e) { |
224 |
if (loaded) Unlock(); |
225 |
if (gig) delete gig; |
226 |
if (riff) delete riff; |
227 |
throw InstrumentManagerException(e.Message); |
228 |
} catch (...) { |
229 |
if (loaded) Unlock(); |
230 |
if (gig) delete gig; |
231 |
if (riff) delete riff; |
232 |
throw InstrumentManagerException("Unknown exception while trying to parse '" + ID.FileName + "'"); |
233 |
} |
234 |
} |
235 |
|
236 |
void InstrumentResourceManager::LaunchInstrumentEditor(instrument_id_t ID) throw (InstrumentManagerException) { |
237 |
const String sDataType = GetInstrumentDataStructureName(ID); |
238 |
const String sDataVersion = GetInstrumentDataStructureVersion(ID); |
239 |
// find instrument editors capable to handle given instrument |
240 |
std::vector<String> vEditors = |
241 |
InstrumentEditorFactory::MatchingEditors(sDataType, sDataVersion); |
242 |
if (!vEditors.size()) |
243 |
throw InstrumentManagerException( |
244 |
"There is no instrument editor capable to handle this instrument" |
245 |
); |
246 |
// simply use the first editor in the result set |
247 |
dmsg(1,("Found matching editor '%s' for instrument ('%s', %d) having data structure ('%s','%s')\n", |
248 |
vEditors[0].c_str(), ID.FileName.c_str(), ID.Index, sDataType.c_str(), sDataVersion.c_str())); |
249 |
InstrumentEditor* pEditor = InstrumentEditorFactory::Create(vEditors[0]); |
250 |
// register for receiving notifications from the instrument editor |
251 |
pEditor->AddListener(this); |
252 |
// create a proxy that reacts on notification on behalf of the editor |
253 |
InstrumentEditorProxy* pProxy = new InstrumentEditorProxy; |
254 |
// borrow the instrument on behalf of the instrument editor |
255 |
::gig::Instrument* pInstrument = Borrow(ID, pProxy); |
256 |
// remember the proxy and instrument for this instrument editor |
257 |
pProxy->pInstrument = pInstrument; |
258 |
pProxy->pEditor = pEditor; |
259 |
InstrumentEditorProxiesMutex.Lock(); |
260 |
InstrumentEditorProxies.add(pProxy); |
261 |
InstrumentEditorProxiesMutex.Unlock(); |
262 |
// launch the instrument editor for the given instrument |
263 |
pEditor->Launch(pInstrument, sDataType, sDataVersion); |
264 |
|
265 |
// register the instrument editor as virtual MIDI device as well ... |
266 |
VirtualMidiDevice* pVirtualMidiDevice = |
267 |
dynamic_cast<VirtualMidiDevice*>(pEditor); |
268 |
if (!pVirtualMidiDevice) { |
269 |
std::cerr << "Instrument editor not a virtual MIDI device\n" << std::flush; |
270 |
return; |
271 |
} |
272 |
// 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 ? ) |
273 |
Lock(); |
274 |
std::set<gig::EngineChannel*> engineChannels = |
275 |
GetEngineChannelsUsing(pInstrument, false/*don't lock again*/); |
276 |
std::set<gig::EngineChannel*>::iterator iter = engineChannels.begin(); |
277 |
std::set<gig::EngineChannel*>::iterator end = engineChannels.end(); |
278 |
for (; iter != end; ++iter) (*iter)->Connect(pVirtualMidiDevice); |
279 |
Unlock(); |
280 |
} |
281 |
|
282 |
/** |
283 |
* Will be called by the respective instrument editor once it left its |
284 |
* Main() loop. That way we can handle cleanup before its thread finally |
285 |
* dies. |
286 |
* |
287 |
* @param pSender - instrument editor that stops execution |
288 |
*/ |
289 |
void InstrumentResourceManager::OnInstrumentEditorQuit(InstrumentEditor* pSender) { |
290 |
dmsg(1,("InstrumentResourceManager: instrument editor quit, doing cleanup\n")); |
291 |
|
292 |
::gig::Instrument* pInstrument = NULL; |
293 |
InstrumentEditorProxy* pProxy = NULL; |
294 |
int iProxyIndex = -1; |
295 |
|
296 |
// first find the editor proxy entry for this editor |
297 |
InstrumentEditorProxiesMutex.Lock(); |
298 |
for (int i = 0; i < InstrumentEditorProxies.size(); i++) { |
299 |
InstrumentEditorProxy* pCurProxy = |
300 |
dynamic_cast<InstrumentEditorProxy*>( |
301 |
InstrumentEditorProxies[i] |
302 |
); |
303 |
if (pCurProxy->pEditor == pSender) { |
304 |
pProxy = pCurProxy; |
305 |
iProxyIndex = i; |
306 |
pInstrument = pCurProxy->pInstrument; |
307 |
} |
308 |
} |
309 |
InstrumentEditorProxiesMutex.Unlock(); |
310 |
|
311 |
if (!pProxy) { |
312 |
std::cerr << "Eeeek, could not find instrument editor proxy, " |
313 |
"this is a bug!\n" << std::flush; |
314 |
return; |
315 |
} |
316 |
|
317 |
// now unregister editor as not being available as a virtual MIDI device anymore |
318 |
VirtualMidiDevice* pVirtualMidiDevice = |
319 |
dynamic_cast<VirtualMidiDevice*>(pSender); |
320 |
if (pVirtualMidiDevice) { |
321 |
Lock(); |
322 |
// NOTE: see note in LaunchInstrumentEditor() |
323 |
std::set<gig::EngineChannel*> engineChannels = |
324 |
GetEngineChannelsUsing(pInstrument, false/*don't lock again*/); |
325 |
std::set<gig::EngineChannel*>::iterator iter = engineChannels.begin(); |
326 |
std::set<gig::EngineChannel*>::iterator end = engineChannels.end(); |
327 |
for (; iter != end; ++iter) (*iter)->Disconnect(pVirtualMidiDevice); |
328 |
Unlock(); |
329 |
} else { |
330 |
std::cerr << "Could not unregister editor as not longer acting as " |
331 |
"virtual MIDI device. Wasn't it registered?\n" |
332 |
<< std::flush; |
333 |
} |
334 |
|
335 |
// finally delete proxy entry and hand back instrument |
336 |
if (pInstrument) { |
337 |
InstrumentEditorProxiesMutex.Lock(); |
338 |
InstrumentEditorProxies.remove(iProxyIndex); |
339 |
InstrumentEditorProxiesMutex.Unlock(); |
340 |
|
341 |
HandBack(pInstrument, pProxy); |
342 |
delete pProxy; |
343 |
} |
344 |
|
345 |
// Note that we don't need to free the editor here. As it |
346 |
// derives from Thread, it will delete itself when the thread |
347 |
// dies. |
348 |
} |
349 |
|
350 |
#if 0 // currently unused : |
351 |
/** |
352 |
* Try to inform the respective instrument editor(s), that a note on |
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::TrySendNoteOnToEditors(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->SendNoteOnToDevice(Key, Velocity); |
368 |
} |
369 |
InstrumentEditorProxiesMutex.Unlock(); // naively assumes RT safe implementation |
370 |
} |
371 |
|
372 |
/** |
373 |
* Try to inform the respective instrument editor(s), that a note off |
374 |
* event just occured. This method is called by the MIDI thread. If any |
375 |
* obstacles are in the way (e.g. if a wait for an unlock would be |
376 |
* required) we give up immediately, since the RT safeness of the MIDI |
377 |
* thread has absolute priority. |
378 |
*/ |
379 |
void InstrumentResourceManager::TrySendNoteOffToEditors(uint8_t Key, uint8_t Velocity, ::gig::Instrument* pInstrument) { |
380 |
const bool bGotLock = InstrumentEditorProxiesMutex.Trylock(); // naively assumes RT safe implementation |
381 |
if (!bGotLock) return; // hell, forget it, not worth the hassle |
382 |
for (int i = 0; i < InstrumentEditorProxies.size(); i++) { |
383 |
InstrumentEditorProxy* pProxy = |
384 |
dynamic_cast<InstrumentEditorProxy*>( |
385 |
InstrumentEditorProxies[i] |
386 |
); |
387 |
if (pProxy->pInstrument == pInstrument) |
388 |
pProxy->pEditor->SendNoteOffToDevice(Key, Velocity); |
389 |
} |
390 |
InstrumentEditorProxiesMutex.Unlock(); // naively assumes RT safe implementation |
391 |
} |
392 |
#endif // unused |
393 |
|
394 |
void InstrumentResourceManager::OnSamplesToBeRemoved(std::set<void*> Samples, InstrumentEditor* pSender) { |
395 |
if (Samples.empty()) { |
396 |
std::cerr << "gig::InstrumentResourceManager: WARNING, " |
397 |
"OnSamplesToBeRemoved() called with empty list, this " |
398 |
"is a bug!\n" << std::flush; |
399 |
return; |
400 |
} |
401 |
// TODO: ATM we assume here that all samples are from the same file |
402 |
::gig::Sample* pFirstSample = (::gig::Sample*) *Samples.begin(); |
403 |
::gig::File* pCriticalFile = dynamic_cast< ::gig::File*>(pFirstSample->GetParent()); |
404 |
// completely suspend all engines that use that same file |
405 |
SuspendEnginesUsing(pCriticalFile); |
406 |
} |
407 |
|
408 |
void InstrumentResourceManager::OnSamplesRemoved(InstrumentEditor* pSender) { |
409 |
// resume all previously, completely suspended engines |
410 |
// (we don't have to un-cache the removed samples here, since that is |
411 |
// automatically done by the gig::Sample destructor) |
412 |
ResumeAllEngines(); |
413 |
} |
414 |
|
415 |
void InstrumentResourceManager::OnDataStructureToBeChanged(void* pStruct, String sStructType, InstrumentEditor* pSender) { |
416 |
//TODO: remove code duplication |
417 |
if (sStructType == "gig::File") { |
418 |
// completely suspend all engines that use that file |
419 |
::gig::File* pFile = (::gig::File*) pStruct; |
420 |
SuspendEnginesUsing(pFile); |
421 |
} else if (sStructType == "gig::Instrument") { |
422 |
// completely suspend all engines that use that instrument |
423 |
::gig::Instrument* pInstrument = (::gig::Instrument*) pStruct; |
424 |
SuspendEnginesUsing(pInstrument); |
425 |
} else if (sStructType == "gig::Region") { |
426 |
// only advice the engines to suspend the given region, so they'll |
427 |
// only ignore that region (and probably already other suspended |
428 |
// ones), but beside that continue normal playback |
429 |
::gig::Region* pRegion = (::gig::Region*) pStruct; |
430 |
::gig::Instrument* pInstrument = |
431 |
(::gig::Instrument*) pRegion->GetParent(); |
432 |
Lock(); |
433 |
std::set<gig::Engine*> engines = |
434 |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
435 |
std::set<gig::Engine*>::iterator iter = engines.begin(); |
436 |
std::set<gig::Engine*>::iterator end = engines.end(); |
437 |
for (; iter != end; ++iter) (*iter)->Suspend(pRegion); |
438 |
Unlock(); |
439 |
} else if (sStructType == "gig::DimensionRegion") { |
440 |
// only advice the engines to suspend the given DimensionRegions's |
441 |
// parent region, so they'll only ignore that region (and probably |
442 |
// already other suspended ones), but beside that continue normal |
443 |
// playback |
444 |
::gig::DimensionRegion* pDimReg = |
445 |
(::gig::DimensionRegion*) pStruct; |
446 |
::gig::Region* pRegion = pDimReg->GetParent(); |
447 |
::gig::Instrument* pInstrument = |
448 |
(::gig::Instrument*) pRegion->GetParent(); |
449 |
Lock(); |
450 |
std::set<gig::Engine*> engines = |
451 |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
452 |
std::set<gig::Engine*>::iterator iter = engines.begin(); |
453 |
std::set<gig::Engine*>::iterator end = engines.end(); |
454 |
for (; iter != end; ++iter) (*iter)->Suspend(pRegion); |
455 |
Unlock(); |
456 |
} else { |
457 |
std::cerr << "gig::InstrumentResourceManager: ERROR, unknown data " |
458 |
"structure '" << sStructType << "' requested to be " |
459 |
"suspended by instrument editor. This is a bug!\n" |
460 |
<< std::flush; |
461 |
//TODO: we should inform the instrument editor that something seriously went wrong |
462 |
} |
463 |
} |
464 |
|
465 |
void InstrumentResourceManager::OnDataStructureChanged(void* pStruct, String sStructType, InstrumentEditor* pSender) { |
466 |
//TODO: remove code duplication |
467 |
if (sStructType == "gig::File") { |
468 |
// resume all previously suspended engines |
469 |
ResumeAllEngines(); |
470 |
} else if (sStructType == "gig::Instrument") { |
471 |
// resume all previously suspended engines |
472 |
ResumeAllEngines(); |
473 |
} else if (sStructType == "gig::Region") { |
474 |
// advice the engines to resume the given region, that is to |
475 |
// using it for playback again |
476 |
::gig::Region* pRegion = (::gig::Region*) pStruct; |
477 |
::gig::Instrument* pInstrument = |
478 |
(::gig::Instrument*) pRegion->GetParent(); |
479 |
Lock(); |
480 |
std::set<gig::Engine*> engines = |
481 |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
482 |
std::set<gig::Engine*>::iterator iter = engines.begin(); |
483 |
std::set<gig::Engine*>::iterator end = engines.end(); |
484 |
for (; iter != end; ++iter) (*iter)->Resume(pRegion); |
485 |
Unlock(); |
486 |
} else if (sStructType == "gig::DimensionRegion") { |
487 |
// advice the engines to resume the given DimensionRegion's parent |
488 |
// region, that is to using it for playback again |
489 |
::gig::DimensionRegion* pDimReg = |
490 |
(::gig::DimensionRegion*) pStruct; |
491 |
::gig::Region* pRegion = pDimReg->GetParent(); |
492 |
::gig::Instrument* pInstrument = |
493 |
(::gig::Instrument*) pRegion->GetParent(); |
494 |
Lock(); |
495 |
std::set<gig::Engine*> engines = |
496 |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
497 |
std::set<gig::Engine*>::iterator iter = engines.begin(); |
498 |
std::set<gig::Engine*>::iterator end = engines.end(); |
499 |
for (; iter != end; ++iter) (*iter)->Resume(pRegion); |
500 |
Unlock(); |
501 |
} else { |
502 |
std::cerr << "gig::InstrumentResourceManager: ERROR, unknown data " |
503 |
"structure '" << sStructType << "' requested to be " |
504 |
"resumed by instrument editor. This is a bug!\n" |
505 |
<< std::flush; |
506 |
//TODO: we should inform the instrument editor that something seriously went wrong |
507 |
} |
508 |
} |
509 |
|
510 |
void InstrumentResourceManager::OnSampleReferenceChanged(void* pOldSample, void* pNewSample, InstrumentEditor* pSender) { |
511 |
// uncache old sample in case it's not used by anybody anymore |
512 |
if (pOldSample) { |
513 |
Lock(); |
514 |
::gig::Sample* pSample = (::gig::Sample*) pOldSample; |
515 |
::gig::File* pFile = (::gig::File*) pSample->GetParent(); |
516 |
std::vector< ::gig::Instrument*> instruments = |
517 |
GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/); |
518 |
for (int i = 0; i < instruments.size(); i++) |
519 |
if (!SampleReferencedByInstrument(pSample, instruments[i])) |
520 |
UncacheInitialSamples(pSample); |
521 |
Unlock(); |
522 |
} |
523 |
// make sure new sample reference is cached |
524 |
if (pNewSample) { |
525 |
Lock(); |
526 |
::gig::Sample* pSample = (::gig::Sample*) pNewSample; |
527 |
::gig::File* pFile = (::gig::File*) pSample->GetParent(); |
528 |
// get all engines that use that same gig::File |
529 |
std::set<gig::Engine*> engines = GetEnginesUsing(pFile, false/*don't lock again*/); |
530 |
std::set<gig::Engine*>::iterator iter = engines.begin(); |
531 |
std::set<gig::Engine*>::iterator end = engines.end(); |
532 |
for (; iter != end; ++iter) |
533 |
CacheInitialSamples(pSample, *iter); |
534 |
Unlock(); |
535 |
} |
536 |
} |
537 |
|
538 |
::gig::Instrument* InstrumentResourceManager::Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg) { |
539 |
// get gig file from internal gig file manager |
540 |
::gig::File* pGig = Gigs.Borrow(Key.FileName, (GigConsumer*) Key.Index); // conversion kinda hackish :/ |
541 |
|
542 |
// we pass this to the progress callback mechanism of libgig |
543 |
progress_callback_arg_t callbackArg; |
544 |
callbackArg.pManager = this; |
545 |
callbackArg.pInstrumentKey = &Key; |
546 |
|
547 |
::gig::progress_t progress; |
548 |
progress.callback = OnInstrumentLoadingProgress; |
549 |
progress.custom = &callbackArg; |
550 |
|
551 |
dmsg(1,("Loading gig instrument ('%s',%d)...",Key.FileName.c_str(),Key.Index)); |
552 |
::gig::Instrument* pInstrument = pGig->GetInstrument(Key.Index, &progress); |
553 |
if (!pInstrument) { |
554 |
std::stringstream msg; |
555 |
msg << "There's no instrument with index " << Key.Index << "."; |
556 |
throw InstrumentManagerException(msg.str()); |
557 |
} |
558 |
pGig->GetFirstSample(); // just to force complete instrument loading |
559 |
dmsg(1,("OK\n")); |
560 |
|
561 |
// cache initial samples points (for actually needed samples) |
562 |
dmsg(1,("Caching initial samples...")); |
563 |
uint iRegion = 0; // just for progress calculation |
564 |
::gig::Region* pRgn = pInstrument->GetFirstRegion(); |
565 |
while (pRgn) { |
566 |
// we randomly schedule 90% for the .gig file loading and the remaining 10% now for sample caching |
567 |
const float localProgress = 0.9f + 0.1f * (float) iRegion / (float) pInstrument->Regions; |
568 |
DispatchResourceProgressEvent(Key, localProgress); |
569 |
|
570 |
if (pRgn->GetSample() && !pRgn->GetSample()->GetCache().Size) { |
571 |
dmsg(2,("C")); |
572 |
CacheInitialSamples(pRgn->GetSample(), (gig::EngineChannel*) pConsumer); |
573 |
} |
574 |
for (uint i = 0; i < pRgn->DimensionRegions; i++) { |
575 |
CacheInitialSamples(pRgn->pDimensionRegions[i]->pSample, (gig::EngineChannel*) pConsumer); |
576 |
} |
577 |
|
578 |
pRgn = pInstrument->GetNextRegion(); |
579 |
iRegion++; |
580 |
} |
581 |
dmsg(1,("OK\n")); |
582 |
DispatchResourceProgressEvent(Key, 1.0f); // done; notify all consumers about progress 100% |
583 |
|
584 |
// we need the following for destruction later |
585 |
instr_entry_t* pEntry = new instr_entry_t; |
586 |
pEntry->ID.FileName = Key.FileName; |
587 |
pEntry->ID.Index = Key.Index; |
588 |
pEntry->pGig = pGig; |
589 |
|
590 |
gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(pConsumer); |
591 |
// and we save this to check if we need to reallocate for a engine with higher value of 'MaxSamplesPerSecond' |
592 |
pEntry->MaxSamplesPerCycle = |
593 |
(!pEngineChannel) ? 0 /* don't care for instrument editors */ : |
594 |
(pEngineChannel->GetEngine()) ? |
595 |
dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle() |
596 |
: GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE; |
597 |
pArg = pEntry; |
598 |
|
599 |
return pInstrument; |
600 |
} |
601 |
|
602 |
void InstrumentResourceManager::Destroy( ::gig::Instrument* pResource, void* pArg) { |
603 |
instr_entry_t* pEntry = (instr_entry_t*) pArg; |
604 |
// we don't need the .gig file here anymore |
605 |
Gigs.HandBack(pEntry->pGig, (GigConsumer*) pEntry->ID.Index); // conversion kinda hackish :/ |
606 |
delete pEntry; |
607 |
} |
608 |
|
609 |
void InstrumentResourceManager::OnBorrow(::gig::Instrument* pResource, InstrumentConsumer* pConsumer, void*& pArg) { |
610 |
instr_entry_t* pEntry = (instr_entry_t*) pArg; |
611 |
gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(pConsumer); |
612 |
uint maxSamplesPerCycle = |
613 |
(pEngineChannel && pEngineChannel->GetEngine()) ? dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle() |
614 |
: GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE; |
615 |
if (pEntry->MaxSamplesPerCycle < maxSamplesPerCycle) { |
616 |
Update(pResource, pConsumer); |
617 |
} |
618 |
} |
619 |
|
620 |
/** |
621 |
* Give back an instrument. This should be used instead of |
622 |
* HandBack if there are some dimension regions that are still in |
623 |
* use. (When an instrument is changed, the voices currently |
624 |
* playing are allowed to keep playing with the old instrument |
625 |
* until note off arrives. New notes will use the new instrument.) |
626 |
*/ |
627 |
void InstrumentResourceManager::HandBackInstrument(::gig::Instrument* pResource, InstrumentConsumer* pConsumer, |
628 |
RTList< ::gig::DimensionRegion*>* pDimRegionsInUse) { |
629 |
DimRegInfoMutex.Lock(); |
630 |
for (RTList< ::gig::DimensionRegion*>::Iterator i = pDimRegionsInUse->first() ; i != pDimRegionsInUse->end() ; i++) { |
631 |
DimRegInfo[*i].refCount++; |
632 |
SampleRefCount[(*i)->pSample]++; |
633 |
} |
634 |
HandBack(pResource, pConsumer, true); |
635 |
DimRegInfoMutex.Unlock(); |
636 |
} |
637 |
|
638 |
/** |
639 |
* Give back a dimension region that belongs to an instrument that |
640 |
* was previously handed back. |
641 |
*/ |
642 |
void InstrumentResourceManager::HandBackDimReg(::gig::DimensionRegion* pDimReg) { |
643 |
DimRegInfoMutex.Lock(); |
644 |
dimreg_info_t& dimRegInfo = DimRegInfo[pDimReg]; |
645 |
int dimRegRefCount = --dimRegInfo.refCount; |
646 |
int sampleRefCount = --SampleRefCount[pDimReg->pSample]; |
647 |
if (dimRegRefCount == 0) { |
648 |
::gig::File* gig = dimRegInfo.file; |
649 |
::RIFF::File* riff = dimRegInfo.riff; |
650 |
DimRegInfo.erase(pDimReg); |
651 |
// TODO: we could delete Region and Instrument here if |
652 |
// they have become unused |
653 |
|
654 |
if (sampleRefCount == 0) { |
655 |
SampleRefCount.erase(pDimReg->pSample); |
656 |
|
657 |
if (gig) { |
658 |
gig->DeleteSample(pDimReg->pSample); |
659 |
if (!gig->GetFirstSample()) { |
660 |
dmsg(2,("No more samples in use - freeing gig\n")); |
661 |
delete gig; |
662 |
delete riff; |
663 |
} |
664 |
} |
665 |
} |
666 |
} |
667 |
DimRegInfoMutex.Unlock(); |
668 |
} |
669 |
|
670 |
/** |
671 |
* Just a wrapper around the other @c CacheInitialSamples() method. |
672 |
* |
673 |
* @param pSample - points to the sample to be cached |
674 |
* @param pEngine - pointer to Gig Engine Channel which caused this call |
675 |
* (may be NULL, in this case default amount of samples |
676 |
* will be cached) |
677 |
*/ |
678 |
void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, gig::EngineChannel* pEngineChannel) { |
679 |
gig::Engine* pEngine = |
680 |
(pEngineChannel && pEngineChannel->GetEngine()) ? |
681 |
dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine()) : NULL; |
682 |
CacheInitialSamples(pSample, pEngine); |
683 |
} |
684 |
|
685 |
/** |
686 |
* Caches a certain size at the beginning of the given sample in RAM. If the |
687 |
* sample is very short, the whole sample will be loaded into RAM and thus |
688 |
* no disk streaming is needed for this sample. Caching an initial part of |
689 |
* samples is needed to compensate disk reading latency. |
690 |
* |
691 |
* @param pSample - points to the sample to be cached |
692 |
* @param pEngine - pointer to Gig Engine which caused this call |
693 |
* (may be NULL, in this case default amount of samples |
694 |
* will be cached) |
695 |
*/ |
696 |
void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, gig::Engine* pEngine) { |
697 |
if (!pSample) { |
698 |
dmsg(4,("gig::InstrumentResourceManager: Skipping sample (pSample == NULL)\n")); |
699 |
return; |
700 |
} |
701 |
if (!pSample->SamplesTotal) return; // skip zero size samples |
702 |
|
703 |
if (pSample->SamplesTotal <= CONFIG_PRELOAD_SAMPLES) { |
704 |
// Sample is too short for disk streaming, so we load the whole |
705 |
// sample into RAM and place 'pAudioIO->FragmentSize << CONFIG_MAX_PITCH' |
706 |
// number of '0' samples (silence samples) behind the official buffer |
707 |
// border, to allow the interpolator do it's work even at the end of |
708 |
// the sample. |
709 |
const uint maxSamplesPerCycle = |
710 |
(pEngine) ? pEngine->pAudioOutputDevice->MaxSamplesPerCycle() |
711 |
: GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE; |
712 |
const uint neededSilenceSamples = (maxSamplesPerCycle << CONFIG_MAX_PITCH) + 3; |
713 |
const uint currentlyCachedSilenceSamples = pSample->GetCache().NullExtensionSize / pSample->FrameSize; |
714 |
if (currentlyCachedSilenceSamples < neededSilenceSamples) { |
715 |
dmsg(3,("Caching whole sample (sample name: \"%s\", sample size: %d)\n", pSample->pInfo->Name.c_str(), pSample->SamplesTotal)); |
716 |
::gig::buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension(neededSilenceSamples); |
717 |
dmsg(4,("Cached %d Bytes, %d silence bytes.\n", buf.Size, buf.NullExtensionSize)); |
718 |
} |
719 |
} |
720 |
else { // we only cache CONFIG_PRELOAD_SAMPLES and stream the other sample points from disk |
721 |
if (!pSample->GetCache().Size) pSample->LoadSampleData(CONFIG_PRELOAD_SAMPLES); |
722 |
} |
723 |
|
724 |
if (!pSample->GetCache().Size) std::cerr << "Unable to cache sample - maybe memory full!" << std::endl << std::flush; |
725 |
} |
726 |
|
727 |
void InstrumentResourceManager::UncacheInitialSamples(::gig::Sample* pSample) { |
728 |
dmsg(1,("Uncaching sample %x\n",pSample)); |
729 |
if (pSample->GetCache().Size) pSample->ReleaseSampleData(); |
730 |
} |
731 |
|
732 |
/** |
733 |
* Returns a list with all instruments currently in use, that are part of |
734 |
* the given file. |
735 |
* |
736 |
* @param pFile - search criteria |
737 |
* @param bLock - whether we should lock (mutex) the instrument manager |
738 |
* during this call and unlock at the end of this call |
739 |
*/ |
740 |
std::vector< ::gig::Instrument*> InstrumentResourceManager::GetInstrumentsCurrentlyUsedOf(::gig::File* pFile, bool bLock) { |
741 |
if (bLock) Lock(); |
742 |
std::vector< ::gig::Instrument*> result; |
743 |
std::vector< ::gig::Instrument*> allInstruments = Resources(false/*don't lock again*/); |
744 |
for (int i = 0; i < allInstruments.size(); i++) |
745 |
if ( |
746 |
(::gig::File*) allInstruments[i]->GetParent() |
747 |
== pFile |
748 |
) result.push_back(allInstruments[i]); |
749 |
if (bLock) Unlock(); |
750 |
return result; |
751 |
} |
752 |
|
753 |
/** |
754 |
* Returns a list with all gig engine channels that are currently using |
755 |
* the given instrument. |
756 |
* |
757 |
* @param pInstrument - search criteria |
758 |
* @param bLock - whether we should lock (mutex) the instrument manager |
759 |
* during this call and unlock at the end of this call |
760 |
*/ |
761 |
std::set<gig::EngineChannel*> InstrumentResourceManager::GetEngineChannelsUsing(::gig::Instrument* pInstrument, bool bLock) { |
762 |
if (bLock) Lock(); |
763 |
std::set<gig::EngineChannel*> result; |
764 |
std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument); |
765 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin(); |
766 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end = consumers.end(); |
767 |
for (; iter != end; ++iter) { |
768 |
gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(*iter); |
769 |
if (!pEngineChannel) continue; |
770 |
result.insert(pEngineChannel); |
771 |
} |
772 |
if (bLock) Unlock(); |
773 |
return result; |
774 |
} |
775 |
|
776 |
/** |
777 |
* Returns a list with all gig Engines that are currently using the given |
778 |
* instrument. |
779 |
* |
780 |
* @param pInstrument - search criteria |
781 |
* @param bLock - whether we should lock (mutex) the instrument manager |
782 |
* during this call and unlock at the end of this call |
783 |
*/ |
784 |
std::set<gig::Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::Instrument* pInstrument, bool bLock) { |
785 |
if (bLock) Lock(); |
786 |
std::set<gig::Engine*> result; |
787 |
std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument); |
788 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin(); |
789 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end = consumers.end(); |
790 |
for (; iter != end; ++iter) { |
791 |
gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(*iter); |
792 |
if (!pEngineChannel) continue; |
793 |
gig::Engine* pEngine = dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine()); |
794 |
if (!pEngine) continue; |
795 |
result.insert(pEngine); |
796 |
} |
797 |
if (bLock) Unlock(); |
798 |
return result; |
799 |
} |
800 |
|
801 |
/** |
802 |
* Returns a list with all gig Engines that are currently using an |
803 |
* instrument that is part of the given instrument file. |
804 |
* |
805 |
* @param pFile - search criteria |
806 |
* @param bLock - whether we should lock (mutex) the instrument manager |
807 |
* during this call and unlock at the end of this call |
808 |
*/ |
809 |
std::set<gig::Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::File* pFile, bool bLock) { |
810 |
if (bLock) Lock(); |
811 |
// get all instruments (currently in usage) that use that same gig::File |
812 |
std::vector< ::gig::Instrument*> instrumentsOfInterest = |
813 |
GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/); |
814 |
|
815 |
// get all engines that use that same gig::File |
816 |
std::set<gig::Engine*> result; |
817 |
{ |
818 |
for (int i = 0; i < instrumentsOfInterest.size(); i++) { |
819 |
std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(instrumentsOfInterest[i]); |
820 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin(); |
821 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end = consumers.end(); |
822 |
for (; iter != end; ++iter) { |
823 |
gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(*iter); |
824 |
if (!pEngineChannel) continue; |
825 |
gig::Engine* pEngine = dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine()); |
826 |
if (!pEngine) continue; |
827 |
// the unique, sorted container std::set makes |
828 |
// sure we won't have duplicates |
829 |
result.insert(pEngine); |
830 |
} |
831 |
} |
832 |
} |
833 |
if (bLock) Unlock(); |
834 |
return result; |
835 |
} |
836 |
|
837 |
/** |
838 |
* Returns @c true in case the given sample is referenced somewhere by the |
839 |
* given instrument, @c false otherwise. |
840 |
* |
841 |
* @param pSample - sample reference |
842 |
* @param pInstrument - instrument that might use that sample |
843 |
*/ |
844 |
bool InstrumentResourceManager::SampleReferencedByInstrument(::gig::Sample* pSample, ::gig::Instrument* pInstrument) { |
845 |
for ( |
846 |
::gig::Region* pRegion = pInstrument->GetFirstRegion(); |
847 |
pRegion; pRegion = pInstrument->GetNextRegion() |
848 |
) { |
849 |
for ( |
850 |
int i = 0; i < pRegion->DimensionRegions && |
851 |
pRegion->pDimensionRegions[i]; i++ |
852 |
) { |
853 |
if (pRegion->pDimensionRegions[i]->pSample == pSample) |
854 |
return true; |
855 |
} |
856 |
} |
857 |
return false; |
858 |
} |
859 |
|
860 |
/** |
861 |
* Suspend all gig engines that use the given instrument. This means |
862 |
* completely stopping playback on those engines and killing all their |
863 |
* voices and disk streams. This method will block until all voices AND |
864 |
* their disk streams are finally deleted and the engine turned into a |
865 |
* complete idle loop. |
866 |
* |
867 |
* All @c SuspendEnginesUsing() methods only serve one thread by one and |
868 |
* block all other threads until the current active thread called |
869 |
* @c ResumeAllEngines() . |
870 |
* |
871 |
* @param pInstrument - search criteria |
872 |
*/ |
873 |
void InstrumentResourceManager::SuspendEnginesUsing(::gig::Instrument* pInstrument) { |
874 |
// make sure no other thread suspends whole engines at the same time |
875 |
suspendedEnginesMutex.Lock(); |
876 |
// get all engines that use that same gig::Instrument |
877 |
suspendedEngines = GetEnginesUsing(pInstrument, true/*lock*/); |
878 |
// finally, completely suspend all engines that use that same gig::Instrument |
879 |
std::set<gig::Engine*>::iterator iter = suspendedEngines.begin(); |
880 |
std::set<gig::Engine*>::iterator end = suspendedEngines.end(); |
881 |
for (; iter != end; ++iter) (*iter)->SuspendAll(); |
882 |
} |
883 |
|
884 |
/** |
885 |
* Suspend all gig engines that use the given instrument file. This means |
886 |
* completely stopping playback on those engines and killing all their |
887 |
* voices and disk streams. This method will block until all voices AND |
888 |
* their disk streams are finally deleted and the engine turned into a |
889 |
* complete idle loop. |
890 |
* |
891 |
* All @c SuspendEnginesUsing() methods only serve one thread by one and |
892 |
* block all other threads until the current active thread called |
893 |
* @c ResumeAllEngines() . |
894 |
* |
895 |
* @param pFile - search criteria |
896 |
*/ |
897 |
void InstrumentResourceManager::SuspendEnginesUsing(::gig::File* pFile) { |
898 |
// make sure no other thread suspends whole engines at the same time |
899 |
suspendedEnginesMutex.Lock(); |
900 |
// get all engines that use that same gig::File |
901 |
suspendedEngines = GetEnginesUsing(pFile, true/*lock*/); |
902 |
// finally, completely suspend all engines that use that same gig::File |
903 |
std::set<gig::Engine*>::iterator iter = suspendedEngines.begin(); |
904 |
std::set<gig::Engine*>::iterator end = suspendedEngines.end(); |
905 |
for (; iter != end; ++iter) (*iter)->SuspendAll(); |
906 |
} |
907 |
|
908 |
/** |
909 |
* MUST be called after one called one of the @c SuspendEnginesUsing() |
910 |
* methods, to resume normal playback on all previously suspended engines. |
911 |
* As it's only possible for one thread to suspend whole engines at the |
912 |
* same time, this method doesn't take any arguments. |
913 |
*/ |
914 |
void InstrumentResourceManager::ResumeAllEngines() { |
915 |
// resume all previously completely suspended engines |
916 |
std::set<Engine*>::iterator iter = suspendedEngines.begin(); |
917 |
std::set<Engine*>::iterator end = suspendedEngines.end(); |
918 |
for (; iter != end; ++iter) (*iter)->ResumeAll(); |
919 |
// no more suspended engines ... |
920 |
suspendedEngines.clear(); |
921 |
// allow another thread to suspend whole engines |
922 |
suspendedEnginesMutex.Unlock(); |
923 |
} |
924 |
|
925 |
|
926 |
|
927 |
// internal gig file manager |
928 |
|
929 |
::gig::File* InstrumentResourceManager::GigResourceManager::Create(String Key, GigConsumer* pConsumer, void*& pArg) { |
930 |
dmsg(1,("Loading gig file \'%s\'...", Key.c_str())); |
931 |
::RIFF::File* pRIFF = new ::RIFF::File(Key); |
932 |
::gig::File* pGig = new ::gig::File(pRIFF); |
933 |
pArg = pRIFF; |
934 |
dmsg(1,("OK\n")); |
935 |
return pGig; |
936 |
} |
937 |
|
938 |
void InstrumentResourceManager::GigResourceManager::Destroy(::gig::File* pResource, void* pArg) { |
939 |
dmsg(1,("Freeing gig file from memory...")); |
940 |
|
941 |
// Delete as much as possible of the gig file. Some of the |
942 |
// dimension regions and samples may still be in use - these |
943 |
// will be deleted later by the HandBackDimReg function. |
944 |
bool deleteFile = true; |
945 |
::gig::Instrument* nextInstrument; |
946 |
for (::gig::Instrument* instrument = pResource->GetFirstInstrument() ; |
947 |
instrument ; |
948 |
instrument = nextInstrument) { |
949 |
nextInstrument = pResource->GetNextInstrument(); |
950 |
bool deleteInstrument = true; |
951 |
::gig::Region* nextRegion; |
952 |
for (::gig::Region *region = instrument->GetFirstRegion() ; |
953 |
region ; |
954 |
region = nextRegion) { |
955 |
nextRegion = instrument->GetNextRegion(); |
956 |
bool deleteRegion = true; |
957 |
for (int i = 0 ; i < region->DimensionRegions ; i++) |
958 |
{ |
959 |
::gig::DimensionRegion *d = region->pDimensionRegions[i]; |
960 |
std::map< ::gig::DimensionRegion*, dimreg_info_t>::iterator iter = parent->DimRegInfo.find(d); |
961 |
if (iter != parent->DimRegInfo.end()) { |
962 |
dimreg_info_t& dimRegInfo = (*iter).second; |
963 |
dimRegInfo.file = pResource; |
964 |
dimRegInfo.riff = (::RIFF::File*)pArg; |
965 |
deleteFile = deleteInstrument = deleteRegion = false; |
966 |
} |
967 |
} |
968 |
if (deleteRegion) instrument->DeleteRegion(region); |
969 |
} |
970 |
if (deleteInstrument) pResource->DeleteInstrument(instrument); |
971 |
} |
972 |
if (deleteFile) { |
973 |
delete pResource; |
974 |
delete (::RIFF::File*) pArg; |
975 |
} else { |
976 |
dmsg(2,("keeping some samples that are in use...")); |
977 |
::gig::Sample* nextSample; |
978 |
for (::gig::Sample* sample = pResource->GetFirstSample() ; |
979 |
sample ; |
980 |
sample = nextSample) { |
981 |
nextSample = pResource->GetNextSample(); |
982 |
if (parent->SampleRefCount.find(sample) == parent->SampleRefCount.end()) { |
983 |
pResource->DeleteSample(sample); |
984 |
} |
985 |
} |
986 |
} |
987 |
dmsg(1,("OK\n")); |
988 |
} |
989 |
|
990 |
}} // namespace LinuxSampler::gig |