/[svn]/linuxsampler/trunk/src/drivers/midi/MidiInstrumentMapper.cpp
ViewVC logotype

Annotation of /linuxsampler/trunk/src/drivers/midi/MidiInstrumentMapper.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1481 - (hide annotations) (download)
Wed Nov 14 23:42:15 2007 UTC (16 years, 4 months ago) by senoner
File size: 16873 byte(s)
* win32 port work in progress:
* - implemented win32 support in the following classes:
* Thread, Condition, Mutex, Path, LscpServer
* - lscp.y use DONTCARE instead of VOID
*  (a win32 symbol defined)
* - completed win32 editor plugin loader

1 schoenebeck 947 /***************************************************************************
2     * *
3 schoenebeck 1040 * Copyright (C) 2006 - 2007 Christian Schoenebeck *
4 schoenebeck 947 * *
5     * 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 *
7     * the Free Software Foundation; either version 2 of the License, or *
8     * (at your option) any later version. *
9     * *
10     * This library is distributed in the hope that it will be useful, *
11     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13     * GNU General Public License for more details. *
14     * *
15     * You should have received a copy of the GNU General Public License *
16     * along with this library; if not, write to the Free Software *
17     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
18     * MA 02111-1307 USA *
19     ***************************************************************************/
20    
21     #include "MidiInstrumentMapper.h"
22    
23 schoenebeck 1424 #include "../../common/global_private.h"
24 schoenebeck 947 #include "../../common/Mutex.h"
25     #include "../../engines/EngineFactory.h"
26     #include "../../engines/Engine.h"
27    
28     namespace LinuxSampler {
29    
30     // same as entry_t but without 'LoadMode'
31     struct private_entry_t {
32     String EngineName;
33     String InstrumentFile;
34     uint InstrumentIndex;
35 schoenebeck 958 float Volume;
36 schoenebeck 947 String Name;
37     };
38    
39 schoenebeck 973 // internal map type (MIDI bank&prog) -> (Engine,File,Index)
40     class MidiInstrumentMap : public std::map<midi_prog_index_t,private_entry_t> {
41     public:
42     String name;
43     };
44 schoenebeck 947
45 schoenebeck 973 // here we store all maps
46     std::map<int,MidiInstrumentMap> midiMaps;
47 schoenebeck 947
48 schoenebeck 973 // for synchronization of midiMaps
49     Mutex midiMapsMutex;
50 schoenebeck 947
51 iliev 1130 ListenerList<MidiInstrumentCountListener*> MidiInstrumentMapper::llMidiInstrumentCountListeners;
52     ListenerList<MidiInstrumentInfoListener*> MidiInstrumentMapper::llMidiInstrumentInfoListeners;
53     ListenerList<MidiInstrumentMapCountListener*> MidiInstrumentMapper::llMidiInstrumentMapCountListeners;
54     ListenerList<MidiInstrumentMapInfoListener*> MidiInstrumentMapper::llMidiInstrumentMapInfoListeners;
55 iliev 1135 int MidiInstrumentMapper::DefaultMap;
56 iliev 1130
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 schoenebeck 973 void MidiInstrumentMapper::AddOrReplaceEntry(int Map, midi_prog_index_t Index, entry_t Entry, bool bInBackground) throw (Exception) {
114 schoenebeck 947 if (bInBackground) {
115 schoenebeck 973 dmsg(3,("MidiInstrumentMapper: updating mapping %d (%d,%d,%d) -> ('%s','%s',%d) with vol=%f, mode=%d in background\n",
116     Map,
117 schoenebeck 947 Index.midi_bank_msb,Index.midi_bank_lsb,Index.midi_prog,
118     Entry.EngineName.c_str(),Entry.InstrumentFile.c_str(),Entry.InstrumentIndex,
119     Entry.Volume,Entry.LoadMode)
120     );
121     } else {
122 schoenebeck 973 dmsg(3,("MidiInstrumentMapper: updating mapping %d (%d,%d,%d) -> ('%s','%s',%d) with vol=%f, mode=%d\n",
123     Map,
124 schoenebeck 947 Index.midi_bank_msb,Index.midi_bank_lsb,Index.midi_prog,
125     Entry.EngineName.c_str(),Entry.InstrumentFile.c_str(),Entry.InstrumentIndex,
126     Entry.Volume,Entry.LoadMode)
127     );
128     }
129 schoenebeck 973 midiMapsMutex.Lock();
130     if (midiMaps.empty()) {
131     midiMapsMutex.Unlock();
132     throw Exception("There is no MIDI instrument map, you have to add one first.");
133     }
134     midiMapsMutex.Unlock();
135 schoenebeck 970 if (!Entry.InstrumentFile.size())
136     throw Exception("No instrument file name given");
137 schoenebeck 1040 // TODO: an easy one - we should check here if given file exists and throw an exception if it doesn't
138 schoenebeck 947 if (Entry.Volume < 0.0)
139     throw Exception("Volume may not be a negative value");
140     Engine* pEngine = EngineFactory::Create(Entry.EngineName);
141     if (!pEngine)
142     throw Exception("No such engine type '"+Entry.EngineName+"'");
143     Entry.EngineName = pEngine->EngineName(); // make sure to use the official engine name
144     if (pEngine->GetInstrumentManager()) {
145     InstrumentManager::instrument_id_t id;
146     id.FileName = Entry.InstrumentFile;
147     id.Index = Entry.InstrumentIndex;
148 senoner 1481 if (Entry.LoadMode != DONTCARE) {
149 schoenebeck 947 if (bInBackground)
150     pEngine->GetInstrumentManager()->SetModeInBackground(id, static_cast<InstrumentManager::mode_t>(Entry.LoadMode));
151     else
152     pEngine->GetInstrumentManager()->SetMode(id, static_cast<InstrumentManager::mode_t>(Entry.LoadMode));
153     }
154     } else {
155     dmsg(1,("WARNING: no InstrumentManager for engine '%s'\n",Entry.EngineName.c_str()));
156     }
157     private_entry_t privateEntry;
158     privateEntry.EngineName = Entry.EngineName;
159     privateEntry.InstrumentFile = Entry.InstrumentFile;
160     privateEntry.InstrumentIndex = Entry.InstrumentIndex;
161 schoenebeck 958 privateEntry.Volume = Entry.Volume;
162 schoenebeck 947 privateEntry.Name = Entry.Name;
163 iliev 981
164     bool Replaced = false;
165     int InstrCount = 0;
166    
167 schoenebeck 973 midiMapsMutex.Lock();
168     std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map);
169     if (iterMap != midiMaps.end()) { // map found
170 iliev 981 Replaced = (iterMap->second.find(Index) != iterMap->second.end());
171 schoenebeck 973 iterMap->second[Index] = privateEntry;
172 iliev 981 InstrCount = iterMap->second.size();
173 schoenebeck 973 } else { // no such map
174     midiMapsMutex.Unlock();
175     EngineFactory::Destroy(pEngine);
176     throw Exception("There is no MIDI instrument map " + ToString(Map));
177     }
178     midiMapsMutex.Unlock();
179 schoenebeck 947 EngineFactory::Destroy(pEngine);
180 iliev 981
181     if (Replaced) {
182     int Bank = (int(Index.midi_bank_msb) << 7) & int(Index.midi_bank_lsb);
183 iliev 1130 fireMidiInstrumentInfoChanged(Map, Bank, Index.midi_prog);
184 iliev 981 } else {
185 iliev 1130 fireMidiInstrumentCountChanged(Map, InstrCount);
186 iliev 981 }
187 schoenebeck 947 }
188    
189 schoenebeck 973 void MidiInstrumentMapper::RemoveEntry(int Map, midi_prog_index_t Index) {
190 iliev 981 int InstrCount = -1;
191    
192 schoenebeck 973 midiMapsMutex.Lock();
193     std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map);
194     if (iterMap != midiMaps.end()) { // map found
195     iterMap->second.erase(Index); // remove entry
196 iliev 981 InstrCount = iterMap->second.size();
197 schoenebeck 973 }
198     midiMapsMutex.Unlock();
199 iliev 981
200     if (InstrCount != -1) {
201 iliev 1130 fireMidiInstrumentCountChanged(Map, InstrCount);
202 iliev 981 }
203 schoenebeck 947 }
204    
205 schoenebeck 973 void MidiInstrumentMapper::RemoveAllEntries(int Map) {
206 iliev 981 int InstrCount = -1;
207    
208 schoenebeck 973 midiMapsMutex.Lock();
209     std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map);
210     if (iterMap != midiMaps.end()) { // map found
211     iterMap->second.clear(); // clear that map
212 iliev 981 InstrCount = 0;
213 schoenebeck 973 }
214     midiMapsMutex.Unlock();
215 iliev 981
216     if (InstrCount != -1) {
217 iliev 1130 fireMidiInstrumentCountChanged(Map, InstrCount);
218 iliev 981 }
219 schoenebeck 947 }
220    
221 schoenebeck 973 std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> MidiInstrumentMapper::Entries(int Map) throw (Exception) {
222 schoenebeck 947 std::map<midi_prog_index_t,entry_t> result;
223 schoenebeck 973
224 schoenebeck 947 // copy the internal map first
225 schoenebeck 973 midiMapsMutex.Lock();
226     std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map);
227     if (iterMap == midiMaps.end()) { // no such map
228     midiMapsMutex.Unlock();
229     throw Exception("There is no MIDI instrument map " + ToString(Map));
230     }
231     for (std::map<midi_prog_index_t,private_entry_t>::iterator iterEntry = iterMap->second.begin();
232     iterEntry != iterMap->second.end(); iterEntry++)
233 schoenebeck 947 {
234     entry_t entry;
235 schoenebeck 973 entry.EngineName = iterEntry->second.EngineName;
236     entry.InstrumentFile = iterEntry->second.InstrumentFile;
237     entry.InstrumentIndex = iterEntry->second.InstrumentIndex;
238     entry.Volume = iterEntry->second.Volume;
239     entry.Name = iterEntry->second.Name;
240     result[iterEntry->first] = entry;
241 schoenebeck 947 }
242 schoenebeck 973 midiMapsMutex.Unlock();
243    
244 schoenebeck 958 // complete it with current LoadMode of each entry
245 schoenebeck 947 for (std::map<midi_prog_index_t,entry_t>::iterator iter = result.begin();
246     iter != result.end(); iter++)
247     {
248     entry_t& entry = iter->second;
249     Engine* pEngine = EngineFactory::Create(entry.EngineName);
250     if (!pEngine) { // invalid mapping
251 schoenebeck 973 RemoveEntry(Map, iter->first);
252 schoenebeck 947 result.erase(iter);
253     continue;
254     }
255     InstrumentManager* pManager = pEngine->GetInstrumentManager();
256     if (pManager) { // engine provides an InstrumentManager
257     InstrumentManager::instrument_id_t id;
258     id.FileName = entry.InstrumentFile;
259     id.Index = entry.InstrumentIndex;
260     entry.LoadMode = static_cast<mode_t>(pManager->GetMode(id));
261     } else { // engine does not provide an InstrumentManager
262 schoenebeck 958 // use default value
263 schoenebeck 947 entry.LoadMode = ON_DEMAND;
264     }
265     EngineFactory::Destroy(pEngine);
266     }
267     return result;
268     }
269    
270 schoenebeck 973 std::vector<int> MidiInstrumentMapper::Maps() {
271     std::vector<int> result;
272     midiMapsMutex.Lock();
273     for (std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.begin();
274     iterMap != midiMaps.end(); iterMap++)
275     {
276     result.push_back(iterMap->first);
277     }
278     midiMapsMutex.Unlock();
279     return result;
280     }
281    
282 iliev 1135 int MidiInstrumentMapper::GetMapCount() {
283     midiMapsMutex.Lock();
284     int i = midiMaps.size();
285     midiMapsMutex.Unlock();
286     return i;
287     }
288    
289 schoenebeck 973 int MidiInstrumentMapper::AddMap(String MapName) throw (Exception) {
290     int ID;
291     midiMapsMutex.Lock();
292     if (midiMaps.empty()) ID = 0;
293     else {
294     // get the highest existing map ID
295     uint lastIndex = (--(midiMaps.end()))->first;
296     // check if we reached the index limit
297     if (lastIndex + 1 < lastIndex) {
298     // search for an unoccupied map ID starting from 0
299     for (uint i = 0; i < lastIndex; i++) {
300     if (midiMaps.find(i) != midiMaps.end()) continue;
301     // we found an unused ID, so insert the new map there
302     ID = i;
303     goto __create_map;
304     }
305     throw Exception("Internal error: could not find unoccupied MIDI instrument map ID.");
306     }
307 iliev 976 ID = lastIndex + 1;
308 schoenebeck 973 }
309     __create_map:
310     midiMaps[ID].name = MapName;
311 iliev 1135
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 schoenebeck 973 midiMapsMutex.Unlock();
316 iliev 1135
317 schoenebeck 973 return ID;
318     }
319    
320     String MidiInstrumentMapper::MapName(int Map) throw (Exception) {
321     String result;
322     midiMapsMutex.Lock();
323     std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map);
324     if (iterMap == midiMaps.end()) {
325     midiMapsMutex.Unlock();
326     throw Exception("There is no MIDI instrument map " + ToString(Map));
327     }
328     result = iterMap->second.name;
329     midiMapsMutex.Unlock();
330     return result;
331     }
332    
333     void MidiInstrumentMapper::RenameMap(int Map, String NewName) throw (Exception) {
334     midiMapsMutex.Lock();
335     std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map);
336     if (iterMap == midiMaps.end()) {
337     midiMapsMutex.Unlock();
338     throw Exception("There is no MIDI instrument map " + ToString(Map));
339     }
340     iterMap->second.name = NewName;
341     midiMapsMutex.Unlock();
342 iliev 1130 fireMidiInstrumentMapInfoChanged(Map);
343 schoenebeck 973 }
344    
345     void MidiInstrumentMapper::RemoveMap(int Map) {
346     midiMapsMutex.Lock();
347     midiMaps.erase(Map);
348 iliev 1135 if(Map == GetDefaultMap()) {
349     SetDefaultMap(midiMaps.empty() ? -1 : (*(midiMaps.begin())).first);
350     }
351     fireMidiInstrumentMapCountChanged(Maps().size());
352 schoenebeck 973 midiMapsMutex.Unlock();
353     }
354    
355     void MidiInstrumentMapper::RemoveAllMaps() {
356     midiMapsMutex.Lock();
357     midiMaps.clear();
358 iliev 1135 SetDefaultMap(-1);
359     fireMidiInstrumentMapCountChanged(Maps().size());
360 schoenebeck 973 midiMapsMutex.Unlock();
361     }
362    
363 iliev 1135 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();
374    
375     if (MapId != -1) fireMidiInstrumentMapInfoChanged(MapId);
376     }
377    
378 schoenebeck 973 optional<MidiInstrumentMapper::entry_t> MidiInstrumentMapper::GetEntry(int Map, midi_prog_index_t Index) {
379 schoenebeck 947 optional<entry_t> result;
380 schoenebeck 973 midiMapsMutex.Lock();
381     std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map);
382     if (iterMap != midiMaps.end()) { // map found
383     std::map<midi_prog_index_t,private_entry_t>::iterator iterEntry = iterMap->second.find(Index);
384     if (iterEntry != iterMap->second.end()) {
385     entry_t entry;
386     entry.EngineName = iterEntry->second.EngineName;
387     entry.InstrumentFile = iterEntry->second.InstrumentFile;
388     entry.InstrumentIndex = iterEntry->second.InstrumentIndex;
389     entry.Volume = iterEntry->second.Volume;
390     //TODO: for now we skip the LoadMode and Name entry here, since we don't need it in the MidiInputPort
391     result = entry;
392     }
393 schoenebeck 947 }
394 schoenebeck 973 midiMapsMutex.Unlock();
395 schoenebeck 947 return result;
396     }
397    
398     } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC