3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
|
* Copyright (C) 2005 - 2008 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
9 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
26 |
|
|
27 |
#include "../../common/global.h" |
#include "../../common/global.h" |
28 |
|
|
29 |
#if DEBUG_HEADERS |
#include <gig.h> |
|
# warning InstrumentResourceManager.h included |
|
|
#endif // DEBUG_HEADERS |
|
30 |
|
|
31 |
#include "../../common/global.h" |
#include "../../common/global.h" |
|
#include "../../common/LinuxSamplerException.h" |
|
32 |
#include "../../common/ResourceManager.h" |
#include "../../common/ResourceManager.h" |
33 |
#include "../../drivers/audio/AudioOutputDevice.h" |
#include "../../drivers/audio/AudioOutputDevice.h" |
34 |
#include "../../lib/fileloader/libgig/gig.h" |
#include "../InstrumentManager.h" |
35 |
|
#include "../../common/ArrayList.h" |
|
// preload 64k samples = 128kB of data in RAM for 16 bit mono samples |
|
|
#define NUM_RAM_PRELOAD_SAMPLES 32768 |
|
36 |
|
|
37 |
//namespace libgig = gig; |
//namespace libgig = gig; |
38 |
|
|
42 |
|
|
43 |
}} // namespace LinuxSampler::gig |
}} // namespace LinuxSampler::gig |
44 |
|
|
45 |
|
#include "EngineChannel.h" |
46 |
#include "Engine.h" |
#include "Engine.h" |
47 |
|
#include "../../plugins/InstrumentEditor.h" |
48 |
|
|
49 |
namespace LinuxSampler { namespace gig { |
namespace LinuxSampler { namespace gig { |
50 |
|
|
51 |
// just symbol prototyping |
// just symbol prototyping |
52 |
|
class EngineChannel; |
53 |
class Engine; |
class Engine; |
54 |
|
|
55 |
/** |
/** @brief Gig instrument manager |
|
* 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 |
|
56 |
* |
* |
57 |
* Manager to share gig instruments between multiple Gigasampler |
* Manager to share gig instruments between multiple Gigasampler |
58 |
* engines. The engines Borrow() instruments when they need them and |
* engine channels. The engine channels Borrow() instruments when they |
59 |
* HandBack() when they don't need them anymore. The |
* need them and HandBack() when they don't need them anymore. The |
60 |
* InstrumentResourceManager loads the corresponding gig file and gig |
* InstrumentResourceManager loads the corresponding gig file and gig |
61 |
* instrument if needed, if it's already in use by another engine, then |
* instrument if needed, if it's already in use by another engine |
62 |
* it just returns the same resource, if an gig instrument / file is not |
* channel, then it just returns the same resource, if an gig |
63 |
* in use by any engine anymore, then it will be freed from memory. |
* instrument / file is not needed anymore, then it will be freed from |
64 |
|
* memory. |
65 |
*/ |
*/ |
66 |
class InstrumentResourceManager : public ResourceManager<instrument_id_t, ::gig::Instrument> { |
class InstrumentResourceManager : public InstrumentManager, public ResourceManager<InstrumentManager::instrument_id_t, ::gig::Instrument>, public InstrumentEditorListener { |
67 |
|
public: |
68 |
|
InstrumentResourceManager() : Gigs(this) {} |
69 |
|
virtual ~InstrumentResourceManager() {} |
70 |
|
static void OnInstrumentLoadingProgress(::gig::progress_t* pProgress); |
71 |
|
|
72 |
|
// implementation of derived abstract methods from 'InstrumentManager' |
73 |
|
virtual std::vector<instrument_id_t> Instruments(); |
74 |
|
virtual InstrumentManager::mode_t GetMode(const instrument_id_t& ID); |
75 |
|
virtual void SetMode(const instrument_id_t& ID, InstrumentManager::mode_t Mode); |
76 |
|
virtual String GetInstrumentName(instrument_id_t ID); |
77 |
|
virtual String GetInstrumentDataStructureName(instrument_id_t ID); |
78 |
|
virtual String GetInstrumentDataStructureVersion(instrument_id_t ID); |
79 |
|
virtual void LaunchInstrumentEditor(instrument_id_t ID) throw (InstrumentManagerException); |
80 |
|
virtual std::vector<instrument_id_t> GetInstrumentFileContent(String File) throw (InstrumentManagerException); |
81 |
|
virtual instrument_info_t GetInstrumentInfo(instrument_id_t ID) throw (InstrumentManagerException); |
82 |
|
|
83 |
|
// implementation of derived abstract methods from 'InstrumentEditorListener' |
84 |
|
virtual void OnInstrumentEditorQuit(InstrumentEditor* pSender); |
85 |
|
virtual void OnSamplesToBeRemoved(std::set<void*> Samples, InstrumentEditor* pSender); |
86 |
|
virtual void OnSamplesRemoved(InstrumentEditor* pSender); |
87 |
|
virtual void OnDataStructureToBeChanged(void* pStruct, String sStructType, InstrumentEditor* pSender); |
88 |
|
virtual void OnDataStructureChanged(void* pStruct, String sStructType, InstrumentEditor* pSender); |
89 |
|
virtual void OnSampleReferenceChanged(void* pOldSample, void* pNewSample, InstrumentEditor* pSender); |
90 |
|
|
91 |
|
void HandBackInstrument(::gig::Instrument* pResource, InstrumentConsumer* pConsumer, |
92 |
|
RTList< ::gig::DimensionRegion*>* pDimRegionsInUse); |
93 |
|
void HandBackDimReg(::gig::DimensionRegion* pDimReg); |
94 |
|
|
95 |
|
#if 0 // currently unused : |
96 |
|
void TrySendNoteOnToEditors(uint8_t Key, uint8_t Velocity, ::gig::Instrument* pInstrument); |
97 |
|
void TrySendNoteOffToEditors(uint8_t Key, uint8_t Velocity, ::gig::Instrument* pInstrument); |
98 |
|
#endif // unused |
99 |
|
|
100 |
protected: |
protected: |
101 |
|
// implementation of derived abstract methods from 'ResourceManager' |
102 |
virtual ::gig::Instrument* Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg); |
virtual ::gig::Instrument* Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg); |
103 |
virtual void Destroy(::gig::Instrument* pResource, void* pArg); |
virtual void Destroy(::gig::Instrument* pResource, void* pArg); |
104 |
virtual void OnBorrow(::gig::Instrument* pResource, InstrumentConsumer* pConsumer, void*& pArg); |
virtual void OnBorrow(::gig::Instrument* pResource, InstrumentConsumer* pConsumer, void*& pArg); |
105 |
private: |
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 |
|
|
}; |
|
|
|
|
106 |
typedef ResourceConsumer< ::gig::File> GigConsumer; |
typedef ResourceConsumer< ::gig::File> GigConsumer; |
107 |
|
|
108 |
class GigResourceManager : public ResourceManager<String, ::gig::File> { |
class GigResourceManager : public ResourceManager<String, ::gig::File> { |
109 |
protected: |
protected: |
110 |
|
// implementation of derived abstract methods from 'ResourceManager' |
111 |
virtual ::gig::File* Create(String Key, GigConsumer* pConsumer, void*& pArg); |
virtual ::gig::File* Create(String Key, GigConsumer* pConsumer, void*& pArg); |
112 |
virtual void Destroy(::gig::File* pResource, void* pArg); |
virtual void Destroy(::gig::File* pResource, void* pArg); |
113 |
virtual void OnBorrow(::gig::File* pResource, GigConsumer* pConsumer, void*& pArg) {} // ignore |
virtual void OnBorrow(::gig::File* pResource, GigConsumer* pConsumer, void*& pArg) {} // ignore |
114 |
|
public: |
115 |
|
GigResourceManager(InstrumentResourceManager* parent) : parent(parent) {} |
116 |
|
virtual ~GigResourceManager() {} |
117 |
|
private: |
118 |
|
InstrumentResourceManager* parent; |
119 |
} Gigs; |
} Gigs; |
120 |
|
|
121 |
|
void CacheInitialSamples(::gig::Sample* pSample, gig::EngineChannel* pEngineChannel); |
122 |
void CacheInitialSamples(::gig::Sample* pSample, gig::Engine* pEngine); |
void CacheInitialSamples(::gig::Sample* pSample, gig::Engine* pEngine); |
123 |
}; |
void UncacheInitialSamples(::gig::Sample* pSample); |
124 |
|
std::vector< ::gig::Instrument*> GetInstrumentsCurrentlyUsedOf(::gig::File* pFile, bool bLock); |
125 |
/** |
std::set<gig::EngineChannel*> GetEngineChannelsUsing(::gig::Instrument* pInstrument, bool bLock); |
126 |
* Will be thrown by the InstrumentResourceManager on errors. |
std::set<gig::Engine*> GetEnginesUsing(::gig::Instrument* pInstrument, bool bLock); |
127 |
*/ |
std::set<gig::Engine*> GetEnginesUsing(::gig::File* pFile, bool bLock); |
128 |
class InstrumentResourceManagerException : public LinuxSamplerException { |
bool SampleReferencedByInstrument(::gig::Sample* pSample, ::gig::Instrument* pInstrument); |
129 |
public: |
void SuspendEnginesUsing(::gig::Instrument* pInstrument); |
130 |
InstrumentResourceManagerException(String msg) : LinuxSamplerException(msg) {} |
void SuspendEnginesUsing(::gig::File* pFile); |
131 |
|
void ResumeAllEngines(); |
132 |
|
|
133 |
|
struct dimreg_info_t { |
134 |
|
int refCount; |
135 |
|
::gig::File* file; |
136 |
|
::RIFF::File* riff; |
137 |
|
}; |
138 |
|
Mutex DimRegInfoMutex; ///< protects the DimRegInfo and SampleRefCount maps from concurrent access by the instrument loader and disk threads |
139 |
|
std::map< ::gig::DimensionRegion*, dimreg_info_t> DimRegInfo; ///< contains dimension regions that are still in use but belong to released instrument |
140 |
|
std::map< ::gig::Sample*, int> SampleRefCount; ///< contains samples that are still in use but belong to a released instrument |
141 |
|
|
142 |
|
Mutex InstrumentEditorProxiesMutex; ///< protects the 'InstrumentEditorProxies' map |
143 |
|
ArrayList<InstrumentConsumer*> InstrumentEditorProxies; ///< here we store the objects that react on instrument specific notifications on behalf of the respective instrument editor |
144 |
|
std::set<Engine*> suspendedEngines; ///< all engines currently completely suspended |
145 |
|
Mutex suspendedEnginesMutex; ///< protects 'suspendedEngines' set |
146 |
}; |
}; |
147 |
|
|
148 |
}} // namespace LinuxSampler::gig |
}} // namespace LinuxSampler::gig |