--- linuxsampler/trunk/src/engines/gig/InstrumentResourceManager.cpp 2009/10/23 17:53:17 2012 +++ linuxsampler/trunk/src/engines/gig/InstrumentResourceManager.cpp 2012/01/30 10:40:19 2309 @@ -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 - 2011 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,13 +30,6 @@ #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 @@ -559,7 +552,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; @@ -609,13 +602,14 @@ pEntry->ID.Index = Key.Index; pEntry->pGig = pGig; + // (try to resolve the audio device context) EngineChannel* pEngineChannel = dynamic_cast(pConsumer); + Engine* pEngine = dynamic_cast(pEngineChannel->GetEngine()); + AudioOutputDevice* pDevice = (pEngine) ? pEngine->pAudioOutputDevice : NULL; // 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; + (pDevice) ? pDevice->MaxSamplesPerCycle() : DefaultMaxSamplesPerCycle(); + pArg = pEntry; return pInstrument; @@ -624,17 +618,23 @@ 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->pGig, 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; + + // (try to resolve the audio device context) EngineChannel* pEngineChannel = dynamic_cast(pConsumer); + Engine* pEngine = pEngineChannel ? dynamic_cast(pEngineChannel->GetEngine()) : NULL; + AudioOutputDevice* pDevice = (pEngine) ? pEngine->pAudioOutputDevice : NULL; + uint maxSamplesPerCycle = - (pEngineChannel && pEngineChannel->GetEngine()) ? dynamic_cast(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle() - : GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE; + (pDevice) ? pDevice->MaxSamplesPerCycle() : DefaultMaxSamplesPerCycle(); + if (pEntry->MaxSamplesPerCycle < maxSamplesPerCycle) { + dmsg(1,("Completely reloading instrument due to insufficient precached samples ...\n")); Update(pResource, pConsumer); } } @@ -642,6 +642,7 @@ 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); @@ -696,7 +697,7 @@ // the sample. const uint maxSamplesPerCycle = (pEngine) ? pEngine->pAudioOutputDevice->MaxSamplesPerCycle() - : GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE; + : DefaultMaxSamplesPerCycle(); const uint neededSilenceSamples = (maxSamplesPerCycle << CONFIG_MAX_PITCH) + 3; const uint currentlyCachedSilenceSamples = pSample->GetCache().NullExtensionSize / pSample->FrameSize; if (currentlyCachedSilenceSamples < neededSilenceSamples) { @@ -924,7 +925,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