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 Christian Schoenebeck * |
* Copyright (C) 2005 - 2007 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* This library is free software; you can redistribute it and/or modify * |
9 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
10 |
* the Free Software Foundation; either version 2 of the License, or * |
* the Free Software Foundation; either version 2 of the License, or * |
11 |
* (at your option) any later version. * |
* (at your option) any later version. * |
12 |
* * |
* * |
13 |
* This program is distributed in the hope that it will be useful, * |
* This library is distributed in the hope that it will be useful, * |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
* GNU General Public License for more details. * |
* GNU General Public License for more details. * |
17 |
* * |
* * |
18 |
* You should have received a copy of the GNU General Public License * |
* You should have received a copy of the GNU General Public License * |
19 |
* along with this program; if not, write to the Free Software * |
* along with this library; if not, write to the Free Software * |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
* MA 02111-1307 USA * |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
***************************************************************************/ |
29 |
#include "engines/EngineChannelFactory.h" |
#include "engines/EngineChannelFactory.h" |
30 |
#include "drivers/audio/AudioOutputDeviceFactory.h" |
#include "drivers/audio/AudioOutputDeviceFactory.h" |
31 |
#include "drivers/midi/MidiInputDeviceFactory.h" |
#include "drivers/midi/MidiInputDeviceFactory.h" |
32 |
|
#include "drivers/midi/MidiInstrumentMapper.h" |
33 |
#include "network/lscpserver.h" |
#include "network/lscpserver.h" |
34 |
|
|
35 |
namespace LinuxSampler { |
namespace LinuxSampler { |
49 |
|
|
50 |
SamplerChannel::~SamplerChannel() { |
SamplerChannel::~SamplerChannel() { |
51 |
if (pEngineChannel) { |
if (pEngineChannel) { |
52 |
|
Engine* engine = pEngineChannel->GetEngine(); |
53 |
|
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine); |
54 |
|
|
55 |
MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : __GetMidiInputDevicePort(GetMidiInputChannel()); |
MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : __GetMidiInputDevicePort(GetMidiInputChannel()); |
56 |
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel); |
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel); |
57 |
if (pEngineChannel) { |
if (pEngineChannel) { |
58 |
if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice(); |
if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice(); |
59 |
EngineChannelFactory::Destroy(pEngineChannel); |
EngineChannelFactory::Destroy(pEngineChannel); |
60 |
|
|
61 |
|
// reconnect engine if it still exists |
62 |
|
const std::set<Engine*>& engines = EngineFactory::EngineInstances(); |
63 |
|
if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine); |
64 |
} |
} |
65 |
} |
} |
66 |
} |
} |
67 |
|
|
68 |
void SamplerChannel::SetEngineType(String EngineType) throw (LinuxSamplerException) { |
void SamplerChannel::SetEngineType(String EngineType) throw (Exception) { |
69 |
dmsg(2,("SamplerChannel: Assigning engine type...")); |
dmsg(2,("SamplerChannel: Assigning engine type...")); |
70 |
|
|
71 |
// create new engine channel |
// create new engine channel |
72 |
EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType); |
EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType); |
73 |
if (!pNewEngineChannel) throw LinuxSamplerException("Unknown engine type"); |
if (!pNewEngineChannel) throw Exception("Unknown engine type"); |
74 |
|
|
75 |
//FIXME: hack to allow fast retrieval of engine channel's sampler channel index |
//FIXME: hack to allow fast retrieval of engine channel's sampler channel index |
76 |
pNewEngineChannel->iSamplerChannelIndex = Index(); |
pNewEngineChannel->iSamplerChannelIndex = Index(); |
79 |
MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort()); |
MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort()); |
80 |
// disconnect old engine channel |
// disconnect old engine channel |
81 |
if (pEngineChannel) { |
if (pEngineChannel) { |
82 |
|
Engine* engine = pEngineChannel->GetEngine(); |
83 |
|
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine); |
84 |
|
|
85 |
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel); |
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel); |
86 |
if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice(); |
if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice(); |
87 |
EngineChannelFactory::Destroy(pEngineChannel); |
EngineChannelFactory::Destroy(pEngineChannel); |
88 |
|
|
89 |
|
// reconnect engine if it still exists |
90 |
|
const std::set<Engine*>& engines = EngineFactory::EngineInstances(); |
91 |
|
if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine); |
92 |
} |
} |
93 |
|
|
94 |
// connect new engine channel |
// connect new engine channel |
|
if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, GetMidiInputChannel()); |
|
95 |
if (pAudioOutputDevice) { |
if (pAudioOutputDevice) { |
96 |
pNewEngineChannel->Connect(pAudioOutputDevice); |
pNewEngineChannel->Connect(pAudioOutputDevice); |
97 |
pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine()); |
pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine()); |
98 |
} |
} |
99 |
|
if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, GetMidiInputChannel()); |
100 |
pEngineChannel = pNewEngineChannel; |
pEngineChannel = pNewEngineChannel; |
101 |
|
|
102 |
// from now on get MIDI device and port from EngineChannel object |
// from now on get MIDI device and port from EngineChannel object |
109 |
} |
} |
110 |
|
|
111 |
void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) { |
void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) { |
112 |
|
if(pAudioOutputDevice == pDevice) return; |
113 |
|
|
114 |
// disconnect old device |
// disconnect old device |
115 |
if (pAudioOutputDevice && pEngineChannel) pEngineChannel->DisconnectAudioOutputDevice(); |
if (pAudioOutputDevice && pEngineChannel) { |
116 |
|
Engine* engine = pEngineChannel->GetEngine(); |
117 |
|
pAudioOutputDevice->Disconnect(engine); |
118 |
|
|
119 |
|
pEngineChannel->DisconnectAudioOutputDevice(); |
120 |
|
|
121 |
|
// reconnect engine if it still exists |
122 |
|
const std::set<Engine*>& engines = EngineFactory::EngineInstances(); |
123 |
|
if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine); |
124 |
|
} |
125 |
|
|
126 |
// connect new device |
// connect new device |
127 |
pAudioOutputDevice = pDevice; |
pAudioOutputDevice = pDevice; |
128 |
if (pEngineChannel) { |
if (pEngineChannel) { |
144 |
} |
} |
145 |
|
|
146 |
void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) { |
void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) { |
147 |
if (!pDevice) throw LinuxSamplerException("No MIDI input device assigned."); |
if (!pDevice) throw Exception("No MIDI input device assigned."); |
148 |
|
|
149 |
// get old and new midi input port |
// get old and new midi input port |
150 |
MidiInputPort* pOldMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort()); |
MidiInputPort* pOldMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort()); |
163 |
if (pNewMidiInputPort && pEngineChannel) pNewMidiInputPort->Connect(pEngineChannel, MidiChannel); |
if (pNewMidiInputPort && pEngineChannel) pNewMidiInputPort->Connect(pEngineChannel, MidiChannel); |
164 |
// Ooops. |
// Ooops. |
165 |
if (pNewMidiInputPort == NULL) |
if (pNewMidiInputPort == NULL) |
166 |
throw LinuxSamplerException("There is no MIDI input port with index " + ToString(iMidiPort) + "."); |
throw Exception("There is no MIDI input port with index " + ToString(iMidiPort) + "."); |
167 |
} |
} |
168 |
|
|
169 |
EngineChannel* SamplerChannel::GetEngineChannel() { |
EngineChannel* SamplerChannel::GetEngineChannel() { |
202 |
} |
} |
203 |
} |
} |
204 |
|
|
205 |
throw LinuxSamplerException("Internal error: SamplerChannel index not found"); |
throw Exception("Internal error: SamplerChannel index not found"); |
206 |
} |
} |
207 |
|
|
208 |
MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) { |
MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) { |
252 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, i)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, i)); |
253 |
return pChannel; |
return pChannel; |
254 |
} |
} |
255 |
throw LinuxSamplerException("Internal error: could not find unoccupied sampler channel index."); |
throw Exception("Internal error: could not find unoccupied sampler channel index."); |
256 |
} |
} |
257 |
|
|
258 |
// 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 |
292 |
return AudioOutputDeviceFactory::AvailableDrivers(); |
return AudioOutputDeviceFactory::AvailableDrivers(); |
293 |
} |
} |
294 |
|
|
295 |
AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) { |
std::vector<String> Sampler::AvailableMidiInputDrivers() { |
296 |
|
return MidiInputDeviceFactory::AvailableDrivers(); |
297 |
|
} |
298 |
|
|
299 |
|
std::vector<String> Sampler::AvailableEngineTypes() { |
300 |
|
return EngineFactory::AvailableEngineTypes(); |
301 |
|
} |
302 |
|
|
303 |
|
AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map<String,String> Parameters) throw (Exception) { |
304 |
// create new device |
// create new device |
305 |
AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters); |
AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters); |
306 |
|
|
312 |
} |
} |
313 |
} |
} |
314 |
|
|
315 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, AudioOutputDevices())); |
316 |
return pDevice; |
return pDevice; |
317 |
} |
} |
318 |
|
|
332 |
return mMidiInputDevices; |
return mMidiInputDevices; |
333 |
} |
} |
334 |
|
|
335 |
void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (LinuxSamplerException) { |
void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) { |
336 |
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
337 |
for (; iter != mAudioOutputDevices.end(); iter++) { |
for (; iter != mAudioOutputDevices.end(); iter++) { |
338 |
if (iter->second == pDevice) { |
if (iter->second == pDevice) { |
339 |
// check if there are still sampler engines connected to this device |
// check if there are still sampler engines connected to this device |
340 |
for (uint i = 0; i < SamplerChannels(); i++) |
for (uint i = 0; i < SamplerChannels(); i++) |
341 |
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."); |
342 |
|
|
343 |
// disable device |
// disable device |
344 |
pDevice->Stop(); |
pDevice->Stop(); |
345 |
|
|
346 |
|
// remove device from the device list |
347 |
|
mAudioOutputDevices.erase(iter); |
348 |
|
|
349 |
// destroy and free device from memory |
// destroy and free device from memory |
350 |
delete pDevice; |
delete pDevice; |
351 |
|
|
352 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, AudioOutputDevices())); |
353 |
|
break; |
354 |
} |
} |
355 |
} |
} |
|
// remove devices from the device list |
|
|
mAudioOutputDevices.clear(); |
|
356 |
} |
} |
357 |
|
|
358 |
void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (LinuxSamplerException) { |
void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) { |
359 |
MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); |
MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); |
360 |
for (; iter != mMidiInputDevices.end(); iter++) { |
for (; iter != mMidiInputDevices.end(); iter++) { |
361 |
if (iter->second == pDevice) { |
if (iter->second == pDevice) { |
362 |
// check if there are still sampler engines connected to this device |
// check if there are still sampler engines connected to this device |
363 |
for (uint i = 0; i < SamplerChannels(); i++) |
for (uint i = 0; i < SamplerChannels(); i++) |
364 |
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."); |
365 |
|
|
366 |
// disable device |
// disable device |
367 |
pDevice->StopListen(); |
pDevice->StopListen(); |
368 |
|
|
369 |
|
// remove device from the device list |
370 |
|
mMidiInputDevices.erase(iter); |
371 |
|
|
372 |
// destroy and free device from memory |
// destroy and free device from memory |
373 |
delete pDevice; |
delete pDevice; |
374 |
|
|
375 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, MidiInputDevices())); |
376 |
|
break; |
377 |
} |
} |
378 |
} |
} |
|
// remove devices from the device list |
|
|
mMidiInputDevices.clear(); |
|
379 |
} |
} |
380 |
|
|
381 |
MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) { |
MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (Exception) { |
382 |
// create new device |
// create new device |
383 |
MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this); |
MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this); |
384 |
|
|
390 |
} |
} |
391 |
} |
} |
392 |
|
|
393 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, MidiInputDevices())); |
394 |
return pDevice; |
return pDevice; |
395 |
} |
} |
396 |
|
|
444 |
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all audio output devices, exiting.\n" << std::flush; |
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all audio output devices, exiting.\n" << std::flush; |
445 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
446 |
} |
} |
447 |
|
|
448 |
|
// delete MIDI instrument maps |
449 |
|
try { |
450 |
|
MidiInstrumentMapper::RemoveAllMaps(); |
451 |
|
} |
452 |
|
catch(...) { |
453 |
|
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI instrument maps, exiting.\n" << std::flush; |
454 |
|
exit(EXIT_FAILURE); |
455 |
|
} |
456 |
} |
} |
457 |
|
|
458 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |