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