1 |
schoenebeck |
53 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
|
|
* LinuxSampler - modular, streaming capable sampler * |
4 |
|
|
* * |
5 |
schoenebeck |
61 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
schoenebeck |
1686 |
* Copyright (C) 2005 - 2008 Christian Schoenebeck * |
7 |
schoenebeck |
53 |
* * |
8 |
schoenebeck |
1008 |
* This library is free software; you can redistribute it and/or modify * |
9 |
schoenebeck |
53 |
* it under the terms of the GNU General Public License as published by * |
10 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
11 |
|
|
* (at your option) any later version. * |
12 |
|
|
* * |
13 |
schoenebeck |
1008 |
* This library is distributed in the hope that it will be useful, * |
14 |
schoenebeck |
53 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
|
|
* GNU General Public License for more details. * |
17 |
|
|
* * |
18 |
|
|
* You should have received a copy of the GNU General Public License * |
19 |
schoenebeck |
1008 |
* along with this library; if not, write to the Free Software * |
20 |
schoenebeck |
53 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
|
|
* MA 02111-1307 USA * |
22 |
|
|
***************************************************************************/ |
23 |
|
|
|
24 |
schoenebeck |
123 |
#include <sstream> |
25 |
|
|
|
26 |
schoenebeck |
53 |
#include "Sampler.h" |
27 |
|
|
|
28 |
schoenebeck |
1424 |
#include "common/global_private.h" |
29 |
iliev |
778 |
#include "engines/EngineFactory.h" |
30 |
schoenebeck |
411 |
#include "engines/EngineChannelFactory.h" |
31 |
schoenebeck |
1375 |
#include "plugins/InstrumentEditorFactory.h" |
32 |
schoenebeck |
203 |
#include "drivers/audio/AudioOutputDeviceFactory.h" |
33 |
|
|
#include "drivers/midi/MidiInputDeviceFactory.h" |
34 |
schoenebeck |
1008 |
#include "drivers/midi/MidiInstrumentMapper.h" |
35 |
schoenebeck |
53 |
|
36 |
|
|
namespace LinuxSampler { |
37 |
|
|
|
38 |
|
|
// ****************************************************************** |
39 |
|
|
// * SamplerChannel |
40 |
|
|
|
41 |
|
|
SamplerChannel::SamplerChannel(Sampler* pS) { |
42 |
|
|
pSampler = pS; |
43 |
schoenebeck |
411 |
pEngineChannel = NULL; |
44 |
schoenebeck |
675 |
pAudioOutputDevice = NULL; |
45 |
capela |
159 |
pMidiInputDevice = NULL; |
46 |
schoenebeck |
675 |
iMidiPort = 0; |
47 |
|
|
midiChannel = midi_chan_all; |
48 |
schoenebeck |
53 |
iIndex = -1; |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
SamplerChannel::~SamplerChannel() { |
52 |
schoenebeck |
411 |
if (pEngineChannel) { |
53 |
persson |
840 |
Engine* engine = pEngineChannel->GetEngine(); |
54 |
|
|
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine); |
55 |
|
|
|
56 |
schoenebeck |
675 |
MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : __GetMidiInputDevicePort(GetMidiInputChannel()); |
57 |
schoenebeck |
411 |
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel); |
58 |
schoenebeck |
420 |
if (pEngineChannel) { |
59 |
|
|
if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice(); |
60 |
schoenebeck |
660 |
EngineChannelFactory::Destroy(pEngineChannel); |
61 |
persson |
840 |
|
62 |
|
|
// reconnect engine if it still exists |
63 |
|
|
const std::set<Engine*>& engines = EngineFactory::EngineInstances(); |
64 |
|
|
if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine); |
65 |
schoenebeck |
420 |
} |
66 |
schoenebeck |
53 |
} |
67 |
|
|
} |
68 |
|
|
|
69 |
schoenebeck |
880 |
void SamplerChannel::SetEngineType(String EngineType) throw (Exception) { |
70 |
schoenebeck |
411 |
dmsg(2,("SamplerChannel: Assigning engine type...")); |
71 |
iliev |
1283 |
|
72 |
|
|
if (pEngineChannel) { |
73 |
|
|
if (!strcasecmp(pEngineChannel->EngineName().c_str(), EngineType.c_str())) { |
74 |
|
|
dmsg(2,("OK\n")); |
75 |
|
|
return; |
76 |
|
|
} |
77 |
|
|
} |
78 |
schoenebeck |
53 |
|
79 |
schoenebeck |
1686 |
fireEngineToBeChanged(); |
80 |
|
|
|
81 |
schoenebeck |
411 |
// create new engine channel |
82 |
|
|
EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType); |
83 |
schoenebeck |
880 |
if (!pNewEngineChannel) throw Exception("Unknown engine type"); |
84 |
schoenebeck |
53 |
|
85 |
schoenebeck |
660 |
//FIXME: hack to allow fast retrieval of engine channel's sampler channel index |
86 |
|
|
pNewEngineChannel->iSamplerChannelIndex = Index(); |
87 |
|
|
|
88 |
capela |
159 |
// dereference midi input port. |
89 |
schoenebeck |
675 |
MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort()); |
90 |
|
|
// disconnect old engine channel |
91 |
schoenebeck |
411 |
if (pEngineChannel) { |
92 |
persson |
840 |
Engine* engine = pEngineChannel->GetEngine(); |
93 |
|
|
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine); |
94 |
|
|
|
95 |
schoenebeck |
411 |
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel); |
96 |
schoenebeck |
412 |
if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice(); |
97 |
schoenebeck |
660 |
EngineChannelFactory::Destroy(pEngineChannel); |
98 |
persson |
840 |
|
99 |
|
|
// reconnect engine if it still exists |
100 |
|
|
const std::set<Engine*>& engines = EngineFactory::EngineInstances(); |
101 |
|
|
if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine); |
102 |
schoenebeck |
53 |
} |
103 |
|
|
|
104 |
schoenebeck |
411 |
// connect new engine channel |
105 |
schoenebeck |
412 |
if (pAudioOutputDevice) { |
106 |
|
|
pNewEngineChannel->Connect(pAudioOutputDevice); |
107 |
|
|
pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine()); |
108 |
|
|
} |
109 |
persson |
846 |
if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, GetMidiInputChannel()); |
110 |
schoenebeck |
675 |
pEngineChannel = pNewEngineChannel; |
111 |
|
|
|
112 |
|
|
// from now on get MIDI device and port from EngineChannel object |
113 |
|
|
this->pMidiInputDevice = NULL; |
114 |
|
|
this->iMidiPort = 0; |
115 |
|
|
|
116 |
iliev |
730 |
pEngineChannel->StatusChanged(true); |
117 |
iliev |
1130 |
fireEngineChanged(); |
118 |
schoenebeck |
64 |
dmsg(2,("OK\n")); |
119 |
schoenebeck |
53 |
} |
120 |
|
|
|
121 |
schoenebeck |
123 |
void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) { |
122 |
iliev |
960 |
if(pAudioOutputDevice == pDevice) return; |
123 |
|
|
|
124 |
schoenebeck |
53 |
// disconnect old device |
125 |
persson |
840 |
if (pAudioOutputDevice && pEngineChannel) { |
126 |
|
|
Engine* engine = pEngineChannel->GetEngine(); |
127 |
|
|
pAudioOutputDevice->Disconnect(engine); |
128 |
|
|
|
129 |
|
|
pEngineChannel->DisconnectAudioOutputDevice(); |
130 |
|
|
|
131 |
|
|
// reconnect engine if it still exists |
132 |
|
|
const std::set<Engine*>& engines = EngineFactory::EngineInstances(); |
133 |
|
|
if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine); |
134 |
|
|
} |
135 |
|
|
|
136 |
schoenebeck |
53 |
// connect new device |
137 |
|
|
pAudioOutputDevice = pDevice; |
138 |
schoenebeck |
412 |
if (pEngineChannel) { |
139 |
|
|
pEngineChannel->Connect(pAudioOutputDevice); |
140 |
|
|
pAudioOutputDevice->Connect(pEngineChannel->GetEngine()); |
141 |
|
|
} |
142 |
schoenebeck |
53 |
} |
143 |
|
|
|
144 |
capela |
159 |
void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) { |
145 |
schoenebeck |
675 |
SetMidiInput(pDevice, 0, GetMidiInputChannel()); |
146 |
capela |
159 |
} |
147 |
schoenebeck |
203 |
|
148 |
capela |
159 |
void SamplerChannel::SetMidiInputPort(int MidiPort) { |
149 |
schoenebeck |
675 |
SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel()); |
150 |
capela |
159 |
} |
151 |
schoenebeck |
203 |
|
152 |
schoenebeck |
675 |
void SamplerChannel::SetMidiInputChannel(midi_chan_t MidiChannel) { |
153 |
|
|
SetMidiInput(GetMidiInputDevice(), GetMidiInputPort(), MidiChannel); |
154 |
capela |
159 |
} |
155 |
schoenebeck |
203 |
|
156 |
schoenebeck |
675 |
void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) { |
157 |
schoenebeck |
880 |
if (!pDevice) throw Exception("No MIDI input device assigned."); |
158 |
schoenebeck |
675 |
|
159 |
|
|
// get old and new midi input port |
160 |
|
|
MidiInputPort* pOldMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort()); |
161 |
|
|
MidiInputPort* pNewMidiInputPort = pDevice->GetPort(iMidiPort); |
162 |
|
|
|
163 |
capela |
159 |
// disconnect old device port |
164 |
schoenebeck |
675 |
if (pOldMidiInputPort && pEngineChannel) pOldMidiInputPort->Disconnect(pEngineChannel); |
165 |
|
|
// remember new device, port and channel if not engine channel yet created |
166 |
|
|
if (!pEngineChannel) { |
167 |
|
|
this->pMidiInputDevice = pDevice; |
168 |
|
|
this->iMidiPort = iMidiPort; |
169 |
|
|
this->midiChannel = MidiChannel; |
170 |
|
|
} |
171 |
|
|
|
172 |
capela |
159 |
// connect new device port |
173 |
schoenebeck |
675 |
if (pNewMidiInputPort && pEngineChannel) pNewMidiInputPort->Connect(pEngineChannel, MidiChannel); |
174 |
capela |
159 |
// Ooops. |
175 |
schoenebeck |
675 |
if (pNewMidiInputPort == NULL) |
176 |
schoenebeck |
880 |
throw Exception("There is no MIDI input port with index " + ToString(iMidiPort) + "."); |
177 |
schoenebeck |
53 |
} |
178 |
|
|
|
179 |
schoenebeck |
411 |
EngineChannel* SamplerChannel::GetEngineChannel() { |
180 |
|
|
return pEngineChannel; |
181 |
schoenebeck |
53 |
} |
182 |
|
|
|
183 |
schoenebeck |
675 |
midi_chan_t SamplerChannel::GetMidiInputChannel() { |
184 |
|
|
if (pEngineChannel) this->midiChannel = pEngineChannel->MidiChannel(); |
185 |
capela |
159 |
return this->midiChannel; |
186 |
schoenebeck |
53 |
} |
187 |
|
|
|
188 |
capela |
159 |
int SamplerChannel::GetMidiInputPort() { |
189 |
schoenebeck |
675 |
MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : NULL; |
190 |
|
|
if (pMidiInputPort) this->iMidiPort = (int) pMidiInputPort->GetPortNumber(); |
191 |
|
|
return iMidiPort; |
192 |
capela |
159 |
} |
193 |
|
|
|
194 |
schoenebeck |
53 |
AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() { |
195 |
|
|
return pAudioOutputDevice; |
196 |
|
|
} |
197 |
|
|
|
198 |
senkov |
155 |
MidiInputDevice* SamplerChannel::GetMidiInputDevice() { |
199 |
schoenebeck |
675 |
if (pEngineChannel) |
200 |
|
|
pMidiInputDevice = (pEngineChannel->GetMidiInputPort()) ? pEngineChannel->GetMidiInputPort()->GetDevice() : NULL; |
201 |
capela |
159 |
return pMidiInputDevice; |
202 |
senkov |
155 |
} |
203 |
|
|
|
204 |
schoenebeck |
53 |
uint SamplerChannel::Index() { |
205 |
|
|
if (iIndex >= 0) return iIndex; |
206 |
|
|
|
207 |
schoenebeck |
209 |
Sampler::SamplerChannelMap::iterator iter = pSampler->mSamplerChannels.begin(); |
208 |
|
|
for (; iter != pSampler->mSamplerChannels.end(); iter++) { |
209 |
|
|
if (iter->second == this) { |
210 |
|
|
iIndex = iter->first; |
211 |
|
|
return iIndex; |
212 |
schoenebeck |
53 |
} |
213 |
|
|
} |
214 |
|
|
|
215 |
schoenebeck |
880 |
throw Exception("Internal error: SamplerChannel index not found"); |
216 |
schoenebeck |
53 |
} |
217 |
|
|
|
218 |
iliev |
1130 |
void SamplerChannel::AddEngineChangeListener(EngineChangeListener* l) { |
219 |
|
|
llEngineChangeListeners.AddListener(l); |
220 |
|
|
} |
221 |
|
|
|
222 |
|
|
void SamplerChannel::RemoveEngineChangeListener(EngineChangeListener* l) { |
223 |
|
|
llEngineChangeListeners.RemoveListener(l); |
224 |
|
|
} |
225 |
|
|
|
226 |
|
|
void SamplerChannel::RemoveAllEngineChangeListeners() { |
227 |
|
|
llEngineChangeListeners.RemoveAllListeners(); |
228 |
|
|
} |
229 |
|
|
|
230 |
schoenebeck |
1686 |
void SamplerChannel::fireEngineToBeChanged() { |
231 |
|
|
for (int i = 0; i < llEngineChangeListeners.GetListenerCount(); i++) { |
232 |
|
|
llEngineChangeListeners.GetListener(i)->EngineToBeChanged(Index()); |
233 |
|
|
} |
234 |
|
|
} |
235 |
|
|
|
236 |
iliev |
1130 |
void SamplerChannel::fireEngineChanged() { |
237 |
|
|
for (int i = 0; i < llEngineChangeListeners.GetListenerCount(); i++) { |
238 |
|
|
llEngineChangeListeners.GetListener(i)->EngineChanged(Index()); |
239 |
|
|
} |
240 |
|
|
} |
241 |
|
|
|
242 |
schoenebeck |
675 |
MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) { |
243 |
schoenebeck |
221 |
MidiInputPort* pMidiInputPort = NULL; |
244 |
schoenebeck |
675 |
MidiInputDevice* pMidiInputDevice = GetMidiInputDevice(); |
245 |
capela |
159 |
if (pMidiInputDevice) |
246 |
schoenebeck |
221 |
pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort); |
247 |
capela |
159 |
return pMidiInputPort; |
248 |
|
|
} |
249 |
schoenebeck |
53 |
|
250 |
schoenebeck |
212 |
|
251 |
|
|
|
252 |
schoenebeck |
53 |
// ****************************************************************** |
253 |
|
|
// * Sampler |
254 |
|
|
|
255 |
|
|
Sampler::Sampler() { |
256 |
iliev |
1130 |
eventHandler.SetSampler(this); |
257 |
schoenebeck |
53 |
} |
258 |
|
|
|
259 |
|
|
Sampler::~Sampler() { |
260 |
schoenebeck |
212 |
Reset(); |
261 |
schoenebeck |
53 |
} |
262 |
|
|
|
263 |
|
|
uint Sampler::SamplerChannels() { |
264 |
schoenebeck |
209 |
return mSamplerChannels.size(); |
265 |
schoenebeck |
53 |
} |
266 |
|
|
|
267 |
iliev |
1130 |
void Sampler::AddChannelCountListener(ChannelCountListener* l) { |
268 |
|
|
llChannelCountListeners.AddListener(l); |
269 |
|
|
} |
270 |
|
|
|
271 |
|
|
void Sampler::RemoveChannelCountListener(ChannelCountListener* l) { |
272 |
|
|
llChannelCountListeners.RemoveListener(l); |
273 |
|
|
} |
274 |
|
|
|
275 |
|
|
void Sampler::fireChannelCountChanged(int NewCount) { |
276 |
|
|
for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) { |
277 |
|
|
llChannelCountListeners.GetListener(i)->ChannelCountChanged(NewCount); |
278 |
|
|
} |
279 |
|
|
} |
280 |
|
|
|
281 |
schoenebeck |
1686 |
void Sampler::fireChannelAdded(SamplerChannel* pChannel) { |
282 |
|
|
for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) { |
283 |
|
|
llChannelCountListeners.GetListener(i)->ChannelAdded(pChannel); |
284 |
|
|
} |
285 |
|
|
} |
286 |
|
|
|
287 |
|
|
void Sampler::fireChannelToBeRemoved(SamplerChannel* pChannel) { |
288 |
|
|
for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) { |
289 |
|
|
llChannelCountListeners.GetListener(i)->ChannelToBeRemoved(pChannel); |
290 |
|
|
} |
291 |
|
|
} |
292 |
|
|
|
293 |
iliev |
1130 |
void Sampler::AddAudioDeviceCountListener(AudioDeviceCountListener* l) { |
294 |
|
|
llAudioDeviceCountListeners.AddListener(l); |
295 |
|
|
} |
296 |
|
|
|
297 |
|
|
void Sampler::RemoveAudioDeviceCountListener(AudioDeviceCountListener* l) { |
298 |
|
|
llAudioDeviceCountListeners.RemoveListener(l); |
299 |
|
|
} |
300 |
|
|
|
301 |
|
|
void Sampler::fireAudioDeviceCountChanged(int NewCount) { |
302 |
|
|
for (int i = 0; i < llAudioDeviceCountListeners.GetListenerCount(); i++) { |
303 |
|
|
llAudioDeviceCountListeners.GetListener(i)->AudioDeviceCountChanged(NewCount); |
304 |
|
|
} |
305 |
|
|
} |
306 |
|
|
|
307 |
|
|
void Sampler::AddMidiDeviceCountListener(MidiDeviceCountListener* l) { |
308 |
|
|
llMidiDeviceCountListeners.AddListener(l); |
309 |
|
|
} |
310 |
|
|
|
311 |
|
|
void Sampler::RemoveMidiDeviceCountListener(MidiDeviceCountListener* l) { |
312 |
|
|
llMidiDeviceCountListeners.RemoveListener(l); |
313 |
|
|
} |
314 |
|
|
|
315 |
|
|
void Sampler::fireMidiDeviceCountChanged(int NewCount) { |
316 |
|
|
for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) { |
317 |
|
|
llMidiDeviceCountListeners.GetListener(i)->MidiDeviceCountChanged(NewCount); |
318 |
|
|
} |
319 |
|
|
} |
320 |
|
|
|
321 |
schoenebeck |
1695 |
void Sampler::fireMidiDeviceToBeDestroyed(MidiInputDevice* pDevice) { |
322 |
|
|
for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) { |
323 |
|
|
llMidiDeviceCountListeners.GetListener(i)->MidiDeviceToBeDestroyed(pDevice); |
324 |
|
|
} |
325 |
|
|
} |
326 |
|
|
|
327 |
|
|
void Sampler::fireMidiDeviceCreated(MidiInputDevice* pDevice) { |
328 |
|
|
for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) { |
329 |
|
|
llMidiDeviceCountListeners.GetListener(i)->MidiDeviceCreated(pDevice); |
330 |
|
|
} |
331 |
|
|
} |
332 |
|
|
|
333 |
iliev |
1130 |
void Sampler::AddVoiceCountListener(VoiceCountListener* l) { |
334 |
|
|
llVoiceCountListeners.AddListener(l); |
335 |
|
|
} |
336 |
|
|
|
337 |
|
|
void Sampler::RemoveVoiceCountListener(VoiceCountListener* l) { |
338 |
|
|
llVoiceCountListeners.RemoveListener(l); |
339 |
|
|
} |
340 |
|
|
|
341 |
|
|
void Sampler::fireVoiceCountChanged(int ChannelId, int NewCount) { |
342 |
|
|
for (int i = 0; i < llVoiceCountListeners.GetListenerCount(); i++) { |
343 |
|
|
llVoiceCountListeners.GetListener(i)->VoiceCountChanged(ChannelId, NewCount); |
344 |
|
|
} |
345 |
|
|
} |
346 |
|
|
|
347 |
|
|
void Sampler::AddStreamCountListener(StreamCountListener* l) { |
348 |
|
|
llStreamCountListeners.AddListener(l); |
349 |
|
|
} |
350 |
|
|
|
351 |
|
|
void Sampler::RemoveStreamCountListener(StreamCountListener* l) { |
352 |
|
|
llStreamCountListeners.RemoveListener(l); |
353 |
|
|
} |
354 |
|
|
|
355 |
|
|
void Sampler::fireStreamCountChanged(int ChannelId, int NewCount) { |
356 |
|
|
for (int i = 0; i < llStreamCountListeners.GetListenerCount(); i++) { |
357 |
|
|
llStreamCountListeners.GetListener(i)->StreamCountChanged(ChannelId, NewCount); |
358 |
|
|
} |
359 |
|
|
} |
360 |
|
|
|
361 |
|
|
void Sampler::AddBufferFillListener(BufferFillListener* l) { |
362 |
|
|
llBufferFillListeners.AddListener(l); |
363 |
|
|
} |
364 |
|
|
|
365 |
|
|
void Sampler::RemoveBufferFillListener(BufferFillListener* l) { |
366 |
|
|
llBufferFillListeners.RemoveListener(l); |
367 |
|
|
} |
368 |
|
|
|
369 |
|
|
void Sampler::fireBufferFillChanged(int ChannelId, String FillData) { |
370 |
|
|
for (int i = 0; i < llBufferFillListeners.GetListenerCount(); i++) { |
371 |
|
|
llBufferFillListeners.GetListener(i)->BufferFillChanged(ChannelId, FillData); |
372 |
|
|
} |
373 |
|
|
} |
374 |
|
|
|
375 |
iliev |
1541 |
void Sampler::AddTotalStreamCountListener(TotalStreamCountListener* l) { |
376 |
|
|
llTotalStreamCountListeners.AddListener(l); |
377 |
|
|
} |
378 |
|
|
|
379 |
|
|
void Sampler::RemoveTotalStreamCountListener(TotalStreamCountListener* l) { |
380 |
|
|
llTotalStreamCountListeners.RemoveListener(l); |
381 |
|
|
} |
382 |
|
|
|
383 |
|
|
void Sampler::fireTotalStreamCountChanged(int NewCount) { |
384 |
|
|
for (int i = 0; i < llTotalStreamCountListeners.GetListenerCount(); i++) { |
385 |
|
|
llTotalStreamCountListeners.GetListener(i)->TotalStreamCountChanged(NewCount); |
386 |
|
|
} |
387 |
|
|
} |
388 |
|
|
|
389 |
iliev |
1130 |
void Sampler::AddTotalVoiceCountListener(TotalVoiceCountListener* l) { |
390 |
|
|
llTotalVoiceCountListeners.AddListener(l); |
391 |
|
|
} |
392 |
|
|
|
393 |
|
|
void Sampler::RemoveTotalVoiceCountListener(TotalVoiceCountListener* l) { |
394 |
|
|
llTotalVoiceCountListeners.RemoveListener(l); |
395 |
|
|
} |
396 |
|
|
|
397 |
|
|
void Sampler::fireTotalVoiceCountChanged(int NewCount) { |
398 |
|
|
for (int i = 0; i < llTotalVoiceCountListeners.GetListenerCount(); i++) { |
399 |
|
|
llTotalVoiceCountListeners.GetListener(i)->TotalVoiceCountChanged(NewCount); |
400 |
|
|
} |
401 |
|
|
} |
402 |
|
|
|
403 |
|
|
void Sampler::AddFxSendCountListener(FxSendCountListener* l) { |
404 |
|
|
llFxSendCountListeners.AddListener(l); |
405 |
|
|
} |
406 |
|
|
|
407 |
|
|
void Sampler::RemoveFxSendCountListener(FxSendCountListener* l) { |
408 |
|
|
llFxSendCountListeners.RemoveListener(l); |
409 |
|
|
} |
410 |
|
|
|
411 |
|
|
void Sampler::fireFxSendCountChanged(int ChannelId, int NewCount) { |
412 |
|
|
for (int i = 0; i < llFxSendCountListeners.GetListenerCount(); i++) { |
413 |
|
|
llFxSendCountListeners.GetListener(i)->FxSendCountChanged(ChannelId, NewCount); |
414 |
|
|
} |
415 |
|
|
} |
416 |
|
|
|
417 |
schoenebeck |
1686 |
void Sampler::EventHandler::EngineToBeChanged(int ChannelId) { |
418 |
|
|
// nothing to do here |
419 |
|
|
} |
420 |
|
|
|
421 |
iliev |
1130 |
void Sampler::EventHandler::EngineChanged(int ChannelId) { |
422 |
|
|
EngineChannel* engineChannel = pSampler->GetSamplerChannel(ChannelId)->GetEngineChannel(); |
423 |
|
|
if(engineChannel == NULL) return; |
424 |
|
|
engineChannel->AddFxSendCountListener(this); |
425 |
|
|
} |
426 |
|
|
|
427 |
|
|
void Sampler::EventHandler::FxSendCountChanged(int ChannelId, int NewCount) { |
428 |
|
|
pSampler->fireFxSendCountChanged(ChannelId, NewCount); |
429 |
|
|
} |
430 |
|
|
|
431 |
|
|
|
432 |
schoenebeck |
53 |
SamplerChannel* Sampler::AddSamplerChannel() { |
433 |
schoenebeck |
209 |
// if there's no sampler channel yet |
434 |
|
|
if (!mSamplerChannels.size()) { |
435 |
|
|
SamplerChannel* pChannel = new SamplerChannel(this); |
436 |
|
|
mSamplerChannels[0] = pChannel; |
437 |
schoenebeck |
1686 |
fireChannelAdded(pChannel); |
438 |
iliev |
1130 |
fireChannelCountChanged(1); |
439 |
|
|
pChannel->AddEngineChangeListener(&eventHandler); |
440 |
schoenebeck |
209 |
return pChannel; |
441 |
|
|
} |
442 |
|
|
|
443 |
|
|
// get the highest used sampler channel index |
444 |
|
|
uint lastIndex = (--(mSamplerChannels.end()))->first; |
445 |
|
|
|
446 |
|
|
// check if we reached the index limit |
447 |
|
|
if (lastIndex + 1 < lastIndex) { |
448 |
|
|
// search for an unoccupied sampler channel index starting from 0 |
449 |
|
|
for (uint i = 0; i < lastIndex; i++) { |
450 |
|
|
if (mSamplerChannels.find(i) != mSamplerChannels.end()) continue; |
451 |
|
|
// we found an unused index, so insert the new channel there |
452 |
|
|
SamplerChannel* pChannel = new SamplerChannel(this); |
453 |
|
|
mSamplerChannels[i] = pChannel; |
454 |
schoenebeck |
1686 |
fireChannelAdded(pChannel); |
455 |
iliev |
1130 |
fireChannelCountChanged(SamplerChannels()); |
456 |
|
|
pChannel->AddEngineChangeListener(&eventHandler); |
457 |
schoenebeck |
209 |
return pChannel; |
458 |
|
|
} |
459 |
schoenebeck |
880 |
throw Exception("Internal error: could not find unoccupied sampler channel index."); |
460 |
schoenebeck |
209 |
} |
461 |
|
|
|
462 |
|
|
// we have not reached the index limit so we just add the channel past the highest index |
463 |
schoenebeck |
53 |
SamplerChannel* pChannel = new SamplerChannel(this); |
464 |
schoenebeck |
209 |
mSamplerChannels[lastIndex + 1] = pChannel; |
465 |
schoenebeck |
1686 |
fireChannelAdded(pChannel); |
466 |
iliev |
1130 |
fireChannelCountChanged(SamplerChannels()); |
467 |
|
|
pChannel->AddEngineChangeListener(&eventHandler); |
468 |
schoenebeck |
53 |
return pChannel; |
469 |
|
|
} |
470 |
|
|
|
471 |
|
|
SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) { |
472 |
schoenebeck |
209 |
return (mSamplerChannels.find(uiSamplerChannel) != mSamplerChannels.end()) ? mSamplerChannels[uiSamplerChannel] : NULL; |
473 |
schoenebeck |
53 |
} |
474 |
|
|
|
475 |
schoenebeck |
209 |
std::map<uint, SamplerChannel*> Sampler::GetSamplerChannels() { |
476 |
|
|
return mSamplerChannels; |
477 |
|
|
} |
478 |
|
|
|
479 |
schoenebeck |
53 |
void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) { |
480 |
schoenebeck |
209 |
SamplerChannelMap::iterator iterChan = mSamplerChannels.begin(); |
481 |
|
|
for (; iterChan != mSamplerChannels.end(); iterChan++) { |
482 |
|
|
if (iterChan->second == pSamplerChannel) { |
483 |
schoenebeck |
1686 |
fireChannelToBeRemoved(pSamplerChannel); |
484 |
iliev |
1130 |
pSamplerChannel->RemoveAllEngineChangeListeners(); |
485 |
schoenebeck |
209 |
mSamplerChannels.erase(iterChan); |
486 |
schoenebeck |
53 |
delete pSamplerChannel; |
487 |
iliev |
1130 |
fireChannelCountChanged(SamplerChannels()); |
488 |
schoenebeck |
53 |
return; |
489 |
|
|
} |
490 |
|
|
} |
491 |
|
|
} |
492 |
|
|
|
493 |
|
|
void Sampler::RemoveSamplerChannel(uint uiSamplerChannel) { |
494 |
|
|
SamplerChannel* pChannel = GetSamplerChannel(uiSamplerChannel); |
495 |
|
|
if (!pChannel) return; |
496 |
|
|
RemoveSamplerChannel(pChannel); |
497 |
|
|
} |
498 |
|
|
|
499 |
schoenebeck |
123 |
std::vector<String> Sampler::AvailableAudioOutputDrivers() { |
500 |
|
|
return AudioOutputDeviceFactory::AvailableDrivers(); |
501 |
|
|
} |
502 |
schoenebeck |
53 |
|
503 |
schoenebeck |
900 |
std::vector<String> Sampler::AvailableMidiInputDrivers() { |
504 |
|
|
return MidiInputDeviceFactory::AvailableDrivers(); |
505 |
|
|
} |
506 |
|
|
|
507 |
|
|
std::vector<String> Sampler::AvailableEngineTypes() { |
508 |
|
|
return EngineFactory::AvailableEngineTypes(); |
509 |
|
|
} |
510 |
|
|
|
511 |
schoenebeck |
880 |
AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map<String,String> Parameters) throw (Exception) { |
512 |
schoenebeck |
53 |
// create new device |
513 |
schoenebeck |
123 |
AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters); |
514 |
schoenebeck |
53 |
|
515 |
schoenebeck |
64 |
// add new audio device to the audio device list |
516 |
schoenebeck |
123 |
for (uint i = 0; ; i++) { // seek for a free place starting from the beginning |
517 |
|
|
if (!mAudioOutputDevices[i]) { |
518 |
|
|
mAudioOutputDevices[i] = pDevice; |
519 |
|
|
break; |
520 |
|
|
} |
521 |
|
|
} |
522 |
schoenebeck |
64 |
|
523 |
iliev |
1130 |
fireAudioDeviceCountChanged(AudioOutputDevices()); |
524 |
schoenebeck |
53 |
return pDevice; |
525 |
|
|
} |
526 |
|
|
|
527 |
schoenebeck |
123 |
uint Sampler::AudioOutputDevices() { |
528 |
|
|
return mAudioOutputDevices.size(); |
529 |
schoenebeck |
53 |
} |
530 |
|
|
|
531 |
senkov |
155 |
uint Sampler::MidiInputDevices() { |
532 |
|
|
return mMidiInputDevices.size(); |
533 |
|
|
} |
534 |
|
|
|
535 |
schoenebeck |
123 |
std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() { |
536 |
|
|
return mAudioOutputDevices; |
537 |
|
|
} |
538 |
|
|
|
539 |
senkov |
155 |
std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() { |
540 |
|
|
return mMidiInputDevices; |
541 |
|
|
} |
542 |
|
|
|
543 |
schoenebeck |
880 |
void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) { |
544 |
schoenebeck |
123 |
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
545 |
|
|
for (; iter != mAudioOutputDevices.end(); iter++) { |
546 |
|
|
if (iter->second == pDevice) { |
547 |
|
|
// check if there are still sampler engines connected to this device |
548 |
|
|
for (uint i = 0; i < SamplerChannels(); i++) |
549 |
schoenebeck |
880 |
if (GetSamplerChannel(i)->GetAudioOutputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the audio output device."); |
550 |
schoenebeck |
123 |
|
551 |
|
|
// disable device |
552 |
|
|
pDevice->Stop(); |
553 |
|
|
|
554 |
persson |
837 |
// remove device from the device list |
555 |
|
|
mAudioOutputDevices.erase(iter); |
556 |
|
|
|
557 |
schoenebeck |
123 |
// destroy and free device from memory |
558 |
|
|
delete pDevice; |
559 |
persson |
837 |
|
560 |
iliev |
1130 |
fireAudioDeviceCountChanged(AudioOutputDevices()); |
561 |
persson |
837 |
break; |
562 |
schoenebeck |
123 |
} |
563 |
|
|
} |
564 |
|
|
} |
565 |
|
|
|
566 |
schoenebeck |
880 |
void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) { |
567 |
senkov |
155 |
MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); |
568 |
|
|
for (; iter != mMidiInputDevices.end(); iter++) { |
569 |
|
|
if (iter->second == pDevice) { |
570 |
|
|
// check if there are still sampler engines connected to this device |
571 |
|
|
for (uint i = 0; i < SamplerChannels(); i++) |
572 |
schoenebeck |
880 |
if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the midi input device."); |
573 |
schoenebeck |
53 |
|
574 |
schoenebeck |
1695 |
fireMidiDeviceToBeDestroyed(pDevice); |
575 |
|
|
|
576 |
senkov |
155 |
// disable device |
577 |
|
|
pDevice->StopListen(); |
578 |
|
|
|
579 |
persson |
837 |
// remove device from the device list |
580 |
|
|
mMidiInputDevices.erase(iter); |
581 |
|
|
|
582 |
senkov |
155 |
// destroy and free device from memory |
583 |
|
|
delete pDevice; |
584 |
persson |
837 |
|
585 |
iliev |
1130 |
fireMidiDeviceCountChanged(MidiInputDevices()); |
586 |
persson |
837 |
break; |
587 |
senkov |
155 |
} |
588 |
schoenebeck |
53 |
} |
589 |
senkov |
155 |
} |
590 |
schoenebeck |
53 |
|
591 |
schoenebeck |
880 |
MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (Exception) { |
592 |
senkov |
155 |
// create new device |
593 |
schoenebeck |
551 |
MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this); |
594 |
senkov |
155 |
|
595 |
|
|
// add new device to the midi device list |
596 |
|
|
for (uint i = 0; ; i++) { // seek for a free place starting from the beginning |
597 |
|
|
if (!mMidiInputDevices[i]) { |
598 |
|
|
mMidiInputDevices[i] = pDevice; |
599 |
|
|
break; |
600 |
|
|
} |
601 |
|
|
} |
602 |
schoenebeck |
64 |
|
603 |
schoenebeck |
1695 |
fireMidiDeviceCreated(pDevice); |
604 |
iliev |
1130 |
fireMidiDeviceCountChanged(MidiInputDevices()); |
605 |
schoenebeck |
53 |
return pDevice; |
606 |
|
|
} |
607 |
|
|
|
608 |
iliev |
1541 |
int Sampler::GetDiskStreamCount() { |
609 |
|
|
int count = 0; |
610 |
|
|
std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin(); |
611 |
|
|
|
612 |
|
|
for(; it != EngineFactory::EngineInstances().end(); it++) { |
613 |
|
|
count += (*it)->DiskStreamCount(); |
614 |
|
|
} |
615 |
|
|
|
616 |
|
|
return count; |
617 |
|
|
} |
618 |
|
|
|
619 |
iliev |
778 |
int Sampler::GetVoiceCount() { |
620 |
|
|
int count = 0; |
621 |
|
|
std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin(); |
622 |
|
|
|
623 |
|
|
for(; it != EngineFactory::EngineInstances().end(); it++) { |
624 |
|
|
count += (*it)->VoiceCount(); |
625 |
|
|
} |
626 |
|
|
|
627 |
|
|
return count; |
628 |
|
|
} |
629 |
|
|
|
630 |
schoenebeck |
212 |
void Sampler::Reset() { |
631 |
|
|
// delete sampler channels |
632 |
|
|
try { |
633 |
senkov |
329 |
while (true) { |
634 |
|
|
SamplerChannelMap::iterator iter = mSamplerChannels.begin(); |
635 |
|
|
if (iter == mSamplerChannels.end()) break; |
636 |
|
|
RemoveSamplerChannel(iter->second); |
637 |
schoenebeck |
212 |
} |
638 |
|
|
} |
639 |
|
|
catch(...) { |
640 |
|
|
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all sampler channels, exiting.\n" << std::flush; |
641 |
|
|
exit(EXIT_FAILURE); |
642 |
|
|
} |
643 |
|
|
|
644 |
|
|
// delete midi input devices |
645 |
|
|
try { |
646 |
senkov |
329 |
while (true) { |
647 |
|
|
MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); |
648 |
|
|
if (iter == mMidiInputDevices.end()) break; |
649 |
|
|
DestroyMidiInputDevice(iter->second); |
650 |
schoenebeck |
212 |
} |
651 |
|
|
} |
652 |
|
|
catch(...) { |
653 |
|
|
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI input devices, exiting.\n" << std::flush; |
654 |
|
|
exit(EXIT_FAILURE); |
655 |
|
|
} |
656 |
|
|
|
657 |
|
|
// delete audio output devices |
658 |
|
|
try { |
659 |
senkov |
329 |
while (true) { |
660 |
|
|
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
661 |
|
|
if (iter == mAudioOutputDevices.end()) break; |
662 |
|
|
DestroyAudioOutputDevice(iter->second); |
663 |
schoenebeck |
212 |
} |
664 |
|
|
} |
665 |
|
|
catch(...) { |
666 |
|
|
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all audio output devices, exiting.\n" << std::flush; |
667 |
|
|
exit(EXIT_FAILURE); |
668 |
|
|
} |
669 |
schoenebeck |
1008 |
|
670 |
|
|
// delete MIDI instrument maps |
671 |
|
|
try { |
672 |
|
|
MidiInstrumentMapper::RemoveAllMaps(); |
673 |
|
|
} |
674 |
|
|
catch(...) { |
675 |
|
|
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI instrument maps, exiting.\n" << std::flush; |
676 |
|
|
exit(EXIT_FAILURE); |
677 |
|
|
} |
678 |
schoenebeck |
1212 |
|
679 |
|
|
// unload all instrument editor DLLs |
680 |
|
|
InstrumentEditorFactory::ClosePlugins(); |
681 |
schoenebeck |
212 |
} |
682 |
|
|
|
683 |
schoenebeck |
53 |
} // namespace LinuxSampler |