2 |
* * |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
|
* Copyright (C) 2005, 2006 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
9 |
* 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 * |
21 |
* MA 02111-1307 USA * |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
***************************************************************************/ |
23 |
|
|
24 |
|
#include <sstream> |
25 |
|
|
26 |
#include "Sampler.h" |
#include "Sampler.h" |
27 |
|
|
28 |
#include "audiodriver/AudioOutputDeviceAlsa.h" |
#include "engines/EngineFactory.h" |
29 |
#include "audiodriver/AudioOutputDeviceJack.h" |
#include "engines/EngineChannelFactory.h" |
30 |
#include "mididriver/MidiInputDeviceAlsa.h" |
#include "drivers/audio/AudioOutputDeviceFactory.h" |
31 |
#include "engines/gig/Engine.h" |
#include "drivers/midi/MidiInputDeviceFactory.h" |
32 |
|
#include "network/lscpserver.h" |
33 |
|
|
34 |
namespace LinuxSampler { |
namespace LinuxSampler { |
35 |
|
|
38 |
|
|
39 |
SamplerChannel::SamplerChannel(Sampler* pS) { |
SamplerChannel::SamplerChannel(Sampler* pS) { |
40 |
pSampler = pS; |
pSampler = pS; |
41 |
pEngine = NULL; |
pEngineChannel = NULL; |
|
pMidiInputDevice = NULL; |
|
42 |
pAudioOutputDevice = NULL; |
pAudioOutputDevice = NULL; |
43 |
|
pMidiInputDevice = NULL; |
44 |
|
iMidiPort = 0; |
45 |
|
midiChannel = midi_chan_all; |
46 |
iIndex = -1; |
iIndex = -1; |
47 |
} |
} |
48 |
|
|
49 |
SamplerChannel::~SamplerChannel() { |
SamplerChannel::~SamplerChannel() { |
50 |
if (pEngine) { |
if (pEngineChannel) { |
51 |
if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine); |
Engine* engine = pEngineChannel->GetEngine(); |
52 |
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); |
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine); |
53 |
delete pEngine; |
|
54 |
|
MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : __GetMidiInputDevicePort(GetMidiInputChannel()); |
55 |
|
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel); |
56 |
|
if (pEngineChannel) { |
57 |
|
if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice(); |
58 |
|
EngineChannelFactory::Destroy(pEngineChannel); |
59 |
|
|
60 |
|
// reconnect engine if it still exists |
61 |
|
const std::set<Engine*>& engines = EngineFactory::EngineInstances(); |
62 |
|
if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine); |
63 |
|
} |
64 |
} |
} |
65 |
} |
} |
66 |
|
|
67 |
void SamplerChannel::LoadEngine(engine_type_t EngineType) { |
void SamplerChannel::SetEngineType(String EngineType) throw (Exception) { |
68 |
dmsg(1,("SamplerChannel: Loading engine\n")); |
dmsg(2,("SamplerChannel: Assigning engine type...")); |
69 |
|
|
70 |
// create new engine |
// create new engine channel |
71 |
Engine* pNewEngine = NULL; |
EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType); |
72 |
switch (EngineType) { |
if (!pNewEngineChannel) throw Exception("Unknown engine type"); |
73 |
case engine_type_gig: |
|
74 |
pNewEngine = new gig::Engine; |
//FIXME: hack to allow fast retrieval of engine channel's sampler channel index |
75 |
break; |
pNewEngineChannel->iSamplerChannelIndex = Index(); |
76 |
default: |
|
77 |
throw LinuxSamplerException("Unknown engine type"); |
// dereference midi input port. |
78 |
|
MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort()); |
79 |
|
// disconnect old engine channel |
80 |
|
if (pEngineChannel) { |
81 |
|
Engine* engine = pEngineChannel->GetEngine(); |
82 |
|
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine); |
83 |
|
|
84 |
|
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel); |
85 |
|
if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice(); |
86 |
|
EngineChannelFactory::Destroy(pEngineChannel); |
87 |
|
|
88 |
|
// reconnect engine if it still exists |
89 |
|
const std::set<Engine*>& engines = EngineFactory::EngineInstances(); |
90 |
|
if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine); |
91 |
} |
} |
92 |
|
|
93 |
// disconnect old engine |
// connect new engine channel |
94 |
if (pEngine) { |
if (pAudioOutputDevice) { |
95 |
if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine); |
pNewEngineChannel->Connect(pAudioOutputDevice); |
96 |
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); |
pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine()); |
|
delete pEngine; |
|
97 |
} |
} |
98 |
|
if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, GetMidiInputChannel()); |
99 |
|
pEngineChannel = pNewEngineChannel; |
100 |
|
|
101 |
// connect new engine |
// from now on get MIDI device and port from EngineChannel object |
102 |
pEngine = pNewEngine; |
this->pMidiInputDevice = NULL; |
103 |
if (pMidiInputDevice) pMidiInputDevice->Connect(pNewEngine, (MidiInputDevice::midi_chan_t) Index()); |
this->iMidiPort = 0; |
|
if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine); |
|
|
dmsg(1,("SamplerChannel: Engine loaded.\n")); |
|
|
} |
|
104 |
|
|
105 |
void SamplerChannel::SetAudioOutputDevice(audio_output_type_t AudioType) { |
pEngineChannel->StatusChanged(true); |
|
// get / create desired audio device |
|
|
AudioOutputDevice* pDevice = pSampler->GetAudioOutputDevice(AudioType); |
|
|
if (!pDevice) pDevice = pSampler->CreateAudioOutputDevice(AudioType); |
|
106 |
|
|
107 |
|
dmsg(2,("OK\n")); |
108 |
|
} |
109 |
|
|
110 |
|
void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) { |
111 |
// disconnect old device |
// disconnect old device |
112 |
if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine); |
if (pAudioOutputDevice && pEngineChannel) { |
113 |
|
Engine* engine = pEngineChannel->GetEngine(); |
114 |
|
pAudioOutputDevice->Disconnect(engine); |
115 |
|
|
116 |
|
pEngineChannel->DisconnectAudioOutputDevice(); |
117 |
|
|
118 |
|
// reconnect engine if it still exists |
119 |
|
const std::set<Engine*>& engines = EngineFactory::EngineInstances(); |
120 |
|
if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine); |
121 |
|
} |
122 |
|
|
123 |
// connect new device |
// connect new device |
124 |
pAudioOutputDevice = pDevice; |
pAudioOutputDevice = pDevice; |
125 |
if (pEngine) pAudioOutputDevice->Connect(pEngine); |
if (pEngineChannel) { |
126 |
|
pEngineChannel->Connect(pAudioOutputDevice); |
127 |
|
pAudioOutputDevice->Connect(pEngineChannel->GetEngine()); |
128 |
|
} |
129 |
} |
} |
130 |
|
|
131 |
void SamplerChannel::SetMidiInputDevice(midi_input_type_t MidiType, MidiInputDevice::midi_chan_t MidiChannel) { |
void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) { |
132 |
// get / create desired midi device |
SetMidiInput(pDevice, 0, GetMidiInputChannel()); |
133 |
MidiInputDevice* pDevice = pSampler->GetMidiInputDevice(MidiType); |
} |
|
if (!pDevice) pDevice = pSampler->CreateMidiInputDevice(MidiType); |
|
134 |
|
|
135 |
// disconnect old device |
void SamplerChannel::SetMidiInputPort(int MidiPort) { |
136 |
if (pMidiInputDevice && pEngine) pMidiInputDevice->Disconnect(pEngine); |
SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel()); |
137 |
|
} |
138 |
|
|
139 |
// connect new device |
void SamplerChannel::SetMidiInputChannel(midi_chan_t MidiChannel) { |
140 |
pMidiInputDevice = pDevice; |
SetMidiInput(GetMidiInputDevice(), GetMidiInputPort(), MidiChannel); |
|
if (pEngine) pMidiInputDevice->Connect(pEngine, MidiChannel); |
|
141 |
} |
} |
142 |
|
|
143 |
Engine* SamplerChannel::GetEngine() { |
void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) { |
144 |
return pEngine; |
if (!pDevice) throw Exception("No MIDI input device assigned."); |
145 |
|
|
146 |
|
// get old and new midi input port |
147 |
|
MidiInputPort* pOldMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort()); |
148 |
|
MidiInputPort* pNewMidiInputPort = pDevice->GetPort(iMidiPort); |
149 |
|
|
150 |
|
// disconnect old device port |
151 |
|
if (pOldMidiInputPort && pEngineChannel) pOldMidiInputPort->Disconnect(pEngineChannel); |
152 |
|
// remember new device, port and channel if not engine channel yet created |
153 |
|
if (!pEngineChannel) { |
154 |
|
this->pMidiInputDevice = pDevice; |
155 |
|
this->iMidiPort = iMidiPort; |
156 |
|
this->midiChannel = MidiChannel; |
157 |
|
} |
158 |
|
|
159 |
|
// connect new device port |
160 |
|
if (pNewMidiInputPort && pEngineChannel) pNewMidiInputPort->Connect(pEngineChannel, MidiChannel); |
161 |
|
// Ooops. |
162 |
|
if (pNewMidiInputPort == NULL) |
163 |
|
throw Exception("There is no MIDI input port with index " + ToString(iMidiPort) + "."); |
164 |
} |
} |
165 |
|
|
166 |
MidiInputDevice* SamplerChannel::GetMidiInputDevice() { |
EngineChannel* SamplerChannel::GetEngineChannel() { |
167 |
return pMidiInputDevice; |
return pEngineChannel; |
168 |
|
} |
169 |
|
|
170 |
|
midi_chan_t SamplerChannel::GetMidiInputChannel() { |
171 |
|
if (pEngineChannel) this->midiChannel = pEngineChannel->MidiChannel(); |
172 |
|
return this->midiChannel; |
173 |
|
} |
174 |
|
|
175 |
|
int SamplerChannel::GetMidiInputPort() { |
176 |
|
MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : NULL; |
177 |
|
if (pMidiInputPort) this->iMidiPort = (int) pMidiInputPort->GetPortNumber(); |
178 |
|
return iMidiPort; |
179 |
} |
} |
180 |
|
|
181 |
AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() { |
AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() { |
182 |
return pAudioOutputDevice; |
return pAudioOutputDevice; |
183 |
} |
} |
184 |
|
|
185 |
|
MidiInputDevice* SamplerChannel::GetMidiInputDevice() { |
186 |
|
if (pEngineChannel) |
187 |
|
pMidiInputDevice = (pEngineChannel->GetMidiInputPort()) ? pEngineChannel->GetMidiInputPort()->GetDevice() : NULL; |
188 |
|
return pMidiInputDevice; |
189 |
|
} |
190 |
|
|
191 |
uint SamplerChannel::Index() { |
uint SamplerChannel::Index() { |
192 |
if (iIndex >= 0) return iIndex; |
if (iIndex >= 0) return iIndex; |
193 |
|
|
194 |
std::vector<SamplerChannel*>::iterator iter = pSampler->vSamplerChannels.begin(); |
Sampler::SamplerChannelMap::iterator iter = pSampler->mSamplerChannels.begin(); |
195 |
for (int i = 0; iter != pSampler->vSamplerChannels.end(); i++, iter++) { |
for (; iter != pSampler->mSamplerChannels.end(); iter++) { |
196 |
if (*iter == this) { |
if (iter->second == this) { |
197 |
iIndex = i; |
iIndex = iter->first; |
198 |
return i; |
return iIndex; |
199 |
} |
} |
200 |
} |
} |
201 |
|
|
202 |
throw LinuxSamplerException("SamplerChannel index not found"); |
throw Exception("Internal error: SamplerChannel index not found"); |
203 |
|
} |
204 |
|
|
205 |
|
MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) { |
206 |
|
MidiInputPort* pMidiInputPort = NULL; |
207 |
|
MidiInputDevice* pMidiInputDevice = GetMidiInputDevice(); |
208 |
|
if (pMidiInputDevice) |
209 |
|
pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort); |
210 |
|
return pMidiInputPort; |
211 |
} |
} |
212 |
|
|
213 |
|
|
214 |
|
|
215 |
// ****************************************************************** |
// ****************************************************************** |
216 |
// * Sampler |
// * Sampler |
217 |
|
|
219 |
} |
} |
220 |
|
|
221 |
Sampler::~Sampler() { |
Sampler::~Sampler() { |
222 |
// delete sampler channels |
Reset(); |
|
{ |
|
|
std::vector<SamplerChannel*>::iterator iter = vSamplerChannels.begin(); |
|
|
for (; iter != vSamplerChannels.end(); iter++) delete *iter; |
|
|
} |
|
|
|
|
|
// delete midi input devices |
|
|
{ |
|
|
MidiInputDeviceMap::iterator iter = MidiInputDevices.begin(); |
|
|
for (; iter != MidiInputDevices.end(); iter++) { |
|
|
MidiInputDevice* pDevice = iter->second; |
|
|
pDevice->StopListen(); |
|
|
delete pDevice; |
|
|
} |
|
|
} |
|
|
|
|
|
// delete audio output devices |
|
|
{ |
|
|
AudioOutputDeviceMap::iterator iter = AudioOutputDevices.begin(); |
|
|
for (; iter != AudioOutputDevices.end(); iter++) { |
|
|
AudioOutputDevice* pDevice = iter->second; |
|
|
pDevice->Stop(); |
|
|
delete pDevice; |
|
|
} |
|
|
} |
|
223 |
} |
} |
224 |
|
|
225 |
uint Sampler::SamplerChannels() { |
uint Sampler::SamplerChannels() { |
226 |
vSamplerChannels.size(); |
return mSamplerChannels.size(); |
227 |
} |
} |
228 |
|
|
229 |
SamplerChannel* Sampler::AddSamplerChannel() { |
SamplerChannel* Sampler::AddSamplerChannel() { |
230 |
|
// if there's no sampler channel yet |
231 |
|
if (!mSamplerChannels.size()) { |
232 |
|
SamplerChannel* pChannel = new SamplerChannel(this); |
233 |
|
mSamplerChannels[0] = pChannel; |
234 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, 1)); |
235 |
|
return pChannel; |
236 |
|
} |
237 |
|
|
238 |
|
// get the highest used sampler channel index |
239 |
|
uint lastIndex = (--(mSamplerChannels.end()))->first; |
240 |
|
|
241 |
|
// check if we reached the index limit |
242 |
|
if (lastIndex + 1 < lastIndex) { |
243 |
|
// search for an unoccupied sampler channel index starting from 0 |
244 |
|
for (uint i = 0; i < lastIndex; i++) { |
245 |
|
if (mSamplerChannels.find(i) != mSamplerChannels.end()) continue; |
246 |
|
// we found an unused index, so insert the new channel there |
247 |
|
SamplerChannel* pChannel = new SamplerChannel(this); |
248 |
|
mSamplerChannels[i] = pChannel; |
249 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, i)); |
250 |
|
return pChannel; |
251 |
|
} |
252 |
|
throw Exception("Internal error: could not find unoccupied sampler channel index."); |
253 |
|
} |
254 |
|
|
255 |
|
// we have not reached the index limit so we just add the channel past the highest index |
256 |
SamplerChannel* pChannel = new SamplerChannel(this); |
SamplerChannel* pChannel = new SamplerChannel(this); |
257 |
vSamplerChannels.push_back(pChannel); |
mSamplerChannels[lastIndex + 1] = pChannel; |
258 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, lastIndex + 1)); |
259 |
return pChannel; |
return pChannel; |
260 |
} |
} |
261 |
|
|
262 |
SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) { |
SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) { |
263 |
if (uiSamplerChannel >= SamplerChannels()) return NULL; |
return (mSamplerChannels.find(uiSamplerChannel) != mSamplerChannels.end()) ? mSamplerChannels[uiSamplerChannel] : NULL; |
264 |
return vSamplerChannels[uiSamplerChannel]; |
} |
265 |
|
|
266 |
|
std::map<uint, SamplerChannel*> Sampler::GetSamplerChannels() { |
267 |
|
return mSamplerChannels; |
268 |
} |
} |
269 |
|
|
270 |
void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) { |
void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) { |
271 |
std::vector<SamplerChannel*>::iterator iterChan = vSamplerChannels.begin(); |
SamplerChannelMap::iterator iterChan = mSamplerChannels.begin(); |
272 |
for (; iterChan != vSamplerChannels.end(); iterChan++) { |
for (; iterChan != mSamplerChannels.end(); iterChan++) { |
273 |
if (*iterChan == pSamplerChannel) { |
if (iterChan->second == pSamplerChannel) { |
274 |
vSamplerChannels.erase(iterChan); |
mSamplerChannels.erase(iterChan); |
275 |
delete pSamplerChannel; |
delete pSamplerChannel; |
276 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, mSamplerChannels.size())); |
277 |
return; |
return; |
278 |
} |
} |
279 |
} |
} |
285 |
RemoveSamplerChannel(pChannel); |
RemoveSamplerChannel(pChannel); |
286 |
} |
} |
287 |
|
|
288 |
AudioOutputDevice* Sampler::CreateAudioOutputDevice(audio_output_type_t AudioType) { |
std::vector<String> Sampler::AvailableAudioOutputDrivers() { |
289 |
// check if device already created |
return AudioOutputDeviceFactory::AvailableDrivers(); |
290 |
AudioOutputDevice* pDevice = GetAudioOutputDevice(AudioType); |
} |
|
if (pDevice) return pDevice; |
|
291 |
|
|
292 |
|
AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map<String,String> Parameters) throw (Exception) { |
293 |
// create new device |
// create new device |
294 |
switch (AudioType) { |
AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters); |
295 |
case audio_output_type_alsa: |
|
296 |
pDevice = new AudioOutputDeviceAlsa; |
// add new audio device to the audio device list |
297 |
break; |
for (uint i = 0; ; i++) { // seek for a free place starting from the beginning |
298 |
case audio_output_type_jack: |
if (!mAudioOutputDevices[i]) { |
299 |
pDevice = new AudioOutputDeviceJack; |
mAudioOutputDevices[i] = pDevice; |
300 |
break; |
break; |
301 |
default: |
} |
|
throw LinuxSamplerException("Unknown audio output device type"); |
|
302 |
} |
} |
303 |
|
|
|
// activate device |
|
|
pDevice->Play(); |
|
|
|
|
304 |
return pDevice; |
return pDevice; |
305 |
} |
} |
306 |
|
|
307 |
AudioOutputDevice* Sampler::GetAudioOutputDevice(audio_output_type_t AudioType) { |
uint Sampler::AudioOutputDevices() { |
308 |
AudioOutputDeviceMap::iterator iter = AudioOutputDevices.find(AudioType); |
return mAudioOutputDevices.size(); |
|
return (iter != AudioOutputDevices.end()) ? iter->second : NULL; |
|
309 |
} |
} |
310 |
|
|
311 |
MidiInputDevice* Sampler::CreateMidiInputDevice(midi_input_type_t MidiType) { |
uint Sampler::MidiInputDevices() { |
312 |
// check if device already created |
return mMidiInputDevices.size(); |
313 |
MidiInputDevice* pDevice = GetMidiInputDevice(MidiType); |
} |
314 |
if (pDevice) return pDevice; |
|
315 |
|
std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() { |
316 |
|
return mAudioOutputDevices; |
317 |
|
} |
318 |
|
|
319 |
|
std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() { |
320 |
|
return mMidiInputDevices; |
321 |
|
} |
322 |
|
|
323 |
|
void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) { |
324 |
|
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
325 |
|
for (; iter != mAudioOutputDevices.end(); iter++) { |
326 |
|
if (iter->second == pDevice) { |
327 |
|
// check if there are still sampler engines connected to this device |
328 |
|
for (uint i = 0; i < SamplerChannels(); i++) |
329 |
|
if (GetSamplerChannel(i)->GetAudioOutputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the audio output device."); |
330 |
|
|
331 |
|
// disable device |
332 |
|
pDevice->Stop(); |
333 |
|
|
334 |
|
// remove device from the device list |
335 |
|
mAudioOutputDevices.erase(iter); |
336 |
|
|
337 |
|
// destroy and free device from memory |
338 |
|
delete pDevice; |
339 |
|
|
340 |
|
break; |
341 |
|
} |
342 |
|
} |
343 |
|
} |
344 |
|
|
345 |
|
void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) { |
346 |
|
MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); |
347 |
|
for (; iter != mMidiInputDevices.end(); iter++) { |
348 |
|
if (iter->second == pDevice) { |
349 |
|
// check if there are still sampler engines connected to this device |
350 |
|
for (uint i = 0; i < SamplerChannels(); i++) |
351 |
|
if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the midi input device."); |
352 |
|
|
353 |
|
// disable device |
354 |
|
pDevice->StopListen(); |
355 |
|
|
356 |
|
// remove device from the device list |
357 |
|
mMidiInputDevices.erase(iter); |
358 |
|
|
359 |
|
// destroy and free device from memory |
360 |
|
delete pDevice; |
361 |
|
|
|
// create new device |
|
|
switch (MidiType) { |
|
|
case midi_input_type_alsa: |
|
|
pDevice = new MidiInputDeviceAlsa; |
|
362 |
break; |
break; |
363 |
default: |
} |
|
throw LinuxSamplerException("Unknown audio output device type"); |
|
364 |
} |
} |
365 |
|
} |
366 |
|
|
367 |
|
MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (Exception) { |
368 |
|
// create new device |
369 |
|
MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this); |
370 |
|
|
371 |
// activate device |
// add new device to the midi device list |
372 |
pDevice->Listen(); |
for (uint i = 0; ; i++) { // seek for a free place starting from the beginning |
373 |
|
if (!mMidiInputDevices[i]) { |
374 |
|
mMidiInputDevices[i] = pDevice; |
375 |
|
break; |
376 |
|
} |
377 |
|
} |
378 |
|
|
379 |
return pDevice; |
return pDevice; |
380 |
} |
} |
381 |
|
|
382 |
MidiInputDevice* Sampler::GetMidiInputDevice(midi_input_type_t MidiType) { |
int Sampler::GetVoiceCount() { |
383 |
MidiInputDeviceMap::iterator iter = MidiInputDevices.find(MidiType); |
int count = 0; |
384 |
return (iter != MidiInputDevices.end()) ? iter->second : NULL; |
std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin(); |
385 |
|
|
386 |
|
for(; it != EngineFactory::EngineInstances().end(); it++) { |
387 |
|
count += (*it)->VoiceCount(); |
388 |
|
} |
389 |
|
|
390 |
|
return count; |
391 |
|
} |
392 |
|
|
393 |
|
void Sampler::Reset() { |
394 |
|
// delete sampler channels |
395 |
|
try { |
396 |
|
while (true) { |
397 |
|
SamplerChannelMap::iterator iter = mSamplerChannels.begin(); |
398 |
|
if (iter == mSamplerChannels.end()) break; |
399 |
|
RemoveSamplerChannel(iter->second); |
400 |
|
} |
401 |
|
} |
402 |
|
catch(...) { |
403 |
|
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all sampler channels, exiting.\n" << std::flush; |
404 |
|
exit(EXIT_FAILURE); |
405 |
|
} |
406 |
|
|
407 |
|
// delete midi input devices |
408 |
|
try { |
409 |
|
while (true) { |
410 |
|
MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); |
411 |
|
if (iter == mMidiInputDevices.end()) break; |
412 |
|
DestroyMidiInputDevice(iter->second); |
413 |
|
} |
414 |
|
} |
415 |
|
catch(...) { |
416 |
|
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI input devices, exiting.\n" << std::flush; |
417 |
|
exit(EXIT_FAILURE); |
418 |
|
} |
419 |
|
|
420 |
|
// delete audio output devices |
421 |
|
try { |
422 |
|
while (true) { |
423 |
|
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
424 |
|
if (iter == mAudioOutputDevices.end()) break; |
425 |
|
DestroyAudioOutputDevice(iter->second); |
426 |
|
} |
427 |
|
} |
428 |
|
catch(...) { |
429 |
|
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all audio output devices, exiting.\n" << std::flush; |
430 |
|
exit(EXIT_FAILURE); |
431 |
|
} |
432 |
} |
} |
433 |
|
|
434 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |