35 |
String Name; |
String Name; |
36 |
}; |
}; |
37 |
|
|
38 |
// here we store all mappings (MIDI bank&prog) -> (Engine,File,Index) |
// internal map type (MIDI bank&prog) -> (Engine,File,Index) |
39 |
std::map<midi_prog_index_t,private_entry_t> midiMap; |
class MidiInstrumentMap : public std::map<midi_prog_index_t,private_entry_t> { |
40 |
|
public: |
41 |
|
String name; |
42 |
|
}; |
43 |
|
|
44 |
|
// here we store all maps |
45 |
|
std::map<int,MidiInstrumentMap> midiMaps; |
46 |
|
|
47 |
// for synchronization of midiMap |
// for synchronization of midiMaps |
48 |
Mutex midiMapMutex; |
Mutex midiMapsMutex; |
49 |
|
|
50 |
|
|
51 |
void MidiInstrumentMapper::AddOrReplaceMapping(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) { |
52 |
if (bInBackground) { |
if (bInBackground) { |
53 |
dmsg(3,("MidiInstrumentMapper: updating mapping (%d,%d,%d) -> ('%s','%s',%d) with vol=%f, mode=%d in background\n", |
dmsg(3,("MidiInstrumentMapper: updating mapping %d (%d,%d,%d) -> ('%s','%s',%d) with vol=%f, mode=%d in background\n", |
54 |
|
Map, |
55 |
Index.midi_bank_msb,Index.midi_bank_lsb,Index.midi_prog, |
Index.midi_bank_msb,Index.midi_bank_lsb,Index.midi_prog, |
56 |
Entry.EngineName.c_str(),Entry.InstrumentFile.c_str(),Entry.InstrumentIndex, |
Entry.EngineName.c_str(),Entry.InstrumentFile.c_str(),Entry.InstrumentIndex, |
57 |
Entry.Volume,Entry.LoadMode) |
Entry.Volume,Entry.LoadMode) |
58 |
); |
); |
59 |
} else { |
} else { |
60 |
dmsg(3,("MidiInstrumentMapper: updating mapping (%d,%d,%d) -> ('%s','%s',%d) with vol=%f, mode=%d\n", |
dmsg(3,("MidiInstrumentMapper: updating mapping %d (%d,%d,%d) -> ('%s','%s',%d) with vol=%f, mode=%d\n", |
61 |
|
Map, |
62 |
Index.midi_bank_msb,Index.midi_bank_lsb,Index.midi_prog, |
Index.midi_bank_msb,Index.midi_bank_lsb,Index.midi_prog, |
63 |
Entry.EngineName.c_str(),Entry.InstrumentFile.c_str(),Entry.InstrumentIndex, |
Entry.EngineName.c_str(),Entry.InstrumentFile.c_str(),Entry.InstrumentIndex, |
64 |
Entry.Volume,Entry.LoadMode) |
Entry.Volume,Entry.LoadMode) |
65 |
); |
); |
66 |
} |
} |
67 |
|
midiMapsMutex.Lock(); |
68 |
|
if (midiMaps.empty()) { |
69 |
|
midiMapsMutex.Unlock(); |
70 |
|
throw Exception("There is no MIDI instrument map, you have to add one first."); |
71 |
|
} |
72 |
|
midiMapsMutex.Unlock(); |
73 |
if (!Entry.InstrumentFile.size()) |
if (!Entry.InstrumentFile.size()) |
74 |
throw Exception("No instrument file name given"); |
throw Exception("No instrument file name given"); |
75 |
if (Entry.Volume < 0.0) |
if (Entry.Volume < 0.0) |
97 |
privateEntry.InstrumentIndex = Entry.InstrumentIndex; |
privateEntry.InstrumentIndex = Entry.InstrumentIndex; |
98 |
privateEntry.Volume = Entry.Volume; |
privateEntry.Volume = Entry.Volume; |
99 |
privateEntry.Name = Entry.Name; |
privateEntry.Name = Entry.Name; |
100 |
midiMapMutex.Lock(); |
midiMapsMutex.Lock(); |
101 |
midiMap[Index] = privateEntry; |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
102 |
midiMapMutex.Unlock(); |
if (iterMap != midiMaps.end()) { // map found |
103 |
|
iterMap->second[Index] = privateEntry; |
104 |
|
} else { // no such map |
105 |
|
midiMapsMutex.Unlock(); |
106 |
|
EngineFactory::Destroy(pEngine); |
107 |
|
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
108 |
|
} |
109 |
|
midiMapsMutex.Unlock(); |
110 |
EngineFactory::Destroy(pEngine); |
EngineFactory::Destroy(pEngine); |
111 |
} |
} |
112 |
|
|
113 |
void MidiInstrumentMapper::RemoveMapping(midi_prog_index_t Index) { |
void MidiInstrumentMapper::RemoveEntry(int Map, midi_prog_index_t Index) { |
114 |
midiMapMutex.Lock(); |
midiMapsMutex.Lock(); |
115 |
midiMap.erase(Index); |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
116 |
midiMapMutex.Unlock(); |
if (iterMap != midiMaps.end()) { // map found |
117 |
|
iterMap->second.erase(Index); // remove entry |
118 |
|
} |
119 |
|
midiMapsMutex.Unlock(); |
120 |
} |
} |
121 |
|
|
122 |
void MidiInstrumentMapper::RemoveAllMappings() { |
void MidiInstrumentMapper::RemoveAllEntries(int Map) { |
123 |
midiMapMutex.Lock(); |
midiMapsMutex.Lock(); |
124 |
midiMap.clear(); |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
125 |
midiMapMutex.Unlock(); |
if (iterMap != midiMaps.end()) { // map found |
126 |
|
iterMap->second.clear(); // clear that map |
127 |
|
} |
128 |
|
midiMapsMutex.Unlock(); |
129 |
} |
} |
130 |
|
|
131 |
std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> MidiInstrumentMapper::Mappings() { |
std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> MidiInstrumentMapper::Entries(int Map) throw (Exception) { |
132 |
std::map<midi_prog_index_t,entry_t> result; |
std::map<midi_prog_index_t,entry_t> result; |
133 |
|
|
134 |
// copy the internal map first |
// copy the internal map first |
135 |
midiMapMutex.Lock(); |
midiMapsMutex.Lock(); |
136 |
for (std::map<midi_prog_index_t,private_entry_t>::iterator iter = midiMap.begin(); |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
137 |
iter != midiMap.end(); iter++) |
if (iterMap == midiMaps.end()) { // no such map |
138 |
|
midiMapsMutex.Unlock(); |
139 |
|
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
140 |
|
} |
141 |
|
for (std::map<midi_prog_index_t,private_entry_t>::iterator iterEntry = iterMap->second.begin(); |
142 |
|
iterEntry != iterMap->second.end(); iterEntry++) |
143 |
{ |
{ |
144 |
entry_t entry; |
entry_t entry; |
145 |
entry.EngineName = iter->second.EngineName; |
entry.EngineName = iterEntry->second.EngineName; |
146 |
entry.InstrumentFile = iter->second.InstrumentFile; |
entry.InstrumentFile = iterEntry->second.InstrumentFile; |
147 |
entry.InstrumentIndex = iter->second.InstrumentIndex; |
entry.InstrumentIndex = iterEntry->second.InstrumentIndex; |
148 |
entry.Volume = iter->second.Volume; |
entry.Volume = iterEntry->second.Volume; |
149 |
entry.Name = iter->second.Name; |
entry.Name = iterEntry->second.Name; |
150 |
result[iter->first] = entry; |
result[iterEntry->first] = entry; |
151 |
} |
} |
152 |
midiMapMutex.Unlock(); |
midiMapsMutex.Unlock(); |
153 |
|
|
154 |
// complete it with current LoadMode of each entry |
// complete it with current LoadMode of each entry |
155 |
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(); |
156 |
iter != result.end(); iter++) |
iter != result.end(); iter++) |
158 |
entry_t& entry = iter->second; |
entry_t& entry = iter->second; |
159 |
Engine* pEngine = EngineFactory::Create(entry.EngineName); |
Engine* pEngine = EngineFactory::Create(entry.EngineName); |
160 |
if (!pEngine) { // invalid mapping |
if (!pEngine) { // invalid mapping |
161 |
RemoveMapping(iter->first); |
RemoveEntry(Map, iter->first); |
162 |
result.erase(iter); |
result.erase(iter); |
163 |
continue; |
continue; |
164 |
} |
} |
177 |
return result; |
return result; |
178 |
} |
} |
179 |
|
|
180 |
optional<MidiInstrumentMapper::entry_t> MidiInstrumentMapper::GetEntry(midi_prog_index_t Index) { |
std::vector<int> MidiInstrumentMapper::Maps() { |
181 |
|
std::vector<int> result; |
182 |
|
midiMapsMutex.Lock(); |
183 |
|
for (std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.begin(); |
184 |
|
iterMap != midiMaps.end(); iterMap++) |
185 |
|
{ |
186 |
|
result.push_back(iterMap->first); |
187 |
|
} |
188 |
|
midiMapsMutex.Unlock(); |
189 |
|
return result; |
190 |
|
} |
191 |
|
|
192 |
|
int MidiInstrumentMapper::AddMap(String MapName) throw (Exception) { |
193 |
|
int ID; |
194 |
|
midiMapsMutex.Lock(); |
195 |
|
if (midiMaps.empty()) ID = 0; |
196 |
|
else { |
197 |
|
// get the highest existing map ID |
198 |
|
uint lastIndex = (--(midiMaps.end()))->first; |
199 |
|
// check if we reached the index limit |
200 |
|
if (lastIndex + 1 < lastIndex) { |
201 |
|
// search for an unoccupied map ID starting from 0 |
202 |
|
for (uint i = 0; i < lastIndex; i++) { |
203 |
|
if (midiMaps.find(i) != midiMaps.end()) continue; |
204 |
|
// we found an unused ID, so insert the new map there |
205 |
|
ID = i; |
206 |
|
goto __create_map; |
207 |
|
} |
208 |
|
throw Exception("Internal error: could not find unoccupied MIDI instrument map ID."); |
209 |
|
} |
210 |
|
ID = lastIndex; |
211 |
|
} |
212 |
|
__create_map: |
213 |
|
midiMaps[ID].name = MapName; |
214 |
|
midiMapsMutex.Unlock(); |
215 |
|
return ID; |
216 |
|
} |
217 |
|
|
218 |
|
String MidiInstrumentMapper::MapName(int Map) throw (Exception) { |
219 |
|
String result; |
220 |
|
midiMapsMutex.Lock(); |
221 |
|
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
222 |
|
if (iterMap == midiMaps.end()) { |
223 |
|
midiMapsMutex.Unlock(); |
224 |
|
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
225 |
|
} |
226 |
|
result = iterMap->second.name; |
227 |
|
midiMapsMutex.Unlock(); |
228 |
|
return result; |
229 |
|
} |
230 |
|
|
231 |
|
void MidiInstrumentMapper::RenameMap(int Map, String NewName) throw (Exception) { |
232 |
|
midiMapsMutex.Lock(); |
233 |
|
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
234 |
|
if (iterMap == midiMaps.end()) { |
235 |
|
midiMapsMutex.Unlock(); |
236 |
|
throw Exception("There is no MIDI instrument map " + ToString(Map)); |
237 |
|
} |
238 |
|
iterMap->second.name = NewName; |
239 |
|
midiMapsMutex.Unlock(); |
240 |
|
} |
241 |
|
|
242 |
|
void MidiInstrumentMapper::RemoveMap(int Map) { |
243 |
|
midiMapsMutex.Lock(); |
244 |
|
midiMaps.erase(Map); |
245 |
|
midiMapsMutex.Unlock(); |
246 |
|
} |
247 |
|
|
248 |
|
void MidiInstrumentMapper::RemoveAllMaps() { |
249 |
|
midiMapsMutex.Lock(); |
250 |
|
midiMaps.clear(); |
251 |
|
midiMapsMutex.Unlock(); |
252 |
|
} |
253 |
|
|
254 |
|
optional<MidiInstrumentMapper::entry_t> MidiInstrumentMapper::GetEntry(int Map, midi_prog_index_t Index) { |
255 |
optional<entry_t> result; |
optional<entry_t> result; |
256 |
midiMapMutex.Lock(); |
midiMapsMutex.Lock(); |
257 |
std::map<midi_prog_index_t,private_entry_t>::iterator iter = midiMap.find(Index); |
std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map); |
258 |
if (iter != midiMap.end()) { |
if (iterMap != midiMaps.end()) { // map found |
259 |
entry_t entry; |
std::map<midi_prog_index_t,private_entry_t>::iterator iterEntry = iterMap->second.find(Index); |
260 |
entry.EngineName = iter->second.EngineName; |
if (iterEntry != iterMap->second.end()) { |
261 |
entry.InstrumentFile = iter->second.InstrumentFile; |
entry_t entry; |
262 |
entry.InstrumentIndex = iter->second.InstrumentIndex; |
entry.EngineName = iterEntry->second.EngineName; |
263 |
entry.Volume = iter->second.Volume; |
entry.InstrumentFile = iterEntry->second.InstrumentFile; |
264 |
//TODO: for now we skip the LoadMode and Name entry here, since we don't need it in the MidiInputPort |
entry.InstrumentIndex = iterEntry->second.InstrumentIndex; |
265 |
result = entry; |
entry.Volume = iterEntry->second.Volume; |
266 |
|
//TODO: for now we skip the LoadMode and Name entry here, since we don't need it in the MidiInputPort |
267 |
|
result = entry; |
268 |
|
} |
269 |
} |
} |
270 |
midiMapMutex.Unlock(); |
midiMapsMutex.Unlock(); |
271 |
return result; |
return result; |
272 |
} |
} |
273 |
|
|