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 |
***************************************************************************/ |
***************************************************************************/ |
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 "engines/InstrumentEditorFactory.h" |
31 |
#include "engines/gig/Engine.h" |
#include "drivers/audio/AudioOutputDeviceFactory.h" |
32 |
|
#include "drivers/midi/MidiInputDeviceFactory.h" |
33 |
|
#include "drivers/midi/MidiInstrumentMapper.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(1,("SamplerChannel: Loading engine\n")); |
dmsg(2,("SamplerChannel: Assigning engine type...")); |
70 |
|
|
71 |
|
if (pEngineChannel) { |
72 |
|
if (!strcasecmp(pEngineChannel->EngineName().c_str(), EngineType.c_str())) { |
73 |
|
dmsg(2,("OK\n")); |
74 |
|
return; |
75 |
|
} |
76 |
|
} |
77 |
|
|
78 |
// create new engine |
// create new engine channel |
79 |
Engine* pNewEngine = NULL; |
EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType); |
80 |
switch (EngineType) { |
if (!pNewEngineChannel) throw Exception("Unknown engine type"); |
81 |
case engine_type_gig: |
|
82 |
pNewEngine = new gig::Engine; |
//FIXME: hack to allow fast retrieval of engine channel's sampler channel index |
83 |
break; |
pNewEngineChannel->iSamplerChannelIndex = Index(); |
84 |
default: |
|
85 |
throw LinuxSamplerException("Unknown engine type"); |
// dereference midi input port. |
86 |
|
MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort()); |
87 |
|
// disconnect old engine channel |
88 |
|
if (pEngineChannel) { |
89 |
|
Engine* engine = pEngineChannel->GetEngine(); |
90 |
|
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine); |
91 |
|
|
92 |
|
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel); |
93 |
|
if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice(); |
94 |
|
EngineChannelFactory::Destroy(pEngineChannel); |
95 |
|
|
96 |
|
// reconnect engine if it still exists |
97 |
|
const std::set<Engine*>& engines = EngineFactory::EngineInstances(); |
98 |
|
if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine); |
99 |
} |
} |
100 |
|
|
101 |
// disconnect old engine |
// connect new engine channel |
102 |
if (pEngine) { |
if (pAudioOutputDevice) { |
103 |
if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine); |
pNewEngineChannel->Connect(pAudioOutputDevice); |
104 |
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); |
pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine()); |
|
delete pEngine; |
|
105 |
} |
} |
106 |
|
if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, GetMidiInputChannel()); |
107 |
|
pEngineChannel = pNewEngineChannel; |
108 |
|
|
109 |
|
// from now on get MIDI device and port from EngineChannel object |
110 |
|
this->pMidiInputDevice = NULL; |
111 |
|
this->iMidiPort = 0; |
112 |
|
|
113 |
// connect new engine |
pEngineChannel->StatusChanged(true); |
114 |
pEngine = pNewEngine; |
fireEngineChanged(); |
115 |
if (pMidiInputDevice) pMidiInputDevice->Connect(pNewEngine, (MidiInputDevice::midi_chan_t) Index()); |
dmsg(2,("OK\n")); |
|
if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine); |
|
|
dmsg(1,("SamplerChannel: Engine loaded.\n")); |
|
116 |
} |
} |
117 |
|
|
118 |
void SamplerChannel::SetAudioOutputDevice(audio_output_type_t AudioType) { |
void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) { |
119 |
// get / create desired audio device |
if(pAudioOutputDevice == pDevice) return; |
|
AudioOutputDevice* pDevice = pSampler->GetAudioOutputDevice(AudioType); |
|
|
if (!pDevice) pDevice = pSampler->CreateAudioOutputDevice(AudioType); |
|
120 |
|
|
121 |
// disconnect old device |
// disconnect old device |
122 |
if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine); |
if (pAudioOutputDevice && pEngineChannel) { |
123 |
|
Engine* engine = pEngineChannel->GetEngine(); |
124 |
|
pAudioOutputDevice->Disconnect(engine); |
125 |
|
|
126 |
|
pEngineChannel->DisconnectAudioOutputDevice(); |
127 |
|
|
128 |
|
// reconnect engine if it still exists |
129 |
|
const std::set<Engine*>& engines = EngineFactory::EngineInstances(); |
130 |
|
if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine); |
131 |
|
} |
132 |
|
|
133 |
// connect new device |
// connect new device |
134 |
pAudioOutputDevice = pDevice; |
pAudioOutputDevice = pDevice; |
135 |
if (pEngine) pAudioOutputDevice->Connect(pEngine); |
if (pEngineChannel) { |
136 |
|
pEngineChannel->Connect(pAudioOutputDevice); |
137 |
|
pAudioOutputDevice->Connect(pEngineChannel->GetEngine()); |
138 |
|
} |
139 |
} |
} |
140 |
|
|
141 |
void SamplerChannel::SetMidiInputDevice(midi_input_type_t MidiType, MidiInputDevice::midi_chan_t MidiChannel) { |
void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) { |
142 |
// get / create desired midi device |
SetMidiInput(pDevice, 0, GetMidiInputChannel()); |
143 |
MidiInputDevice* pDevice = pSampler->GetMidiInputDevice(MidiType); |
} |
|
if (!pDevice) pDevice = pSampler->CreateMidiInputDevice(MidiType); |
|
144 |
|
|
145 |
// disconnect old device |
void SamplerChannel::SetMidiInputPort(int MidiPort) { |
146 |
if (pMidiInputDevice && pEngine) pMidiInputDevice->Disconnect(pEngine); |
SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel()); |
147 |
|
} |
148 |
|
|
149 |
// connect new device |
void SamplerChannel::SetMidiInputChannel(midi_chan_t MidiChannel) { |
150 |
pMidiInputDevice = pDevice; |
SetMidiInput(GetMidiInputDevice(), GetMidiInputPort(), MidiChannel); |
|
if (pEngine) pMidiInputDevice->Connect(pEngine, MidiChannel); |
|
151 |
} |
} |
152 |
|
|
153 |
Engine* SamplerChannel::GetEngine() { |
void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) { |
154 |
return pEngine; |
if (!pDevice) throw Exception("No MIDI input device assigned."); |
155 |
|
|
156 |
|
// get old and new midi input port |
157 |
|
MidiInputPort* pOldMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort()); |
158 |
|
MidiInputPort* pNewMidiInputPort = pDevice->GetPort(iMidiPort); |
159 |
|
|
160 |
|
// disconnect old device port |
161 |
|
if (pOldMidiInputPort && pEngineChannel) pOldMidiInputPort->Disconnect(pEngineChannel); |
162 |
|
// remember new device, port and channel if not engine channel yet created |
163 |
|
if (!pEngineChannel) { |
164 |
|
this->pMidiInputDevice = pDevice; |
165 |
|
this->iMidiPort = iMidiPort; |
166 |
|
this->midiChannel = MidiChannel; |
167 |
|
} |
168 |
|
|
169 |
|
// connect new device port |
170 |
|
if (pNewMidiInputPort && pEngineChannel) pNewMidiInputPort->Connect(pEngineChannel, MidiChannel); |
171 |
|
// Ooops. |
172 |
|
if (pNewMidiInputPort == NULL) |
173 |
|
throw Exception("There is no MIDI input port with index " + ToString(iMidiPort) + "."); |
174 |
} |
} |
175 |
|
|
176 |
MidiInputDevice* SamplerChannel::GetMidiInputDevice() { |
EngineChannel* SamplerChannel::GetEngineChannel() { |
177 |
return pMidiInputDevice; |
return pEngineChannel; |
178 |
|
} |
179 |
|
|
180 |
|
midi_chan_t SamplerChannel::GetMidiInputChannel() { |
181 |
|
if (pEngineChannel) this->midiChannel = pEngineChannel->MidiChannel(); |
182 |
|
return this->midiChannel; |
183 |
|
} |
184 |
|
|
185 |
|
int SamplerChannel::GetMidiInputPort() { |
186 |
|
MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : NULL; |
187 |
|
if (pMidiInputPort) this->iMidiPort = (int) pMidiInputPort->GetPortNumber(); |
188 |
|
return iMidiPort; |
189 |
} |
} |
190 |
|
|
191 |
AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() { |
AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() { |
192 |
return pAudioOutputDevice; |
return pAudioOutputDevice; |
193 |
} |
} |
194 |
|
|
195 |
|
MidiInputDevice* SamplerChannel::GetMidiInputDevice() { |
196 |
|
if (pEngineChannel) |
197 |
|
pMidiInputDevice = (pEngineChannel->GetMidiInputPort()) ? pEngineChannel->GetMidiInputPort()->GetDevice() : NULL; |
198 |
|
return pMidiInputDevice; |
199 |
|
} |
200 |
|
|
201 |
uint SamplerChannel::Index() { |
uint SamplerChannel::Index() { |
202 |
if (iIndex >= 0) return iIndex; |
if (iIndex >= 0) return iIndex; |
203 |
|
|
204 |
std::vector<SamplerChannel*>::iterator iter = pSampler->vSamplerChannels.begin(); |
Sampler::SamplerChannelMap::iterator iter = pSampler->mSamplerChannels.begin(); |
205 |
for (int i = 0; iter != pSampler->vSamplerChannels.end(); i++, iter++) { |
for (; iter != pSampler->mSamplerChannels.end(); iter++) { |
206 |
if (*iter == this) { |
if (iter->second == this) { |
207 |
iIndex = i; |
iIndex = iter->first; |
208 |
return i; |
return iIndex; |
209 |
} |
} |
210 |
} |
} |
211 |
|
|
212 |
throw LinuxSamplerException("SamplerChannel index not found"); |
throw Exception("Internal error: SamplerChannel index not found"); |
213 |
|
} |
214 |
|
|
215 |
|
void SamplerChannel::AddEngineChangeListener(EngineChangeListener* l) { |
216 |
|
llEngineChangeListeners.AddListener(l); |
217 |
|
} |
218 |
|
|
219 |
|
void SamplerChannel::RemoveEngineChangeListener(EngineChangeListener* l) { |
220 |
|
llEngineChangeListeners.RemoveListener(l); |
221 |
|
} |
222 |
|
|
223 |
|
void SamplerChannel::RemoveAllEngineChangeListeners() { |
224 |
|
llEngineChangeListeners.RemoveAllListeners(); |
225 |
|
} |
226 |
|
|
227 |
|
void SamplerChannel::fireEngineChanged() { |
228 |
|
for (int i = 0; i < llEngineChangeListeners.GetListenerCount(); i++) { |
229 |
|
llEngineChangeListeners.GetListener(i)->EngineChanged(Index()); |
230 |
|
} |
231 |
|
} |
232 |
|
|
233 |
|
MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) { |
234 |
|
MidiInputPort* pMidiInputPort = NULL; |
235 |
|
MidiInputDevice* pMidiInputDevice = GetMidiInputDevice(); |
236 |
|
if (pMidiInputDevice) |
237 |
|
pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort); |
238 |
|
return pMidiInputPort; |
239 |
} |
} |
240 |
|
|
241 |
|
|
242 |
|
|
243 |
// ****************************************************************** |
// ****************************************************************** |
244 |
// * Sampler |
// * Sampler |
245 |
|
|
246 |
Sampler::Sampler() { |
Sampler::Sampler() { |
247 |
|
eventHandler.SetSampler(this); |
248 |
} |
} |
249 |
|
|
250 |
Sampler::~Sampler() { |
Sampler::~Sampler() { |
251 |
// delete sampler channels |
Reset(); |
252 |
{ |
} |
253 |
std::vector<SamplerChannel*>::iterator iter = vSamplerChannels.begin(); |
|
254 |
for (; iter != vSamplerChannels.end(); iter++) delete *iter; |
uint Sampler::SamplerChannels() { |
255 |
|
return mSamplerChannels.size(); |
256 |
|
} |
257 |
|
|
258 |
|
void Sampler::AddChannelCountListener(ChannelCountListener* l) { |
259 |
|
llChannelCountListeners.AddListener(l); |
260 |
|
} |
261 |
|
|
262 |
|
void Sampler::RemoveChannelCountListener(ChannelCountListener* l) { |
263 |
|
llChannelCountListeners.RemoveListener(l); |
264 |
|
} |
265 |
|
|
266 |
|
void Sampler::fireChannelCountChanged(int NewCount) { |
267 |
|
for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) { |
268 |
|
llChannelCountListeners.GetListener(i)->ChannelCountChanged(NewCount); |
269 |
} |
} |
270 |
|
} |
271 |
|
|
272 |
// delete midi input devices |
void Sampler::AddAudioDeviceCountListener(AudioDeviceCountListener* l) { |
273 |
{ |
llAudioDeviceCountListeners.AddListener(l); |
274 |
MidiInputDeviceMap::iterator iter = MidiInputDevices.begin(); |
} |
275 |
for (; iter != MidiInputDevices.end(); iter++) { |
|
276 |
MidiInputDevice* pDevice = iter->second; |
void Sampler::RemoveAudioDeviceCountListener(AudioDeviceCountListener* l) { |
277 |
pDevice->StopListen(); |
llAudioDeviceCountListeners.RemoveListener(l); |
278 |
delete pDevice; |
} |
279 |
} |
|
280 |
|
void Sampler::fireAudioDeviceCountChanged(int NewCount) { |
281 |
|
for (int i = 0; i < llAudioDeviceCountListeners.GetListenerCount(); i++) { |
282 |
|
llAudioDeviceCountListeners.GetListener(i)->AudioDeviceCountChanged(NewCount); |
283 |
} |
} |
284 |
|
} |
285 |
|
|
286 |
// delete audio output devices |
void Sampler::AddMidiDeviceCountListener(MidiDeviceCountListener* l) { |
287 |
{ |
llMidiDeviceCountListeners.AddListener(l); |
288 |
AudioOutputDeviceMap::iterator iter = AudioOutputDevices.begin(); |
} |
289 |
for (; iter != AudioOutputDevices.end(); iter++) { |
|
290 |
AudioOutputDevice* pDevice = iter->second; |
void Sampler::RemoveMidiDeviceCountListener(MidiDeviceCountListener* l) { |
291 |
pDevice->Stop(); |
llMidiDeviceCountListeners.RemoveListener(l); |
292 |
delete pDevice; |
} |
293 |
} |
|
294 |
|
void Sampler::fireMidiDeviceCountChanged(int NewCount) { |
295 |
|
for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) { |
296 |
|
llMidiDeviceCountListeners.GetListener(i)->MidiDeviceCountChanged(NewCount); |
297 |
} |
} |
298 |
} |
} |
299 |
|
|
300 |
uint Sampler::SamplerChannels() { |
void Sampler::AddVoiceCountListener(VoiceCountListener* l) { |
301 |
vSamplerChannels.size(); |
llVoiceCountListeners.AddListener(l); |
302 |
|
} |
303 |
|
|
304 |
|
void Sampler::RemoveVoiceCountListener(VoiceCountListener* l) { |
305 |
|
llVoiceCountListeners.RemoveListener(l); |
306 |
|
} |
307 |
|
|
308 |
|
void Sampler::fireVoiceCountChanged(int ChannelId, int NewCount) { |
309 |
|
for (int i = 0; i < llVoiceCountListeners.GetListenerCount(); i++) { |
310 |
|
llVoiceCountListeners.GetListener(i)->VoiceCountChanged(ChannelId, NewCount); |
311 |
|
} |
312 |
|
} |
313 |
|
|
314 |
|
void Sampler::AddStreamCountListener(StreamCountListener* l) { |
315 |
|
llStreamCountListeners.AddListener(l); |
316 |
} |
} |
317 |
|
|
318 |
|
void Sampler::RemoveStreamCountListener(StreamCountListener* l) { |
319 |
|
llStreamCountListeners.RemoveListener(l); |
320 |
|
} |
321 |
|
|
322 |
|
void Sampler::fireStreamCountChanged(int ChannelId, int NewCount) { |
323 |
|
for (int i = 0; i < llStreamCountListeners.GetListenerCount(); i++) { |
324 |
|
llStreamCountListeners.GetListener(i)->StreamCountChanged(ChannelId, NewCount); |
325 |
|
} |
326 |
|
} |
327 |
|
|
328 |
|
void Sampler::AddBufferFillListener(BufferFillListener* l) { |
329 |
|
llBufferFillListeners.AddListener(l); |
330 |
|
} |
331 |
|
|
332 |
|
void Sampler::RemoveBufferFillListener(BufferFillListener* l) { |
333 |
|
llBufferFillListeners.RemoveListener(l); |
334 |
|
} |
335 |
|
|
336 |
|
void Sampler::fireBufferFillChanged(int ChannelId, String FillData) { |
337 |
|
for (int i = 0; i < llBufferFillListeners.GetListenerCount(); i++) { |
338 |
|
llBufferFillListeners.GetListener(i)->BufferFillChanged(ChannelId, FillData); |
339 |
|
} |
340 |
|
} |
341 |
|
|
342 |
|
void Sampler::AddTotalVoiceCountListener(TotalVoiceCountListener* l) { |
343 |
|
llTotalVoiceCountListeners.AddListener(l); |
344 |
|
} |
345 |
|
|
346 |
|
void Sampler::RemoveTotalVoiceCountListener(TotalVoiceCountListener* l) { |
347 |
|
llTotalVoiceCountListeners.RemoveListener(l); |
348 |
|
} |
349 |
|
|
350 |
|
void Sampler::fireTotalVoiceCountChanged(int NewCount) { |
351 |
|
for (int i = 0; i < llTotalVoiceCountListeners.GetListenerCount(); i++) { |
352 |
|
llTotalVoiceCountListeners.GetListener(i)->TotalVoiceCountChanged(NewCount); |
353 |
|
} |
354 |
|
} |
355 |
|
|
356 |
|
void Sampler::AddFxSendCountListener(FxSendCountListener* l) { |
357 |
|
llFxSendCountListeners.AddListener(l); |
358 |
|
} |
359 |
|
|
360 |
|
void Sampler::RemoveFxSendCountListener(FxSendCountListener* l) { |
361 |
|
llFxSendCountListeners.RemoveListener(l); |
362 |
|
} |
363 |
|
|
364 |
|
void Sampler::fireFxSendCountChanged(int ChannelId, int NewCount) { |
365 |
|
for (int i = 0; i < llFxSendCountListeners.GetListenerCount(); i++) { |
366 |
|
llFxSendCountListeners.GetListener(i)->FxSendCountChanged(ChannelId, NewCount); |
367 |
|
} |
368 |
|
} |
369 |
|
|
370 |
|
void Sampler::EventHandler::EngineChanged(int ChannelId) { |
371 |
|
EngineChannel* engineChannel = pSampler->GetSamplerChannel(ChannelId)->GetEngineChannel(); |
372 |
|
if(engineChannel == NULL) return; |
373 |
|
engineChannel->AddFxSendCountListener(this); |
374 |
|
} |
375 |
|
|
376 |
|
void Sampler::EventHandler::FxSendCountChanged(int ChannelId, int NewCount) { |
377 |
|
pSampler->fireFxSendCountChanged(ChannelId, NewCount); |
378 |
|
} |
379 |
|
|
380 |
|
|
381 |
SamplerChannel* Sampler::AddSamplerChannel() { |
SamplerChannel* Sampler::AddSamplerChannel() { |
382 |
|
// if there's no sampler channel yet |
383 |
|
if (!mSamplerChannels.size()) { |
384 |
|
SamplerChannel* pChannel = new SamplerChannel(this); |
385 |
|
mSamplerChannels[0] = pChannel; |
386 |
|
fireChannelCountChanged(1); |
387 |
|
pChannel->AddEngineChangeListener(&eventHandler); |
388 |
|
return pChannel; |
389 |
|
} |
390 |
|
|
391 |
|
// get the highest used sampler channel index |
392 |
|
uint lastIndex = (--(mSamplerChannels.end()))->first; |
393 |
|
|
394 |
|
// check if we reached the index limit |
395 |
|
if (lastIndex + 1 < lastIndex) { |
396 |
|
// search for an unoccupied sampler channel index starting from 0 |
397 |
|
for (uint i = 0; i < lastIndex; i++) { |
398 |
|
if (mSamplerChannels.find(i) != mSamplerChannels.end()) continue; |
399 |
|
// we found an unused index, so insert the new channel there |
400 |
|
SamplerChannel* pChannel = new SamplerChannel(this); |
401 |
|
mSamplerChannels[i] = pChannel; |
402 |
|
fireChannelCountChanged(SamplerChannels()); |
403 |
|
pChannel->AddEngineChangeListener(&eventHandler); |
404 |
|
return pChannel; |
405 |
|
} |
406 |
|
throw Exception("Internal error: could not find unoccupied sampler channel index."); |
407 |
|
} |
408 |
|
|
409 |
|
// we have not reached the index limit so we just add the channel past the highest index |
410 |
SamplerChannel* pChannel = new SamplerChannel(this); |
SamplerChannel* pChannel = new SamplerChannel(this); |
411 |
vSamplerChannels.push_back(pChannel); |
mSamplerChannels[lastIndex + 1] = pChannel; |
412 |
|
fireChannelCountChanged(SamplerChannels()); |
413 |
|
pChannel->AddEngineChangeListener(&eventHandler); |
414 |
return pChannel; |
return pChannel; |
415 |
} |
} |
416 |
|
|
417 |
SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) { |
SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) { |
418 |
if (uiSamplerChannel >= SamplerChannels()) return NULL; |
return (mSamplerChannels.find(uiSamplerChannel) != mSamplerChannels.end()) ? mSamplerChannels[uiSamplerChannel] : NULL; |
419 |
return vSamplerChannels[uiSamplerChannel]; |
} |
420 |
|
|
421 |
|
std::map<uint, SamplerChannel*> Sampler::GetSamplerChannels() { |
422 |
|
return mSamplerChannels; |
423 |
} |
} |
424 |
|
|
425 |
void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) { |
void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) { |
426 |
std::vector<SamplerChannel*>::iterator iterChan = vSamplerChannels.begin(); |
SamplerChannelMap::iterator iterChan = mSamplerChannels.begin(); |
427 |
for (; iterChan != vSamplerChannels.end(); iterChan++) { |
for (; iterChan != mSamplerChannels.end(); iterChan++) { |
428 |
if (*iterChan == pSamplerChannel) { |
if (iterChan->second == pSamplerChannel) { |
429 |
vSamplerChannels.erase(iterChan); |
pSamplerChannel->RemoveAllEngineChangeListeners(); |
430 |
|
mSamplerChannels.erase(iterChan); |
431 |
delete pSamplerChannel; |
delete pSamplerChannel; |
432 |
|
fireChannelCountChanged(SamplerChannels()); |
433 |
return; |
return; |
434 |
} |
} |
435 |
} |
} |
441 |
RemoveSamplerChannel(pChannel); |
RemoveSamplerChannel(pChannel); |
442 |
} |
} |
443 |
|
|
444 |
AudioOutputDevice* Sampler::CreateAudioOutputDevice(audio_output_type_t AudioType) { |
std::vector<String> Sampler::AvailableAudioOutputDrivers() { |
445 |
// check if device already created |
return AudioOutputDeviceFactory::AvailableDrivers(); |
446 |
AudioOutputDevice* pDevice = GetAudioOutputDevice(AudioType); |
} |
447 |
if (pDevice) return pDevice; |
|
448 |
|
std::vector<String> Sampler::AvailableMidiInputDrivers() { |
449 |
|
return MidiInputDeviceFactory::AvailableDrivers(); |
450 |
|
} |
451 |
|
|
452 |
|
std::vector<String> Sampler::AvailableEngineTypes() { |
453 |
|
return EngineFactory::AvailableEngineTypes(); |
454 |
|
} |
455 |
|
|
456 |
|
AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map<String,String> Parameters) throw (Exception) { |
457 |
// create new device |
// create new device |
458 |
switch (AudioType) { |
AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters); |
459 |
case audio_output_type_alsa: |
|
460 |
pDevice = new AudioOutputDeviceAlsa; |
// add new audio device to the audio device list |
461 |
break; |
for (uint i = 0; ; i++) { // seek for a free place starting from the beginning |
462 |
case audio_output_type_jack: |
if (!mAudioOutputDevices[i]) { |
463 |
pDevice = new AudioOutputDeviceJack; |
mAudioOutputDevices[i] = pDevice; |
464 |
break; |
break; |
465 |
default: |
} |
|
throw LinuxSamplerException("Unknown audio output device type"); |
|
466 |
} |
} |
467 |
|
|
468 |
// activate device |
fireAudioDeviceCountChanged(AudioOutputDevices()); |
|
pDevice->Play(); |
|
|
|
|
469 |
return pDevice; |
return pDevice; |
470 |
} |
} |
471 |
|
|
472 |
AudioOutputDevice* Sampler::GetAudioOutputDevice(audio_output_type_t AudioType) { |
uint Sampler::AudioOutputDevices() { |
473 |
AudioOutputDeviceMap::iterator iter = AudioOutputDevices.find(AudioType); |
return mAudioOutputDevices.size(); |
|
return (iter != AudioOutputDevices.end()) ? iter->second : NULL; |
|
474 |
} |
} |
475 |
|
|
476 |
MidiInputDevice* Sampler::CreateMidiInputDevice(midi_input_type_t MidiType) { |
uint Sampler::MidiInputDevices() { |
477 |
// check if device already created |
return mMidiInputDevices.size(); |
478 |
MidiInputDevice* pDevice = GetMidiInputDevice(MidiType); |
} |
|
if (pDevice) return pDevice; |
|
479 |
|
|
480 |
// create new device |
std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() { |
481 |
switch (MidiType) { |
return mAudioOutputDevices; |
482 |
case midi_input_type_alsa: |
} |
483 |
pDevice = new MidiInputDeviceAlsa; |
|
484 |
|
std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() { |
485 |
|
return mMidiInputDevices; |
486 |
|
} |
487 |
|
|
488 |
|
void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) { |
489 |
|
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
490 |
|
for (; iter != mAudioOutputDevices.end(); iter++) { |
491 |
|
if (iter->second == pDevice) { |
492 |
|
// check if there are still sampler engines connected to this device |
493 |
|
for (uint i = 0; i < SamplerChannels(); i++) |
494 |
|
if (GetSamplerChannel(i)->GetAudioOutputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the audio output device."); |
495 |
|
|
496 |
|
// disable device |
497 |
|
pDevice->Stop(); |
498 |
|
|
499 |
|
// remove device from the device list |
500 |
|
mAudioOutputDevices.erase(iter); |
501 |
|
|
502 |
|
// destroy and free device from memory |
503 |
|
delete pDevice; |
504 |
|
|
505 |
|
fireAudioDeviceCountChanged(AudioOutputDevices()); |
506 |
|
break; |
507 |
|
} |
508 |
|
} |
509 |
|
} |
510 |
|
|
511 |
|
void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) { |
512 |
|
MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); |
513 |
|
for (; iter != mMidiInputDevices.end(); iter++) { |
514 |
|
if (iter->second == pDevice) { |
515 |
|
// check if there are still sampler engines connected to this device |
516 |
|
for (uint i = 0; i < SamplerChannels(); i++) |
517 |
|
if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the midi input device."); |
518 |
|
|
519 |
|
// disable device |
520 |
|
pDevice->StopListen(); |
521 |
|
|
522 |
|
// remove device from the device list |
523 |
|
mMidiInputDevices.erase(iter); |
524 |
|
|
525 |
|
// destroy and free device from memory |
526 |
|
delete pDevice; |
527 |
|
|
528 |
|
fireMidiDeviceCountChanged(MidiInputDevices()); |
529 |
break; |
break; |
530 |
default: |
} |
|
throw LinuxSamplerException("Unknown audio output device type"); |
|
531 |
} |
} |
532 |
|
} |
533 |
|
|
534 |
|
MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (Exception) { |
535 |
|
// create new device |
536 |
|
MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this); |
537 |
|
|
538 |
// activate device |
// add new device to the midi device list |
539 |
pDevice->Listen(); |
for (uint i = 0; ; i++) { // seek for a free place starting from the beginning |
540 |
|
if (!mMidiInputDevices[i]) { |
541 |
|
mMidiInputDevices[i] = pDevice; |
542 |
|
break; |
543 |
|
} |
544 |
|
} |
545 |
|
|
546 |
|
fireMidiDeviceCountChanged(MidiInputDevices()); |
547 |
return pDevice; |
return pDevice; |
548 |
} |
} |
549 |
|
|
550 |
MidiInputDevice* Sampler::GetMidiInputDevice(midi_input_type_t MidiType) { |
int Sampler::GetVoiceCount() { |
551 |
MidiInputDeviceMap::iterator iter = MidiInputDevices.find(MidiType); |
int count = 0; |
552 |
return (iter != MidiInputDevices.end()) ? iter->second : NULL; |
std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin(); |
553 |
|
|
554 |
|
for(; it != EngineFactory::EngineInstances().end(); it++) { |
555 |
|
count += (*it)->VoiceCount(); |
556 |
|
} |
557 |
|
|
558 |
|
return count; |
559 |
|
} |
560 |
|
|
561 |
|
void Sampler::Reset() { |
562 |
|
// delete sampler channels |
563 |
|
try { |
564 |
|
while (true) { |
565 |
|
SamplerChannelMap::iterator iter = mSamplerChannels.begin(); |
566 |
|
if (iter == mSamplerChannels.end()) break; |
567 |
|
RemoveSamplerChannel(iter->second); |
568 |
|
} |
569 |
|
} |
570 |
|
catch(...) { |
571 |
|
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all sampler channels, exiting.\n" << std::flush; |
572 |
|
exit(EXIT_FAILURE); |
573 |
|
} |
574 |
|
|
575 |
|
// delete midi input devices |
576 |
|
try { |
577 |
|
while (true) { |
578 |
|
MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); |
579 |
|
if (iter == mMidiInputDevices.end()) break; |
580 |
|
DestroyMidiInputDevice(iter->second); |
581 |
|
} |
582 |
|
} |
583 |
|
catch(...) { |
584 |
|
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI input devices, exiting.\n" << std::flush; |
585 |
|
exit(EXIT_FAILURE); |
586 |
|
} |
587 |
|
|
588 |
|
// delete audio output devices |
589 |
|
try { |
590 |
|
while (true) { |
591 |
|
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
592 |
|
if (iter == mAudioOutputDevices.end()) break; |
593 |
|
DestroyAudioOutputDevice(iter->second); |
594 |
|
} |
595 |
|
} |
596 |
|
catch(...) { |
597 |
|
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all audio output devices, exiting.\n" << std::flush; |
598 |
|
exit(EXIT_FAILURE); |
599 |
|
} |
600 |
|
|
601 |
|
// delete MIDI instrument maps |
602 |
|
try { |
603 |
|
MidiInstrumentMapper::RemoveAllMaps(); |
604 |
|
} |
605 |
|
catch(...) { |
606 |
|
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI instrument maps, exiting.\n" << std::flush; |
607 |
|
exit(EXIT_FAILURE); |
608 |
|
} |
609 |
|
|
610 |
|
// unload all instrument editor DLLs |
611 |
|
InstrumentEditorFactory::ClosePlugins(); |
612 |
} |
} |
613 |
|
|
614 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |