--- linuxsampler/trunk/src/engines/gig/InstrumentResourceManager.h 2005/05/03 01:00:25 505 +++ linuxsampler/trunk/src/engines/gig/InstrumentResourceManager.h 2008/01/30 01:51:46 1653 @@ -3,7 +3,7 @@ * LinuxSampler - modular, streaming capable sampler * * * * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * - * Copyright (C) 2005 Christian Schoenebeck * + * Copyright (C) 2005 - 2008 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 * @@ -26,19 +26,13 @@ #include "../../common/global.h" -#if DEBUG_HEADERS -# warning InstrumentResourceManager.h included -#endif // DEBUG_HEADERS - #include #include "../../common/global.h" -#include "../../common/LinuxSamplerException.h" #include "../../common/ResourceManager.h" #include "../../drivers/audio/AudioOutputDevice.h" - -// preload 64k samples = 128kB of data in RAM for 16 bit mono samples -#define NUM_RAM_PRELOAD_SAMPLES 32768 +#include "../InstrumentManager.h" +#include "../../common/ArrayList.h" //namespace libgig = gig; @@ -50,26 +44,15 @@ #include "EngineChannel.h" #include "Engine.h" +#include "../../plugins/InstrumentEditor.h" namespace LinuxSampler { namespace gig { // just symbol prototyping class EngineChannel; + class Engine; - /** - * Explicitly identifies a Gigasampler instrument. - */ - struct instrument_id_t { - String FileName; ///< name of the file which contains the instrument - uint iInstrument; ///< index of the instrument in the instrument file - - // TODO: we should extend operator<() so it will be able to detect that file x and file y are actually the same files, e.g. because one of them is a symlink / share the same inode - bool operator<(const instrument_id_t& o) const { - return (iInstrument < o.iInstrument || (iInstrument == o.iInstrument && FileName < o.FileName)); - } - }; - - /** Gig instrument manager + /** @brief Gig instrument manager * * Manager to share gig instruments between multiple Gigasampler * engine channels. The engine channels Borrow() instruments when they @@ -77,43 +60,86 @@ * InstrumentResourceManager loads the corresponding gig file and gig * instrument if needed, if it's already in use by another engine * channel, then it just returns the same resource, if an gig - * instrument / file is not in use by any engine channel anymore, then - * it will be freed from memory. + * instrument / file is not needed anymore, then it will be freed from + * memory. */ - class InstrumentResourceManager : public ResourceManager { + class InstrumentResourceManager : public InstrumentManager, public ResourceManager, public InstrumentEditorListener { public: + InstrumentResourceManager() : Gigs(this) {} virtual ~InstrumentResourceManager() {} + static void OnInstrumentLoadingProgress(::gig::progress_t* pProgress); + + // implementation of derived abstract methods from 'InstrumentManager' + virtual std::vector Instruments(); + virtual InstrumentManager::mode_t GetMode(const instrument_id_t& ID); + virtual void SetMode(const instrument_id_t& ID, InstrumentManager::mode_t Mode); + virtual String GetInstrumentName(instrument_id_t ID); + virtual String GetInstrumentDataStructureName(instrument_id_t ID); + virtual String GetInstrumentDataStructureVersion(instrument_id_t ID); + virtual void LaunchInstrumentEditor(instrument_id_t ID) throw (InstrumentManagerException); + virtual std::vector GetInstrumentFileContent(String File) throw (InstrumentManagerException); + virtual instrument_info_t GetInstrumentInfo(instrument_id_t ID) throw (InstrumentManagerException); + + // implementation of derived abstract methods from 'InstrumentEditorListener' + virtual void OnInstrumentEditorQuit(InstrumentEditor* pSender); + virtual void OnSamplesToBeRemoved(std::set Samples, InstrumentEditor* pSender); + virtual void OnSamplesRemoved(InstrumentEditor* pSender); + virtual void OnDataStructureToBeChanged(void* pStruct, String sStructType, InstrumentEditor* pSender); + virtual void OnDataStructureChanged(void* pStruct, String sStructType, InstrumentEditor* pSender); + virtual void OnSampleReferenceChanged(void* pOldSample, void* pNewSample, InstrumentEditor* pSender); + + void HandBackInstrument(::gig::Instrument* pResource, InstrumentConsumer* pConsumer, + RTList< ::gig::DimensionRegion*>* pDimRegionsInUse); + void HandBackDimReg(::gig::DimensionRegion* pDimReg); + + void TrySendNoteOnToEditors(uint8_t Key, uint8_t Velocity, ::gig::Instrument* pInstrument); + void TrySendNoteOffToEditors(uint8_t Key, uint8_t Velocity, ::gig::Instrument* pInstrument); + protected: + // implementation of derived abstract methods from 'ResourceManager' virtual ::gig::Instrument* Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg); virtual void Destroy(::gig::Instrument* pResource, void* pArg); virtual void OnBorrow(::gig::Instrument* pResource, InstrumentConsumer* pConsumer, void*& pArg); private: - struct instr_entry_t { - ::gig::File* pGig; - uint iInstrument; - uint MaxSamplesPerCycle; ///< if some engine requests an already allocated instrument with a higher value, we have to reallocate the instrument - }; - typedef ResourceConsumer< ::gig::File> GigConsumer; class GigResourceManager : public ResourceManager { protected: + // implementation of derived abstract methods from 'ResourceManager' virtual ::gig::File* Create(String Key, GigConsumer* pConsumer, void*& pArg); virtual void Destroy(::gig::File* pResource, void* pArg); virtual void OnBorrow(::gig::File* pResource, GigConsumer* pConsumer, void*& pArg) {} // ignore public: + GigResourceManager(InstrumentResourceManager* parent) : parent(parent) {} virtual ~GigResourceManager() {} + private: + InstrumentResourceManager* parent; } Gigs; void CacheInitialSamples(::gig::Sample* pSample, gig::EngineChannel* pEngineChannel); - }; - - /** - * Will be thrown by the InstrumentResourceManager on errors. - */ - class InstrumentResourceManagerException : public LinuxSamplerException { - public: - InstrumentResourceManagerException(String msg) : LinuxSamplerException(msg) {} + void CacheInitialSamples(::gig::Sample* pSample, gig::Engine* pEngine); + void UncacheInitialSamples(::gig::Sample* pSample); + std::vector< ::gig::Instrument*> GetInstrumentsCurrentlyUsedOf(::gig::File* pFile, bool bLock); + std::set GetEnginesUsing(::gig::Instrument* pFile, bool bLock); + std::set GetEnginesUsing(::gig::File* pFile, bool bLock); + bool SampleReferencedByInstrument(::gig::Sample* pSample, ::gig::Instrument* pInstrument); + void SuspendEnginesUsing(::gig::Instrument* pInstrument); + void SuspendEnginesUsing(::gig::File* pFile); + void ResumeAllEngines(); + + struct dimreg_info_t { + int refCount; + ::gig::File* file; + ::RIFF::File* riff; + }; + Mutex DimRegInfoMutex; ///< protects the DimRegInfo and SampleRefCount maps from concurrent access by the instrument loader and disk threads + std::map< ::gig::DimensionRegion*, dimreg_info_t> DimRegInfo; ///< contains dimension regions that are still in use but belong to released instrument + std::map< ::gig::Sample*, int> SampleRefCount; ///< contains samples that are still in use but belong to a released instrument + + Mutex InstrumentEditorProxiesMutex; ///< protects the 'InstrumentEditorProxies' map + ArrayList InstrumentEditorProxies; ///< here we store the objects that react on instrument specific notifications on behalf of the respective instrument editor + std::set suspendedEngines; ///< all engines currently completely suspended + Mutex suspendedEnginesMutex; ///< protects 'suspendedEngines' set }; }} // namespace LinuxSampler::gig