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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 970 - (show annotations) (download)
Wed Dec 6 22:28:17 2006 UTC (17 years, 4 months ago) by schoenebeck
File size: 7549 byte(s)
* fixed crash occuring in conjunction with the new 'MAP MIDI_INSTRUMENT'
  LSCP command (cause: RingBuffer was not able to do deep copies)

1 /***************************************************************************
2 * *
3 * Copyright (C) 2006 Christian Schoenebeck *
4 * *
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 #include "../../common/Mutex.h"
24 #include "../../engines/EngineFactory.h"
25 #include "../../engines/Engine.h"
26
27 namespace LinuxSampler {
28
29 // same as entry_t but without 'LoadMode'
30 struct private_entry_t {
31 String EngineName;
32 String InstrumentFile;
33 uint InstrumentIndex;
34 float Volume;
35 String Name;
36 };
37
38 // here we store all mappings (MIDI bank&prog) -> (Engine,File,Index)
39 std::map<midi_prog_index_t,private_entry_t> midiMap;
40
41 // for synchronization of midiMap
42 Mutex midiMapMutex;
43
44
45 void MidiInstrumentMapper::AddOrReplaceMapping(midi_prog_index_t Index, entry_t Entry, bool bInBackground) throw (Exception) {
46 if (bInBackground) {
47 dmsg(3,("MidiInstrumentMapper: updating mapping (%d,%d,%d) -> ('%s','%s',%d) with vol=%f, mode=%d in background\n",
48 Index.midi_bank_msb,Index.midi_bank_lsb,Index.midi_prog,
49 Entry.EngineName.c_str(),Entry.InstrumentFile.c_str(),Entry.InstrumentIndex,
50 Entry.Volume,Entry.LoadMode)
51 );
52 } else {
53 dmsg(3,("MidiInstrumentMapper: updating mapping (%d,%d,%d) -> ('%s','%s',%d) with vol=%f, mode=%d\n",
54 Index.midi_bank_msb,Index.midi_bank_lsb,Index.midi_prog,
55 Entry.EngineName.c_str(),Entry.InstrumentFile.c_str(),Entry.InstrumentIndex,
56 Entry.Volume,Entry.LoadMode)
57 );
58 }
59 if (!Entry.InstrumentFile.size())
60 throw Exception("No instrument file name given");
61 if (Entry.Volume < 0.0)
62 throw Exception("Volume may not be a negative value");
63 Engine* pEngine = EngineFactory::Create(Entry.EngineName);
64 if (!pEngine)
65 throw Exception("No such engine type '"+Entry.EngineName+"'");
66 Entry.EngineName = pEngine->EngineName(); // make sure to use the official engine name
67 if (pEngine->GetInstrumentManager()) {
68 InstrumentManager::instrument_id_t id;
69 id.FileName = Entry.InstrumentFile;
70 id.Index = Entry.InstrumentIndex;
71 if (Entry.LoadMode != VOID) {
72 if (bInBackground)
73 pEngine->GetInstrumentManager()->SetModeInBackground(id, static_cast<InstrumentManager::mode_t>(Entry.LoadMode));
74 else
75 pEngine->GetInstrumentManager()->SetMode(id, static_cast<InstrumentManager::mode_t>(Entry.LoadMode));
76 }
77 } else {
78 dmsg(1,("WARNING: no InstrumentManager for engine '%s'\n",Entry.EngineName.c_str()));
79 }
80 private_entry_t privateEntry;
81 privateEntry.EngineName = Entry.EngineName;
82 privateEntry.InstrumentFile = Entry.InstrumentFile;
83 privateEntry.InstrumentIndex = Entry.InstrumentIndex;
84 privateEntry.Volume = Entry.Volume;
85 privateEntry.Name = Entry.Name;
86 midiMapMutex.Lock();
87 midiMap[Index] = privateEntry;
88 midiMapMutex.Unlock();
89 EngineFactory::Destroy(pEngine);
90 }
91
92 void MidiInstrumentMapper::RemoveMapping(midi_prog_index_t Index) {
93 midiMapMutex.Lock();
94 midiMap.erase(Index);
95 midiMapMutex.Unlock();
96 }
97
98 void MidiInstrumentMapper::RemoveAllMappings() {
99 midiMapMutex.Lock();
100 midiMap.clear();
101 midiMapMutex.Unlock();
102 }
103
104 std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> MidiInstrumentMapper::Mappings() {
105 std::map<midi_prog_index_t,entry_t> result;
106 // copy the internal map first
107 midiMapMutex.Lock();
108 for (std::map<midi_prog_index_t,private_entry_t>::iterator iter = midiMap.begin();
109 iter != midiMap.end(); iter++)
110 {
111 entry_t entry;
112 entry.EngineName = iter->second.EngineName;
113 entry.InstrumentFile = iter->second.InstrumentFile;
114 entry.InstrumentIndex = iter->second.InstrumentIndex;
115 entry.Volume = iter->second.Volume;
116 entry.Name = iter->second.Name;
117 result[iter->first] = entry;
118 }
119 midiMapMutex.Unlock();
120 // complete it with current LoadMode of each entry
121 for (std::map<midi_prog_index_t,entry_t>::iterator iter = result.begin();
122 iter != result.end(); iter++)
123 {
124 entry_t& entry = iter->second;
125 Engine* pEngine = EngineFactory::Create(entry.EngineName);
126 if (!pEngine) { // invalid mapping
127 RemoveMapping(iter->first);
128 result.erase(iter);
129 continue;
130 }
131 InstrumentManager* pManager = pEngine->GetInstrumentManager();
132 if (pManager) { // engine provides an InstrumentManager
133 InstrumentManager::instrument_id_t id;
134 id.FileName = entry.InstrumentFile;
135 id.Index = entry.InstrumentIndex;
136 entry.LoadMode = static_cast<mode_t>(pManager->GetMode(id));
137 } else { // engine does not provide an InstrumentManager
138 // use default value
139 entry.LoadMode = ON_DEMAND;
140 }
141 EngineFactory::Destroy(pEngine);
142 }
143 return result;
144 }
145
146 optional<MidiInstrumentMapper::entry_t> MidiInstrumentMapper::GetEntry(midi_prog_index_t Index) {
147 optional<entry_t> result;
148 midiMapMutex.Lock();
149 std::map<midi_prog_index_t,private_entry_t>::iterator iter = midiMap.find(Index);
150 if (iter != midiMap.end()) {
151 entry_t entry;
152 entry.EngineName = iter->second.EngineName;
153 entry.InstrumentFile = iter->second.InstrumentFile;
154 entry.InstrumentIndex = iter->second.InstrumentIndex;
155 entry.Volume = iter->second.Volume;
156 //TODO: for now we skip the LoadMode and Name entry here, since we don't need it in the MidiInputPort
157 result = entry;
158 }
159 midiMapMutex.Unlock();
160 return result;
161 }
162
163 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC