--- linuxsampler/trunk/src/engines/gig/InstrumentResourceManager.cpp 2005/02/26 02:01:14 411 +++ linuxsampler/trunk/src/engines/gig/InstrumentResourceManager.cpp 2005/03/03 03:25:17 420 @@ -105,24 +105,32 @@ * @param pSample - points to the sample to be cached * @param pEngineChannel - pointer to Gig Engine Channel which caused this call */ - void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, gig::EngineChannel* pEngineChannel) { - if (!pSample || pSample->GetCache().Size || !pSample->SamplesTotal) return; + void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, gig::EngineChannel* pEngineChannel) { + if (!pSample) { + dmsg(1,("gig::InstrumentResourceManager: Warning, skipping sample (pSample == NULL)\n")); + return; + } + if (!pSample->SamplesTotal) return; // skip zero size samples + if (pSample->SamplesTotal <= NUM_RAM_PRELOAD_SAMPLES) { // Sample is too short for disk streaming, so we load the whole // sample into RAM and place 'pAudioIO->FragmentSize << MAX_PITCH' // 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. - dmsg(3,("Caching whole sample (sample name: \"%s\", sample size: %d)\n", pSample->pInfo->Name.c_str(), pSample->SamplesTotal)); + // the sample. const uint maxSamplesPerCycle = (pEngineChannel->GetEngine()) ? dynamic_cast(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle() : GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE; - const uint silenceSamples = (maxSamplesPerCycle << MAX_PITCH) + 3; - ::gig::buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension(silenceSamples); - dmsg(4,("Cached %d Bytes, %d silence bytes.\n", buf.Size, buf.NullExtensionSize)); + const uint neededSilenceSamples = (maxSamplesPerCycle << MAX_PITCH) + 3; + const uint currentlyCachedSilenceSamples = pSample->GetCache().NullExtensionSize / pSample->FrameSize; + if (currentlyCachedSilenceSamples < neededSilenceSamples) { + dmsg(3,("Caching whole sample (sample name: \"%s\", sample size: %d)\n", pSample->pInfo->Name.c_str(), pSample->SamplesTotal)); + ::gig::buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension(neededSilenceSamples); + dmsg(4,("Cached %d Bytes, %d silence bytes.\n", buf.Size, buf.NullExtensionSize)); + } } else { // we only cache NUM_RAM_PRELOAD_SAMPLES and stream the other sample points from disk - pSample->LoadSampleData(NUM_RAM_PRELOAD_SAMPLES); + if (!pSample->GetCache().Size) pSample->LoadSampleData(NUM_RAM_PRELOAD_SAMPLES); } if (!pSample->GetCache().Size) std::cerr << "Unable to cache sample - maybe memory full!" << std::endl << std::flush;