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" |
35 |
#include "network/lscpserver.h" |
#include "common/Features.h" |
36 |
|
|
37 |
namespace LinuxSampler { |
namespace LinuxSampler { |
38 |
|
|
69 |
|
|
70 |
void SamplerChannel::SetEngineType(String EngineType) throw (Exception) { |
void SamplerChannel::SetEngineType(String EngineType) throw (Exception) { |
71 |
dmsg(2,("SamplerChannel: Assigning engine type...")); |
dmsg(2,("SamplerChannel: Assigning engine type...")); |
72 |
|
|
73 |
|
if (pEngineChannel) { |
74 |
|
if (!strcasecmp(pEngineChannel->EngineName().c_str(), EngineType.c_str())) { |
75 |
|
dmsg(2,("OK\n")); |
76 |
|
return; |
77 |
|
} |
78 |
|
} |
79 |
|
|
80 |
|
fireEngineToBeChanged(); |
81 |
|
|
82 |
// create new engine channel |
// create new engine channel |
83 |
EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType); |
EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType); |
115 |
this->iMidiPort = 0; |
this->iMidiPort = 0; |
116 |
|
|
117 |
pEngineChannel->StatusChanged(true); |
pEngineChannel->StatusChanged(true); |
118 |
|
fireEngineChanged(); |
119 |
dmsg(2,("OK\n")); |
dmsg(2,("OK\n")); |
120 |
} |
} |
121 |
|
|
216 |
throw Exception("Internal error: SamplerChannel index not found"); |
throw Exception("Internal error: SamplerChannel index not found"); |
217 |
} |
} |
218 |
|
|
219 |
|
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 |
|
void SamplerChannel::fireEngineToBeChanged() { |
232 |
|
for (int i = 0; i < llEngineChangeListeners.GetListenerCount(); i++) { |
233 |
|
llEngineChangeListeners.GetListener(i)->EngineToBeChanged(Index()); |
234 |
|
} |
235 |
|
} |
236 |
|
|
237 |
|
void SamplerChannel::fireEngineChanged() { |
238 |
|
for (int i = 0; i < llEngineChangeListeners.GetListenerCount(); i++) { |
239 |
|
llEngineChangeListeners.GetListener(i)->EngineChanged(Index()); |
240 |
|
} |
241 |
|
} |
242 |
|
|
243 |
MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) { |
MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) { |
244 |
MidiInputPort* pMidiInputPort = NULL; |
MidiInputPort* pMidiInputPort = NULL; |
245 |
MidiInputDevice* pMidiInputDevice = GetMidiInputDevice(); |
MidiInputDevice* pMidiInputDevice = GetMidiInputDevice(); |
254 |
// * Sampler |
// * Sampler |
255 |
|
|
256 |
Sampler::Sampler() { |
Sampler::Sampler() { |
257 |
|
eventHandler.SetSampler(this); |
258 |
} |
} |
259 |
|
|
260 |
Sampler::~Sampler() { |
Sampler::~Sampler() { |
265 |
return mSamplerChannels.size(); |
return mSamplerChannels.size(); |
266 |
} |
} |
267 |
|
|
268 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
void Sampler::EventHandler::EngineToBeChanged(int ChannelId) { |
419 |
|
// nothing to do here |
420 |
|
} |
421 |
|
|
422 |
|
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 |
SamplerChannel* Sampler::AddSamplerChannel() { |
SamplerChannel* Sampler::AddSamplerChannel() { |
434 |
// if there's no sampler channel yet |
// if there's no sampler channel yet |
435 |
if (!mSamplerChannels.size()) { |
if (!mSamplerChannels.size()) { |
436 |
SamplerChannel* pChannel = new SamplerChannel(this); |
SamplerChannel* pChannel = new SamplerChannel(this); |
437 |
mSamplerChannels[0] = pChannel; |
mSamplerChannels[0] = pChannel; |
438 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, 1)); |
fireChannelAdded(pChannel); |
439 |
|
fireChannelCountChanged(1); |
440 |
|
pChannel->AddEngineChangeListener(&eventHandler); |
441 |
return pChannel; |
return pChannel; |
442 |
} |
} |
443 |
|
|
452 |
// we found an unused index, so insert the new channel there |
// we found an unused index, so insert the new channel there |
453 |
SamplerChannel* pChannel = new SamplerChannel(this); |
SamplerChannel* pChannel = new SamplerChannel(this); |
454 |
mSamplerChannels[i] = pChannel; |
mSamplerChannels[i] = pChannel; |
455 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, i)); |
fireChannelAdded(pChannel); |
456 |
|
fireChannelCountChanged(SamplerChannels()); |
457 |
|
pChannel->AddEngineChangeListener(&eventHandler); |
458 |
return pChannel; |
return pChannel; |
459 |
} |
} |
460 |
throw Exception("Internal error: could not find unoccupied sampler channel index."); |
throw Exception("Internal error: could not find unoccupied sampler channel index."); |
463 |
// 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 |
464 |
SamplerChannel* pChannel = new SamplerChannel(this); |
SamplerChannel* pChannel = new SamplerChannel(this); |
465 |
mSamplerChannels[lastIndex + 1] = pChannel; |
mSamplerChannels[lastIndex + 1] = pChannel; |
466 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, lastIndex + 1)); |
fireChannelAdded(pChannel); |
467 |
|
fireChannelCountChanged(SamplerChannels()); |
468 |
|
pChannel->AddEngineChangeListener(&eventHandler); |
469 |
return pChannel; |
return pChannel; |
470 |
} |
} |
471 |
|
|
481 |
SamplerChannelMap::iterator iterChan = mSamplerChannels.begin(); |
SamplerChannelMap::iterator iterChan = mSamplerChannels.begin(); |
482 |
for (; iterChan != mSamplerChannels.end(); iterChan++) { |
for (; iterChan != mSamplerChannels.end(); iterChan++) { |
483 |
if (iterChan->second == pSamplerChannel) { |
if (iterChan->second == pSamplerChannel) { |
484 |
|
fireChannelToBeRemoved(pSamplerChannel); |
485 |
|
pSamplerChannel->RemoveAllEngineChangeListeners(); |
486 |
mSamplerChannels.erase(iterChan); |
mSamplerChannels.erase(iterChan); |
487 |
delete pSamplerChannel; |
delete pSamplerChannel; |
488 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, mSamplerChannels.size())); |
fireChannelCountChanged(SamplerChannels()); |
489 |
return; |
return; |
490 |
} |
} |
491 |
} |
} |
521 |
} |
} |
522 |
} |
} |
523 |
|
|
524 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, AudioOutputDevices())); |
fireAudioDeviceCountChanged(AudioOutputDevices()); |
525 |
return pDevice; |
return pDevice; |
526 |
} |
} |
527 |
|
|
558 |
// destroy and free device from memory |
// destroy and free device from memory |
559 |
delete pDevice; |
delete pDevice; |
560 |
|
|
561 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, AudioOutputDevices())); |
fireAudioDeviceCountChanged(AudioOutputDevices()); |
562 |
break; |
break; |
563 |
} |
} |
564 |
} |
} |
572 |
for (uint i = 0; i < SamplerChannels(); i++) |
for (uint i = 0; i < SamplerChannels(); i++) |
573 |
if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw Exception("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."); |
574 |
|
|
575 |
|
fireMidiDeviceToBeDestroyed(pDevice); |
576 |
|
|
577 |
// disable device |
// disable device |
578 |
pDevice->StopListen(); |
pDevice->StopListen(); |
579 |
|
|
583 |
// destroy and free device from memory |
// destroy and free device from memory |
584 |
delete pDevice; |
delete pDevice; |
585 |
|
|
586 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, MidiInputDevices())); |
fireMidiDeviceCountChanged(MidiInputDevices()); |
587 |
break; |
break; |
588 |
} |
} |
589 |
} |
} |
601 |
} |
} |
602 |
} |
} |
603 |
|
|
604 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, MidiInputDevices())); |
fireMidiDeviceCreated(pDevice); |
605 |
|
fireMidiDeviceCountChanged(MidiInputDevices()); |
606 |
return pDevice; |
return pDevice; |
607 |
} |
} |
608 |
|
|
609 |
|
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 |
int Sampler::GetVoiceCount() { |
int Sampler::GetVoiceCount() { |
621 |
int count = 0; |
int count = 0; |
622 |
std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin(); |
std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin(); |
676 |
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; |
677 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
678 |
} |
} |
679 |
|
|
680 |
|
// unload all instrument editor DLLs |
681 |
|
InstrumentEditorFactory::ClosePlugins(); |
682 |
|
} |
683 |
|
|
684 |
|
bool Sampler::EnableDenormalsAreZeroMode() { |
685 |
|
Features::detect(); |
686 |
|
return Features::enableDenormalsAreZeroMode(); |
687 |
} |
} |
688 |
|
|
689 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |