1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* Copyright (C) 2006 - 2007 Christian Schoenebeck * |
* Copyright (C) 2006 - 2016 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 * |
25 |
#include "../../engines/EngineFactory.h" |
#include "../../engines/EngineFactory.h" |
26 |
#include "../../engines/Engine.h" |
#include "../../engines/Engine.h" |
27 |
|
|
28 |
#include <RIFF.h> |
#if AC_APPLE_UNIVERSAL_BUILD |
29 |
|
# include <libgig/RIFF.h> |
30 |
|
#else |
31 |
|
# include <RIFF.h> |
32 |
|
#endif |
33 |
|
|
34 |
namespace LinuxSampler { |
namespace LinuxSampler { |
35 |
|
|
132 |
Entry.Volume,Entry.LoadMode) |
Entry.Volume,Entry.LoadMode) |
133 |
); |
); |
134 |
} |
} |
135 |
midiMapsMutex.Lock(); |
{ |
136 |
if (midiMaps.empty()) { |
LockGuard lock(midiMapsMutex); |
137 |
midiMapsMutex.Unlock(); |
if (midiMaps.empty()) { |
138 |
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."); |
139 |
|
} |
140 |
} |
} |
|
midiMapsMutex.Unlock(); |
|
141 |
if (!Entry.InstrumentFile.size()) |
if (!Entry.InstrumentFile.size()) |
142 |
throw Exception("No instrument file name given"); |
throw Exception("No instrument file name given"); |
143 |
// 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 |
170 |
|
|
171 |
bool Replaced = false; |
bool Replaced = false; |
172 |
int InstrCount = 0; |
int InstrCount = 0; |
173 |
|
bool MapFound = false; |
174 |
midiMapsMutex.Lock(); |
{ |
175 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
LockGuard lock(midiMapsMutex); |
176 |
if (iterMap != midiMaps.end()) { // map found |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
177 |
Replaced = (iterMap->second.find(Index) != iterMap->second.end()); |
if (iterMap != midiMaps.end()) { // map found |
178 |
iterMap->second[Index] = privateEntry; |
MapFound = true; |
179 |
InstrCount = iterMap->second.size(); |
Replaced = (iterMap->second.find(Index) != iterMap->second.end()); |
180 |
} else { // no such map |
iterMap->second[Index] = privateEntry; |
181 |
midiMapsMutex.Unlock(); |
InstrCount = (int)iterMap->second.size(); |
182 |
EngineFactory::Destroy(pEngine); |
} |
|
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
|
183 |
} |
} |
|
midiMapsMutex.Unlock(); |
|
184 |
EngineFactory::Destroy(pEngine); |
EngineFactory::Destroy(pEngine); |
185 |
|
if (!MapFound) { |
186 |
|
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
187 |
|
} |
188 |
|
|
189 |
if (Replaced) { |
if (Replaced) { |
190 |
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); |
191 |
fireMidiInstrumentInfoChanged(Map, Bank, Index.midi_prog); |
fireMidiInstrumentInfoChanged(Map, Bank, Index.midi_prog); |
215 |
} |
} |
216 |
|
|
217 |
MidiInstrumentMapper::entry_t MidiInstrumentMapper::GetEntry(int Map, uint MidiBank, uint MidiProg) { |
MidiInstrumentMapper::entry_t MidiInstrumentMapper::GetEntry(int Map, uint MidiBank, uint MidiProg) { |
218 |
midiMapsMutex.Lock(); |
LockGuard lock(midiMapsMutex); |
219 |
|
|
220 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
221 |
if (iterMap == midiMaps.end()) { // no such map |
if (iterMap == midiMaps.end()) { // no such map |
|
midiMapsMutex.Unlock(); |
|
222 |
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
223 |
} |
} |
224 |
|
|
229 |
|
|
230 |
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); |
231 |
if (iterEntry == iterMap->second.end()) { |
if (iterEntry == iterMap->second.end()) { |
|
midiMapsMutex.Unlock(); |
|
232 |
throw Exception("There is no map entry with that index"); |
throw Exception("There is no map entry with that index"); |
233 |
} |
} |
234 |
|
|
242 |
try { |
try { |
243 |
SetLoadMode(&entry); |
SetLoadMode(&entry); |
244 |
} catch(Exception e) { |
} catch(Exception e) { |
|
midiMapsMutex.Unlock(); |
|
245 |
throw e; |
throw e; |
246 |
} |
} |
247 |
|
|
|
midiMapsMutex.Unlock(); |
|
|
|
|
248 |
return entry; |
return entry; |
249 |
} |
} |
250 |
|
|
251 |
void MidiInstrumentMapper::RemoveEntry(int Map, midi_prog_index_t Index) { |
void MidiInstrumentMapper::RemoveEntry(int Map, midi_prog_index_t Index) { |
252 |
int InstrCount = -1; |
int InstrCount = -1; |
253 |
|
{ |
254 |
|
LockGuard lock(midiMapsMutex); |
255 |
|
|
256 |
midiMapsMutex.Lock(); |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
257 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
if (iterMap != midiMaps.end()) { // map found |
258 |
if (iterMap != midiMaps.end()) { // map found |
iterMap->second.erase(Index); // remove entry |
259 |
iterMap->second.erase(Index); // remove entry |
InstrCount = (int)iterMap->second.size(); |
260 |
InstrCount = iterMap->second.size(); |
} |
261 |
} |
} |
|
midiMapsMutex.Unlock(); |
|
262 |
|
|
263 |
if (InstrCount != -1) { |
if (InstrCount != -1) { |
264 |
fireMidiInstrumentCountChanged(Map, InstrCount); |
fireMidiInstrumentCountChanged(Map, InstrCount); |
267 |
|
|
268 |
void MidiInstrumentMapper::RemoveAllEntries(int Map) { |
void MidiInstrumentMapper::RemoveAllEntries(int Map) { |
269 |
int InstrCount = -1; |
int InstrCount = -1; |
270 |
|
{ |
271 |
|
LockGuard lock(midiMapsMutex); |
272 |
|
|
273 |
midiMapsMutex.Lock(); |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
274 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
if (iterMap != midiMaps.end()) { // map found |
275 |
if (iterMap != midiMaps.end()) { // map found |
iterMap->second.clear(); // clear that map |
276 |
iterMap->second.clear(); // clear that map |
InstrCount = 0; |
277 |
InstrCount = 0; |
} |
278 |
} |
} |
|
midiMapsMutex.Unlock(); |
|
279 |
|
|
280 |
if (InstrCount != -1) { |
if (InstrCount != -1) { |
281 |
fireMidiInstrumentCountChanged(Map, InstrCount); |
fireMidiInstrumentCountChanged(Map, InstrCount); |
286 |
std::map<midi_prog_index_t,entry_t> result; |
std::map<midi_prog_index_t,entry_t> result; |
287 |
|
|
288 |
// 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++) |
|
289 |
{ |
{ |
290 |
entry_t entry; |
LockGuard lock(midiMapsMutex); |
291 |
entry.EngineName = iterEntry->second.EngineName; |
|
292 |
entry.InstrumentFile = iterEntry->second.InstrumentFile; |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
293 |
entry.InstrumentIndex = iterEntry->second.InstrumentIndex; |
if (iterMap == midiMaps.end()) { // no such map |
294 |
entry.Volume = iterEntry->second.Volume; |
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
295 |
entry.Name = iterEntry->second.Name; |
} |
296 |
result[iterEntry->first] = entry; |
for (std::map<midi_prog_index_t,private_entry_t>::iterator iterEntry = iterMap->second.begin(); |
297 |
|
iterEntry != iterMap->second.end(); iterEntry++) |
298 |
|
{ |
299 |
|
entry_t entry; |
300 |
|
entry.EngineName = iterEntry->second.EngineName; |
301 |
|
entry.InstrumentFile = iterEntry->second.InstrumentFile; |
302 |
|
entry.InstrumentIndex = iterEntry->second.InstrumentIndex; |
303 |
|
entry.Volume = iterEntry->second.Volume; |
304 |
|
entry.Name = iterEntry->second.Name; |
305 |
|
result[iterEntry->first] = entry; |
306 |
|
} |
307 |
} |
} |
|
midiMapsMutex.Unlock(); |
|
308 |
|
|
309 |
// complete it with current LoadMode of each entry |
// complete it with current LoadMode of each entry |
310 |
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(); |
322 |
|
|
323 |
std::vector<int> MidiInstrumentMapper::Maps() { |
std::vector<int> MidiInstrumentMapper::Maps() { |
324 |
std::vector<int> result; |
std::vector<int> result; |
325 |
midiMapsMutex.Lock(); |
LockGuard lock(midiMapsMutex); |
326 |
for (std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.begin(); |
for (std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.begin(); |
327 |
iterMap != midiMaps.end(); iterMap++) |
iterMap != midiMaps.end(); iterMap++) |
328 |
{ |
{ |
329 |
result.push_back(iterMap->first); |
result.push_back(iterMap->first); |
330 |
} |
} |
|
midiMapsMutex.Unlock(); |
|
331 |
return result; |
return result; |
332 |
} |
} |
333 |
|
|
334 |
int MidiInstrumentMapper::GetMapCount() { |
int MidiInstrumentMapper::GetMapCount() { |
335 |
midiMapsMutex.Lock(); |
LockGuard lock(midiMapsMutex); |
336 |
int i = midiMaps.size(); |
return (int) midiMaps.size(); |
|
midiMapsMutex.Unlock(); |
|
|
return i; |
|
337 |
} |
} |
338 |
|
|
339 |
int MidiInstrumentMapper::GetInstrumentCount(int Map) { |
int MidiInstrumentMapper::GetInstrumentCount(int Map) { |
340 |
midiMapsMutex.Lock(); |
LockGuard lock(midiMapsMutex); |
341 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
342 |
if (iterMap == midiMaps.end()) { // no such map |
if (iterMap == midiMaps.end()) { // no such map |
|
midiMapsMutex.Unlock(); |
|
343 |
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
344 |
} |
} |
345 |
|
|
346 |
int i = iterMap->second.size(); |
return (int) iterMap->second.size(); |
|
midiMapsMutex.Unlock(); |
|
|
return i; |
|
347 |
} |
} |
348 |
|
|
349 |
int MidiInstrumentMapper::GetInstrumentCount() { |
int MidiInstrumentMapper::GetInstrumentCount() { |
350 |
int count = 0; |
int count = 0; |
351 |
|
|
352 |
midiMapsMutex.Lock(); |
LockGuard lock(midiMapsMutex); |
353 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.begin(); |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.begin(); |
354 |
for (;iterMap != midiMaps.end(); iterMap++) { |
for (;iterMap != midiMaps.end(); iterMap++) { |
355 |
count += iterMap->second.size(); |
count += iterMap->second.size(); |
356 |
} |
} |
|
midiMapsMutex.Unlock(); |
|
357 |
|
|
358 |
return count; |
return count; |
359 |
} |
} |
360 |
|
|
361 |
int MidiInstrumentMapper::AddMap(String MapName) throw (Exception) { |
int MidiInstrumentMapper::AddMap(String MapName) throw (Exception) { |
362 |
int ID; |
int ID; |
363 |
midiMapsMutex.Lock(); |
LockGuard lock(midiMapsMutex); |
364 |
|
|
365 |
if (midiMaps.empty()) ID = 0; |
if (midiMaps.empty()) ID = 0; |
366 |
else { |
else { |
367 |
// get the highest existing map ID |
// get the highest existing map ID |
382 |
__create_map: |
__create_map: |
383 |
midiMaps[ID].name = MapName; |
midiMaps[ID].name = MapName; |
384 |
|
|
385 |
fireMidiInstrumentMapCountChanged(Maps().size()); |
fireMidiInstrumentMapCountChanged((int)Maps().size()); |
386 |
// 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. |
387 |
if (midiMaps.size() == 1) SetDefaultMap(ID); |
if (midiMaps.size() == 1) SetDefaultMap(ID); |
|
midiMapsMutex.Unlock(); |
|
388 |
|
|
389 |
return ID; |
return ID; |
390 |
} |
} |
391 |
|
|
392 |
String MidiInstrumentMapper::MapName(int Map) throw (Exception) { |
String MidiInstrumentMapper::MapName(int Map) throw (Exception) { |
393 |
String result; |
LockGuard lock(midiMapsMutex); |
|
midiMapsMutex.Lock(); |
|
394 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
395 |
if (iterMap == midiMaps.end()) { |
if (iterMap == midiMaps.end()) { |
|
midiMapsMutex.Unlock(); |
|
396 |
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
397 |
} |
} |
398 |
result = iterMap->second.name; |
return iterMap->second.name; |
|
midiMapsMutex.Unlock(); |
|
|
return result; |
|
399 |
} |
} |
400 |
|
|
401 |
void MidiInstrumentMapper::RenameMap(int Map, String NewName) throw (Exception) { |
void MidiInstrumentMapper::RenameMap(int Map, String NewName) throw (Exception) { |
402 |
midiMapsMutex.Lock(); |
{ |
403 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
LockGuard lock(midiMapsMutex); |
404 |
if (iterMap == midiMaps.end()) { |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
405 |
midiMapsMutex.Unlock(); |
if (iterMap == midiMaps.end()) { |
406 |
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
407 |
|
} |
408 |
|
iterMap->second.name = NewName; |
409 |
} |
} |
|
iterMap->second.name = NewName; |
|
|
midiMapsMutex.Unlock(); |
|
410 |
fireMidiInstrumentMapInfoChanged(Map); |
fireMidiInstrumentMapInfoChanged(Map); |
411 |
} |
} |
412 |
|
|
413 |
void MidiInstrumentMapper::RemoveMap(int Map) { |
void MidiInstrumentMapper::RemoveMap(int Map) { |
414 |
midiMapsMutex.Lock(); |
LockGuard lock(midiMapsMutex); |
415 |
|
|
416 |
midiMaps.erase(Map); |
midiMaps.erase(Map); |
417 |
if(Map == GetDefaultMap()) { |
if (Map == GetDefaultMap()) { |
418 |
SetDefaultMap(midiMaps.empty() ? -1 : (*(midiMaps.begin())).first); |
SetDefaultMap(midiMaps.empty() ? -1 : (*(midiMaps.begin())).first); |
419 |
} |
} |
420 |
fireMidiInstrumentMapCountChanged(Maps().size()); |
fireMidiInstrumentMapCountChanged((int)Maps().size()); |
|
midiMapsMutex.Unlock(); |
|
421 |
} |
} |
422 |
|
|
423 |
void MidiInstrumentMapper::RemoveAllMaps() { |
void MidiInstrumentMapper::RemoveAllMaps() { |
424 |
midiMapsMutex.Lock(); |
LockGuard lock(midiMapsMutex); |
425 |
|
|
426 |
midiMaps.clear(); |
midiMaps.clear(); |
427 |
SetDefaultMap(-1); |
SetDefaultMap(-1); |
428 |
fireMidiInstrumentMapCountChanged(Maps().size()); |
fireMidiInstrumentMapCountChanged((int)Maps().size()); |
|
midiMapsMutex.Unlock(); |
|
429 |
} |
} |
430 |
|
|
431 |
int MidiInstrumentMapper::GetDefaultMap() { |
int MidiInstrumentMapper::GetDefaultMap() { |
432 |
midiMapsMutex.Lock(); |
LockGuard lock(midiMapsMutex); |
433 |
int i = DefaultMap; |
|
434 |
midiMapsMutex.Unlock(); |
return DefaultMap; |
|
return i; |
|
435 |
} |
} |
436 |
|
|
437 |
void MidiInstrumentMapper::SetDefaultMap(int MapId) { |
void MidiInstrumentMapper::SetDefaultMap(int MapId) { |
438 |
midiMapsMutex.Lock(); |
{ |
439 |
DefaultMap = MapId; |
LockGuard lock(midiMapsMutex); |
440 |
midiMapsMutex.Unlock(); |
|
441 |
|
DefaultMap = MapId; |
442 |
|
} |
443 |
|
|
444 |
if (MapId != -1) fireMidiInstrumentMapInfoChanged(MapId); |
if (MapId != -1) fireMidiInstrumentMapInfoChanged(MapId); |
445 |
} |
} |
446 |
|
|
447 |
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) { |
448 |
optional<entry_t> result; |
optional<entry_t> result; |
449 |
midiMapsMutex.Lock(); |
LockGuard lock(midiMapsMutex); |
450 |
|
|
451 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
452 |
if (iterMap != midiMaps.end()) { // map found |
if (iterMap != midiMaps.end()) { // map found |
453 |
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 |
entry.InstrumentFile = iterEntry->second.InstrumentFile; |
entry.InstrumentFile = iterEntry->second.InstrumentFile; |
458 |
entry.InstrumentIndex = iterEntry->second.InstrumentIndex; |
entry.InstrumentIndex = iterEntry->second.InstrumentIndex; |
459 |
entry.Volume = iterEntry->second.Volume; |
entry.Volume = iterEntry->second.Volume; |
460 |
//TODO: for now we skip the LoadMode and Name entry here, since we don't need it in the MidiInputPort |
//TODO: for now we skip the LoadMode and Name entry here, since we don't need it in the EngineChannel |
461 |
result = entry; |
result = entry; |
462 |
} |
} |
463 |
} |
} |
|
midiMapsMutex.Unlock(); |
|
464 |
return result; |
return result; |
465 |
} |
} |
466 |
|
|