2 |
* * |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* * |
* * |
7 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
8 |
* 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 * |
20 |
* MA 02111-1307 USA * |
* MA 02111-1307 USA * |
21 |
***************************************************************************/ |
***************************************************************************/ |
22 |
|
|
23 |
|
#include <sstream> |
24 |
|
|
25 |
#include "Sampler.h" |
#include "Sampler.h" |
26 |
|
|
27 |
#include "audiodriver/AudioOutputDeviceAlsa.h" |
#include "audiodriver/AudioOutputDeviceFactory.h" |
28 |
#include "audiodriver/AudioOutputDeviceJack.h" |
#include "mididriver/MidiInputDeviceFactory.h" |
|
#include "mididriver/MidiInputDeviceAlsa.h" |
|
29 |
#include "engines/gig/Engine.h" |
#include "engines/gig/Engine.h" |
30 |
|
|
31 |
namespace LinuxSampler { |
namespace LinuxSampler { |
36 |
SamplerChannel::SamplerChannel(Sampler* pS) { |
SamplerChannel::SamplerChannel(Sampler* pS) { |
37 |
pSampler = pS; |
pSampler = pS; |
38 |
pEngine = NULL; |
pEngine = NULL; |
39 |
pMidiInputDevice = NULL; |
pMidiInputPort = NULL; |
40 |
pAudioOutputDevice = NULL; |
pAudioOutputDevice = NULL; |
41 |
iIndex = -1; |
iIndex = -1; |
42 |
|
midiChannel = MidiInputDevice::MidiInputPort::midi_chan_all; |
43 |
} |
} |
44 |
|
|
45 |
SamplerChannel::~SamplerChannel() { |
SamplerChannel::~SamplerChannel() { |
46 |
if (pEngine) { |
if (pEngine) { |
47 |
if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine); |
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine); |
48 |
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); |
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); |
49 |
delete pEngine; |
delete pEngine; |
50 |
} |
} |
51 |
} |
} |
52 |
|
|
53 |
void SamplerChannel::LoadEngine(engine_type_t EngineType) { |
void SamplerChannel::LoadEngine(Engine::type_t EngineType) { |
54 |
dmsg(1,("SamplerChannel: Loading engine\n")); |
dmsg(2,("SamplerChannel: Loading engine...")); |
55 |
|
|
56 |
// create new engine |
// create new engine |
57 |
Engine* pNewEngine = NULL; |
Engine* pNewEngine = NULL; |
58 |
switch (EngineType) { |
switch (EngineType) { |
59 |
case engine_type_gig: |
case Engine::type_gig: |
60 |
pNewEngine = new gig::Engine; |
pNewEngine = new gig::Engine; |
61 |
break; |
break; |
62 |
default: |
default: |
65 |
|
|
66 |
// disconnect old engine |
// disconnect old engine |
67 |
if (pEngine) { |
if (pEngine) { |
68 |
if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine); |
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine); |
69 |
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); |
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); |
70 |
delete pEngine; |
delete pEngine; |
71 |
} |
} |
72 |
|
|
73 |
// connect new engine |
// connect new engine |
74 |
pEngine = pNewEngine; |
pEngine = pNewEngine; |
75 |
if (pMidiInputDevice) pMidiInputDevice->Connect(pNewEngine, (MidiInputDevice::midi_chan_t) Index()); |
if (pMidiInputPort) pMidiInputPort->Connect(pNewEngine, (MidiInputDevice::MidiInputPort::midi_chan_t) Index()); |
76 |
if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine); |
if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine); |
77 |
dmsg(1,("SamplerChannel: Engine loaded.\n")); |
dmsg(2,("OK\n")); |
78 |
} |
} |
79 |
|
|
80 |
void SamplerChannel::SetAudioOutputDevice(audio_output_type_t AudioType) { |
void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) { |
|
// get / create desired audio device |
|
|
AudioOutputDevice* pDevice = pSampler->GetAudioOutputDevice(AudioType); |
|
|
if (!pDevice) pDevice = pSampler->CreateAudioOutputDevice(AudioType); |
|
|
|
|
81 |
// disconnect old device |
// disconnect old device |
82 |
if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine); |
if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine); |
83 |
|
|
86 |
if (pEngine) pAudioOutputDevice->Connect(pEngine); |
if (pEngine) pAudioOutputDevice->Connect(pEngine); |
87 |
} |
} |
88 |
|
|
89 |
void SamplerChannel::SetMidiInputDevice(midi_input_type_t MidiType, MidiInputDevice::midi_chan_t MidiChannel) { |
void SamplerChannel::SetMidiInputPort(MidiInputDevice* pDevice, int midiPort, MidiInputDevice::MidiInputPort::midi_chan_t MidiChannel) { |
90 |
// get / create desired midi device |
// disconnect old port |
91 |
MidiInputDevice* pDevice = pSampler->GetMidiInputDevice(MidiType); |
if (pMidiInputPort && pEngine) pMidiInputPort->Disconnect(pEngine); |
92 |
if (!pDevice) pDevice = pSampler->CreateMidiInputDevice(MidiType); |
|
93 |
|
// connect new port |
94 |
// disconnect old device |
pMidiInputPort = pDevice->GetPort(midiPort); |
95 |
if (pMidiInputDevice && pEngine) pMidiInputDevice->Disconnect(pEngine); |
if (!pMidiInputPort) |
96 |
|
throw LinuxSamplerException("MIDI port doesn't exist"); |
97 |
// connect new device |
if (pEngine) { |
98 |
pMidiInputDevice = pDevice; |
pMidiInputPort->Connect(pEngine, MidiChannel); |
99 |
if (pEngine) pMidiInputDevice->Connect(pEngine, MidiChannel); |
this->midiChannel = MidiChannel; |
100 |
|
} |
101 |
} |
} |
102 |
|
|
103 |
Engine* SamplerChannel::GetEngine() { |
Engine* SamplerChannel::GetEngine() { |
104 |
return pEngine; |
return pEngine; |
105 |
} |
} |
106 |
|
|
107 |
MidiInputDevice* SamplerChannel::GetMidiInputDevice() { |
MidiInputDevice::MidiInputPort* SamplerChannel::GetMidiInputPort() { |
108 |
return pMidiInputDevice; |
return pMidiInputPort; |
109 |
} |
} |
110 |
|
|
111 |
AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() { |
AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() { |
112 |
return pAudioOutputDevice; |
return pAudioOutputDevice; |
113 |
} |
} |
114 |
|
|
115 |
|
MidiInputDevice* SamplerChannel::GetMidiInputDevice() { |
116 |
|
MidiInputDevice::MidiInputPort* port = GetMidiInputPort(); |
117 |
|
if (!port) |
118 |
|
return NULL; |
119 |
|
return port->GetDevice(); |
120 |
|
} |
121 |
|
|
122 |
uint SamplerChannel::Index() { |
uint SamplerChannel::Index() { |
123 |
if (iIndex >= 0) return iIndex; |
if (iIndex >= 0) return iIndex; |
124 |
|
|
149 |
|
|
150 |
// delete midi input devices |
// delete midi input devices |
151 |
{ |
{ |
152 |
MidiInputDeviceMap::iterator iter = MidiInputDevices.begin(); |
MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); |
153 |
for (; iter != MidiInputDevices.end(); iter++) { |
for (; iter != mMidiInputDevices.end(); iter++) { |
154 |
MidiInputDevice* pDevice = iter->second; |
MidiInputDevice* pDevice = iter->second; |
155 |
pDevice->StopListen(); |
pDevice->StopListen(); |
156 |
delete pDevice; |
delete pDevice; |
159 |
|
|
160 |
// delete audio output devices |
// delete audio output devices |
161 |
{ |
{ |
162 |
AudioOutputDeviceMap::iterator iter = AudioOutputDevices.begin(); |
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
163 |
for (; iter != AudioOutputDevices.end(); iter++) { |
for (; iter != mAudioOutputDevices.end(); iter++) { |
164 |
AudioOutputDevice* pDevice = iter->second; |
AudioOutputDevice* pDevice = iter->second; |
165 |
pDevice->Stop(); |
pDevice->Stop(); |
166 |
delete pDevice; |
delete pDevice; |
169 |
} |
} |
170 |
|
|
171 |
uint Sampler::SamplerChannels() { |
uint Sampler::SamplerChannels() { |
172 |
vSamplerChannels.size(); |
return vSamplerChannels.size(); |
173 |
} |
} |
174 |
|
|
175 |
SamplerChannel* Sampler::AddSamplerChannel() { |
SamplerChannel* Sampler::AddSamplerChannel() { |
200 |
RemoveSamplerChannel(pChannel); |
RemoveSamplerChannel(pChannel); |
201 |
} |
} |
202 |
|
|
203 |
AudioOutputDevice* Sampler::CreateAudioOutputDevice(audio_output_type_t AudioType) { |
std::vector<String> Sampler::AvailableAudioOutputDrivers() { |
204 |
// check if device already created |
return AudioOutputDeviceFactory::AvailableDrivers(); |
205 |
AudioOutputDevice* pDevice = GetAudioOutputDevice(AudioType); |
} |
|
if (pDevice) return pDevice; |
|
206 |
|
|
207 |
|
AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) { |
208 |
// create new device |
// create new device |
209 |
switch (AudioType) { |
AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters); |
|
case audio_output_type_alsa: |
|
|
pDevice = new AudioOutputDeviceAlsa; |
|
|
break; |
|
|
case audio_output_type_jack: |
|
|
pDevice = new AudioOutputDeviceJack; |
|
|
break; |
|
|
default: |
|
|
throw LinuxSamplerException("Unknown audio output device type"); |
|
|
} |
|
210 |
|
|
211 |
// activate device |
// activate device |
212 |
pDevice->Play(); |
pDevice->Play(); |
213 |
|
|
214 |
|
// add new audio device to the audio device list |
215 |
|
for (uint i = 0; ; i++) { // seek for a free place starting from the beginning |
216 |
|
if (!mAudioOutputDevices[i]) { |
217 |
|
mAudioOutputDevices[i] = pDevice; |
218 |
|
break; |
219 |
|
} |
220 |
|
} |
221 |
|
|
222 |
return pDevice; |
return pDevice; |
223 |
} |
} |
224 |
|
|
225 |
AudioOutputDevice* Sampler::GetAudioOutputDevice(audio_output_type_t AudioType) { |
uint Sampler::AudioOutputDevices() { |
226 |
AudioOutputDeviceMap::iterator iter = AudioOutputDevices.find(AudioType); |
return mAudioOutputDevices.size(); |
|
return (iter != AudioOutputDevices.end()) ? iter->second : NULL; |
|
227 |
} |
} |
228 |
|
|
229 |
MidiInputDevice* Sampler::CreateMidiInputDevice(midi_input_type_t MidiType) { |
uint Sampler::MidiInputDevices() { |
230 |
// check if device already created |
return mMidiInputDevices.size(); |
231 |
MidiInputDevice* pDevice = GetMidiInputDevice(MidiType); |
} |
|
if (pDevice) return pDevice; |
|
232 |
|
|
233 |
// create new device |
std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() { |
234 |
switch (MidiType) { |
return mAudioOutputDevices; |
235 |
case midi_input_type_alsa: |
} |
236 |
pDevice = new MidiInputDeviceAlsa; |
|
237 |
break; |
std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() { |
238 |
default: |
return mMidiInputDevices; |
239 |
throw LinuxSamplerException("Unknown audio output device type"); |
} |
240 |
|
|
241 |
|
void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (LinuxSamplerException) { |
242 |
|
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
243 |
|
for (; iter != mAudioOutputDevices.end(); iter++) { |
244 |
|
if (iter->second == pDevice) { |
245 |
|
// check if there are still sampler engines connected to this device |
246 |
|
for (uint i = 0; i < SamplerChannels(); i++) |
247 |
|
if (GetSamplerChannel(i)->GetAudioOutputDevice() == pDevice) throw LinuxSamplerException("Sampler channel " + ToString(i) + " is still connected to the audio output device."); |
248 |
|
|
249 |
|
// disable device |
250 |
|
pDevice->Stop(); |
251 |
|
|
252 |
|
// remove device from the device list |
253 |
|
mAudioOutputDevices.erase(iter); |
254 |
|
|
255 |
|
// destroy and free device from memory |
256 |
|
delete pDevice; |
257 |
|
} |
258 |
|
} |
259 |
|
} |
260 |
|
|
261 |
|
void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (LinuxSamplerException) { |
262 |
|
MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); |
263 |
|
for (; iter != mMidiInputDevices.end(); iter++) { |
264 |
|
if (iter->second == pDevice) { |
265 |
|
// check if there are still sampler engines connected to this device |
266 |
|
for (uint i = 0; i < SamplerChannels(); i++) |
267 |
|
if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw LinuxSamplerException("Sampler channel " + ToString(i) + " is still connected to the midi input device."); |
268 |
|
|
269 |
|
// disable device |
270 |
|
pDevice->StopListen(); |
271 |
|
|
272 |
|
// remove device from the device list |
273 |
|
mMidiInputDevices.erase(iter); |
274 |
|
|
275 |
|
// destroy and free device from memory |
276 |
|
delete pDevice; |
277 |
|
} |
278 |
} |
} |
279 |
|
} |
280 |
|
|
281 |
|
MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) { |
282 |
|
// create new device |
283 |
|
MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters); |
284 |
|
|
285 |
// activate device |
// activate device |
286 |
pDevice->Listen(); |
pDevice->Listen(); |
287 |
|
|
288 |
return pDevice; |
// add new device to the midi device list |
289 |
} |
for (uint i = 0; ; i++) { // seek for a free place starting from the beginning |
290 |
|
if (!mMidiInputDevices[i]) { |
291 |
|
mMidiInputDevices[i] = pDevice; |
292 |
|
break; |
293 |
|
} |
294 |
|
} |
295 |
|
|
296 |
MidiInputDevice* Sampler::GetMidiInputDevice(midi_input_type_t MidiType) { |
return pDevice; |
|
MidiInputDeviceMap::iterator iter = MidiInputDevices.find(MidiType); |
|
|
return (iter != MidiInputDevices.end()) ? iter->second : NULL; |
|
297 |
} |
} |
298 |
|
|
299 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |