3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 - 2007 Christian Schoenebeck * |
* Copyright (C) 2005 - 2008 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This library 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 * |
25 |
|
|
26 |
#include "Sampler.h" |
#include "Sampler.h" |
27 |
|
|
28 |
|
#include "common/global_private.h" |
29 |
#include "engines/EngineFactory.h" |
#include "engines/EngineFactory.h" |
30 |
#include "engines/EngineChannelFactory.h" |
#include "engines/EngineChannelFactory.h" |
31 |
|
#include "plugins/InstrumentEditorFactory.h" |
32 |
#include "drivers/audio/AudioOutputDeviceFactory.h" |
#include "drivers/audio/AudioOutputDeviceFactory.h" |
33 |
#include "drivers/midi/MidiInputDeviceFactory.h" |
#include "drivers/midi/MidiInputDeviceFactory.h" |
34 |
#include "drivers/midi/MidiInstrumentMapper.h" |
#include "drivers/midi/MidiInstrumentMapper.h" |
|
#include "network/lscpserver.h" |
|
35 |
|
|
36 |
namespace LinuxSampler { |
namespace LinuxSampler { |
37 |
|
|
68 |
|
|
69 |
void SamplerChannel::SetEngineType(String EngineType) throw (Exception) { |
void SamplerChannel::SetEngineType(String EngineType) throw (Exception) { |
70 |
dmsg(2,("SamplerChannel: Assigning engine type...")); |
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 |
|
fireEngineToBeChanged(); |
80 |
|
|
81 |
// create new engine channel |
// create new engine channel |
82 |
EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType); |
EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType); |
114 |
this->iMidiPort = 0; |
this->iMidiPort = 0; |
115 |
|
|
116 |
pEngineChannel->StatusChanged(true); |
pEngineChannel->StatusChanged(true); |
117 |
|
fireEngineChanged(); |
118 |
dmsg(2,("OK\n")); |
dmsg(2,("OK\n")); |
119 |
} |
} |
120 |
|
|
215 |
throw Exception("Internal error: 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 |
MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) { |
MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) { |
243 |
MidiInputPort* pMidiInputPort = NULL; |
MidiInputPort* pMidiInputPort = NULL; |
244 |
MidiInputDevice* pMidiInputDevice = GetMidiInputDevice(); |
MidiInputDevice* pMidiInputDevice = GetMidiInputDevice(); |
253 |
// * Sampler |
// * Sampler |
254 |
|
|
255 |
Sampler::Sampler() { |
Sampler::Sampler() { |
256 |
|
eventHandler.SetSampler(this); |
257 |
} |
} |
258 |
|
|
259 |
Sampler::~Sampler() { |
Sampler::~Sampler() { |
264 |
return mSamplerChannels.size(); |
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 |
|
void Sampler::fireChannelAdded(SamplerChannel* pChannel) { |
282 |
|
for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) { |
283 |
|
llChannelCountListeners.GetListener(i)->ChannelAdded(pChannel); |
284 |
|
} |
285 |
|
} |
286 |
|
|
287 |
|
void Sampler::fireChannelToBeRemoved(SamplerChannel* pChannel) { |
288 |
|
for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) { |
289 |
|
llChannelCountListeners.GetListener(i)->ChannelToBeRemoved(pChannel); |
290 |
|
} |
291 |
|
} |
292 |
|
|
293 |
|
void Sampler::AddAudioDeviceCountListener(AudioDeviceCountListener* l) { |
294 |
|
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 |
// if there's no sampler channel yet |
422 |
if (!mSamplerChannels.size()) { |
if (!mSamplerChannels.size()) { |
423 |
SamplerChannel* pChannel = new SamplerChannel(this); |
SamplerChannel* pChannel = new SamplerChannel(this); |
424 |
mSamplerChannels[0] = pChannel; |
mSamplerChannels[0] = pChannel; |
425 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, 1)); |
fireChannelAdded(pChannel); |
426 |
|
fireChannelCountChanged(1); |
427 |
|
pChannel->AddEngineChangeListener(&eventHandler); |
428 |
return pChannel; |
return pChannel; |
429 |
} |
} |
430 |
|
|
439 |
// we found an unused index, so insert the new channel there |
// we found an unused index, so insert the new channel there |
440 |
SamplerChannel* pChannel = new SamplerChannel(this); |
SamplerChannel* pChannel = new SamplerChannel(this); |
441 |
mSamplerChannels[i] = pChannel; |
mSamplerChannels[i] = pChannel; |
442 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, i)); |
fireChannelAdded(pChannel); |
443 |
|
fireChannelCountChanged(SamplerChannels()); |
444 |
|
pChannel->AddEngineChangeListener(&eventHandler); |
445 |
return pChannel; |
return pChannel; |
446 |
} |
} |
447 |
throw Exception("Internal error: could not find unoccupied sampler channel index."); |
throw Exception("Internal error: could not find unoccupied sampler channel index."); |
450 |
// we have not reached the index limit so we just add the channel past the highest index |
// 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 |
mSamplerChannels[lastIndex + 1] = pChannel; |
mSamplerChannels[lastIndex + 1] = pChannel; |
453 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, lastIndex + 1)); |
fireChannelAdded(pChannel); |
454 |
|
fireChannelCountChanged(SamplerChannels()); |
455 |
|
pChannel->AddEngineChangeListener(&eventHandler); |
456 |
return pChannel; |
return pChannel; |
457 |
} |
} |
458 |
|
|
468 |
SamplerChannelMap::iterator iterChan = mSamplerChannels.begin(); |
SamplerChannelMap::iterator iterChan = mSamplerChannels.begin(); |
469 |
for (; iterChan != mSamplerChannels.end(); iterChan++) { |
for (; iterChan != mSamplerChannels.end(); iterChan++) { |
470 |
if (iterChan->second == pSamplerChannel) { |
if (iterChan->second == pSamplerChannel) { |
471 |
|
fireChannelToBeRemoved(pSamplerChannel); |
472 |
|
pSamplerChannel->RemoveAllEngineChangeListeners(); |
473 |
mSamplerChannels.erase(iterChan); |
mSamplerChannels.erase(iterChan); |
474 |
delete pSamplerChannel; |
delete pSamplerChannel; |
475 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, mSamplerChannels.size())); |
fireChannelCountChanged(SamplerChannels()); |
476 |
return; |
return; |
477 |
} |
} |
478 |
} |
} |
508 |
} |
} |
509 |
} |
} |
510 |
|
|
511 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, AudioOutputDevices())); |
fireAudioDeviceCountChanged(AudioOutputDevices()); |
512 |
return pDevice; |
return pDevice; |
513 |
} |
} |
514 |
|
|
545 |
// destroy and free device from memory |
// destroy and free device from memory |
546 |
delete pDevice; |
delete pDevice; |
547 |
|
|
548 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, AudioOutputDevices())); |
fireAudioDeviceCountChanged(AudioOutputDevices()); |
549 |
break; |
break; |
550 |
} |
} |
551 |
} |
} |
568 |
// destroy and free device from memory |
// destroy and free device from memory |
569 |
delete pDevice; |
delete pDevice; |
570 |
|
|
571 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, MidiInputDevices())); |
fireMidiDeviceCountChanged(MidiInputDevices()); |
572 |
break; |
break; |
573 |
} |
} |
574 |
} |
} |
586 |
} |
} |
587 |
} |
} |
588 |
|
|
589 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, MidiInputDevices())); |
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() { |
int Sampler::GetVoiceCount() { |
605 |
int count = 0; |
int count = 0; |
606 |
std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin(); |
std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin(); |
660 |
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI instrument maps, exiting.\n" << std::flush; |
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI instrument maps, exiting.\n" << std::flush; |
661 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
662 |
} |
} |
663 |
|
|
664 |
|
// unload all instrument editor DLLs |
665 |
|
InstrumentEditorFactory::ClosePlugins(); |
666 |
} |
} |
667 |
|
|
668 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |