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