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