--- linuxsampler/trunk/src/engines/gig/InstrumentResourceManager.cpp 2009/10/23 17:53:17 2012 +++ linuxsampler/trunk/src/engines/gig/InstrumentResourceManager.cpp 2012/03/10 16:16:14 2327 @@ -3,7 +3,7 @@ * LinuxSampler - modular, streaming capable sampler * * * * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * - * Copyright (C) 2005 - 2009 Christian Schoenebeck * + * Copyright (C) 2005 - 2012 Christian Schoenebeck * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -30,22 +30,8 @@ #include "../../common/global_private.h" #include "../../plugins/InstrumentEditorFactory.h" -// 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 - namespace LinuxSampler { namespace gig { - // 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 - }; - // some data needed for the libgig callback function struct progress_callback_arg_t { InstrumentResourceManager* pManager; @@ -90,10 +76,6 @@ pArg->pManager->DispatchResourceProgressEvent(*pArg->pInstrumentKey, localProgress); } - std::vector InstrumentResourceManager::Instruments() { - return Entries(); - } - String InstrumentResourceManager::GetInstrumentName(instrument_id_t ID) { Lock(); ::gig::Instrument* pInstrument = Resource(ID, false); @@ -147,7 +129,7 @@ ::RIFF::File* riff = NULL; ::gig::File* gig = NULL; try { - if(!loaded) { + if (!loaded) { riff = new ::RIFF::File(ID.FileName); gig = new ::gig::File(riff); gig->SetAutoLoad(false); // avoid time consuming samples scanning @@ -559,7 +541,7 @@ ::gig::Instrument* InstrumentResourceManager::Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg) { // get gig file from internal gig file manager - ::gig::File* pGig = Gigs.Borrow(Key.FileName, (GigConsumer*) Key.Index); // conversion kinda hackish :/ + ::gig::File* pGig = Gigs.Borrow(Key.FileName, reinterpret_cast(Key.Index)); // conversion kinda hackish :/ // we pass this to the progress callback mechanism of libgig progress_callback_arg_t callbackArg; @@ -580,6 +562,8 @@ pGig->GetFirstSample(); // just to force complete instrument loading dmsg(1,("OK\n")); + uint maxSamplesPerCycle = GetMaxSamplesPerCycle(pConsumer); + // cache initial samples points (for actually needed samples) dmsg(1,("Caching initial samples...")); uint iRegion = 0; // just for progress calculation @@ -591,10 +575,10 @@ if (pRgn->GetSample() && !pRgn->GetSample()->GetCache().Size) { dmsg(2,("C")); - CacheInitialSamples(pRgn->GetSample(), (EngineChannel*) pConsumer); + CacheInitialSamples(pRgn->GetSample(), maxSamplesPerCycle); } for (uint i = 0; i < pRgn->DimensionRegions; i++) { - CacheInitialSamples(pRgn->pDimensionRegions[i]->pSample, (EngineChannel*) pConsumer); + CacheInitialSamples(pRgn->pDimensionRegions[i]->pSample, maxSamplesPerCycle); } pRgn = pInstrument->GetNextRegion(); @@ -607,41 +591,27 @@ instr_entry_t* pEntry = new instr_entry_t; pEntry->ID.FileName = Key.FileName; pEntry->ID.Index = Key.Index; - pEntry->pGig = pGig; + pEntry->pFile = pGig; - EngineChannel* pEngineChannel = dynamic_cast(pConsumer); - // and we save this to check if we need to reallocate for a engine with higher value of 'MaxSamplesPerSecond' - pEntry->MaxSamplesPerCycle = - (!pEngineChannel) ? 0 /* don't care for instrument editors */ : - (pEngineChannel->GetEngine()) ? - dynamic_cast(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle() - : GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE; + // and we save this to check if we need to reallocate for an engine with higher value of 'MaxSamplesPerSecond' + pEntry->MaxSamplesPerCycle = maxSamplesPerCycle; + pArg = pEntry; return pInstrument; } - void InstrumentResourceManager::Destroy( ::gig::Instrument* pResource, void* pArg) { + void InstrumentResourceManager::Destroy(::gig::Instrument* pResource, void* pArg) { instr_entry_t* pEntry = (instr_entry_t*) pArg; // we don't need the .gig file here anymore - Gigs.HandBack(pEntry->pGig, (GigConsumer*) pEntry->ID.Index); // conversion kinda hackish :/ + Gigs.HandBack(pEntry->pFile, reinterpret_cast(pEntry->ID.Index)); // conversion kinda hackish :/ delete pEntry; } - void InstrumentResourceManager::OnBorrow(::gig::Instrument* pResource, InstrumentConsumer* pConsumer, void*& pArg) { - instr_entry_t* pEntry = (instr_entry_t*) pArg; - EngineChannel* pEngineChannel = dynamic_cast(pConsumer); - uint maxSamplesPerCycle = - (pEngineChannel && pEngineChannel->GetEngine()) ? dynamic_cast(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle() - : GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE; - if (pEntry->MaxSamplesPerCycle < maxSamplesPerCycle) { - Update(pResource, pConsumer); - } - } - void InstrumentResourceManager::DeleteRegionIfNotUsed(::gig::DimensionRegion* pRegion, region_info_t* pRegInfo) { // TODO: we could delete Region and Instrument here if they have become unused } + void InstrumentResourceManager::DeleteSampleIfNotUsed(::gig::Sample* pSample, region_info_t* pRegInfo) { ::gig::File* gig = pRegInfo->file; ::RIFF::File* riff = static_cast< ::RIFF::File*>(pRegInfo->pArg); @@ -682,6 +652,13 @@ * will be cached) */ void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, AbstractEngine* pEngine) { + uint maxSamplesPerCycle = + (pEngine) ? pEngine->pAudioOutputDevice->MaxSamplesPerCycle() : + DefaultMaxSamplesPerCycle(); + CacheInitialSamples(pSample, maxSamplesPerCycle); + } + + void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, uint maxSamplesPerCycle) { if (!pSample) { dmsg(4,("gig::InstrumentResourceManager: Skipping sample (pSample == NULL)\n")); return; @@ -694,9 +671,6 @@ // number of '0' samples (silence samples) behind the official buffer // border, to allow the interpolator do it's work even at the end of // the sample. - const uint maxSamplesPerCycle = - (pEngine) ? pEngine->pAudioOutputDevice->MaxSamplesPerCycle() - : GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE; const uint neededSilenceSamples = (maxSamplesPerCycle << CONFIG_MAX_PITCH) + 3; const uint currentlyCachedSilenceSamples = pSample->GetCache().NullExtensionSize / pSample->FrameSize; if (currentlyCachedSilenceSamples < neededSilenceSamples) { @@ -924,7 +898,7 @@ } void InstrumentResourceManager::GigResourceManager::Destroy(::gig::File* pResource, void* pArg) { - dmsg(1,("Freeing gig file from memory...")); + dmsg(1,("Freeing gig file '%s' from memory ...", pResource->GetFileName().c_str())); // Delete as much as possible of the gig file. Some of the // dimension regions and samples may still be in use - these