--- linuxsampler/trunk/src/engines/gig/InstrumentResourceManager.cpp 2004/04/27 09:21:58 56 +++ linuxsampler/trunk/src/engines/gig/InstrumentResourceManager.cpp 2005/05/08 00:26:21 517 @@ -3,6 +3,7 @@ * LinuxSampler - modular, streaming capable sampler * * * * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * + * Copyright (C) 2005 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 * @@ -24,14 +25,53 @@ #include "InstrumentResourceManager.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 { + // some data needed for the libgig callback function + struct progress_callback_arg_t { + InstrumentResourceManager* pManager; + instrument_id_t* pInstrumentKey; + }; + + /** + * Callback function which will be called by libgig during loading of + * instruments to inform about the current progress. Or to be more + * specific; it will be called during the GetInstrument() call. + * + * @param pProgress - contains current progress value, pointer to the + * InstrumentResourceManager instance and + * instrument ID + */ + void InstrumentResourceManager::OnInstrumentLoadingProgress(::gig::progress_t* pProgress) { + dmsg(7,("gig::InstrumentResourceManager: progress %f%", pProgress->factor)); + progress_callback_arg_t* pArg = static_cast(pProgress->custom); + // we randomly schedule 90% for the .gig file loading and the remaining 10% later for sample caching + const float localProgress = 0.9f * pProgress->factor; + pArg->pManager->DispatchResourceProgressEvent(*pArg->pInstrumentKey, localProgress); + } + ::gig::Instrument* InstrumentResourceManager::Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg) { // get gig file from inernal gig file manager ::gig::File* pGig = Gigs.Borrow(Key.FileName, (GigConsumer*) Key.iInstrument); // conversion kinda hackish :/ + // we pass this to the progress callback mechanism of libgig + progress_callback_arg_t callbackArg; + callbackArg.pManager = this; + callbackArg.pInstrumentKey = &Key; + + ::gig::progress_t progress; + progress.callback = OnInstrumentLoadingProgress; + progress.custom = &callbackArg; + dmsg(1,("Loading gig instrument...")); - ::gig::Instrument* pInstrument = pGig->GetInstrument(Key.iInstrument); + ::gig::Instrument* pInstrument = pGig->GetInstrument(Key.iInstrument, &progress); if (!pInstrument) { std::stringstream msg; msg << "There's no instrument with index " << Key.iInstrument << "."; @@ -42,26 +82,37 @@ // cache initial samples points (for actually needed samples) dmsg(1,("Caching initial samples...")); + uint iRegion = 0; // just for progress calculation ::gig::Region* pRgn = pInstrument->GetFirstRegion(); while (pRgn) { - if (!pRgn->GetSample()->GetCache().Size) { + // we randomly schedule 90% for the .gig file loading and the remaining 10% now for sample caching + const float localProgress = 0.9f + 0.1f * (float) iRegion / (float) pInstrument->Regions; + DispatchResourceProgressEvent(Key, localProgress); + + if (pRgn->GetSample() && !pRgn->GetSample()->GetCache().Size) { dmsg(2,("C")); - CacheInitialSamples(pRgn->GetSample(), dynamic_cast(pConsumer)); + CacheInitialSamples(pRgn->GetSample(), dynamic_cast(pConsumer)); } for (uint i = 0; i < pRgn->DimensionRegions; i++) { - CacheInitialSamples(pRgn->pDimensionRegions[i]->pSample, dynamic_cast(pConsumer)); + CacheInitialSamples(pRgn->pDimensionRegions[i]->pSample, dynamic_cast(pConsumer)); } pRgn = pInstrument->GetNextRegion(); + iRegion++; } dmsg(1,("OK\n")); + DispatchResourceProgressEvent(Key, 1.0f); // done; notify all consumers about progress 100% // we need the following for destruction later instr_entry_t* pEntry = new instr_entry_t; pEntry->iInstrument = Key.iInstrument; pEntry->pGig = pGig; - // and this to check if we need to reallocate for a engine with higher value of 'MaxSamplesPerSecond' - pEntry->MaxSamplesPerCycle = dynamic_cast(pConsumer)->pAudioOutputDevice->MaxSamplesPerCycle(); + + gig::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->GetEngine()) ? dynamic_cast(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle() + : GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE; pArg = pEntry; return pInstrument; @@ -75,7 +126,11 @@ void InstrumentResourceManager::OnBorrow(::gig::Instrument* pResource, InstrumentConsumer* pConsumer, void*& pArg) { instr_entry_t* pEntry = (instr_entry_t*) pArg; - if (pEntry->MaxSamplesPerCycle < dynamic_cast(pConsumer)->pAudioOutputDevice->MaxSamplesPerCycle()) { + gig::EngineChannel* pEngineChannel = dynamic_cast(pConsumer); + uint maxSamplesPerCycle = + (pEngineChannel->GetEngine()) ? dynamic_cast(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle() + : GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE; + if (pEntry->MaxSamplesPerCycle < maxSamplesPerCycle) { Update(pResource, pConsumer); } } @@ -87,21 +142,34 @@ * samples is needed to compensate disk reading latency. * * @param pSample - points to the sample to be cached - * @param pEngine - pointer to Gig Engine which caused this call + * @param pEngineChannel - pointer to Gig Engine Channel which caused this call */ - void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, gig::Engine* pEngine) { - if (!pSample || pSample->GetCache().Size) return; + void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, gig::EngineChannel* pEngineChannel) { + if (!pSample) { + dmsg(4,("gig::InstrumentResourceManager: 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. - ::gig::buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension((pEngine->pAudioOutputDevice->MaxSamplesPerCycle() << MAX_PITCH) + 3); - dmsg(4,("Cached %d Bytes, %d silence bytes.\n", buf.Size, buf.NullExtensionSize)); + const uint maxSamplesPerCycle = + (pEngineChannel->GetEngine()) ? dynamic_cast(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle() + : GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE; + 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;