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 - 2009 Christian Schoenebeck * |
* Copyright (C) 2005 - 2011 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 |
|
|
35 |
// data stored as long as an instrument resource exists |
// data stored as long as an instrument resource exists |
87 |
return Entries(); |
return Entries(); |
88 |
} |
} |
89 |
|
|
|
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)); |
|
|
} |
|
|
|
|
90 |
String InstrumentResourceManager::GetInstrumentName(instrument_id_t ID) { |
String InstrumentResourceManager::GetInstrumentName(instrument_id_t ID) { |
91 |
Lock(); |
Lock(); |
92 |
::gig::Instrument* pInstrument = Resource(ID, false); |
::gig::Instrument* pInstrument = Resource(ID, false); |
219 |
} |
} |
220 |
} |
} |
221 |
|
|
222 |
void InstrumentResourceManager::LaunchInstrumentEditor(instrument_id_t ID) throw (InstrumentManagerException) { |
InstrumentEditor* InstrumentResourceManager::LaunchInstrumentEditor(instrument_id_t ID, void* pUserData) throw (InstrumentManagerException) { |
223 |
const String sDataType = GetInstrumentDataStructureName(ID); |
const String sDataType = GetInstrumentDataStructureName(ID); |
224 |
const String sDataVersion = GetInstrumentDataStructureVersion(ID); |
const String sDataVersion = GetInstrumentDataStructureVersion(ID); |
225 |
// find instrument editors capable to handle given instrument |
// find instrument editors capable to handle given instrument |
246 |
InstrumentEditorProxies.add(pProxy); |
InstrumentEditorProxies.add(pProxy); |
247 |
InstrumentEditorProxiesMutex.Unlock(); |
InstrumentEditorProxiesMutex.Unlock(); |
248 |
// launch the instrument editor for the given instrument |
// launch the instrument editor for the given instrument |
249 |
pEditor->Launch(pInstrument, sDataType, sDataVersion); |
pEditor->Launch(pInstrument, sDataType, sDataVersion, pUserData); |
250 |
|
|
251 |
// register the instrument editor as virtual MIDI device as well ... |
// register the instrument editor as virtual MIDI device as well ... |
252 |
VirtualMidiDevice* pVirtualMidiDevice = |
VirtualMidiDevice* pVirtualMidiDevice = |
253 |
dynamic_cast<VirtualMidiDevice*>(pEditor); |
dynamic_cast<VirtualMidiDevice*>(pEditor); |
254 |
if (!pVirtualMidiDevice) { |
if (!pVirtualMidiDevice) { |
255 |
std::cerr << "Instrument editor not a virtual MIDI device\n" << std::flush; |
std::cerr << "Instrument editor not a virtual MIDI device\n" << std::flush; |
256 |
return; |
return pEditor; |
257 |
} |
} |
258 |
// 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 ? ) |
259 |
Lock(); |
Lock(); |
260 |
std::set<gig::EngineChannel*> engineChannels = |
std::set<EngineChannel*> engineChannels = |
261 |
GetEngineChannelsUsing(pInstrument, false/*don't lock again*/); |
GetEngineChannelsUsing(pInstrument, false/*don't lock again*/); |
262 |
std::set<gig::EngineChannel*>::iterator iter = engineChannels.begin(); |
std::set<EngineChannel*>::iterator iter = engineChannels.begin(); |
263 |
std::set<gig::EngineChannel*>::iterator end = engineChannels.end(); |
std::set<EngineChannel*>::iterator end = engineChannels.end(); |
264 |
for (; iter != end; ++iter) (*iter)->Connect(pVirtualMidiDevice); |
for (; iter != end; ++iter) (static_cast<AbstractEngineChannel*>(*iter))->Connect(pVirtualMidiDevice); |
265 |
Unlock(); |
Unlock(); |
266 |
|
|
267 |
|
return pEditor; |
268 |
} |
} |
269 |
|
|
270 |
/** |
/** |
308 |
if (pVirtualMidiDevice) { |
if (pVirtualMidiDevice) { |
309 |
Lock(); |
Lock(); |
310 |
// NOTE: see note in LaunchInstrumentEditor() |
// NOTE: see note in LaunchInstrumentEditor() |
311 |
std::set<gig::EngineChannel*> engineChannels = |
std::set<EngineChannel*> engineChannels = |
312 |
GetEngineChannelsUsing(pInstrument, false/*don't lock again*/); |
GetEngineChannelsUsing(pInstrument, false/*don't lock again*/); |
313 |
std::set<gig::EngineChannel*>::iterator iter = engineChannels.begin(); |
std::set<EngineChannel*>::iterator iter = engineChannels.begin(); |
314 |
std::set<gig::EngineChannel*>::iterator end = engineChannels.end(); |
std::set<EngineChannel*>::iterator end = engineChannels.end(); |
315 |
for (; iter != end; ++iter) (*iter)->Disconnect(pVirtualMidiDevice); |
for (; iter != end; ++iter) (*iter)->Disconnect(pVirtualMidiDevice); |
316 |
Unlock(); |
Unlock(); |
317 |
} else { |
} else { |
418 |
::gig::Instrument* pInstrument = |
::gig::Instrument* pInstrument = |
419 |
(::gig::Instrument*) pRegion->GetParent(); |
(::gig::Instrument*) pRegion->GetParent(); |
420 |
Lock(); |
Lock(); |
421 |
std::set<gig::Engine*> engines = |
std::set<Engine*> engines = |
422 |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
423 |
std::set<gig::Engine*>::iterator iter = engines.begin(); |
std::set<Engine*>::iterator iter = engines.begin(); |
424 |
std::set<gig::Engine*>::iterator end = engines.end(); |
std::set<Engine*>::iterator end = engines.end(); |
425 |
for (; iter != end; ++iter) (*iter)->Suspend(pRegion); |
for (; iter != end; ++iter) (*iter)->Suspend(pRegion); |
426 |
Unlock(); |
Unlock(); |
427 |
} else if (sStructType == "gig::DimensionRegion") { |
} else if (sStructType == "gig::DimensionRegion") { |
435 |
::gig::Instrument* pInstrument = |
::gig::Instrument* pInstrument = |
436 |
(::gig::Instrument*) pRegion->GetParent(); |
(::gig::Instrument*) pRegion->GetParent(); |
437 |
Lock(); |
Lock(); |
438 |
std::set<gig::Engine*> engines = |
std::set<Engine*> engines = |
439 |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
440 |
std::set<gig::Engine*>::iterator iter = engines.begin(); |
std::set<Engine*>::iterator iter = engines.begin(); |
441 |
std::set<gig::Engine*>::iterator end = engines.end(); |
std::set<Engine*>::iterator end = engines.end(); |
442 |
for (; iter != end; ++iter) (*iter)->Suspend(pRegion); |
for (; iter != end; ++iter) (*iter)->Suspend(pRegion); |
443 |
Unlock(); |
Unlock(); |
444 |
} else { |
} else { |
471 |
GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/); |
GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/); |
472 |
for (int i = 0; i < instruments.size(); i++) { |
for (int i = 0; i < instruments.size(); i++) { |
473 |
if (SampleReferencedByInstrument(pSample, instruments[i])) { |
if (SampleReferencedByInstrument(pSample, instruments[i])) { |
474 |
std::set<gig::EngineChannel*> engineChannels = |
std::set<EngineChannel*> engineChannels = |
475 |
GetEngineChannelsUsing(instruments[i], false/*don't lock again*/); |
GetEngineChannelsUsing(instruments[i], false/*don't lock again*/); |
476 |
std::set<gig::EngineChannel*>::iterator iter = engineChannels.begin(); |
std::set<EngineChannel*>::iterator iter = engineChannels.begin(); |
477 |
std::set<gig::EngineChannel*>::iterator end = engineChannels.end(); |
std::set<EngineChannel*>::iterator end = engineChannels.end(); |
478 |
for (; iter != end; ++iter) |
for (; iter != end; ++iter) |
479 |
CacheInitialSamples(pSample, *iter); |
CacheInitialSamples(pSample, *iter); |
480 |
} |
} |
487 |
::gig::Instrument* pInstrument = |
::gig::Instrument* pInstrument = |
488 |
(::gig::Instrument*) pRegion->GetParent(); |
(::gig::Instrument*) pRegion->GetParent(); |
489 |
Lock(); |
Lock(); |
490 |
std::set<gig::Engine*> engines = |
std::set<Engine*> engines = |
491 |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
492 |
std::set<gig::Engine*>::iterator iter = engines.begin(); |
std::set<Engine*>::iterator iter = engines.begin(); |
493 |
std::set<gig::Engine*>::iterator end = engines.end(); |
std::set<Engine*>::iterator end = engines.end(); |
494 |
for (; iter != end; ++iter) (*iter)->Resume(pRegion); |
for (; iter != end; ++iter) (*iter)->Resume(pRegion); |
495 |
Unlock(); |
Unlock(); |
496 |
} else if (sStructType == "gig::DimensionRegion") { |
} else if (sStructType == "gig::DimensionRegion") { |
502 |
::gig::Instrument* pInstrument = |
::gig::Instrument* pInstrument = |
503 |
(::gig::Instrument*) pRegion->GetParent(); |
(::gig::Instrument*) pRegion->GetParent(); |
504 |
Lock(); |
Lock(); |
505 |
std::set<gig::Engine*> engines = |
std::set<Engine*> engines = |
506 |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
GetEnginesUsing(pInstrument, false/*don't lock again*/); |
507 |
std::set<gig::Engine*>::iterator iter = engines.begin(); |
std::set<Engine*>::iterator iter = engines.begin(); |
508 |
std::set<gig::Engine*>::iterator end = engines.end(); |
std::set<Engine*>::iterator end = engines.end(); |
509 |
for (; iter != end; ++iter) (*iter)->Resume(pRegion); |
for (; iter != end; ++iter) (*iter)->Resume(pRegion); |
510 |
Unlock(); |
Unlock(); |
511 |
} else { |
} else { |
541 |
::gig::Sample* pSample = (::gig::Sample*) pNewSample; |
::gig::Sample* pSample = (::gig::Sample*) pNewSample; |
542 |
::gig::File* pFile = (::gig::File*) pSample->GetParent(); |
::gig::File* pFile = (::gig::File*) pSample->GetParent(); |
543 |
// get all engines that use that same gig::File |
// get all engines that use that same gig::File |
544 |
std::set<gig::Engine*> engines = GetEnginesUsing(pFile, false/*don't lock again*/); |
std::set<Engine*> engines = GetEnginesUsing(pFile, false/*don't lock again*/); |
545 |
std::set<gig::Engine*>::iterator iter = engines.begin(); |
std::set<Engine*>::iterator iter = engines.begin(); |
546 |
std::set<gig::Engine*>::iterator end = engines.end(); |
std::set<Engine*>::iterator end = engines.end(); |
547 |
for (; iter != end; ++iter) |
for (; iter != end; ++iter) |
548 |
CacheInitialSamples(pSample, *iter); |
CacheInitialSamples(pSample, *iter); |
549 |
Unlock(); |
Unlock(); |
552 |
|
|
553 |
::gig::Instrument* InstrumentResourceManager::Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg) { |
::gig::Instrument* InstrumentResourceManager::Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg) { |
554 |
// get gig file from internal gig file manager |
// get gig file from internal gig file manager |
555 |
::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 :/ |
556 |
|
|
557 |
// we pass this to the progress callback mechanism of libgig |
// we pass this to the progress callback mechanism of libgig |
558 |
progress_callback_arg_t callbackArg; |
progress_callback_arg_t callbackArg; |
584 |
|
|
585 |
if (pRgn->GetSample() && !pRgn->GetSample()->GetCache().Size) { |
if (pRgn->GetSample() && !pRgn->GetSample()->GetCache().Size) { |
586 |
dmsg(2,("C")); |
dmsg(2,("C")); |
587 |
CacheInitialSamples(pRgn->GetSample(), (gig::EngineChannel*) pConsumer); |
CacheInitialSamples(pRgn->GetSample(), (EngineChannel*) pConsumer); |
588 |
} |
} |
589 |
for (uint i = 0; i < pRgn->DimensionRegions; i++) { |
for (uint i = 0; i < pRgn->DimensionRegions; i++) { |
590 |
CacheInitialSamples(pRgn->pDimensionRegions[i]->pSample, (gig::EngineChannel*) pConsumer); |
CacheInitialSamples(pRgn->pDimensionRegions[i]->pSample, (EngineChannel*) pConsumer); |
591 |
} |
} |
592 |
|
|
593 |
pRgn = pInstrument->GetNextRegion(); |
pRgn = pInstrument->GetNextRegion(); |
602 |
pEntry->ID.Index = Key.Index; |
pEntry->ID.Index = Key.Index; |
603 |
pEntry->pGig = pGig; |
pEntry->pGig = pGig; |
604 |
|
|
605 |
gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(pConsumer); |
// (try to resolve the audio device context) |
606 |
|
EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(pConsumer); |
607 |
|
AudioOutputDevice* pDevice = |
608 |
|
(pEngineChannel) ? dynamic_cast<Engine*>(pEngineChannel->GetEngine())->pAudioOutputDevice : NULL; |
609 |
|
|
610 |
// and we save this to check if we need to reallocate for a engine with higher value of 'MaxSamplesPerSecond' |
// and we save this to check if we need to reallocate for a engine with higher value of 'MaxSamplesPerSecond' |
611 |
pEntry->MaxSamplesPerCycle = |
pEntry->MaxSamplesPerCycle = |
612 |
(!pEngineChannel) ? 0 /* don't care for instrument editors */ : |
(pDevice) ? pDevice->MaxSamplesPerCycle() : DefaultMaxSamplesPerCycle(); |
613 |
(pEngineChannel->GetEngine()) ? |
|
|
dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle() |
|
|
: GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE; |
|
614 |
pArg = pEntry; |
pArg = pEntry; |
615 |
|
|
616 |
return pInstrument; |
return pInstrument; |
619 |
void InstrumentResourceManager::Destroy( ::gig::Instrument* pResource, void* pArg) { |
void InstrumentResourceManager::Destroy( ::gig::Instrument* pResource, void* pArg) { |
620 |
instr_entry_t* pEntry = (instr_entry_t*) pArg; |
instr_entry_t* pEntry = (instr_entry_t*) pArg; |
621 |
// we don't need the .gig file here anymore |
// we don't need the .gig file here anymore |
622 |
Gigs.HandBack(pEntry->pGig, (GigConsumer*) pEntry->ID.Index); // conversion kinda hackish :/ |
Gigs.HandBack(pEntry->pGig, reinterpret_cast<GigConsumer*>(pEntry->ID.Index)); // conversion kinda hackish :/ |
623 |
delete pEntry; |
delete pEntry; |
624 |
} |
} |
625 |
|
|
626 |
void InstrumentResourceManager::OnBorrow(::gig::Instrument* pResource, InstrumentConsumer* pConsumer, void*& pArg) { |
void InstrumentResourceManager::OnBorrow(::gig::Instrument* pResource, InstrumentConsumer* pConsumer, void*& pArg) { |
627 |
instr_entry_t* pEntry = (instr_entry_t*) pArg; |
instr_entry_t* pEntry = (instr_entry_t*) pArg; |
628 |
gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(pConsumer); |
|
629 |
|
// (try to resolve the audio device context) |
630 |
|
EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(pConsumer); |
631 |
|
AudioOutputDevice* pDevice = |
632 |
|
(pEngineChannel) ? dynamic_cast<Engine*>(pEngineChannel->GetEngine())->pAudioOutputDevice : NULL; |
633 |
|
|
634 |
uint maxSamplesPerCycle = |
uint maxSamplesPerCycle = |
635 |
(pEngineChannel && pEngineChannel->GetEngine()) ? dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle() |
(pDevice) ? pDevice->MaxSamplesPerCycle() : DefaultMaxSamplesPerCycle(); |
636 |
: GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE; |
|
637 |
if (pEntry->MaxSamplesPerCycle < maxSamplesPerCycle) { |
if (pEntry->MaxSamplesPerCycle < maxSamplesPerCycle) { |
638 |
|
dmsg(1,("Completely reloading instrument due to insufficient precached samples ...\n")); |
639 |
Update(pResource, pConsumer); |
Update(pResource, pConsumer); |
640 |
} |
} |
641 |
} |
} |
642 |
|
|
643 |
/** |
void InstrumentResourceManager::DeleteRegionIfNotUsed(::gig::DimensionRegion* pRegion, region_info_t* pRegInfo) { |
644 |
* Give back an instrument. This should be used instead of |
// TODO: we could delete Region and Instrument here if they have become unused |
|
* HandBack if there are some dimension regions that are still in |
|
|
* use. (When an instrument is changed, the voices currently |
|
|
* playing are allowed to keep playing with the old instrument |
|
|
* until note off arrives. New notes will use the new instrument.) |
|
|
*/ |
|
|
void InstrumentResourceManager::HandBackInstrument(::gig::Instrument* pResource, InstrumentConsumer* pConsumer, |
|
|
RTList< ::gig::DimensionRegion*>* pDimRegionsInUse) { |
|
|
DimRegInfoMutex.Lock(); |
|
|
for (RTList< ::gig::DimensionRegion*>::Iterator i = pDimRegionsInUse->first() ; i != pDimRegionsInUse->end() ; i++) { |
|
|
DimRegInfo[*i].refCount++; |
|
|
SampleRefCount[(*i)->pSample]++; |
|
|
} |
|
|
HandBack(pResource, pConsumer, true); |
|
|
DimRegInfoMutex.Unlock(); |
|
645 |
} |
} |
646 |
|
|
647 |
/** |
void InstrumentResourceManager::DeleteSampleIfNotUsed(::gig::Sample* pSample, region_info_t* pRegInfo) { |
648 |
* Give back a dimension region that belongs to an instrument that |
::gig::File* gig = pRegInfo->file; |
649 |
* was previously handed back. |
::RIFF::File* riff = static_cast< ::RIFF::File*>(pRegInfo->pArg); |
650 |
*/ |
if (gig) { |
651 |
void InstrumentResourceManager::HandBackDimReg(::gig::DimensionRegion* pDimReg) { |
gig->DeleteSample(pSample); |
652 |
DimRegInfoMutex.Lock(); |
if (!gig->GetFirstSample()) { |
653 |
dimreg_info_t& dimRegInfo = DimRegInfo[pDimReg]; |
dmsg(2,("No more samples in use - freeing gig\n")); |
654 |
int dimRegRefCount = --dimRegInfo.refCount; |
delete gig; |
655 |
int sampleRefCount = --SampleRefCount[pDimReg->pSample]; |
delete riff; |
|
if (dimRegRefCount == 0) { |
|
|
::gig::File* gig = dimRegInfo.file; |
|
|
::RIFF::File* riff = dimRegInfo.riff; |
|
|
DimRegInfo.erase(pDimReg); |
|
|
// TODO: we could delete Region and Instrument here if |
|
|
// they have become unused |
|
|
|
|
|
if (sampleRefCount == 0) { |
|
|
SampleRefCount.erase(pDimReg->pSample); |
|
|
|
|
|
if (gig) { |
|
|
gig->DeleteSample(pDimReg->pSample); |
|
|
if (!gig->GetFirstSample()) { |
|
|
dmsg(2,("No more samples in use - freeing gig\n")); |
|
|
delete gig; |
|
|
delete riff; |
|
|
} |
|
|
} |
|
656 |
} |
} |
657 |
} |
} |
|
DimRegInfoMutex.Unlock(); |
|
658 |
} |
} |
659 |
|
|
660 |
/** |
/** |
665 |
* (may be NULL, in this case default amount of samples |
* (may be NULL, in this case default amount of samples |
666 |
* will be cached) |
* will be cached) |
667 |
*/ |
*/ |
668 |
void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, gig::EngineChannel* pEngineChannel) { |
void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, EngineChannel* pEngineChannel) { |
669 |
gig::Engine* pEngine = |
Engine* pEngine = |
670 |
(pEngineChannel && pEngineChannel->GetEngine()) ? |
(pEngineChannel && pEngineChannel->GetEngine()) ? |
671 |
dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine()) : NULL; |
dynamic_cast<Engine*>(pEngineChannel->GetEngine()) : NULL; |
672 |
CacheInitialSamples(pSample, pEngine); |
CacheInitialSamples(pSample, pEngine); |
673 |
} |
} |
674 |
|
|
683 |
* (may be NULL, in this case default amount of samples |
* (may be NULL, in this case default amount of samples |
684 |
* will be cached) |
* will be cached) |
685 |
*/ |
*/ |
686 |
void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, gig::Engine* pEngine) { |
void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, AbstractEngine* pEngine) { |
687 |
if (!pSample) { |
if (!pSample) { |
688 |
dmsg(4,("gig::InstrumentResourceManager: Skipping sample (pSample == NULL)\n")); |
dmsg(4,("gig::InstrumentResourceManager: Skipping sample (pSample == NULL)\n")); |
689 |
return; |
return; |
698 |
// the sample. |
// the sample. |
699 |
const uint maxSamplesPerCycle = |
const uint maxSamplesPerCycle = |
700 |
(pEngine) ? pEngine->pAudioOutputDevice->MaxSamplesPerCycle() |
(pEngine) ? pEngine->pAudioOutputDevice->MaxSamplesPerCycle() |
701 |
: GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE; |
: DefaultMaxSamplesPerCycle(); |
702 |
const uint neededSilenceSamples = (maxSamplesPerCycle << CONFIG_MAX_PITCH) + 3; |
const uint neededSilenceSamples = (maxSamplesPerCycle << CONFIG_MAX_PITCH) + 3; |
703 |
const uint currentlyCachedSilenceSamples = pSample->GetCache().NullExtensionSize / pSample->FrameSize; |
const uint currentlyCachedSilenceSamples = pSample->GetCache().NullExtensionSize / pSample->FrameSize; |
704 |
if (currentlyCachedSilenceSamples < neededSilenceSamples) { |
if (currentlyCachedSilenceSamples < neededSilenceSamples) { |
748 |
* @param bLock - whether we should lock (mutex) the instrument manager |
* @param bLock - whether we should lock (mutex) the instrument manager |
749 |
* during this call and unlock at the end of this call |
* during this call and unlock at the end of this call |
750 |
*/ |
*/ |
751 |
std::set<gig::EngineChannel*> InstrumentResourceManager::GetEngineChannelsUsing(::gig::Instrument* pInstrument, bool bLock) { |
std::set<EngineChannel*> InstrumentResourceManager::GetEngineChannelsUsing(::gig::Instrument* pInstrument, bool bLock) { |
752 |
if (bLock) Lock(); |
if (bLock) Lock(); |
753 |
std::set<gig::EngineChannel*> result; |
std::set<EngineChannel*> result; |
754 |
std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument); |
std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument); |
755 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin(); |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin(); |
756 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end = consumers.end(); |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end = consumers.end(); |
757 |
for (; iter != end; ++iter) { |
for (; iter != end; ++iter) { |
758 |
gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(*iter); |
EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(*iter); |
759 |
if (!pEngineChannel) continue; |
if (!pEngineChannel) continue; |
760 |
result.insert(pEngineChannel); |
result.insert(pEngineChannel); |
761 |
} |
} |
771 |
* @param bLock - whether we should lock (mutex) the instrument manager |
* @param bLock - whether we should lock (mutex) the instrument manager |
772 |
* during this call and unlock at the end of this call |
* during this call and unlock at the end of this call |
773 |
*/ |
*/ |
774 |
std::set<gig::Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::Instrument* pInstrument, bool bLock) { |
std::set<Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::Instrument* pInstrument, bool bLock) { |
775 |
if (bLock) Lock(); |
if (bLock) Lock(); |
776 |
std::set<gig::Engine*> result; |
std::set<Engine*> result; |
777 |
std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument); |
std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument); |
778 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin(); |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin(); |
779 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end = consumers.end(); |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end = consumers.end(); |
780 |
for (; iter != end; ++iter) { |
for (; iter != end; ++iter) { |
781 |
gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(*iter); |
EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(*iter); |
782 |
if (!pEngineChannel) continue; |
if (!pEngineChannel) continue; |
783 |
gig::Engine* pEngine = dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine()); |
Engine* pEngine = dynamic_cast<Engine*>(pEngineChannel->GetEngine()); |
784 |
if (!pEngine) continue; |
if (!pEngine) continue; |
785 |
result.insert(pEngine); |
result.insert(pEngine); |
786 |
} |
} |
796 |
* @param bLock - whether we should lock (mutex) the instrument manager |
* @param bLock - whether we should lock (mutex) the instrument manager |
797 |
* during this call and unlock at the end of this call |
* during this call and unlock at the end of this call |
798 |
*/ |
*/ |
799 |
std::set<gig::Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::File* pFile, bool bLock) { |
std::set<Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::File* pFile, bool bLock) { |
800 |
if (bLock) Lock(); |
if (bLock) Lock(); |
801 |
// get all instruments (currently in usage) that use that same gig::File |
// get all instruments (currently in usage) that use that same gig::File |
802 |
std::vector< ::gig::Instrument*> instrumentsOfInterest = |
std::vector< ::gig::Instrument*> instrumentsOfInterest = |
803 |
GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/); |
GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/); |
804 |
|
|
805 |
// get all engines that use that same gig::File |
// get all engines that use that same gig::File |
806 |
std::set<gig::Engine*> result; |
std::set<Engine*> result; |
807 |
{ |
{ |
808 |
for (int i = 0; i < instrumentsOfInterest.size(); i++) { |
for (int i = 0; i < instrumentsOfInterest.size(); i++) { |
809 |
std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(instrumentsOfInterest[i]); |
std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(instrumentsOfInterest[i]); |
810 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin(); |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin(); |
811 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end = consumers.end(); |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end = consumers.end(); |
812 |
for (; iter != end; ++iter) { |
for (; iter != end; ++iter) { |
813 |
gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(*iter); |
EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(*iter); |
814 |
if (!pEngineChannel) continue; |
if (!pEngineChannel) continue; |
815 |
gig::Engine* pEngine = dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine()); |
Engine* pEngine = dynamic_cast<Engine*>(pEngineChannel->GetEngine()); |
816 |
if (!pEngine) continue; |
if (!pEngine) continue; |
817 |
// the unique, sorted container std::set makes |
// the unique, sorted container std::set makes |
818 |
// sure we won't have duplicates |
// sure we won't have duplicates |
866 |
// get all engines that use that same gig::Instrument |
// get all engines that use that same gig::Instrument |
867 |
suspendedEngines = GetEnginesUsing(pInstrument, true/*lock*/); |
suspendedEngines = GetEnginesUsing(pInstrument, true/*lock*/); |
868 |
// finally, completely suspend all engines that use that same gig::Instrument |
// finally, completely suspend all engines that use that same gig::Instrument |
869 |
std::set<gig::Engine*>::iterator iter = suspendedEngines.begin(); |
std::set<Engine*>::iterator iter = suspendedEngines.begin(); |
870 |
std::set<gig::Engine*>::iterator end = suspendedEngines.end(); |
std::set<Engine*>::iterator end = suspendedEngines.end(); |
871 |
for (; iter != end; ++iter) (*iter)->SuspendAll(); |
for (; iter != end; ++iter) (*iter)->SuspendAll(); |
872 |
} |
} |
873 |
|
|
890 |
// get all engines that use that same gig::File |
// get all engines that use that same gig::File |
891 |
suspendedEngines = GetEnginesUsing(pFile, true/*lock*/); |
suspendedEngines = GetEnginesUsing(pFile, true/*lock*/); |
892 |
// finally, completely suspend all engines that use that same gig::File |
// finally, completely suspend all engines that use that same gig::File |
893 |
std::set<gig::Engine*>::iterator iter = suspendedEngines.begin(); |
std::set<Engine*>::iterator iter = suspendedEngines.begin(); |
894 |
std::set<gig::Engine*>::iterator end = suspendedEngines.end(); |
std::set<Engine*>::iterator end = suspendedEngines.end(); |
895 |
for (; iter != end; ++iter) (*iter)->SuspendAll(); |
for (; iter != end; ++iter) (*iter)->SuspendAll(); |
896 |
} |
} |
897 |
|
|
926 |
} |
} |
927 |
|
|
928 |
void InstrumentResourceManager::GigResourceManager::Destroy(::gig::File* pResource, void* pArg) { |
void InstrumentResourceManager::GigResourceManager::Destroy(::gig::File* pResource, void* pArg) { |
929 |
dmsg(1,("Freeing gig file from memory...")); |
dmsg(1,("Freeing gig file '%s' from memory ...", pResource->GetFileName().c_str())); |
930 |
|
|
931 |
// Delete as much as possible of the gig file. Some of the |
// Delete as much as possible of the gig file. Some of the |
932 |
// dimension regions and samples may still be in use - these |
// dimension regions and samples may still be in use - these |
947 |
for (int i = 0 ; i < region->DimensionRegions ; i++) |
for (int i = 0 ; i < region->DimensionRegions ; i++) |
948 |
{ |
{ |
949 |
::gig::DimensionRegion *d = region->pDimensionRegions[i]; |
::gig::DimensionRegion *d = region->pDimensionRegions[i]; |
950 |
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); |
951 |
if (iter != parent->DimRegInfo.end()) { |
if (iter != parent->RegionInfo.end()) { |
952 |
dimreg_info_t& dimRegInfo = (*iter).second; |
region_info_t& dimRegInfo = (*iter).second; |
953 |
dimRegInfo.file = pResource; |
dimRegInfo.file = pResource; |
954 |
dimRegInfo.riff = (::RIFF::File*)pArg; |
dimRegInfo.pArg = (::RIFF::File*)pArg; |
955 |
deleteFile = deleteInstrument = deleteRegion = false; |
deleteFile = deleteInstrument = deleteRegion = false; |
956 |
} |
} |
957 |
} |
} |