1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* Copyright (C) 2006 - 2009 Christian Schoenebeck * |
* Copyright (C) 2006 - 2013 Christian Schoenebeck * |
4 |
* * |
* * |
5 |
* This library is free software; you can redistribute it and/or modify * |
* 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 * |
* it under the terms of the GNU General Public License as published by * |
128 |
Entry.Volume,Entry.LoadMode) |
Entry.Volume,Entry.LoadMode) |
129 |
); |
); |
130 |
} |
} |
131 |
midiMapsMutex.Lock(); |
{ |
132 |
if (midiMaps.empty()) { |
LockGuard lock(midiMapsMutex); |
133 |
midiMapsMutex.Unlock(); |
if (midiMaps.empty()) { |
134 |
throw Exception("There is no MIDI instrument map, you have to add one first."); |
throw Exception("There is no MIDI instrument map, you have to add one first."); |
135 |
|
} |
136 |
} |
} |
|
midiMapsMutex.Unlock(); |
|
137 |
if (!Entry.InstrumentFile.size()) |
if (!Entry.InstrumentFile.size()) |
138 |
throw Exception("No instrument file name given"); |
throw Exception("No instrument file name given"); |
139 |
// TODO: an easy one - we should check here if given file exists and throw an exception if it doesn't |
// TODO: an easy one - we should check here if given file exists and throw an exception if it doesn't |
166 |
|
|
167 |
bool Replaced = false; |
bool Replaced = false; |
168 |
int InstrCount = 0; |
int InstrCount = 0; |
169 |
|
bool MapFound = false; |
170 |
midiMapsMutex.Lock(); |
{ |
171 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
LockGuard lock(midiMapsMutex); |
172 |
if (iterMap != midiMaps.end()) { // map found |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
173 |
Replaced = (iterMap->second.find(Index) != iterMap->second.end()); |
if (iterMap != midiMaps.end()) { // map found |
174 |
iterMap->second[Index] = privateEntry; |
MapFound = true; |
175 |
InstrCount = iterMap->second.size(); |
Replaced = (iterMap->second.find(Index) != iterMap->second.end()); |
176 |
} else { // no such map |
iterMap->second[Index] = privateEntry; |
177 |
midiMapsMutex.Unlock(); |
InstrCount = iterMap->second.size(); |
178 |
EngineFactory::Destroy(pEngine); |
} |
|
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
|
179 |
} |
} |
|
midiMapsMutex.Unlock(); |
|
180 |
EngineFactory::Destroy(pEngine); |
EngineFactory::Destroy(pEngine); |
181 |
|
if (!MapFound) { |
182 |
|
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
183 |
|
} |
184 |
|
|
185 |
if (Replaced) { |
if (Replaced) { |
186 |
int Bank = (int(Index.midi_bank_msb) << 7) | int(Index.midi_bank_lsb); |
int Bank = (int(Index.midi_bank_msb) << 7) | int(Index.midi_bank_lsb); |
187 |
fireMidiInstrumentInfoChanged(Map, Bank, Index.midi_prog); |
fireMidiInstrumentInfoChanged(Map, Bank, Index.midi_prog); |
211 |
} |
} |
212 |
|
|
213 |
MidiInstrumentMapper::entry_t MidiInstrumentMapper::GetEntry(int Map, uint MidiBank, uint MidiProg) { |
MidiInstrumentMapper::entry_t MidiInstrumentMapper::GetEntry(int Map, uint MidiBank, uint MidiProg) { |
214 |
midiMapsMutex.Lock(); |
LockGuard lock(midiMapsMutex); |
215 |
|
|
216 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
217 |
if (iterMap == midiMaps.end()) { // no such map |
if (iterMap == midiMaps.end()) { // no such map |
|
midiMapsMutex.Unlock(); |
|
218 |
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
219 |
} |
} |
220 |
|
|
225 |
|
|
226 |
std::map<midi_prog_index_t,private_entry_t>::iterator iterEntry = iterMap->second.find(idx); |
std::map<midi_prog_index_t,private_entry_t>::iterator iterEntry = iterMap->second.find(idx); |
227 |
if (iterEntry == iterMap->second.end()) { |
if (iterEntry == iterMap->second.end()) { |
|
midiMapsMutex.Unlock(); |
|
228 |
throw Exception("There is no map entry with that index"); |
throw Exception("There is no map entry with that index"); |
229 |
} |
} |
230 |
|
|
238 |
try { |
try { |
239 |
SetLoadMode(&entry); |
SetLoadMode(&entry); |
240 |
} catch(Exception e) { |
} catch(Exception e) { |
|
midiMapsMutex.Unlock(); |
|
241 |
throw e; |
throw e; |
242 |
} |
} |
243 |
|
|
|
midiMapsMutex.Unlock(); |
|
|
|
|
244 |
return entry; |
return entry; |
245 |
} |
} |
246 |
|
|
247 |
void MidiInstrumentMapper::RemoveEntry(int Map, midi_prog_index_t Index) { |
void MidiInstrumentMapper::RemoveEntry(int Map, midi_prog_index_t Index) { |
248 |
int InstrCount = -1; |
int InstrCount = -1; |
249 |
|
{ |
250 |
|
LockGuard lock(midiMapsMutex); |
251 |
|
|
252 |
midiMapsMutex.Lock(); |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
253 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
if (iterMap != midiMaps.end()) { // map found |
254 |
if (iterMap != midiMaps.end()) { // map found |
iterMap->second.erase(Index); // remove entry |
255 |
iterMap->second.erase(Index); // remove entry |
InstrCount = iterMap->second.size(); |
256 |
InstrCount = iterMap->second.size(); |
} |
257 |
} |
} |
|
midiMapsMutex.Unlock(); |
|
258 |
|
|
259 |
if (InstrCount != -1) { |
if (InstrCount != -1) { |
260 |
fireMidiInstrumentCountChanged(Map, InstrCount); |
fireMidiInstrumentCountChanged(Map, InstrCount); |
263 |
|
|
264 |
void MidiInstrumentMapper::RemoveAllEntries(int Map) { |
void MidiInstrumentMapper::RemoveAllEntries(int Map) { |
265 |
int InstrCount = -1; |
int InstrCount = -1; |
266 |
|
{ |
267 |
|
LockGuard lock(midiMapsMutex); |
268 |
|
|
269 |
midiMapsMutex.Lock(); |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
270 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
if (iterMap != midiMaps.end()) { // map found |
271 |
if (iterMap != midiMaps.end()) { // map found |
iterMap->second.clear(); // clear that map |
272 |
iterMap->second.clear(); // clear that map |
InstrCount = 0; |
273 |
InstrCount = 0; |
} |
274 |
} |
} |
|
midiMapsMutex.Unlock(); |
|
275 |
|
|
276 |
if (InstrCount != -1) { |
if (InstrCount != -1) { |
277 |
fireMidiInstrumentCountChanged(Map, InstrCount); |
fireMidiInstrumentCountChanged(Map, InstrCount); |
282 |
std::map<midi_prog_index_t,entry_t> result; |
std::map<midi_prog_index_t,entry_t> result; |
283 |
|
|
284 |
// copy the internal map first |
// copy the internal map first |
|
midiMapsMutex.Lock(); |
|
|
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
|
|
if (iterMap == midiMaps.end()) { // no such map |
|
|
midiMapsMutex.Unlock(); |
|
|
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
|
|
} |
|
|
for (std::map<midi_prog_index_t,private_entry_t>::iterator iterEntry = iterMap->second.begin(); |
|
|
iterEntry != iterMap->second.end(); iterEntry++) |
|
285 |
{ |
{ |
286 |
entry_t entry; |
LockGuard lock(midiMapsMutex); |
287 |
entry.EngineName = iterEntry->second.EngineName; |
|
288 |
entry.InstrumentFile = iterEntry->second.InstrumentFile; |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
289 |
entry.InstrumentIndex = iterEntry->second.InstrumentIndex; |
if (iterMap == midiMaps.end()) { // no such map |
290 |
entry.Volume = iterEntry->second.Volume; |
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
291 |
entry.Name = iterEntry->second.Name; |
} |
292 |
result[iterEntry->first] = entry; |
for (std::map<midi_prog_index_t,private_entry_t>::iterator iterEntry = iterMap->second.begin(); |
293 |
|
iterEntry != iterMap->second.end(); iterEntry++) |
294 |
|
{ |
295 |
|
entry_t entry; |
296 |
|
entry.EngineName = iterEntry->second.EngineName; |
297 |
|
entry.InstrumentFile = iterEntry->second.InstrumentFile; |
298 |
|
entry.InstrumentIndex = iterEntry->second.InstrumentIndex; |
299 |
|
entry.Volume = iterEntry->second.Volume; |
300 |
|
entry.Name = iterEntry->second.Name; |
301 |
|
result[iterEntry->first] = entry; |
302 |
|
} |
303 |
} |
} |
|
midiMapsMutex.Unlock(); |
|
304 |
|
|
305 |
// complete it with current LoadMode of each entry |
// complete it with current LoadMode of each entry |
306 |
for (std::map<midi_prog_index_t,entry_t>::iterator iter = result.begin(); |
for (std::map<midi_prog_index_t,entry_t>::iterator iter = result.begin(); |
318 |
|
|
319 |
std::vector<int> MidiInstrumentMapper::Maps() { |
std::vector<int> MidiInstrumentMapper::Maps() { |
320 |
std::vector<int> result; |
std::vector<int> result; |
321 |
midiMapsMutex.Lock(); |
LockGuard lock(midiMapsMutex); |
322 |
for (std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.begin(); |
for (std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.begin(); |
323 |
iterMap != midiMaps.end(); iterMap++) |
iterMap != midiMaps.end(); iterMap++) |
324 |
{ |
{ |
325 |
result.push_back(iterMap->first); |
result.push_back(iterMap->first); |
326 |
} |
} |
|
midiMapsMutex.Unlock(); |
|
327 |
return result; |
return result; |
328 |
} |
} |
329 |
|
|
330 |
int MidiInstrumentMapper::GetMapCount() { |
int MidiInstrumentMapper::GetMapCount() { |
331 |
midiMapsMutex.Lock(); |
LockGuard lock(midiMapsMutex); |
332 |
int i = midiMaps.size(); |
return midiMaps.size(); |
|
midiMapsMutex.Unlock(); |
|
|
return i; |
|
333 |
} |
} |
334 |
|
|
335 |
int MidiInstrumentMapper::GetInstrumentCount(int Map) { |
int MidiInstrumentMapper::GetInstrumentCount(int Map) { |
336 |
midiMapsMutex.Lock(); |
LockGuard lock(midiMapsMutex); |
337 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
338 |
if (iterMap == midiMaps.end()) { // no such map |
if (iterMap == midiMaps.end()) { // no such map |
|
midiMapsMutex.Unlock(); |
|
339 |
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
340 |
} |
} |
341 |
|
|
342 |
int i = iterMap->second.size(); |
return iterMap->second.size(); |
|
midiMapsMutex.Unlock(); |
|
|
return i; |
|
343 |
} |
} |
344 |
|
|
345 |
int MidiInstrumentMapper::GetInstrumentCount() { |
int MidiInstrumentMapper::GetInstrumentCount() { |
346 |
int count = 0; |
int count = 0; |
347 |
|
|
348 |
midiMapsMutex.Lock(); |
LockGuard lock(midiMapsMutex); |
349 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.begin(); |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.begin(); |
350 |
for (;iterMap != midiMaps.end(); iterMap++) { |
for (;iterMap != midiMaps.end(); iterMap++) { |
351 |
count += iterMap->second.size(); |
count += iterMap->second.size(); |
352 |
} |
} |
|
midiMapsMutex.Unlock(); |
|
353 |
|
|
354 |
return count; |
return count; |
355 |
} |
} |
356 |
|
|
357 |
int MidiInstrumentMapper::AddMap(String MapName) throw (Exception) { |
int MidiInstrumentMapper::AddMap(String MapName) throw (Exception) { |
358 |
int ID; |
int ID; |
359 |
midiMapsMutex.Lock(); |
LockGuard lock(midiMapsMutex); |
360 |
|
|
361 |
if (midiMaps.empty()) ID = 0; |
if (midiMaps.empty()) ID = 0; |
362 |
else { |
else { |
363 |
// get the highest existing map ID |
// get the highest existing map ID |
381 |
fireMidiInstrumentMapCountChanged(Maps().size()); |
fireMidiInstrumentMapCountChanged(Maps().size()); |
382 |
// If there were no maps until now we must set a default map. |
// If there were no maps until now we must set a default map. |
383 |
if (midiMaps.size() == 1) SetDefaultMap(ID); |
if (midiMaps.size() == 1) SetDefaultMap(ID); |
|
midiMapsMutex.Unlock(); |
|
384 |
|
|
385 |
return ID; |
return ID; |
386 |
} |
} |
387 |
|
|
388 |
String MidiInstrumentMapper::MapName(int Map) throw (Exception) { |
String MidiInstrumentMapper::MapName(int Map) throw (Exception) { |
389 |
String result; |
LockGuard lock(midiMapsMutex); |
|
midiMapsMutex.Lock(); |
|
390 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
391 |
if (iterMap == midiMaps.end()) { |
if (iterMap == midiMaps.end()) { |
|
midiMapsMutex.Unlock(); |
|
392 |
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
393 |
} |
} |
394 |
result = iterMap->second.name; |
return iterMap->second.name; |
|
midiMapsMutex.Unlock(); |
|
|
return result; |
|
395 |
} |
} |
396 |
|
|
397 |
void MidiInstrumentMapper::RenameMap(int Map, String NewName) throw (Exception) { |
void MidiInstrumentMapper::RenameMap(int Map, String NewName) throw (Exception) { |
398 |
midiMapsMutex.Lock(); |
{ |
399 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
LockGuard lock(midiMapsMutex); |
400 |
if (iterMap == midiMaps.end()) { |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
401 |
midiMapsMutex.Unlock(); |
if (iterMap == midiMaps.end()) { |
402 |
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
403 |
|
} |
404 |
|
iterMap->second.name = NewName; |
405 |
} |
} |
|
iterMap->second.name = NewName; |
|
|
midiMapsMutex.Unlock(); |
|
406 |
fireMidiInstrumentMapInfoChanged(Map); |
fireMidiInstrumentMapInfoChanged(Map); |
407 |
} |
} |
408 |
|
|
409 |
void MidiInstrumentMapper::RemoveMap(int Map) { |
void MidiInstrumentMapper::RemoveMap(int Map) { |
410 |
midiMapsMutex.Lock(); |
LockGuard lock(midiMapsMutex); |
411 |
|
|
412 |
midiMaps.erase(Map); |
midiMaps.erase(Map); |
413 |
if(Map == GetDefaultMap()) { |
if (Map == GetDefaultMap()) { |
414 |
SetDefaultMap(midiMaps.empty() ? -1 : (*(midiMaps.begin())).first); |
SetDefaultMap(midiMaps.empty() ? -1 : (*(midiMaps.begin())).first); |
415 |
} |
} |
416 |
fireMidiInstrumentMapCountChanged(Maps().size()); |
fireMidiInstrumentMapCountChanged(Maps().size()); |
|
midiMapsMutex.Unlock(); |
|
417 |
} |
} |
418 |
|
|
419 |
void MidiInstrumentMapper::RemoveAllMaps() { |
void MidiInstrumentMapper::RemoveAllMaps() { |
420 |
midiMapsMutex.Lock(); |
LockGuard lock(midiMapsMutex); |
421 |
|
|
422 |
midiMaps.clear(); |
midiMaps.clear(); |
423 |
SetDefaultMap(-1); |
SetDefaultMap(-1); |
424 |
fireMidiInstrumentMapCountChanged(Maps().size()); |
fireMidiInstrumentMapCountChanged(Maps().size()); |
|
midiMapsMutex.Unlock(); |
|
425 |
} |
} |
426 |
|
|
427 |
int MidiInstrumentMapper::GetDefaultMap() { |
int MidiInstrumentMapper::GetDefaultMap() { |
428 |
midiMapsMutex.Lock(); |
LockGuard lock(midiMapsMutex); |
429 |
int i = DefaultMap; |
|
430 |
midiMapsMutex.Unlock(); |
return DefaultMap; |
|
return i; |
|
431 |
} |
} |
432 |
|
|
433 |
void MidiInstrumentMapper::SetDefaultMap(int MapId) { |
void MidiInstrumentMapper::SetDefaultMap(int MapId) { |
434 |
midiMapsMutex.Lock(); |
{ |
435 |
DefaultMap = MapId; |
LockGuard lock(midiMapsMutex); |
436 |
midiMapsMutex.Unlock(); |
|
437 |
|
DefaultMap = MapId; |
438 |
|
} |
439 |
|
|
440 |
if (MapId != -1) fireMidiInstrumentMapInfoChanged(MapId); |
if (MapId != -1) fireMidiInstrumentMapInfoChanged(MapId); |
441 |
} |
} |
442 |
|
|
443 |
optional<MidiInstrumentMapper::entry_t> MidiInstrumentMapper::GetEntry(int Map, midi_prog_index_t Index) { |
optional<MidiInstrumentMapper::entry_t> MidiInstrumentMapper::GetEntry(int Map, midi_prog_index_t Index) { |
444 |
optional<entry_t> result; |
optional<entry_t> result; |
445 |
midiMapsMutex.Lock(); |
LockGuard lock(midiMapsMutex); |
446 |
|
|
447 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
448 |
if (iterMap != midiMaps.end()) { // map found |
if (iterMap != midiMaps.end()) { // map found |
449 |
std::map<midi_prog_index_t,private_entry_t>::iterator iterEntry = iterMap->second.find(Index); |
std::map<midi_prog_index_t,private_entry_t>::iterator iterEntry = iterMap->second.find(Index); |
457 |
result = entry; |
result = entry; |
458 |
} |
} |
459 |
} |
} |
|
midiMapsMutex.Unlock(); |
|
460 |
return result; |
return result; |
461 |
} |
} |
462 |
|
|