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 * |
20 |
|
|
21 |
#include "MidiInstrumentMapper.h" |
#include "MidiInstrumentMapper.h" |
22 |
|
|
23 |
|
#include "../../common/global_private.h" |
24 |
#include "../../common/Mutex.h" |
#include "../../common/Mutex.h" |
25 |
#include "../../engines/EngineFactory.h" |
#include "../../engines/EngineFactory.h" |
26 |
#include "../../engines/Engine.h" |
#include "../../engines/Engine.h" |
48 |
// for synchronization of midiMaps |
// for synchronization of midiMaps |
49 |
Mutex midiMapsMutex; |
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) { |
void MidiInstrumentMapper::AddOrReplaceEntry(int Map, midi_prog_index_t Index, entry_t Entry, bool bInBackground) throw (Exception) { |
114 |
if (bInBackground) { |
if (bInBackground) { |
134 |
midiMapsMutex.Unlock(); |
midiMapsMutex.Unlock(); |
135 |
if (!Entry.InstrumentFile.size()) |
if (!Entry.InstrumentFile.size()) |
136 |
throw Exception("No instrument file name given"); |
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) |
if (Entry.Volume < 0.0) |
139 |
throw Exception("Volume may not be a negative value"); |
throw Exception("Volume may not be a negative value"); |
140 |
Engine* pEngine = EngineFactory::Create(Entry.EngineName); |
Engine* pEngine = EngineFactory::Create(Entry.EngineName); |
145 |
InstrumentManager::instrument_id_t id; |
InstrumentManager::instrument_id_t id; |
146 |
id.FileName = Entry.InstrumentFile; |
id.FileName = Entry.InstrumentFile; |
147 |
id.Index = Entry.InstrumentIndex; |
id.Index = Entry.InstrumentIndex; |
148 |
if (Entry.LoadMode != VOID) { |
if (Entry.LoadMode != DONTCARE) { |
149 |
if (bInBackground) |
if (bInBackground) |
150 |
pEngine->GetInstrumentManager()->SetModeInBackground(id, static_cast<InstrumentManager::mode_t>(Entry.LoadMode)); |
pEngine->GetInstrumentManager()->SetModeInBackground(id, static_cast<InstrumentManager::mode_t>(Entry.LoadMode)); |
151 |
else |
else |
160 |
privateEntry.InstrumentIndex = Entry.InstrumentIndex; |
privateEntry.InstrumentIndex = Entry.InstrumentIndex; |
161 |
privateEntry.Volume = Entry.Volume; |
privateEntry.Volume = Entry.Volume; |
162 |
privateEntry.Name = Entry.Name; |
privateEntry.Name = Entry.Name; |
163 |
|
|
164 |
|
bool Replaced = false; |
165 |
|
int InstrCount = 0; |
166 |
|
|
167 |
midiMapsMutex.Lock(); |
midiMapsMutex.Lock(); |
168 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
169 |
if (iterMap != midiMaps.end()) { // map found |
if (iterMap != midiMaps.end()) { // map found |
170 |
|
Replaced = (iterMap->second.find(Index) != iterMap->second.end()); |
171 |
iterMap->second[Index] = privateEntry; |
iterMap->second[Index] = privateEntry; |
172 |
|
InstrCount = iterMap->second.size(); |
173 |
} else { // no such map |
} else { // no such map |
174 |
midiMapsMutex.Unlock(); |
midiMapsMutex.Unlock(); |
175 |
EngineFactory::Destroy(pEngine); |
EngineFactory::Destroy(pEngine); |
177 |
} |
} |
178 |
midiMapsMutex.Unlock(); |
midiMapsMutex.Unlock(); |
179 |
EngineFactory::Destroy(pEngine); |
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) { |
void MidiInstrumentMapper::RemoveEntry(int Map, midi_prog_index_t Index) { |
190 |
|
int InstrCount = -1; |
191 |
|
|
192 |
midiMapsMutex.Lock(); |
midiMapsMutex.Lock(); |
193 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
194 |
if (iterMap != midiMaps.end()) { // map found |
if (iterMap != midiMaps.end()) { // map found |
195 |
iterMap->second.erase(Index); // remove entry |
iterMap->second.erase(Index); // remove entry |
196 |
|
InstrCount = iterMap->second.size(); |
197 |
} |
} |
198 |
midiMapsMutex.Unlock(); |
midiMapsMutex.Unlock(); |
199 |
|
|
200 |
|
if (InstrCount != -1) { |
201 |
|
fireMidiInstrumentCountChanged(Map, InstrCount); |
202 |
|
} |
203 |
} |
} |
204 |
|
|
205 |
void MidiInstrumentMapper::RemoveAllEntries(int Map) { |
void MidiInstrumentMapper::RemoveAllEntries(int Map) { |
206 |
|
int InstrCount = -1; |
207 |
|
|
208 |
midiMapsMutex.Lock(); |
midiMapsMutex.Lock(); |
209 |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
210 |
if (iterMap != midiMaps.end()) { // map found |
if (iterMap != midiMaps.end()) { // map found |
211 |
iterMap->second.clear(); // clear that map |
iterMap->second.clear(); // clear that map |
212 |
|
InstrCount = 0; |
213 |
} |
} |
214 |
midiMapsMutex.Unlock(); |
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) { |
std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> MidiInstrumentMapper::Entries(int Map) throw (Exception) { |
279 |
return result; |
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) { |
int MidiInstrumentMapper::AddMap(String MapName) throw (Exception) { |
290 |
int ID; |
int ID; |
291 |
midiMapsMutex.Lock(); |
midiMapsMutex.Lock(); |
308 |
} |
} |
309 |
__create_map: |
__create_map: |
310 |
midiMaps[ID].name = MapName; |
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(); |
midiMapsMutex.Unlock(); |
316 |
|
|
317 |
return ID; |
return ID; |
318 |
} |
} |
319 |
|
|
339 |
} |
} |
340 |
iterMap->second.name = NewName; |
iterMap->second.name = NewName; |
341 |
midiMapsMutex.Unlock(); |
midiMapsMutex.Unlock(); |
342 |
|
fireMidiInstrumentMapInfoChanged(Map); |
343 |
} |
} |
344 |
|
|
345 |
void MidiInstrumentMapper::RemoveMap(int Map) { |
void MidiInstrumentMapper::RemoveMap(int Map) { |
346 |
midiMapsMutex.Lock(); |
midiMapsMutex.Lock(); |
347 |
midiMaps.erase(Map); |
midiMaps.erase(Map); |
348 |
|
if(Map == GetDefaultMap()) { |
349 |
|
SetDefaultMap(midiMaps.empty() ? -1 : (*(midiMaps.begin())).first); |
350 |
|
} |
351 |
|
fireMidiInstrumentMapCountChanged(Maps().size()); |
352 |
midiMapsMutex.Unlock(); |
midiMapsMutex.Unlock(); |
353 |
} |
} |
354 |
|
|
355 |
void MidiInstrumentMapper::RemoveAllMaps() { |
void MidiInstrumentMapper::RemoveAllMaps() { |
356 |
midiMapsMutex.Lock(); |
midiMapsMutex.Lock(); |
357 |
midiMaps.clear(); |
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(); |
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) { |
optional<MidiInstrumentMapper::entry_t> MidiInstrumentMapper::GetEntry(int Map, midi_prog_index_t Index) { |