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