--- linuxsampler/trunk/src/engines/gig/InstrumentResourceManager.cpp 2011/09/19 21:48:45 2275 +++ linuxsampler/trunk/src/engines/gig/InstrumentResourceManager.cpp 2015/01/04 17:16:05 2687 @@ -3,7 +3,7 @@ * LinuxSampler - modular, streaming capable sampler * * * * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * - * Copyright (C) 2005 - 2011 Christian Schoenebeck * + * Copyright (C) 2005 - 2013 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 * @@ -32,13 +32,6 @@ 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; @@ -83,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); @@ -140,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 @@ -219,7 +208,7 @@ } } - InstrumentEditor* InstrumentResourceManager::LaunchInstrumentEditor(instrument_id_t ID, void* pUserData) throw (InstrumentManagerException) { + InstrumentEditor* InstrumentResourceManager::LaunchInstrumentEditor(LinuxSampler::EngineChannel* pEngineChannel, instrument_id_t ID, void* pUserData) throw (InstrumentManagerException) { const String sDataType = GetInstrumentDataStructureName(ID); const String sDataVersion = GetInstrumentDataStructureVersion(ID); // find instrument editors capable to handle given instrument @@ -246,7 +235,7 @@ InstrumentEditorProxies.add(pProxy); InstrumentEditorProxiesMutex.Unlock(); // launch the instrument editor for the given instrument - pEditor->Launch(pInstrument, sDataType, sDataVersion, pUserData); + pEditor->Launch(pEngineChannel, pInstrument, sDataType, sDataVersion, pUserData); // register the instrument editor as virtual MIDI device as well ... VirtualMidiDevice* pVirtualMidiDevice = @@ -282,19 +271,20 @@ int iProxyIndex = -1; // first find the editor proxy entry for this editor - InstrumentEditorProxiesMutex.Lock(); - for (int i = 0; i < InstrumentEditorProxies.size(); i++) { - InstrumentEditorProxy* pCurProxy = - dynamic_cast( - InstrumentEditorProxies[i] - ); - if (pCurProxy->pEditor == pSender) { - pProxy = pCurProxy; - iProxyIndex = i; - pInstrument = pCurProxy->pInstrument; + { + LockGuard lock(InstrumentEditorProxiesMutex); + for (int i = 0; i < InstrumentEditorProxies.size(); i++) { + InstrumentEditorProxy* pCurProxy = + dynamic_cast( + InstrumentEditorProxies[i] + ); + if (pCurProxy->pEditor == pSender) { + pProxy = pCurProxy; + iProxyIndex = i; + pInstrument = pCurProxy->pInstrument; + } } } - InstrumentEditorProxiesMutex.Unlock(); if (!pProxy) { std::cerr << "Eeeek, could not find instrument editor proxy, " @@ -322,9 +312,10 @@ // finally delete proxy entry and hand back instrument if (pInstrument) { - InstrumentEditorProxiesMutex.Lock(); - InstrumentEditorProxies.remove(iProxyIndex); - InstrumentEditorProxiesMutex.Unlock(); + { + LockGuard lock(InstrumentEditorProxiesMutex); + InstrumentEditorProxies.remove(iProxyIndex); + } HandBack(pInstrument, pProxy); delete pProxy; @@ -573,6 +564,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 @@ -584,10 +577,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(); @@ -600,46 +593,23 @@ instr_entry_t* pEntry = new instr_entry_t; pEntry->ID.FileName = Key.FileName; pEntry->ID.Index = Key.Index; - pEntry->pGig = pGig; + pEntry->pFile = pGig; - // (try to resolve the audio device context) - EngineChannel* pEngineChannel = dynamic_cast(pConsumer); - AudioOutputDevice* pDevice = - (pEngineChannel) ? dynamic_cast(pEngineChannel->GetEngine())->pAudioOutputDevice : NULL; - - // and we save this to check if we need to reallocate for a engine with higher value of 'MaxSamplesPerSecond' - pEntry->MaxSamplesPerCycle = - (pDevice) ? pDevice->MaxSamplesPerCycle() : DefaultMaxSamplesPerCycle(); + // 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, reinterpret_cast(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; - - // (try to resolve the audio device context) - EngineChannel* pEngineChannel = dynamic_cast(pConsumer); - AudioOutputDevice* pDevice = - (pEngineChannel) ? dynamic_cast(pEngineChannel->GetEngine())->pAudioOutputDevice : NULL; - - uint maxSamplesPerCycle = - (pDevice) ? pDevice->MaxSamplesPerCycle() : DefaultMaxSamplesPerCycle(); - - if (pEntry->MaxSamplesPerCycle < maxSamplesPerCycle) { - dmsg(1,("Completely reloading instrument due to insufficient precached samples ...\n")); - 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 } @@ -684,6 +654,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; @@ -696,9 +673,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() - : DefaultMaxSamplesPerCycle(); const uint neededSilenceSamples = (maxSamplesPerCycle << CONFIG_MAX_PITCH) + 3; const uint currentlyCachedSilenceSamples = pSample->GetCache().NullExtensionSize / pSample->FrameSize; if (currentlyCachedSilenceSamples < neededSilenceSamples) {