1 |
/* |
2 |
* Copyright (c) 2014-2022 Christian Schoenebeck |
3 |
* |
4 |
* http://www.linuxsampler.org |
5 |
* |
6 |
* This file is part of LinuxSampler and released under the same terms. |
7 |
* See README file for details. |
8 |
*/ |
9 |
|
10 |
#ifndef LS_ABSTRACTINSTRUMENTMANAGER_H |
11 |
#define LS_ABSTRACTINSTRUMENTMANAGER_H |
12 |
|
13 |
#include "../InstrumentManager.h" |
14 |
#include "../../common/ResourceManager.h" |
15 |
#include "../../common/global_private.h" |
16 |
#include "InstrumentScriptVM.h" |
17 |
#include "../InstrumentManagerThread.h" |
18 |
|
19 |
namespace LinuxSampler { |
20 |
|
21 |
typedef ResourceConsumer<VMParserContext> InstrumentScriptConsumer; |
22 |
|
23 |
/** |
24 |
* Identifies uniquely a compiled script. |
25 |
* |
26 |
* Note: every engine channel now has its own compiled script object |
27 |
* (a.k.a. VMParserContext). Originally a compiled script was shared by |
28 |
* multiple engine channels. This was wrong: we cannot share compiled |
29 |
* script instances among multiple engine channels (parts), for two |
30 |
* reasons: |
31 |
* |
32 |
* 1. VMParserContext not only encompasses the compiled tree |
33 |
* presentation of the requested script, but also global variables |
34 |
* and we don't want those global variables to be modified by |
35 |
* different sampler parts, as this would not be expected behaviour |
36 |
* by instrument script authors. |
37 |
* |
38 |
* 2. If there is more than one sampler engine instance (e.g. if there |
39 |
* are multiple audio output device instances) this would even crash, |
40 |
* because each sampler engine instance has its own ScriptVM |
41 |
* instance, and a (VM)ParserContext is always tied to exactly one |
42 |
* ScriptVM instance. |
43 |
* |
44 |
* We would not be buying much by sharing compiled scripts anyway, as a |
45 |
* script usually compiles in couple microseconds and RAM usage is also |
46 |
* neglectable. |
47 |
*/ |
48 |
struct ScriptKey { |
49 |
String code; ///< Script's source code. |
50 |
std::map<String,String> patchVars; ///< Patch variables being overridden by instrument. |
51 |
EngineChannel* engineChannel; ///< Unique owning engine channel on which the compiled script is/was loaded onto. |
52 |
bool wildcardPatchVars; ///< Seldom use: Allows lookup for consumers of a specific script by ignoring all (overridden) patch variables. |
53 |
bool wildcardEngineChannel; ///< Seldom use: Allows lookup for consumers of a specific script by ignoring on which engine channel it was loaded. |
54 |
|
55 |
inline bool operator<(const ScriptKey& o) const { |
56 |
if (wildcardPatchVars && wildcardEngineChannel) |
57 |
return code < o.code; |
58 |
else if (wildcardPatchVars) |
59 |
return code < o.code || (code == o.code && engineChannel < o.engineChannel); |
60 |
else if (wildcardEngineChannel) |
61 |
return code < o.code || (code == o.code && patchVars < o.patchVars); |
62 |
else |
63 |
return code < o.code || (code == o.code && (patchVars < o.patchVars || (patchVars == o.patchVars && engineChannel < o.engineChannel))); |
64 |
} |
65 |
|
66 |
inline bool operator>(const ScriptKey& o) const { |
67 |
if (wildcardPatchVars && wildcardEngineChannel) |
68 |
return code > o.code; |
69 |
else if (wildcardPatchVars) |
70 |
return code > o.code || (code == o.code && engineChannel > o.engineChannel); |
71 |
else if (wildcardEngineChannel) |
72 |
return code > o.code || (code == o.code && patchVars > o.patchVars); |
73 |
else |
74 |
return code > o.code || (code == o.code && (patchVars > o.patchVars || (patchVars == o.patchVars && engineChannel > o.engineChannel))); |
75 |
} |
76 |
|
77 |
inline bool operator==(const ScriptKey& o) const { |
78 |
if (wildcardPatchVars && wildcardEngineChannel) |
79 |
return code == o.code; |
80 |
else if (wildcardPatchVars) |
81 |
return code == o.code && engineChannel == o.engineChannel; |
82 |
else if (wildcardEngineChannel) |
83 |
return code == o.code && patchVars == o.patchVars; |
84 |
else |
85 |
return code == o.code && patchVars == o.patchVars && engineChannel == o.engineChannel; |
86 |
} |
87 |
|
88 |
inline bool operator!=(const ScriptKey& o) const { |
89 |
return !(operator==(o)); |
90 |
} |
91 |
}; |
92 |
|
93 |
class AbstractInstrumentManager : public InstrumentManager { |
94 |
public: |
95 |
AbstractInstrumentManager() { } |
96 |
InstrumentManagerThread* GetInstrumentManagerThread(); |
97 |
virtual ~AbstractInstrumentManager() { } |
98 |
|
99 |
/** |
100 |
* Resource manager for loading and sharing the parsed (executable) VM |
101 |
* presentation of real-time instrument scripts. The key used here, and |
102 |
* associated with each script resource, is not as one might expect the |
103 |
* script name or something equivalent, instead the key used is |
104 |
* actually the entire script's source code text (and additionally |
105 |
* potentially patched variables). The value (the actual resource) is of |
106 |
* type @c VMParserContext, which is the parsed (executable) VM |
107 |
* representation of the respective script. |
108 |
*/ |
109 |
class ScriptResourceManager : public ResourceManager<ScriptKey, VMParserContext> { |
110 |
public: |
111 |
ScriptResourceManager() {} |
112 |
virtual ~ScriptResourceManager() {} |
113 |
protected: |
114 |
// implementation of derived abstract methods from 'ResourceManager' |
115 |
virtual VMParserContext* Create(ScriptKey key, InstrumentScriptConsumer* pConsumer, void*& pArg); |
116 |
virtual void Destroy(VMParserContext* pResource, void* pArg); |
117 |
virtual void OnBorrow(VMParserContext* pResource, InstrumentScriptConsumer* pConsumer, void*& pArg) {} // ignore |
118 |
} scripts; |
119 |
}; |
120 |
|
121 |
} // namespace LinuxSampler |
122 |
|
123 |
#endif // LS_ABSTRACTINSTRUMENTMANAGER_H |