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.Volume < 0.0) |
60 |
throw Exception("Volume may not be a negative value"); |
61 |
Engine* pEngine = EngineFactory::Create(Entry.EngineName); |
62 |
if (!pEngine) |
63 |
throw Exception("No such engine type '"+Entry.EngineName+"'"); |
64 |
Entry.EngineName = pEngine->EngineName(); // make sure to use the official engine name |
65 |
if (pEngine->GetInstrumentManager()) { |
66 |
InstrumentManager::instrument_id_t id; |
67 |
id.FileName = Entry.InstrumentFile; |
68 |
id.Index = Entry.InstrumentIndex; |
69 |
if (Entry.LoadMode != VOID) { |
70 |
if (bInBackground) |
71 |
pEngine->GetInstrumentManager()->SetModeInBackground(id, static_cast<InstrumentManager::mode_t>(Entry.LoadMode)); |
72 |
else |
73 |
pEngine->GetInstrumentManager()->SetMode(id, static_cast<InstrumentManager::mode_t>(Entry.LoadMode)); |
74 |
} |
75 |
} else { |
76 |
dmsg(1,("WARNING: no InstrumentManager for engine '%s'\n",Entry.EngineName.c_str())); |
77 |
} |
78 |
private_entry_t privateEntry; |
79 |
privateEntry.EngineName = Entry.EngineName; |
80 |
privateEntry.InstrumentFile = Entry.InstrumentFile; |
81 |
privateEntry.InstrumentIndex = Entry.InstrumentIndex; |
82 |
privateEntry.Volume = Entry.Volume; |
83 |
privateEntry.Name = Entry.Name; |
84 |
midiMapMutex.Lock(); |
85 |
midiMap[Index] = privateEntry; |
86 |
midiMapMutex.Unlock(); |
87 |
EngineFactory::Destroy(pEngine); |
88 |
} |
89 |
|
90 |
void MidiInstrumentMapper::RemoveMapping(midi_prog_index_t Index) { |
91 |
midiMapMutex.Lock(); |
92 |
midiMap.erase(Index); |
93 |
midiMapMutex.Unlock(); |
94 |
} |
95 |
|
96 |
void MidiInstrumentMapper::RemoveAllMappings() { |
97 |
midiMapMutex.Lock(); |
98 |
midiMap.clear(); |
99 |
midiMapMutex.Unlock(); |
100 |
} |
101 |
|
102 |
std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> MidiInstrumentMapper::Mappings() { |
103 |
std::map<midi_prog_index_t,entry_t> result; |
104 |
// copy the internal map first |
105 |
midiMapMutex.Lock(); |
106 |
for (std::map<midi_prog_index_t,private_entry_t>::iterator iter = midiMap.begin(); |
107 |
iter != midiMap.end(); iter++) |
108 |
{ |
109 |
entry_t entry; |
110 |
entry.EngineName = iter->second.EngineName; |
111 |
entry.InstrumentFile = iter->second.InstrumentFile; |
112 |
entry.InstrumentIndex = iter->second.InstrumentIndex; |
113 |
entry.Volume = iter->second.Volume; |
114 |
entry.Name = iter->second.Name; |
115 |
result[iter->first] = entry; |
116 |
} |
117 |
midiMapMutex.Unlock(); |
118 |
// complete it with current LoadMode of each entry |
119 |
for (std::map<midi_prog_index_t,entry_t>::iterator iter = result.begin(); |
120 |
iter != result.end(); iter++) |
121 |
{ |
122 |
entry_t& entry = iter->second; |
123 |
Engine* pEngine = EngineFactory::Create(entry.EngineName); |
124 |
if (!pEngine) { // invalid mapping |
125 |
RemoveMapping(iter->first); |
126 |
result.erase(iter); |
127 |
continue; |
128 |
} |
129 |
InstrumentManager* pManager = pEngine->GetInstrumentManager(); |
130 |
if (pManager) { // engine provides an InstrumentManager |
131 |
InstrumentManager::instrument_id_t id; |
132 |
id.FileName = entry.InstrumentFile; |
133 |
id.Index = entry.InstrumentIndex; |
134 |
entry.LoadMode = static_cast<mode_t>(pManager->GetMode(id)); |
135 |
} else { // engine does not provide an InstrumentManager |
136 |
// use default value |
137 |
entry.LoadMode = ON_DEMAND; |
138 |
} |
139 |
EngineFactory::Destroy(pEngine); |
140 |
} |
141 |
return result; |
142 |
} |
143 |
|
144 |
optional<MidiInstrumentMapper::entry_t> MidiInstrumentMapper::GetEntry(midi_prog_index_t Index) { |
145 |
optional<entry_t> result; |
146 |
midiMapMutex.Lock(); |
147 |
std::map<midi_prog_index_t,private_entry_t>::iterator iter = midiMap.find(Index); |
148 |
if (iter != midiMap.end()) { |
149 |
entry_t entry; |
150 |
entry.EngineName = iter->second.EngineName; |
151 |
entry.InstrumentFile = iter->second.InstrumentFile; |
152 |
entry.InstrumentIndex = iter->second.InstrumentIndex; |
153 |
entry.Volume = iter->second.Volume; |
154 |
//TODO: for now we skip the LoadMode and Name entry here, since we don't need it in the MidiInputPort |
155 |
result = entry; |
156 |
} |
157 |
midiMapMutex.Unlock(); |
158 |
return result; |
159 |
} |
160 |
|
161 |
} // namespace LinuxSampler |