1 |
/*************************************************************************** |
2 |
* * |
3 |
* Copyright (C) 2006 - 2007 Christian Schoenebeck * |
4 |
* * |
5 |
* This library is free software; you can redistribute it and/or modify * |
6 |
* it under the terms of the GNU General Public License as published by * |
7 |
* the Free Software Foundation; either version 2 of the License, or * |
8 |
* (at your option) any later version. * |
9 |
* * |
10 |
* This library is distributed in the hope that it will be useful, * |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 |
* GNU General Public License for more details. * |
14 |
* * |
15 |
* You should have received a copy of the GNU General Public License * |
16 |
* along with this library; if not, write to the Free Software * |
17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
18 |
* MA 02111-1307 USA * |
19 |
***************************************************************************/ |
20 |
|
21 |
#include "MidiInstrumentMapper.h" |
22 |
|
23 |
#include "../../common/global_private.h" |
24 |
#include "../../common/Mutex.h" |
25 |
#include "../../engines/EngineFactory.h" |
26 |
#include "../../engines/Engine.h" |
27 |
|
28 |
namespace LinuxSampler { |
29 |
|
30 |
// same as entry_t but without 'LoadMode' |
31 |
struct private_entry_t { |
32 |
String EngineName; |
33 |
String InstrumentFile; |
34 |
uint InstrumentIndex; |
35 |
float Volume; |
36 |
String Name; |
37 |
}; |
38 |
|
39 |
// internal map type (MIDI bank&prog) -> (Engine,File,Index) |
40 |
class MidiInstrumentMap : public std::map<midi_prog_index_t,private_entry_t> { |
41 |
public: |
42 |
String name; |
43 |
}; |
44 |
|
45 |
// here we store all maps |
46 |
std::map<int,MidiInstrumentMap> midiMaps; |
47 |
|
48 |
// for synchronization of midiMaps |
49 |
Mutex midiMapsMutex; |
50 |
|
51 |
ListenerList<MidiInstrumentCountListener*> MidiInstrumentMapper::llMidiInstrumentCountListeners; |
52 |
ListenerList<MidiInstrumentInfoListener*> MidiInstrumentMapper::llMidiInstrumentInfoListeners; |
53 |
ListenerList<MidiInstrumentMapCountListener*> MidiInstrumentMapper::llMidiInstrumentMapCountListeners; |
54 |
ListenerList<MidiInstrumentMapInfoListener*> MidiInstrumentMapper::llMidiInstrumentMapInfoListeners; |
55 |
int MidiInstrumentMapper::DefaultMap; |
56 |
|
57 |
void MidiInstrumentMapper::AddMidiInstrumentCountListener(MidiInstrumentCountListener* l) { |
58 |
llMidiInstrumentCountListeners.AddListener(l); |
59 |
} |
60 |
|
61 |
void MidiInstrumentMapper::RemoveMidiInstrumentCountListener(MidiInstrumentCountListener* l) { |
62 |
llMidiInstrumentCountListeners.RemoveListener(l); |
63 |
} |
64 |
|
65 |
void MidiInstrumentMapper::fireMidiInstrumentCountChanged(int MapId, int NewCount) { |
66 |
for (int i = 0; i < llMidiInstrumentCountListeners.GetListenerCount(); i++) { |
67 |
llMidiInstrumentCountListeners.GetListener(i)->MidiInstrumentCountChanged(MapId, NewCount); |
68 |
} |
69 |
} |
70 |
|
71 |
void MidiInstrumentMapper::AddMidiInstrumentInfoListener(MidiInstrumentInfoListener* l) { |
72 |
llMidiInstrumentInfoListeners.AddListener(l); |
73 |
} |
74 |
|
75 |
void MidiInstrumentMapper::RemoveMidiInstrumentInfoListener(MidiInstrumentInfoListener* l) { |
76 |
llMidiInstrumentInfoListeners.RemoveListener(l); |
77 |
} |
78 |
|
79 |
void MidiInstrumentMapper::fireMidiInstrumentInfoChanged(int MapId, int Bank, int Program) { |
80 |
for (int i = 0; i < llMidiInstrumentInfoListeners.GetListenerCount(); i++) { |
81 |
llMidiInstrumentInfoListeners.GetListener(i)->MidiInstrumentInfoChanged(MapId, Bank, Program); |
82 |
} |
83 |
} |
84 |
|
85 |
void MidiInstrumentMapper::AddMidiInstrumentMapCountListener(MidiInstrumentMapCountListener* l) { |
86 |
llMidiInstrumentMapCountListeners.AddListener(l); |
87 |
} |
88 |
|
89 |
void MidiInstrumentMapper::RemoveMidiInstrumentMapCountListener(MidiInstrumentMapCountListener* l) { |
90 |
llMidiInstrumentMapCountListeners.RemoveListener(l); |
91 |
} |
92 |
|
93 |
void MidiInstrumentMapper::fireMidiInstrumentMapCountChanged(int NewCount) { |
94 |
for (int i = 0; i < llMidiInstrumentMapCountListeners.GetListenerCount(); i++) { |
95 |
llMidiInstrumentMapCountListeners.GetListener(i)->MidiInstrumentMapCountChanged(NewCount); |
96 |
} |
97 |
} |
98 |
|
99 |
void MidiInstrumentMapper::AddMidiInstrumentMapInfoListener(MidiInstrumentMapInfoListener* l) { |
100 |
llMidiInstrumentMapInfoListeners.AddListener(l); |
101 |
} |
102 |
|
103 |
void MidiInstrumentMapper::RemoveMidiInstrumentMapInfoListener(MidiInstrumentMapInfoListener* l) { |
104 |
llMidiInstrumentMapInfoListeners.RemoveListener(l); |
105 |
} |
106 |
|
107 |
void MidiInstrumentMapper::fireMidiInstrumentMapInfoChanged(int MapId) { |
108 |
for (int i = 0; i < llMidiInstrumentMapInfoListeners.GetListenerCount(); i++) { |
109 |
llMidiInstrumentMapInfoListeners.GetListener(i)->MidiInstrumentMapInfoChanged(MapId); |
110 |
} |
111 |
} |
112 |
|
113 |
void MidiInstrumentMapper::AddOrReplaceEntry(int Map, midi_prog_index_t Index, entry_t Entry, bool bInBackground) throw (Exception) { |
114 |
if (bInBackground) { |
115 |
dmsg(3,("MidiInstrumentMapper: updating mapping %d (%d,%d,%d) -> ('%s','%s',%d) with vol=%f, mode=%d in background\n", |
116 |
Map, |
117 |
Index.midi_bank_msb,Index.midi_bank_lsb,Index.midi_prog, |
118 |
Entry.EngineName.c_str(),Entry.InstrumentFile.c_str(),Entry.InstrumentIndex, |
119 |
Entry.Volume,Entry.LoadMode) |
120 |
); |
121 |
} else { |
122 |
dmsg(3,("MidiInstrumentMapper: updating mapping %d (%d,%d,%d) -> ('%s','%s',%d) with vol=%f, mode=%d\n", |
123 |
Map, |
124 |
Index.midi_bank_msb,Index.midi_bank_lsb,Index.midi_prog, |
125 |
Entry.EngineName.c_str(),Entry.InstrumentFile.c_str(),Entry.InstrumentIndex, |
126 |
Entry.Volume,Entry.LoadMode) |
127 |
); |
128 |
} |
129 |
midiMapsMutex.Lock(); |
130 |
if (midiMaps.empty()) { |
131 |
midiMapsMutex.Unlock(); |
132 |
throw Exception("There is no MIDI instrument map, you have to add one first."); |
133 |
} |
134 |
midiMapsMutex.Unlock(); |
135 |
if (!Entry.InstrumentFile.size()) |
136 |
throw Exception("No instrument file name given"); |
137 |
// TODO: an easy one - we should check here if given file exists and throw an exception if it doesn't |
138 |
if (Entry.Volume < 0.0) |
139 |
throw Exception("Volume may not be a negative value"); |
140 |
Engine* pEngine = EngineFactory::Create(Entry.EngineName); |
141 |
if (!pEngine) |
142 |
throw Exception("No such engine type '"+Entry.EngineName+"'"); |
143 |
Entry.EngineName = pEngine->EngineName(); // make sure to use the official engine name |
144 |
if (pEngine->GetInstrumentManager()) { |
145 |
InstrumentManager::instrument_id_t id; |
146 |
id.FileName = Entry.InstrumentFile; |
147 |
id.Index = Entry.InstrumentIndex; |
148 |
if (Entry.LoadMode != DONTCARE) { |
149 |
if (bInBackground) |
150 |
pEngine->GetInstrumentManager()->SetModeInBackground(id, static_cast<InstrumentManager::mode_t>(Entry.LoadMode)); |
151 |
else |
152 |
pEngine->GetInstrumentManager()->SetMode(id, static_cast<InstrumentManager::mode_t>(Entry.LoadMode)); |
153 |
} |
154 |
} else { |
155 |
dmsg(1,("WARNING: no InstrumentManager for engine '%s'\n",Entry.EngineName.c_str())); |
156 |
} |
157 |
private_entry_t privateEntry; |
158 |
privateEntry.EngineName = Entry.EngineName; |
159 |
privateEntry.InstrumentFile = Entry.InstrumentFile; |
160 |
privateEntry.InstrumentIndex = Entry.InstrumentIndex; |
161 |
privateEntry.Volume = Entry.Volume; |
162 |
privateEntry.Name = Entry.Name; |
163 |
|
164 |
bool Replaced = false; |
165 |
int InstrCount = 0; |
166 |
|
167 |
midiMapsMutex.Lock(); |
168 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
169 |
if (iterMap != midiMaps.end()) { // map found |
170 |
Replaced = (iterMap->second.find(Index) != iterMap->second.end()); |
171 |
iterMap->second[Index] = privateEntry; |
172 |
InstrCount = iterMap->second.size(); |
173 |
} else { // no such map |
174 |
midiMapsMutex.Unlock(); |
175 |
EngineFactory::Destroy(pEngine); |
176 |
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
177 |
} |
178 |
midiMapsMutex.Unlock(); |
179 |
EngineFactory::Destroy(pEngine); |
180 |
|
181 |
if (Replaced) { |
182 |
int Bank = (int(Index.midi_bank_msb) << 7) | int(Index.midi_bank_lsb); |
183 |
fireMidiInstrumentInfoChanged(Map, Bank, Index.midi_prog); |
184 |
} else { |
185 |
fireMidiInstrumentCountChanged(Map, InstrCount); |
186 |
} |
187 |
} |
188 |
|
189 |
void MidiInstrumentMapper::RemoveEntry(int Map, midi_prog_index_t Index) { |
190 |
int InstrCount = -1; |
191 |
|
192 |
midiMapsMutex.Lock(); |
193 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
194 |
if (iterMap != midiMaps.end()) { // map found |
195 |
iterMap->second.erase(Index); // remove entry |
196 |
InstrCount = iterMap->second.size(); |
197 |
} |
198 |
midiMapsMutex.Unlock(); |
199 |
|
200 |
if (InstrCount != -1) { |
201 |
fireMidiInstrumentCountChanged(Map, InstrCount); |
202 |
} |
203 |
} |
204 |
|
205 |
void MidiInstrumentMapper::RemoveAllEntries(int Map) { |
206 |
int InstrCount = -1; |
207 |
|
208 |
midiMapsMutex.Lock(); |
209 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
210 |
if (iterMap != midiMaps.end()) { // map found |
211 |
iterMap->second.clear(); // clear that map |
212 |
InstrCount = 0; |
213 |
} |
214 |
midiMapsMutex.Unlock(); |
215 |
|
216 |
if (InstrCount != -1) { |
217 |
fireMidiInstrumentCountChanged(Map, InstrCount); |
218 |
} |
219 |
} |
220 |
|
221 |
std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> MidiInstrumentMapper::Entries(int Map) throw (Exception) { |
222 |
std::map<midi_prog_index_t,entry_t> result; |
223 |
|
224 |
// copy the internal map first |
225 |
midiMapsMutex.Lock(); |
226 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
227 |
if (iterMap == midiMaps.end()) { // no such map |
228 |
midiMapsMutex.Unlock(); |
229 |
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
230 |
} |
231 |
for (std::map<midi_prog_index_t,private_entry_t>::iterator iterEntry = iterMap->second.begin(); |
232 |
iterEntry != iterMap->second.end(); iterEntry++) |
233 |
{ |
234 |
entry_t entry; |
235 |
entry.EngineName = iterEntry->second.EngineName; |
236 |
entry.InstrumentFile = iterEntry->second.InstrumentFile; |
237 |
entry.InstrumentIndex = iterEntry->second.InstrumentIndex; |
238 |
entry.Volume = iterEntry->second.Volume; |
239 |
entry.Name = iterEntry->second.Name; |
240 |
result[iterEntry->first] = entry; |
241 |
} |
242 |
midiMapsMutex.Unlock(); |
243 |
|
244 |
// complete it with current LoadMode of each entry |
245 |
for (std::map<midi_prog_index_t,entry_t>::iterator iter = result.begin(); |
246 |
iter != result.end(); iter++) |
247 |
{ |
248 |
entry_t& entry = iter->second; |
249 |
Engine* pEngine = EngineFactory::Create(entry.EngineName); |
250 |
if (!pEngine) { // invalid mapping |
251 |
RemoveEntry(Map, iter->first); |
252 |
result.erase(iter); |
253 |
continue; |
254 |
} |
255 |
InstrumentManager* pManager = pEngine->GetInstrumentManager(); |
256 |
if (pManager) { // engine provides an InstrumentManager |
257 |
InstrumentManager::instrument_id_t id; |
258 |
id.FileName = entry.InstrumentFile; |
259 |
id.Index = entry.InstrumentIndex; |
260 |
entry.LoadMode = static_cast<mode_t>(pManager->GetMode(id)); |
261 |
} else { // engine does not provide an InstrumentManager |
262 |
// use default value |
263 |
entry.LoadMode = ON_DEMAND; |
264 |
} |
265 |
EngineFactory::Destroy(pEngine); |
266 |
} |
267 |
return result; |
268 |
} |
269 |
|
270 |
std::vector<int> MidiInstrumentMapper::Maps() { |
271 |
std::vector<int> result; |
272 |
midiMapsMutex.Lock(); |
273 |
for (std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.begin(); |
274 |
iterMap != midiMaps.end(); iterMap++) |
275 |
{ |
276 |
result.push_back(iterMap->first); |
277 |
} |
278 |
midiMapsMutex.Unlock(); |
279 |
return result; |
280 |
} |
281 |
|
282 |
int MidiInstrumentMapper::GetMapCount() { |
283 |
midiMapsMutex.Lock(); |
284 |
int i = midiMaps.size(); |
285 |
midiMapsMutex.Unlock(); |
286 |
return i; |
287 |
} |
288 |
|
289 |
int MidiInstrumentMapper::AddMap(String MapName) throw (Exception) { |
290 |
int ID; |
291 |
midiMapsMutex.Lock(); |
292 |
if (midiMaps.empty()) ID = 0; |
293 |
else { |
294 |
// get the highest existing map ID |
295 |
uint lastIndex = (--(midiMaps.end()))->first; |
296 |
// check if we reached the index limit |
297 |
if (lastIndex + 1 < lastIndex) { |
298 |
// search for an unoccupied map ID starting from 0 |
299 |
for (uint i = 0; i < lastIndex; i++) { |
300 |
if (midiMaps.find(i) != midiMaps.end()) continue; |
301 |
// we found an unused ID, so insert the new map there |
302 |
ID = i; |
303 |
goto __create_map; |
304 |
} |
305 |
throw Exception("Internal error: could not find unoccupied MIDI instrument map ID."); |
306 |
} |
307 |
ID = lastIndex + 1; |
308 |
} |
309 |
__create_map: |
310 |
midiMaps[ID].name = MapName; |
311 |
|
312 |
fireMidiInstrumentMapCountChanged(Maps().size()); |
313 |
// If there were no maps until now we must set a default map. |
314 |
if (midiMaps.size() == 1) SetDefaultMap(ID); |
315 |
midiMapsMutex.Unlock(); |
316 |
|
317 |
return ID; |
318 |
} |
319 |
|
320 |
String MidiInstrumentMapper::MapName(int Map) throw (Exception) { |
321 |
String result; |
322 |
midiMapsMutex.Lock(); |
323 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
324 |
if (iterMap == midiMaps.end()) { |
325 |
midiMapsMutex.Unlock(); |
326 |
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
327 |
} |
328 |
result = iterMap->second.name; |
329 |
midiMapsMutex.Unlock(); |
330 |
return result; |
331 |
} |
332 |
|
333 |
void MidiInstrumentMapper::RenameMap(int Map, String NewName) throw (Exception) { |
334 |
midiMapsMutex.Lock(); |
335 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
336 |
if (iterMap == midiMaps.end()) { |
337 |
midiMapsMutex.Unlock(); |
338 |
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
339 |
} |
340 |
iterMap->second.name = NewName; |
341 |
midiMapsMutex.Unlock(); |
342 |
fireMidiInstrumentMapInfoChanged(Map); |
343 |
} |
344 |
|
345 |
void MidiInstrumentMapper::RemoveMap(int Map) { |
346 |
midiMapsMutex.Lock(); |
347 |
midiMaps.erase(Map); |
348 |
if(Map == GetDefaultMap()) { |
349 |
SetDefaultMap(midiMaps.empty() ? -1 : (*(midiMaps.begin())).first); |
350 |
} |
351 |
fireMidiInstrumentMapCountChanged(Maps().size()); |
352 |
midiMapsMutex.Unlock(); |
353 |
} |
354 |
|
355 |
void MidiInstrumentMapper::RemoveAllMaps() { |
356 |
midiMapsMutex.Lock(); |
357 |
midiMaps.clear(); |
358 |
SetDefaultMap(-1); |
359 |
fireMidiInstrumentMapCountChanged(Maps().size()); |
360 |
midiMapsMutex.Unlock(); |
361 |
} |
362 |
|
363 |
int MidiInstrumentMapper::GetDefaultMap() { |
364 |
midiMapsMutex.Lock(); |
365 |
int i = DefaultMap; |
366 |
midiMapsMutex.Unlock(); |
367 |
return i; |
368 |
} |
369 |
|
370 |
void MidiInstrumentMapper::SetDefaultMap(int MapId) { |
371 |
midiMapsMutex.Lock(); |
372 |
DefaultMap = MapId; |
373 |
midiMapsMutex.Unlock(); |
374 |
|
375 |
if (MapId != -1) fireMidiInstrumentMapInfoChanged(MapId); |
376 |
} |
377 |
|
378 |
optional<MidiInstrumentMapper::entry_t> MidiInstrumentMapper::GetEntry(int Map, midi_prog_index_t Index) { |
379 |
optional<entry_t> result; |
380 |
midiMapsMutex.Lock(); |
381 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
382 |
if (iterMap != midiMaps.end()) { // map found |
383 |
std::map<midi_prog_index_t,private_entry_t>::iterator iterEntry = iterMap->second.find(Index); |
384 |
if (iterEntry != iterMap->second.end()) { |
385 |
entry_t entry; |
386 |
entry.EngineName = iterEntry->second.EngineName; |
387 |
entry.InstrumentFile = iterEntry->second.InstrumentFile; |
388 |
entry.InstrumentIndex = iterEntry->second.InstrumentIndex; |
389 |
entry.Volume = iterEntry->second.Volume; |
390 |
//TODO: for now we skip the LoadMode and Name entry here, since we don't need it in the MidiInputPort |
391 |
result = entry; |
392 |
} |
393 |
} |
394 |
midiMapsMutex.Unlock(); |
395 |
return result; |
396 |
} |
397 |
|
398 |
} // namespace LinuxSampler |