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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 958 by schoenebeck, Wed Nov 29 19:48:38 2006 UTC revision 1481 by senoner, Wed Nov 14 23:42:15 2007 UTC
# Line 1  Line 1 
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  *
# Line 20  Line 20 
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"
# Line 35  namespace LinuxSampler { Line 36  namespace LinuxSampler {
36          String Name;          String Name;
37      };      };
38    
39      // here we store all mappings (MIDI bank&prog) -> (Engine,File,Index)      // internal map type (MIDI bank&prog) -> (Engine,File,Index)
40      std::map<midi_prog_index_t,private_entry_t> midiMap;      class MidiInstrumentMap : public std::map<midi_prog_index_t,private_entry_t> {
41            public:
42                String name;
43        };
44    
45        // here we store all maps
46        std::map<int,MidiInstrumentMap> midiMaps;
47    
48        // for synchronization of midiMaps
49        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      // for synchronization of midiMap      void MidiInstrumentMapper::RemoveMidiInstrumentCountListener(MidiInstrumentCountListener* l) {
62      Mutex midiMapMutex;          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::AddOrReplaceMapping(midi_prog_index_t Index, entry_t Entry, bool bInBackground) throw (Exception) {      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) {
114          if (bInBackground) {          if (bInBackground) {
115              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",
116                    Map,
117                  Index.midi_bank_msb,Index.midi_bank_lsb,Index.midi_prog,                  Index.midi_bank_msb,Index.midi_bank_lsb,Index.midi_prog,
118                  Entry.EngineName.c_str(),Entry.InstrumentFile.c_str(),Entry.InstrumentIndex,                  Entry.EngineName.c_str(),Entry.InstrumentFile.c_str(),Entry.InstrumentIndex,
119                  Entry.Volume,Entry.LoadMode)                  Entry.Volume,Entry.LoadMode)
120              );              );
121          } else {          } else {
122              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",
123                    Map,
124                  Index.midi_bank_msb,Index.midi_bank_lsb,Index.midi_prog,                  Index.midi_bank_msb,Index.midi_bank_lsb,Index.midi_prog,
125                  Entry.EngineName.c_str(),Entry.InstrumentFile.c_str(),Entry.InstrumentIndex,                  Entry.EngineName.c_str(),Entry.InstrumentFile.c_str(),Entry.InstrumentIndex,
126                  Entry.Volume,Entry.LoadMode)                  Entry.Volume,Entry.LoadMode)
127              );              );
128          }          }
129            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            if (!Entry.InstrumentFile.size())
136                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);
# Line 66  namespace LinuxSampler { Line 145  namespace LinuxSampler {
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
# Line 81  namespace LinuxSampler { Line 160  namespace LinuxSampler {
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          midiMapMutex.Lock();  
164          midiMap[Index] = privateEntry;          bool Replaced = false;
165          midiMapMutex.Unlock();          int InstrCount = 0;
166    
167            midiMapsMutex.Lock();
168            std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map);
169            if (iterMap != midiMaps.end()) { // map found
170                Replaced = (iterMap->second.find(Index) != iterMap->second.end());
171                iterMap->second[Index] = privateEntry;
172                InstrCount = iterMap->second.size();
173            } 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          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::RemoveMapping(midi_prog_index_t Index) {      void MidiInstrumentMapper::RemoveEntry(int Map, midi_prog_index_t Index) {
190          midiMapMutex.Lock();          int InstrCount = -1;
191          midiMap.erase(Index);  
192          midiMapMutex.Unlock();          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                InstrCount = iterMap->second.size();
197            }
198            midiMapsMutex.Unlock();
199            
200            if (InstrCount != -1) {
201                fireMidiInstrumentCountChanged(Map, InstrCount);
202            }
203      }      }
204    
205      void MidiInstrumentMapper::RemoveAllMappings() {      void MidiInstrumentMapper::RemoveAllEntries(int Map) {
206          midiMapMutex.Lock();          int InstrCount = -1;
207          midiMap.clear();  
208          midiMapMutex.Unlock();          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                InstrCount = 0;
213            }
214            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::Mappings() {      std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> MidiInstrumentMapper::Entries(int Map) throw (Exception) {
222          std::map<midi_prog_index_t,entry_t> result;          std::map<midi_prog_index_t,entry_t> result;
223    
224          // copy the internal map first          // copy the internal map first
225          midiMapMutex.Lock();          midiMapsMutex.Lock();
226          for (std::map<midi_prog_index_t,private_entry_t>::iterator iter = midiMap.begin();          std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map);
227               iter != midiMap.end(); iter++)          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          {          {
234              entry_t entry;              entry_t entry;
235              entry.EngineName      = iter->second.EngineName;              entry.EngineName      = iterEntry->second.EngineName;
236              entry.InstrumentFile  = iter->second.InstrumentFile;              entry.InstrumentFile  = iterEntry->second.InstrumentFile;
237              entry.InstrumentIndex = iter->second.InstrumentIndex;              entry.InstrumentIndex = iterEntry->second.InstrumentIndex;
238              entry.Volume          = iter->second.Volume;              entry.Volume          = iterEntry->second.Volume;
239              entry.Name            = iter->second.Name;              entry.Name            = iterEntry->second.Name;
240              result[iter->first] = entry;              result[iterEntry->first] = entry;
241          }          }
242          midiMapMutex.Unlock();          midiMapsMutex.Unlock();
243    
244          // complete it with current LoadMode of each entry          // complete it with current LoadMode of each entry
245          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();
246               iter != result.end(); iter++)               iter != result.end(); iter++)
# Line 122  namespace LinuxSampler { Line 248  namespace LinuxSampler {
248              entry_t& entry = iter->second;              entry_t& entry = iter->second;
249              Engine* pEngine = EngineFactory::Create(entry.EngineName);              Engine* pEngine = EngineFactory::Create(entry.EngineName);
250              if (!pEngine) { // invalid mapping              if (!pEngine) { // invalid mapping
251                  RemoveMapping(iter->first);                  RemoveEntry(Map, iter->first);
252                  result.erase(iter);                  result.erase(iter);
253                  continue;                  continue;
254              }              }
# Line 141  namespace LinuxSampler { Line 267  namespace LinuxSampler {
267          return result;          return result;
268      }      }
269    
270      optional<MidiInstrumentMapper::entry_t> MidiInstrumentMapper::GetEntry(midi_prog_index_t Index) {      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        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) {
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                ID = lastIndex + 1;
308            }
309            __create_map:
310            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();
316            
317            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            fireMidiInstrumentMapInfoChanged(Map);
343        }
344    
345        void MidiInstrumentMapper::RemoveMap(int Map) {
346            midiMapsMutex.Lock();
347            midiMaps.erase(Map);
348            if(Map == GetDefaultMap()) {
349                SetDefaultMap(midiMaps.empty() ? -1 : (*(midiMaps.begin())).first);
350            }
351            fireMidiInstrumentMapCountChanged(Maps().size());
352            midiMapsMutex.Unlock();
353        }
354    
355        void MidiInstrumentMapper::RemoveAllMaps() {
356            midiMapsMutex.Lock();
357            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();
374            
375            if (MapId != -1) fireMidiInstrumentMapInfoChanged(MapId);
376        }
377    
378        optional<MidiInstrumentMapper::entry_t> MidiInstrumentMapper::GetEntry(int Map, midi_prog_index_t Index) {
379          optional<entry_t> result;          optional<entry_t> result;
380          midiMapMutex.Lock();          midiMapsMutex.Lock();
381          std::map<midi_prog_index_t,private_entry_t>::iterator iter = midiMap.find(Index);          std::map<int,MidiInstrumentMap>::iterator iterMap = midiMaps.find(Map);
382          if (iter != midiMap.end()) {          if (iterMap != midiMaps.end()) { // map found
383              entry_t entry;              std::map<midi_prog_index_t,private_entry_t>::iterator iterEntry = iterMap->second.find(Index);
384              entry.EngineName      = iter->second.EngineName;              if (iterEntry != iterMap->second.end()) {
385              entry.InstrumentFile  = iter->second.InstrumentFile;                  entry_t entry;
386              entry.InstrumentIndex = iter->second.InstrumentIndex;                  entry.EngineName      = iterEntry->second.EngineName;
387              entry.Volume          = iter->second.Volume;                  entry.InstrumentFile  = iterEntry->second.InstrumentFile;
388              //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;
389              result = entry;                  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          }          }
394          midiMapMutex.Unlock();          midiMapsMutex.Unlock();
395          return result;          return result;
396      }      }
397    

Legend:
Removed from v.958  
changed lines
  Added in v.1481

  ViewVC Help
Powered by ViewVC