/[svn]/linuxsampler/trunk/src/effects/EffectFactory.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/effects/EffectFactory.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3054 - (show annotations) (download)
Thu Dec 15 12:47:45 2016 UTC (7 years, 3 months ago) by schoenebeck
File size: 5687 byte(s)
* Fixed numerous compiler warnings.
* Bumped version (2.0.0.svn32).

1 /*
2 Copyright (C) 2010 - 2016 Christian Schoenebeck
3 */
4
5 #include "EffectFactory.h"
6 #include "LadspaEffect.h"
7 #include "../common/Path.h"
8 #include "../common/IDGenerator.h"
9 #include <algorithm>
10
11 namespace LinuxSampler {
12
13 namespace {
14
15 ////////////////////////////////////////////////////////////////////////////
16 // class 'EffectInfos'
17
18 class EffectInfos {
19 public:
20 EffectInfos();
21 ~EffectInfos();
22 void Update();
23 uint Count();
24 EffectInfo* GetEffectInfo(uint index);
25 private:
26 std::vector<EffectInfo*> infos;
27 bool bInitialized;
28 };
29
30 EffectInfos::EffectInfos() : bInitialized(false) {
31 }
32
33 EffectInfos::~EffectInfos() {
34 for (int i = 0; i < infos.size(); i++) delete infos[i];
35 }
36
37 void EffectInfos::Update() {
38 // clear out all old effect infos
39 for (int i = 0; i < infos.size(); i++) delete infos[i];
40
41 // scan for LADSPA effects
42 infos = LadspaEffect::AvailableEffects();
43 }
44
45 uint EffectInfos::Count() {
46 if (!bInitialized) {
47 Update();
48 bInitialized = true;
49 }
50 return (uint) infos.size();
51 }
52
53 EffectInfo* EffectInfos::GetEffectInfo(uint index) {
54 if (index >= infos.size()) return NULL;
55 return infos[index];
56 }
57
58
59 ////////////////////////////////////////////////////////////////////////////
60 // private static variables
61
62 EffectInfos effectInfos;
63
64 std::vector<Effect*> vEffectInstances;
65
66 IDGenerator idGenerator;
67
68 }
69
70
71 ////////////////////////////////////////////////////////////////////////////
72 // class 'EffectFactory'
73
74 String EffectFactory::AvailableEffectSystemsAsString() {
75 return "LADSPA";
76 }
77
78 uint EffectFactory::AvailableEffectsCount() {
79 return effectInfos.Count();
80 }
81
82 EffectInfo* EffectFactory::GetEffectInfo(uint index) {
83 return effectInfos.GetEffectInfo(index);
84 }
85
86 static String _toLower(String s) {
87 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
88 return s;
89 }
90
91 static String _stripFileExtension(String sFileName) {
92 #if defined(WIN32)
93 Path p = Path::fromWindows(sFileName);
94 return p.stripLastName() + "\\" + p.getBaseName();
95 #else
96 Path p = Path::fromPosix(sFileName);
97 return p.stripLastName() + "/" + p.getBaseName();
98 #endif
99 }
100
101 static String _stripFilePath(String sFileName) {
102 #if defined(WIN32)
103 Path p = Path::fromWindows(sFileName);
104 #else
105 Path p = Path::fromPosix(sFileName);
106 #endif
107 return p.getName();
108 }
109
110 static bool _moduleNameMatches(String name1, String name2, int iModuleMatchFlags) {
111 if (iModuleMatchFlags == EffectFactory::MODULE_IGNORE_ALL) return true;
112 if (iModuleMatchFlags == EffectFactory::MODULE_MATCH_EXACTLY) return name1 == name2;
113 if (iModuleMatchFlags & EffectFactory::MODULE_IGNORE_CASE) {
114 name1 = _toLower(name1);
115 name2 = _toLower(name2);
116 }
117 if (iModuleMatchFlags & EffectFactory::MODULE_IGNORE_EXTENSION) {
118 name1 = _stripFileExtension(name1);
119 name2 = _stripFileExtension(name2);
120 }
121 if (iModuleMatchFlags & EffectFactory::MODULE_IGNORE_PATH) {
122 name1 = _stripFilePath(name1);
123 name2 = _stripFilePath(name2);
124 }
125 return name1 == name2;
126 }
127
128 EffectInfo* EffectFactory::GetEffectInfo(String effectSystem, String module, String effectName, int iModuleMatchFlags) {
129 for (int i = 0; i < effectInfos.Count(); i++) {
130 EffectInfo* pEffectInfo = effectInfos.GetEffectInfo(i);
131 if (pEffectInfo->EffectSystem() == effectSystem &&
132 _moduleNameMatches(pEffectInfo->Module(), module, iModuleMatchFlags) &&
133 pEffectInfo->Name() == effectName
134 ) {
135 return pEffectInfo;
136 }
137 }
138 return NULL;
139 }
140
141 Effect* EffectFactory::Create(EffectInfo* pEffectInfo) throw (Exception) {
142 Effect* pEffect = NULL;
143 try {
144 if (pEffectInfo->EffectSystem() == "LADSPA") {
145 pEffect = new LadspaEffect(pEffectInfo);
146 } else {
147 throw Exception(
148 "Effect system '" + pEffectInfo->EffectSystem() +
149 "' not supported"
150 );
151 }
152 } catch (Exception e) {
153 throw Exception("Could not create effect: " + e.Message());
154 } catch (...) {
155 throw Exception("Could not create effect: unknown exception");
156 }
157 if (!pEffect) {
158 // should never happen
159 throw Exception("Oops, EffectFactory bug: !pEffect");
160 }
161
162 // stick a new unique effect ID to the effect instance
163 const int id = idGenerator.create();
164 if (id < 0) {
165 delete pEffect;
166 throw Exception("Could not generate a new effect ID, whole ID value range is occupied!");
167 }
168 pEffect->SetId(id);
169
170 vEffectInstances.push_back(pEffect);
171 return pEffect;
172 }
173
174 void EffectFactory::Destroy(Effect* pEffect) throw (Exception) {
175 // check if effect is still in use
176 if (pEffect->Parent())
177 throw Exception("effect still in use");
178
179 // now delete effect
180 for (int i = 0; i < vEffectInstances.size(); i++) {
181 if (vEffectInstances[i] == pEffect) {
182 vEffectInstances.erase(vEffectInstances.begin() + i);
183
184 // free the effect's ID
185 idGenerator.destroy(pEffect->ID());
186
187 delete pEffect;
188 }
189 }
190 }
191
192 void EffectFactory::UpdateAvailableEffects() {
193 effectInfos.Update();
194 }
195
196 uint EffectFactory::EffectInstancesCount() {
197 return (uint) vEffectInstances.size();
198 }
199
200 Effect* EffectFactory::GetEffectInstance(uint index) {
201 if (index >= vEffectInstances.size()) return NULL;
202 return vEffectInstances[index];
203 }
204
205 Effect* EffectFactory::GetEffectInstanceByID(int id) {
206 for (int i = 0; i < vEffectInstances.size(); i++) {
207 if (vEffectInstances[i]->ID() == id)
208 return vEffectInstances[i];
209 }
210 return NULL;
211 }
212
213 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC