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 - 2012 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; |
76 |
pArg->pManager->DispatchResourceProgressEvent(*pArg->pInstrumentKey, localProgress); |
pArg->pManager->DispatchResourceProgressEvent(*pArg->pInstrumentKey, localProgress); |
77 |
} |
} |
78 |
|
|
|
std::vector<InstrumentResourceManager::instrument_id_t> InstrumentResourceManager::Instruments() { |
|
|
return Entries(); |
|
|
} |
|
|
|
|
|
InstrumentManager::mode_t InstrumentResourceManager::GetMode(const instrument_id_t& ID) { |
|
|
return static_cast<InstrumentManager::mode_t>(AvailabilityMode(ID)); |
|
|
} |
|
|
|
|
|
void InstrumentResourceManager::SetMode(const instrument_id_t& ID, InstrumentManager::mode_t Mode) { |
|
|
dmsg(2,("gig::InstrumentResourceManager: setting mode for %s (Index=%d) to %d\n",ID.FileName.c_str(),ID.Index,Mode)); |
|
|
SetAvailabilityMode(ID, static_cast<ResourceManager<InstrumentManager::instrument_id_t, ::gig::Instrument>::mode_t>(Mode)); |
|
|
} |
|
|
|
|
79 |
String InstrumentResourceManager::GetInstrumentName(instrument_id_t ID) { |
String InstrumentResourceManager::GetInstrumentName(instrument_id_t ID) { |
80 |
Lock(); |
Lock(); |
81 |
::gig::Instrument* pInstrument = Resource(ID, false); |
::gig::Instrument* pInstrument = Resource(ID, false); |
129 |
::RIFF::File* riff = NULL; |
::RIFF::File* riff = NULL; |
130 |
::gig::File* gig = NULL; |
::gig::File* gig = NULL; |
131 |
try { |
try { |
132 |
if(!loaded) { |
if (!loaded) { |
133 |
riff = new ::RIFF::File(ID.FileName); |
riff = new ::RIFF::File(ID.FileName); |
134 |
gig = new ::gig::File(riff); |
gig = new ::gig::File(riff); |
135 |
gig->SetAutoLoad(false); // avoid time consuming samples scanning |
gig->SetAutoLoad(false); // avoid time consuming samples scanning |
136 |
pInstrument = gig->GetInstrument(ID.Index); |
pInstrument = gig->GetInstrument(ID.Index); |
137 |
} |
} |
138 |
|
|
139 |
if (!pInstrument) throw InstrumentManagerException("There is no instrument " + ToString(ID.Index) + " in " + ID.FileName); |
if (!pInstrument) throw InstrumentManagerException("There is no instrument " + ToString(ID.Index) + " in " + ID.FileName); |
140 |
|
|
141 |
instrument_info_t info; |
instrument_info_t info; |
160 |
} else { |
} else { |
161 |
for (int i = low; i <= high; i++) info.KeyBindings[i] = 1; |
for (int i = low; i <= high; i++) info.KeyBindings[i] = 1; |
162 |
} |
} |
163 |
|
|
164 |
pRegion = pInstrument->GetNextRegion(); |
pRegion = pInstrument->GetNextRegion(); |
165 |
} |
} |
166 |
|
|
167 |
int low = pInstrument->DimensionKeyRange.low; |
if (loaded) { // retrieve keyswitching only if the instrument is fully loaded. |
168 |
int high = pInstrument->DimensionKeyRange.high; |
|
169 |
if (low < 0 || low > 127 || high < 0 || high > 127 || low > high) { |
// only return keyswitch range if keyswitching is used |
170 |
std::cerr << "Invalid keyswitch range: " << low << " - " << high << std::endl; |
bool hasKeyswitches = false; |
171 |
} else { |
for (::gig::Region* pRegion = pInstrument->GetFirstRegion() ; |
172 |
for (int i = low; i <= high; i++) info.KeySwitchBindings[i] = 1; |
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(); |
if (loaded) Unlock(); |
208 |
} |
} |
209 |
} |
} |
210 |
|
|
211 |
void InstrumentResourceManager::LaunchInstrumentEditor(instrument_id_t ID) throw (InstrumentManagerException) { |
InstrumentEditor* InstrumentResourceManager::LaunchInstrumentEditor(instrument_id_t ID, void* pUserData) throw (InstrumentManagerException) { |
212 |
const String sDataType = GetInstrumentDataStructureName(ID); |
const String sDataType = GetInstrumentDataStructureName(ID); |
213 |
const String sDataVersion = GetInstrumentDataStructureVersion(ID); |
const String sDataVersion = GetInstrumentDataStructureVersion(ID); |
214 |
// find instrument editors capable to handle given instrument |
// find instrument editors capable to handle given instrument |
235 |
InstrumentEditorProxies.add(pProxy); |
InstrumentEditorProxies.add(pProxy); |
236 |
InstrumentEditorProxiesMutex.Unlock(); |
InstrumentEditorProxiesMutex.Unlock(); |
237 |
// launch the instrument editor for the given instrument |
// launch the instrument editor for the given instrument |
238 |
pEditor->Launch(pInstrument, sDataType, sDataVersion); |
pEditor->Launch(pInstrument, sDataType, sDataVersion, pUserData); |
239 |
|
|
240 |
// register the instrument editor as virtual MIDI device as well ... |
// register the instrument editor as virtual MIDI device as well ... |
241 |
VirtualMidiDevice* pVirtualMidiDevice = |
VirtualMidiDevice* pVirtualMidiDevice = |
242 |
dynamic_cast<VirtualMidiDevice*>(pEditor); |
dynamic_cast<VirtualMidiDevice*>(pEditor); |
243 |
if (!pVirtualMidiDevice) { |
if (!pVirtualMidiDevice) { |
244 |
std::cerr << "Instrument editor not a virtual MIDI device\n" << std::flush; |
std::cerr << "Instrument editor not a virtual MIDI device\n" << std::flush; |
245 |
return; |
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 ? ) |
// 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(); |
Lock(); |
249 |
std::set<gig::EngineChannel*> engineChannels = |
std::set<EngineChannel*> engineChannels = |
250 |
GetEngineChannelsUsing(pInstrument, false/*don't lock again*/); |
GetEngineChannelsUsing(pInstrument, false/*don't lock again*/); |
251 |
std::set<gig::EngineChannel*>::iterator iter = engineChannels.begin(); |
std::set<EngineChannel*>::iterator iter = engineChannels.begin(); |
252 |
std::set<gig::EngineChannel*>::iterator end = engineChannels.end(); |
std::set<EngineChannel*>::iterator end = engineChannels.end(); |
253 |
for (; iter != end; ++iter) (*iter)->Connect(pVirtualMidiDevice); |
for (; iter != end; ++iter) (static_cast<AbstractEngineChannel*>(*iter))->Connect(pVirtualMidiDevice); |
254 |
Unlock(); |
Unlock(); |
255 |
|
|
256 |
|
return pEditor; |
257 |
} |
} |
258 |
|
|
259 |
/** |
/** |
297 |
if (pVirtualMidiDevice) { |
if (pVirtualMidiDevice) { |
298 |
Lock(); |
Lock(); |
299 |
// NOTE: see note in LaunchInstrumentEditor() |
// NOTE: see note in LaunchInstrumentEditor() |
300 |
std::set<gig::EngineChannel*> engineChannels = |
std::set<EngineChannel*> engineChannels = |
301 |
GetEngineChannelsUsing(pInstrument, false/*don't lock again*/); |
GetEngineChannelsUsing(pInstrument, false/*don't lock again*/); |
302 |
std::set<gig::EngineChannel*>::iterator iter = engineChannels.begin(); |
std::set<EngineChannel*>::iterator iter = engineChannels.begin(); |
303 |
std::set<gig::EngineChannel*>::iterator end = engineChannels.end(); |
std::set<EngineChannel*>::iterator end = engineChannels.end(); |
304 |
for (; iter != end; ++iter) (*iter)->Disconnect(pVirtualMidiDevice); |
for (; iter != end; ++iter) (*iter)->Disconnect(pVirtualMidiDevice); |
305 |
Unlock(); |
Unlock(); |
306 |
} else { |
} else { |
407 |
::gig::Instrument* pInstrument = |
::gig::Instrument* pInstrument = |
408 |
(::gig::Instrument*) pRegion->GetParent(); |
(::gig::Instrument*) pRegion->GetParent(); |
409 |
Lock(); |
Lock(); |
410 |
std::set<gig::Engine*> engines = |
std::set<Engine*> engines = |
411 |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
412 |
std::set<gig::Engine*>::iterator iter = engines.begin(); |
std::set<Engine*>::iterator iter = engines.begin(); |
413 |
std::set<gig::Engine*>::iterator end = engines.end(); |
std::set<Engine*>::iterator end = engines.end(); |
414 |
for (; iter != end; ++iter) (*iter)->Suspend(pRegion); |
for (; iter != end; ++iter) (*iter)->Suspend(pRegion); |
415 |
Unlock(); |
Unlock(); |
416 |
} else if (sStructType == "gig::DimensionRegion") { |
} else if (sStructType == "gig::DimensionRegion") { |
424 |
::gig::Instrument* pInstrument = |
::gig::Instrument* pInstrument = |
425 |
(::gig::Instrument*) pRegion->GetParent(); |
(::gig::Instrument*) pRegion->GetParent(); |
426 |
Lock(); |
Lock(); |
427 |
std::set<gig::Engine*> engines = |
std::set<Engine*> engines = |
428 |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
429 |
std::set<gig::Engine*>::iterator iter = engines.begin(); |
std::set<Engine*>::iterator iter = engines.begin(); |
430 |
std::set<gig::Engine*>::iterator end = engines.end(); |
std::set<Engine*>::iterator end = engines.end(); |
431 |
for (; iter != end; ++iter) (*iter)->Suspend(pRegion); |
for (; iter != end; ++iter) (*iter)->Suspend(pRegion); |
432 |
Unlock(); |
Unlock(); |
433 |
} else { |
} else { |
447 |
} else if (sStructType == "gig::Instrument") { |
} else if (sStructType == "gig::Instrument") { |
448 |
// resume all previously suspended engines |
// resume all previously suspended engines |
449 |
ResumeAllEngines(); |
ResumeAllEngines(); |
450 |
|
} else if (sStructType == "gig::Sample") { |
451 |
|
// we're assuming here, that OnDataStructureToBeChanged() with |
452 |
|
// "gig::File" was called previously, so we won't resume anything |
453 |
|
// here, but just re-cache the given sample |
454 |
|
Lock(); |
455 |
|
::gig::Sample* pSample = (::gig::Sample*) pStruct; |
456 |
|
::gig::File* pFile = (::gig::File*) pSample->GetParent(); |
457 |
|
UncacheInitialSamples(pSample); |
458 |
|
// now re-cache ... |
459 |
|
std::vector< ::gig::Instrument*> instruments = |
460 |
|
GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/); |
461 |
|
for (int i = 0; i < instruments.size(); i++) { |
462 |
|
if (SampleReferencedByInstrument(pSample, instruments[i])) { |
463 |
|
std::set<EngineChannel*> engineChannels = |
464 |
|
GetEngineChannelsUsing(instruments[i], false/*don't lock again*/); |
465 |
|
std::set<EngineChannel*>::iterator iter = engineChannels.begin(); |
466 |
|
std::set<EngineChannel*>::iterator end = engineChannels.end(); |
467 |
|
for (; iter != end; ++iter) |
468 |
|
CacheInitialSamples(pSample, *iter); |
469 |
|
} |
470 |
|
} |
471 |
|
Unlock(); |
472 |
} else if (sStructType == "gig::Region") { |
} else if (sStructType == "gig::Region") { |
473 |
// advice the engines to resume the given region, that is to |
// advice the engines to resume the given region, that is to |
474 |
// using it for playback again |
// using it for playback again |
476 |
::gig::Instrument* pInstrument = |
::gig::Instrument* pInstrument = |
477 |
(::gig::Instrument*) pRegion->GetParent(); |
(::gig::Instrument*) pRegion->GetParent(); |
478 |
Lock(); |
Lock(); |
479 |
std::set<gig::Engine*> engines = |
std::set<Engine*> engines = |
480 |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
481 |
std::set<gig::Engine*>::iterator iter = engines.begin(); |
std::set<Engine*>::iterator iter = engines.begin(); |
482 |
std::set<gig::Engine*>::iterator end = engines.end(); |
std::set<Engine*>::iterator end = engines.end(); |
483 |
for (; iter != end; ++iter) (*iter)->Resume(pRegion); |
for (; iter != end; ++iter) (*iter)->Resume(pRegion); |
484 |
Unlock(); |
Unlock(); |
485 |
} else if (sStructType == "gig::DimensionRegion") { |
} else if (sStructType == "gig::DimensionRegion") { |
491 |
::gig::Instrument* pInstrument = |
::gig::Instrument* pInstrument = |
492 |
(::gig::Instrument*) pRegion->GetParent(); |
(::gig::Instrument*) pRegion->GetParent(); |
493 |
Lock(); |
Lock(); |
494 |
std::set<gig::Engine*> engines = |
std::set<Engine*> engines = |
495 |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
496 |
std::set<gig::Engine*>::iterator iter = engines.begin(); |
std::set<Engine*>::iterator iter = engines.begin(); |
497 |
std::set<gig::Engine*>::iterator end = engines.end(); |
std::set<Engine*>::iterator end = engines.end(); |
498 |
for (; iter != end; ++iter) (*iter)->Resume(pRegion); |
for (; iter != end; ++iter) (*iter)->Resume(pRegion); |
499 |
Unlock(); |
Unlock(); |
500 |
} else { |
} else { |
512 |
Lock(); |
Lock(); |
513 |
::gig::Sample* pSample = (::gig::Sample*) pOldSample; |
::gig::Sample* pSample = (::gig::Sample*) pOldSample; |
514 |
::gig::File* pFile = (::gig::File*) pSample->GetParent(); |
::gig::File* pFile = (::gig::File*) pSample->GetParent(); |
515 |
|
bool bSampleStillInUse = false; |
516 |
std::vector< ::gig::Instrument*> instruments = |
std::vector< ::gig::Instrument*> instruments = |
517 |
GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/); |
GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/); |
518 |
for (int i = 0; i < instruments.size(); i++) |
for (int i = 0; i < instruments.size(); i++) { |
519 |
if (!SampleReferencedByInstrument(pSample, instruments[i])) |
if (SampleReferencedByInstrument(pSample, instruments[i])) { |
520 |
UncacheInitialSamples(pSample); |
bSampleStillInUse = true; |
521 |
|
break; |
522 |
|
} |
523 |
|
} |
524 |
|
if (!bSampleStillInUse) UncacheInitialSamples(pSample); |
525 |
Unlock(); |
Unlock(); |
526 |
} |
} |
527 |
// make sure new sample reference is cached |
// make sure new sample reference is cached |
530 |
::gig::Sample* pSample = (::gig::Sample*) pNewSample; |
::gig::Sample* pSample = (::gig::Sample*) pNewSample; |
531 |
::gig::File* pFile = (::gig::File*) pSample->GetParent(); |
::gig::File* pFile = (::gig::File*) pSample->GetParent(); |
532 |
// get all engines that use that same gig::File |
// get all engines that use that same gig::File |
533 |
std::set<gig::Engine*> engines = GetEnginesUsing(pFile, false/*don't lock again*/); |
std::set<Engine*> engines = GetEnginesUsing(pFile, false/*don't lock again*/); |
534 |
std::set<gig::Engine*>::iterator iter = engines.begin(); |
std::set<Engine*>::iterator iter = engines.begin(); |
535 |
std::set<gig::Engine*>::iterator end = engines.end(); |
std::set<Engine*>::iterator end = engines.end(); |
536 |
for (; iter != end; ++iter) |
for (; iter != end; ++iter) |
537 |
CacheInitialSamples(pSample, *iter); |
CacheInitialSamples(pSample, *iter); |
538 |
Unlock(); |
Unlock(); |
541 |
|
|
542 |
::gig::Instrument* InstrumentResourceManager::Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg) { |
::gig::Instrument* InstrumentResourceManager::Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg) { |
543 |
// get gig file from internal gig file manager |
// get gig file from internal gig file manager |
544 |
::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 :/ |
545 |
|
|
546 |
// we pass this to the progress callback mechanism of libgig |
// we pass this to the progress callback mechanism of libgig |
547 |
progress_callback_arg_t callbackArg; |
progress_callback_arg_t callbackArg; |
562 |
pGig->GetFirstSample(); // just to force complete instrument loading |
pGig->GetFirstSample(); // just to force complete instrument loading |
563 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
564 |
|
|
565 |
|
uint maxSamplesPerCycle = GetMaxSamplesPerCycle(pConsumer); |
566 |
|
|
567 |
// cache initial samples points (for actually needed samples) |
// cache initial samples points (for actually needed samples) |
568 |
dmsg(1,("Caching initial samples...")); |
dmsg(1,("Caching initial samples...")); |
569 |
uint iRegion = 0; // just for progress calculation |
uint iRegion = 0; // just for progress calculation |
575 |
|
|
576 |
if (pRgn->GetSample() && !pRgn->GetSample()->GetCache().Size) { |
if (pRgn->GetSample() && !pRgn->GetSample()->GetCache().Size) { |
577 |
dmsg(2,("C")); |
dmsg(2,("C")); |
578 |
CacheInitialSamples(pRgn->GetSample(), (gig::EngineChannel*) pConsumer); |
CacheInitialSamples(pRgn->GetSample(), maxSamplesPerCycle); |
579 |
} |
} |
580 |
for (uint i = 0; i < pRgn->DimensionRegions; i++) { |
for (uint i = 0; i < pRgn->DimensionRegions; i++) { |
581 |
CacheInitialSamples(pRgn->pDimensionRegions[i]->pSample, (gig::EngineChannel*) pConsumer); |
CacheInitialSamples(pRgn->pDimensionRegions[i]->pSample, maxSamplesPerCycle); |
582 |
} |
} |
583 |
|
|
584 |
pRgn = pInstrument->GetNextRegion(); |
pRgn = pInstrument->GetNextRegion(); |
591 |
instr_entry_t* pEntry = new instr_entry_t; |
instr_entry_t* pEntry = new instr_entry_t; |
592 |
pEntry->ID.FileName = Key.FileName; |
pEntry->ID.FileName = Key.FileName; |
593 |
pEntry->ID.Index = Key.Index; |
pEntry->ID.Index = Key.Index; |
594 |
pEntry->pGig = pGig; |
pEntry->pFile = pGig; |
595 |
|
|
596 |
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' |
597 |
// and we save this to check if we need to reallocate for a engine with higher value of 'MaxSamplesPerSecond' |
pEntry->MaxSamplesPerCycle = maxSamplesPerCycle; |
598 |
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; |
|
599 |
pArg = pEntry; |
pArg = pEntry; |
600 |
|
|
601 |
return pInstrument; |
return pInstrument; |
602 |
} |
} |
603 |
|
|
604 |
void InstrumentResourceManager::Destroy( ::gig::Instrument* pResource, void* pArg) { |
void InstrumentResourceManager::Destroy(::gig::Instrument* pResource, void* pArg) { |
605 |
instr_entry_t* pEntry = (instr_entry_t*) pArg; |
instr_entry_t* pEntry = (instr_entry_t*) pArg; |
606 |
// we don't need the .gig file here anymore |
// we don't need the .gig file here anymore |
607 |
Gigs.HandBack(pEntry->pGig, (GigConsumer*) pEntry->ID.Index); // conversion kinda hackish :/ |
Gigs.HandBack(pEntry->pFile, reinterpret_cast<GigConsumer*>(pEntry->ID.Index)); // conversion kinda hackish :/ |
608 |
delete pEntry; |
delete pEntry; |
609 |
} |
} |
610 |
|
|
611 |
void InstrumentResourceManager::OnBorrow(::gig::Instrument* pResource, InstrumentConsumer* pConsumer, void*& pArg) { |
void InstrumentResourceManager::DeleteRegionIfNotUsed(::gig::DimensionRegion* pRegion, region_info_t* pRegInfo) { |
612 |
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); |
|
|
} |
|
613 |
} |
} |
614 |
|
|
615 |
/** |
void InstrumentResourceManager::DeleteSampleIfNotUsed(::gig::Sample* pSample, region_info_t* pRegInfo) { |
616 |
* Give back an instrument. This should be used instead of |
::gig::File* gig = pRegInfo->file; |
617 |
* HandBack if there are some dimension regions that are still in |
::RIFF::File* riff = static_cast< ::RIFF::File*>(pRegInfo->pArg); |
618 |
* use. (When an instrument is changed, the voices currently |
if (gig) { |
619 |
* playing are allowed to keep playing with the old instrument |
gig->DeleteSample(pSample); |
620 |
* until note off arrives. New notes will use the new instrument.) |
if (!gig->GetFirstSample()) { |
621 |
*/ |
dmsg(2,("No more samples in use - freeing gig\n")); |
622 |
void InstrumentResourceManager::HandBackInstrument(::gig::Instrument* pResource, InstrumentConsumer* pConsumer, |
delete gig; |
623 |
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; |
|
|
} |
|
|
} |
|
624 |
} |
} |
625 |
} |
} |
|
DimRegInfoMutex.Unlock(); |
|
626 |
} |
} |
627 |
|
|
628 |
/** |
/** |
633 |
* (may be NULL, in this case default amount of samples |
* (may be NULL, in this case default amount of samples |
634 |
* will be cached) |
* will be cached) |
635 |
*/ |
*/ |
636 |
void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, gig::EngineChannel* pEngineChannel) { |
void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, EngineChannel* pEngineChannel) { |
637 |
gig::Engine* pEngine = |
Engine* pEngine = |
638 |
(pEngineChannel && pEngineChannel->GetEngine()) ? |
(pEngineChannel && pEngineChannel->GetEngine()) ? |
639 |
dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine()) : NULL; |
dynamic_cast<Engine*>(pEngineChannel->GetEngine()) : NULL; |
640 |
CacheInitialSamples(pSample, pEngine); |
CacheInitialSamples(pSample, pEngine); |
641 |
} |
} |
642 |
|
|
651 |
* (may be NULL, in this case default amount of samples |
* (may be NULL, in this case default amount of samples |
652 |
* will be cached) |
* will be cached) |
653 |
*/ |
*/ |
654 |
void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, gig::Engine* pEngine) { |
void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, AbstractEngine* pEngine) { |
655 |
|
uint maxSamplesPerCycle = |
656 |
|
(pEngine) ? pEngine->pAudioOutputDevice->MaxSamplesPerCycle() : |
657 |
|
DefaultMaxSamplesPerCycle(); |
658 |
|
CacheInitialSamples(pSample, maxSamplesPerCycle); |
659 |
|
} |
660 |
|
|
661 |
|
void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, uint maxSamplesPerCycle) { |
662 |
if (!pSample) { |
if (!pSample) { |
663 |
dmsg(4,("gig::InstrumentResourceManager: Skipping sample (pSample == NULL)\n")); |
dmsg(4,("gig::InstrumentResourceManager: Skipping sample (pSample == NULL)\n")); |
664 |
return; |
return; |
671 |
// number of '0' samples (silence samples) behind the official buffer |
// number of '0' samples (silence samples) behind the official buffer |
672 |
// 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 |
673 |
// the sample. |
// the sample. |
|
const uint maxSamplesPerCycle = |
|
|
(pEngine) ? pEngine->pAudioOutputDevice->MaxSamplesPerCycle() |
|
|
: GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE; |
|
674 |
const uint neededSilenceSamples = (maxSamplesPerCycle << CONFIG_MAX_PITCH) + 3; |
const uint neededSilenceSamples = (maxSamplesPerCycle << CONFIG_MAX_PITCH) + 3; |
675 |
const uint currentlyCachedSilenceSamples = pSample->GetCache().NullExtensionSize / pSample->FrameSize; |
const uint currentlyCachedSilenceSamples = pSample->GetCache().NullExtensionSize / pSample->FrameSize; |
676 |
if (currentlyCachedSilenceSamples < neededSilenceSamples) { |
if (currentlyCachedSilenceSamples < neededSilenceSamples) { |
720 |
* @param bLock - whether we should lock (mutex) the instrument manager |
* @param bLock - whether we should lock (mutex) the instrument manager |
721 |
* during this call and unlock at the end of this call |
* during this call and unlock at the end of this call |
722 |
*/ |
*/ |
723 |
std::set<gig::EngineChannel*> InstrumentResourceManager::GetEngineChannelsUsing(::gig::Instrument* pInstrument, bool bLock) { |
std::set<EngineChannel*> InstrumentResourceManager::GetEngineChannelsUsing(::gig::Instrument* pInstrument, bool bLock) { |
724 |
if (bLock) Lock(); |
if (bLock) Lock(); |
725 |
std::set<gig::EngineChannel*> result; |
std::set<EngineChannel*> result; |
726 |
std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument); |
std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument); |
727 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin(); |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin(); |
728 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end = consumers.end(); |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end = consumers.end(); |
729 |
for (; iter != end; ++iter) { |
for (; iter != end; ++iter) { |
730 |
gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(*iter); |
EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(*iter); |
731 |
if (!pEngineChannel) continue; |
if (!pEngineChannel) continue; |
732 |
result.insert(pEngineChannel); |
result.insert(pEngineChannel); |
733 |
} |
} |
743 |
* @param bLock - whether we should lock (mutex) the instrument manager |
* @param bLock - whether we should lock (mutex) the instrument manager |
744 |
* during this call and unlock at the end of this call |
* during this call and unlock at the end of this call |
745 |
*/ |
*/ |
746 |
std::set<gig::Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::Instrument* pInstrument, bool bLock) { |
std::set<Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::Instrument* pInstrument, bool bLock) { |
747 |
if (bLock) Lock(); |
if (bLock) Lock(); |
748 |
std::set<gig::Engine*> result; |
std::set<Engine*> result; |
749 |
std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument); |
std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument); |
750 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin(); |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin(); |
751 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end = consumers.end(); |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end = consumers.end(); |
752 |
for (; iter != end; ++iter) { |
for (; iter != end; ++iter) { |
753 |
gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(*iter); |
EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(*iter); |
754 |
if (!pEngineChannel) continue; |
if (!pEngineChannel) continue; |
755 |
gig::Engine* pEngine = dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine()); |
Engine* pEngine = dynamic_cast<Engine*>(pEngineChannel->GetEngine()); |
756 |
if (!pEngine) continue; |
if (!pEngine) continue; |
757 |
result.insert(pEngine); |
result.insert(pEngine); |
758 |
} |
} |
768 |
* @param bLock - whether we should lock (mutex) the instrument manager |
* @param bLock - whether we should lock (mutex) the instrument manager |
769 |
* during this call and unlock at the end of this call |
* during this call and unlock at the end of this call |
770 |
*/ |
*/ |
771 |
std::set<gig::Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::File* pFile, bool bLock) { |
std::set<Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::File* pFile, bool bLock) { |
772 |
if (bLock) Lock(); |
if (bLock) Lock(); |
773 |
// get all instruments (currently in usage) that use that same gig::File |
// get all instruments (currently in usage) that use that same gig::File |
774 |
std::vector< ::gig::Instrument*> instrumentsOfInterest = |
std::vector< ::gig::Instrument*> instrumentsOfInterest = |
775 |
GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/); |
GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/); |
776 |
|
|
777 |
// get all engines that use that same gig::File |
// get all engines that use that same gig::File |
778 |
std::set<gig::Engine*> result; |
std::set<Engine*> result; |
779 |
{ |
{ |
780 |
for (int i = 0; i < instrumentsOfInterest.size(); i++) { |
for (int i = 0; i < instrumentsOfInterest.size(); i++) { |
781 |
std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(instrumentsOfInterest[i]); |
std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(instrumentsOfInterest[i]); |
782 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin(); |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin(); |
783 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end = consumers.end(); |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end = consumers.end(); |
784 |
for (; iter != end; ++iter) { |
for (; iter != end; ++iter) { |
785 |
gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(*iter); |
EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(*iter); |
786 |
if (!pEngineChannel) continue; |
if (!pEngineChannel) continue; |
787 |
gig::Engine* pEngine = dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine()); |
Engine* pEngine = dynamic_cast<Engine*>(pEngineChannel->GetEngine()); |
788 |
if (!pEngine) continue; |
if (!pEngine) continue; |
789 |
// the unique, sorted container std::set makes |
// the unique, sorted container std::set makes |
790 |
// sure we won't have duplicates |
// sure we won't have duplicates |
838 |
// get all engines that use that same gig::Instrument |
// get all engines that use that same gig::Instrument |
839 |
suspendedEngines = GetEnginesUsing(pInstrument, true/*lock*/); |
suspendedEngines = GetEnginesUsing(pInstrument, true/*lock*/); |
840 |
// finally, completely suspend all engines that use that same gig::Instrument |
// finally, completely suspend all engines that use that same gig::Instrument |
841 |
std::set<gig::Engine*>::iterator iter = suspendedEngines.begin(); |
std::set<Engine*>::iterator iter = suspendedEngines.begin(); |
842 |
std::set<gig::Engine*>::iterator end = suspendedEngines.end(); |
std::set<Engine*>::iterator end = suspendedEngines.end(); |
843 |
for (; iter != end; ++iter) (*iter)->SuspendAll(); |
for (; iter != end; ++iter) (*iter)->SuspendAll(); |
844 |
} |
} |
845 |
|
|
862 |
// get all engines that use that same gig::File |
// get all engines that use that same gig::File |
863 |
suspendedEngines = GetEnginesUsing(pFile, true/*lock*/); |
suspendedEngines = GetEnginesUsing(pFile, true/*lock*/); |
864 |
// finally, completely suspend all engines that use that same gig::File |
// finally, completely suspend all engines that use that same gig::File |
865 |
std::set<gig::Engine*>::iterator iter = suspendedEngines.begin(); |
std::set<Engine*>::iterator iter = suspendedEngines.begin(); |
866 |
std::set<gig::Engine*>::iterator end = suspendedEngines.end(); |
std::set<Engine*>::iterator end = suspendedEngines.end(); |
867 |
for (; iter != end; ++iter) (*iter)->SuspendAll(); |
for (; iter != end; ++iter) (*iter)->SuspendAll(); |
868 |
} |
} |
869 |
|
|
898 |
} |
} |
899 |
|
|
900 |
void InstrumentResourceManager::GigResourceManager::Destroy(::gig::File* pResource, void* pArg) { |
void InstrumentResourceManager::GigResourceManager::Destroy(::gig::File* pResource, void* pArg) { |
901 |
dmsg(1,("Freeing gig file from memory...")); |
dmsg(1,("Freeing gig file '%s' from memory ...", pResource->GetFileName().c_str())); |
902 |
|
|
903 |
// Delete as much as possible of the gig file. Some of the |
// Delete as much as possible of the gig file. Some of the |
904 |
// dimension regions and samples may still be in use - these |
// dimension regions and samples may still be in use - these |
919 |
for (int i = 0 ; i < region->DimensionRegions ; i++) |
for (int i = 0 ; i < region->DimensionRegions ; i++) |
920 |
{ |
{ |
921 |
::gig::DimensionRegion *d = region->pDimensionRegions[i]; |
::gig::DimensionRegion *d = region->pDimensionRegions[i]; |
922 |
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); |
923 |
if (iter != parent->DimRegInfo.end()) { |
if (iter != parent->RegionInfo.end()) { |
924 |
dimreg_info_t& dimRegInfo = (*iter).second; |
region_info_t& dimRegInfo = (*iter).second; |
925 |
dimRegInfo.file = pResource; |
dimRegInfo.file = pResource; |
926 |
dimRegInfo.riff = (::RIFF::File*)pArg; |
dimRegInfo.pArg = (::RIFF::File*)pArg; |
927 |
deleteFile = deleteInstrument = deleteRegion = false; |
deleteFile = deleteInstrument = deleteRegion = false; |
928 |
} |
} |
929 |
} |
} |