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