1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* Copyright (C) 2006 Christian Schoenebeck * |
* Copyright (C) 2006 - 2007 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 * |
23 |
#include "../../common/Mutex.h" |
#include "../../common/Mutex.h" |
24 |
#include "../../engines/EngineFactory.h" |
#include "../../engines/EngineFactory.h" |
25 |
#include "../../engines/Engine.h" |
#include "../../engines/Engine.h" |
|
#include "../../network/lscpserver.h" |
|
26 |
|
|
27 |
namespace LinuxSampler { |
namespace LinuxSampler { |
28 |
|
|
47 |
// for synchronization of midiMaps |
// for synchronization of midiMaps |
48 |
Mutex midiMapsMutex; |
Mutex midiMapsMutex; |
49 |
|
|
50 |
|
ListenerList<MidiInstrumentCountListener*> MidiInstrumentMapper::llMidiInstrumentCountListeners; |
51 |
|
ListenerList<MidiInstrumentInfoListener*> MidiInstrumentMapper::llMidiInstrumentInfoListeners; |
52 |
|
ListenerList<MidiInstrumentMapCountListener*> MidiInstrumentMapper::llMidiInstrumentMapCountListeners; |
53 |
|
ListenerList<MidiInstrumentMapInfoListener*> MidiInstrumentMapper::llMidiInstrumentMapInfoListeners; |
54 |
|
int MidiInstrumentMapper::DefaultMap; |
55 |
|
|
56 |
|
void MidiInstrumentMapper::AddMidiInstrumentCountListener(MidiInstrumentCountListener* l) { |
57 |
|
llMidiInstrumentCountListeners.AddListener(l); |
58 |
|
} |
59 |
|
|
60 |
|
void MidiInstrumentMapper::RemoveMidiInstrumentCountListener(MidiInstrumentCountListener* l) { |
61 |
|
llMidiInstrumentCountListeners.RemoveListener(l); |
62 |
|
} |
63 |
|
|
64 |
|
void MidiInstrumentMapper::fireMidiInstrumentCountChanged(int MapId, int NewCount) { |
65 |
|
for (int i = 0; i < llMidiInstrumentCountListeners.GetListenerCount(); i++) { |
66 |
|
llMidiInstrumentCountListeners.GetListener(i)->MidiInstrumentCountChanged(MapId, NewCount); |
67 |
|
} |
68 |
|
} |
69 |
|
|
70 |
|
void MidiInstrumentMapper::AddMidiInstrumentInfoListener(MidiInstrumentInfoListener* l) { |
71 |
|
llMidiInstrumentInfoListeners.AddListener(l); |
72 |
|
} |
73 |
|
|
74 |
|
void MidiInstrumentMapper::RemoveMidiInstrumentInfoListener(MidiInstrumentInfoListener* l) { |
75 |
|
llMidiInstrumentInfoListeners.RemoveListener(l); |
76 |
|
} |
77 |
|
|
78 |
|
void MidiInstrumentMapper::fireMidiInstrumentInfoChanged(int MapId, int Bank, int Program) { |
79 |
|
for (int i = 0; i < llMidiInstrumentInfoListeners.GetListenerCount(); i++) { |
80 |
|
llMidiInstrumentInfoListeners.GetListener(i)->MidiInstrumentInfoChanged(MapId, Bank, Program); |
81 |
|
} |
82 |
|
} |
83 |
|
|
84 |
|
void MidiInstrumentMapper::AddMidiInstrumentMapCountListener(MidiInstrumentMapCountListener* l) { |
85 |
|
llMidiInstrumentMapCountListeners.AddListener(l); |
86 |
|
} |
87 |
|
|
88 |
|
void MidiInstrumentMapper::RemoveMidiInstrumentMapCountListener(MidiInstrumentMapCountListener* l) { |
89 |
|
llMidiInstrumentMapCountListeners.RemoveListener(l); |
90 |
|
} |
91 |
|
|
92 |
|
void MidiInstrumentMapper::fireMidiInstrumentMapCountChanged(int NewCount) { |
93 |
|
for (int i = 0; i < llMidiInstrumentMapCountListeners.GetListenerCount(); i++) { |
94 |
|
llMidiInstrumentMapCountListeners.GetListener(i)->MidiInstrumentMapCountChanged(NewCount); |
95 |
|
} |
96 |
|
} |
97 |
|
|
98 |
|
void MidiInstrumentMapper::AddMidiInstrumentMapInfoListener(MidiInstrumentMapInfoListener* l) { |
99 |
|
llMidiInstrumentMapInfoListeners.AddListener(l); |
100 |
|
} |
101 |
|
|
102 |
|
void MidiInstrumentMapper::RemoveMidiInstrumentMapInfoListener(MidiInstrumentMapInfoListener* l) { |
103 |
|
llMidiInstrumentMapInfoListeners.RemoveListener(l); |
104 |
|
} |
105 |
|
|
106 |
|
void MidiInstrumentMapper::fireMidiInstrumentMapInfoChanged(int MapId) { |
107 |
|
for (int i = 0; i < llMidiInstrumentMapInfoListeners.GetListenerCount(); i++) { |
108 |
|
llMidiInstrumentMapInfoListeners.GetListener(i)->MidiInstrumentMapInfoChanged(MapId); |
109 |
|
} |
110 |
|
} |
111 |
|
|
112 |
void MidiInstrumentMapper::AddOrReplaceEntry(int Map, midi_prog_index_t Index, entry_t Entry, bool bInBackground) throw (Exception) { |
void MidiInstrumentMapper::AddOrReplaceEntry(int Map, midi_prog_index_t Index, entry_t Entry, bool bInBackground) throw (Exception) { |
113 |
if (bInBackground) { |
if (bInBackground) { |
133 |
midiMapsMutex.Unlock(); |
midiMapsMutex.Unlock(); |
134 |
if (!Entry.InstrumentFile.size()) |
if (!Entry.InstrumentFile.size()) |
135 |
throw Exception("No instrument file name given"); |
throw Exception("No instrument file name given"); |
136 |
|
// TODO: an easy one - we should check here if given file exists and throw an exception if it doesn't |
137 |
if (Entry.Volume < 0.0) |
if (Entry.Volume < 0.0) |
138 |
throw Exception("Volume may not be a negative value"); |
throw Exception("Volume may not be a negative value"); |
139 |
Engine* pEngine = EngineFactory::Create(Entry.EngineName); |
Engine* pEngine = EngineFactory::Create(Entry.EngineName); |
179 |
|
|
180 |
if (Replaced) { |
if (Replaced) { |
181 |
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); |
182 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_info, Map, Bank, Index.midi_prog)); |
fireMidiInstrumentInfoChanged(Map, Bank, Index.midi_prog); |
183 |
} else { |
} else { |
184 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, Map, InstrCount)); |
fireMidiInstrumentCountChanged(Map, InstrCount); |
185 |
} |
} |
186 |
} |
} |
187 |
|
|
197 |
midiMapsMutex.Unlock(); |
midiMapsMutex.Unlock(); |
198 |
|
|
199 |
if (InstrCount != -1) { |
if (InstrCount != -1) { |
200 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, Map, InstrCount)); |
fireMidiInstrumentCountChanged(Map, InstrCount); |
201 |
} |
} |
202 |
} |
} |
203 |
|
|
213 |
midiMapsMutex.Unlock(); |
midiMapsMutex.Unlock(); |
214 |
|
|
215 |
if (InstrCount != -1) { |
if (InstrCount != -1) { |
216 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, Map, InstrCount)); |
fireMidiInstrumentCountChanged(Map, InstrCount); |
217 |
} |
} |
218 |
} |
} |
219 |
|
|
278 |
return result; |
return result; |
279 |
} |
} |
280 |
|
|
281 |
|
int MidiInstrumentMapper::GetMapCount() { |
282 |
|
midiMapsMutex.Lock(); |
283 |
|
int i = midiMaps.size(); |
284 |
|
midiMapsMutex.Unlock(); |
285 |
|
return i; |
286 |
|
} |
287 |
|
|
288 |
int MidiInstrumentMapper::AddMap(String MapName) throw (Exception) { |
int MidiInstrumentMapper::AddMap(String MapName) throw (Exception) { |
289 |
int ID; |
int ID; |
290 |
midiMapsMutex.Lock(); |
midiMapsMutex.Lock(); |
307 |
} |
} |
308 |
__create_map: |
__create_map: |
309 |
midiMaps[ID].name = MapName; |
midiMaps[ID].name = MapName; |
310 |
|
|
311 |
|
fireMidiInstrumentMapCountChanged(Maps().size()); |
312 |
|
// If there were no maps until now we must set a default map. |
313 |
|
if (midiMaps.size() == 1) SetDefaultMap(ID); |
314 |
midiMapsMutex.Unlock(); |
midiMapsMutex.Unlock(); |
315 |
|
|
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_count, Maps().size())); |
|
316 |
return ID; |
return ID; |
317 |
} |
} |
318 |
|
|
338 |
} |
} |
339 |
iterMap->second.name = NewName; |
iterMap->second.name = NewName; |
340 |
midiMapsMutex.Unlock(); |
midiMapsMutex.Unlock(); |
341 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_info, Map)); |
fireMidiInstrumentMapInfoChanged(Map); |
342 |
} |
} |
343 |
|
|
344 |
void MidiInstrumentMapper::RemoveMap(int Map) { |
void MidiInstrumentMapper::RemoveMap(int Map) { |
345 |
midiMapsMutex.Lock(); |
midiMapsMutex.Lock(); |
346 |
midiMaps.erase(Map); |
midiMaps.erase(Map); |
347 |
|
if(Map == GetDefaultMap()) { |
348 |
|
SetDefaultMap(midiMaps.empty() ? -1 : (*(midiMaps.begin())).first); |
349 |
|
} |
350 |
|
fireMidiInstrumentMapCountChanged(Maps().size()); |
351 |
midiMapsMutex.Unlock(); |
midiMapsMutex.Unlock(); |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_count, Maps().size())); |
|
352 |
} |
} |
353 |
|
|
354 |
void MidiInstrumentMapper::RemoveAllMaps() { |
void MidiInstrumentMapper::RemoveAllMaps() { |
355 |
midiMapsMutex.Lock(); |
midiMapsMutex.Lock(); |
356 |
midiMaps.clear(); |
midiMaps.clear(); |
357 |
|
SetDefaultMap(-1); |
358 |
|
fireMidiInstrumentMapCountChanged(Maps().size()); |
359 |
|
midiMapsMutex.Unlock(); |
360 |
|
} |
361 |
|
|
362 |
|
int MidiInstrumentMapper::GetDefaultMap() { |
363 |
|
midiMapsMutex.Lock(); |
364 |
|
int i = DefaultMap; |
365 |
midiMapsMutex.Unlock(); |
midiMapsMutex.Unlock(); |
366 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_count, Maps().size())); |
return i; |
367 |
|
} |
368 |
|
|
369 |
|
void MidiInstrumentMapper::SetDefaultMap(int MapId) { |
370 |
|
midiMapsMutex.Lock(); |
371 |
|
DefaultMap = MapId; |
372 |
|
midiMapsMutex.Unlock(); |
373 |
|
|
374 |
|
if (MapId != -1) fireMidiInstrumentMapInfoChanged(MapId); |
375 |
} |
} |
376 |
|
|
377 |
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) { |