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, 2006 Christian Schoenebeck * |
* Copyright (C) 2005 - 2016 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 * |
28 |
|
|
29 |
#include <gig.h> |
#include <gig.h> |
30 |
|
|
31 |
#include "../../common/global.h" |
#include "../../common/Pool.h" |
|
#include "../../common/Exception.h" |
|
32 |
#include "../../common/ResourceManager.h" |
#include "../../common/ResourceManager.h" |
33 |
|
#include "../InstrumentManagerBase.h" |
34 |
#include "../../drivers/audio/AudioOutputDevice.h" |
#include "../../drivers/audio/AudioOutputDevice.h" |
35 |
|
#include "../InstrumentManager.h" |
36 |
|
#include "../../common/ArrayList.h" |
37 |
|
|
38 |
//namespace libgig = gig; |
//namespace libgig = gig; |
39 |
|
|
43 |
|
|
44 |
}} // namespace LinuxSampler::gig |
}} // namespace LinuxSampler::gig |
45 |
|
|
46 |
#include "EngineChannel.h" |
#include "../../plugins/InstrumentEditor.h" |
|
#include "Engine.h" |
|
47 |
|
|
48 |
namespace LinuxSampler { namespace gig { |
namespace LinuxSampler { namespace gig { |
49 |
|
|
50 |
// just symbol prototyping |
class Engine; |
51 |
class EngineChannel; |
class EngineChannel; |
52 |
|
|
53 |
/** |
/** @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 |
|
54 |
* |
* |
55 |
* Manager to share gig instruments between multiple Gigasampler |
* Manager to share gig instruments between multiple Gigasampler |
56 |
* engine channels. The engine channels Borrow() instruments when they |
* engine channels. The engine channels Borrow() instruments when they |
58 |
* InstrumentResourceManager loads the corresponding gig file and gig |
* InstrumentResourceManager loads the corresponding gig file and gig |
59 |
* instrument if needed, if it's already in use by another engine |
* instrument if needed, if it's already in use by another engine |
60 |
* channel, then it just returns the same resource, if an gig |
* channel, then it just returns the same resource, if an gig |
61 |
* instrument / file is not in use by any engine channel anymore, then |
* instrument / file is not needed anymore, then it will be freed from |
62 |
* it will be freed from memory. |
* memory. |
63 |
*/ |
*/ |
64 |
class InstrumentResourceManager : public ResourceManager<instrument_id_t, ::gig::Instrument> { |
class InstrumentResourceManager : public InstrumentManagerBase< ::gig::File, ::gig::Instrument, ::gig::DimensionRegion, ::gig::Sample>, public InstrumentEditorListener { |
65 |
public: |
public: |
66 |
|
InstrumentResourceManager() : Gigs(this) {} |
67 |
virtual ~InstrumentResourceManager() {} |
virtual ~InstrumentResourceManager() {} |
68 |
static void OnInstrumentLoadingProgress(::gig::progress_t* pProgress); |
static void OnInstrumentLoadingProgress(::gig::progress_t* pProgress); |
69 |
|
|
70 |
|
// implementation of derived abstract methods from 'InstrumentManager' |
71 |
|
virtual String GetInstrumentName(instrument_id_t ID); |
72 |
|
virtual String GetInstrumentDataStructureName(instrument_id_t ID); |
73 |
|
virtual String GetInstrumentDataStructureVersion(instrument_id_t ID); |
74 |
|
virtual InstrumentEditor* LaunchInstrumentEditor(LinuxSampler::EngineChannel* pEngineChannel, instrument_id_t ID, void* pUserData = NULL) throw (InstrumentManagerException) OVERRIDE; |
75 |
|
virtual std::vector<instrument_id_t> GetInstrumentFileContent(String File) throw (InstrumentManagerException); |
76 |
|
virtual instrument_info_t GetInstrumentInfo(instrument_id_t ID) throw (InstrumentManagerException); |
77 |
|
|
78 |
|
// implementation of derived abstract methods from 'InstrumentEditorListener' |
79 |
|
virtual void OnInstrumentEditorQuit(InstrumentEditor* pSender); |
80 |
|
virtual void OnSamplesToBeRemoved(std::set<void*> Samples, InstrumentEditor* pSender); |
81 |
|
virtual void OnSamplesRemoved(InstrumentEditor* pSender); |
82 |
|
virtual void OnDataStructureToBeChanged(void* pStruct, String sStructType, InstrumentEditor* pSender); |
83 |
|
virtual void OnDataStructureChanged(void* pStruct, String sStructType, InstrumentEditor* pSender); |
84 |
|
virtual void OnSampleReferenceChanged(void* pOldSample, void* pNewSample, InstrumentEditor* pSender); |
85 |
|
|
86 |
|
#if 0 // currently unused : |
87 |
|
void TrySendNoteOnToEditors(uint8_t Key, uint8_t Velocity, ::gig::Instrument* pInstrument); |
88 |
|
void TrySendNoteOffToEditors(uint8_t Key, uint8_t Velocity, ::gig::Instrument* pInstrument); |
89 |
|
#endif // unused |
90 |
|
|
91 |
protected: |
protected: |
92 |
|
// implementation of derived abstract methods from 'ResourceManager' |
93 |
virtual ::gig::Instrument* Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg); |
virtual ::gig::Instrument* Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg); |
94 |
virtual void Destroy(::gig::Instrument* pResource, void* pArg); |
virtual void Destroy(::gig::Instrument* pResource, void* pArg); |
95 |
virtual void OnBorrow(::gig::Instrument* pResource, InstrumentConsumer* pConsumer, void*& pArg); |
virtual void DeleteRegionIfNotUsed(::gig::DimensionRegion* pRegion, region_info_t* pRegInfo); |
96 |
|
virtual void DeleteSampleIfNotUsed(::gig::Sample* pSample, region_info_t* pRegInfo); |
97 |
private: |
private: |
98 |
struct instr_entry_t { |
void CacheInitialSamples(::gig::Sample* pSample, AbstractEngine* pEngine); |
99 |
::gig::File* pGig; |
void CacheInitialSamples(::gig::Sample* pSample, EngineChannel* pEngineChannel); |
100 |
uint iInstrument; |
void CacheInitialSamples(::gig::Sample* pSample, uint maxSamplesPerCycle); |
|
uint MaxSamplesPerCycle; ///< if some engine requests an already allocated instrument with a higher value, we have to reallocate the instrument |
|
|
}; |
|
101 |
|
|
102 |
typedef ResourceConsumer< ::gig::File> GigConsumer; |
typedef ResourceConsumer< ::gig::File> GigConsumer; |
103 |
|
|
104 |
class GigResourceManager : public ResourceManager<String, ::gig::File> { |
class GigResourceManager : public ResourceManager<String, ::gig::File> { |
105 |
protected: |
protected: |
106 |
|
// implementation of derived abstract methods from 'ResourceManager' |
107 |
virtual ::gig::File* Create(String Key, GigConsumer* pConsumer, void*& pArg); |
virtual ::gig::File* Create(String Key, GigConsumer* pConsumer, void*& pArg); |
108 |
virtual void Destroy(::gig::File* pResource, void* pArg); |
virtual void Destroy(::gig::File* pResource, void* pArg); |
109 |
virtual void OnBorrow(::gig::File* pResource, GigConsumer* pConsumer, void*& pArg) {} // ignore |
virtual void OnBorrow(::gig::File* pResource, GigConsumer* pConsumer, void*& pArg) {} // ignore |
110 |
public: |
public: |
111 |
|
GigResourceManager(InstrumentResourceManager* parent) : parent(parent) {} |
112 |
virtual ~GigResourceManager() {} |
virtual ~GigResourceManager() {} |
113 |
|
private: |
114 |
|
InstrumentResourceManager* parent; |
115 |
} Gigs; |
} Gigs; |
116 |
|
|
117 |
void CacheInitialSamples(::gig::Sample* pSample, gig::EngineChannel* pEngineChannel); |
void UncacheInitialSamples(::gig::Sample* pSample); |
118 |
}; |
std::vector< ::gig::Instrument*> GetInstrumentsCurrentlyUsedOf(::gig::File* pFile, bool bLock); |
119 |
|
std::set<EngineChannel*> GetEngineChannelsUsingScriptSourceCode(const String& code, bool bLock); |
120 |
/** |
std::set<EngineChannel*> GetEngineChannelsUsing(::gig::Instrument* pInstrument, bool bLock); |
121 |
* Will be thrown by the InstrumentResourceManager on errors. |
std::set<Engine*> GetEnginesUsing(::gig::Instrument* pInstrument, bool bLock); |
122 |
*/ |
std::set<Engine*> GetEnginesUsing(::gig::File* pFile, bool bLock); |
123 |
class InstrumentResourceManagerException : public Exception { |
bool SampleReferencedByInstrument(::gig::Sample* pSample, ::gig::Instrument* pInstrument); |
124 |
public: |
void SuspendEnginesUsing(::gig::Instrument* pInstrument); |
125 |
InstrumentResourceManagerException(String msg) : Exception(msg) {} |
void SuspendEnginesUsing(::gig::File* pFile); |
126 |
|
void ResumeAllEngines(); |
127 |
|
|
128 |
|
Mutex InstrumentEditorProxiesMutex; ///< protects the 'InstrumentEditorProxies' map |
129 |
|
ArrayList<InstrumentConsumer*> InstrumentEditorProxies; ///< here we store the objects that react on instrument specific notifications on behalf of the respective instrument editor |
130 |
|
std::set<Engine*> suspendedEngines; ///< all engines currently completely suspended |
131 |
|
Mutex suspendedEnginesMutex; ///< protects 'suspendedEngines' set |
132 |
|
std::map< ::gig::Script*,String> pendingScriptUpdates; ///< Used to prepare updates of instrument scripts (value of the map is the original source code of the script before it is modified). |
133 |
|
Mutex pendingScriptUpdatesMutex; ///< Protectes 'pendingScriptUpdates'. |
134 |
}; |
}; |
135 |
|
|
136 |
}} // namespace LinuxSampler::gig |
}} // namespace LinuxSampler::gig |