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 "drivers/audio/AudioOutputDeviceFactory.h" |
28 |
#include "audiodriver/AudioOutputDeviceJack.h" |
#include "drivers/midi/MidiInputDeviceFactory.h" |
|
#include "mididriver/MidiInputDeviceAlsa.h" |
|
29 |
#include "engines/gig/Engine.h" |
#include "engines/gig/Engine.h" |
30 |
|
|
31 |
namespace LinuxSampler { |
namespace LinuxSampler { |
38 |
pEngine = NULL; |
pEngine = NULL; |
39 |
pMidiInputDevice = NULL; |
pMidiInputDevice = NULL; |
40 |
pAudioOutputDevice = NULL; |
pAudioOutputDevice = NULL; |
41 |
|
midiPort = 0; |
42 |
|
midiChannel = MidiInputDevice::MidiInputPort::midi_chan_all; |
43 |
iIndex = -1; |
iIndex = -1; |
44 |
} |
} |
45 |
|
|
46 |
SamplerChannel::~SamplerChannel() { |
SamplerChannel::~SamplerChannel() { |
47 |
if (pEngine) { |
if (pEngine) { |
48 |
if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine); |
MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort); |
49 |
|
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine); |
50 |
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); |
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); |
51 |
delete pEngine; |
delete pEngine; |
52 |
} |
} |
53 |
} |
} |
54 |
|
|
55 |
void SamplerChannel::LoadEngine(engine_type_t EngineType) { |
void SamplerChannel::LoadEngine(Engine::type_t EngineType) { |
56 |
dmsg(1,("SamplerChannel: Loading engine\n")); |
dmsg(2,("SamplerChannel: Loading engine...")); |
57 |
|
|
58 |
// create new engine |
// create new engine |
59 |
Engine* pNewEngine = NULL; |
Engine* pNewEngine = NULL; |
60 |
switch (EngineType) { |
switch (EngineType) { |
61 |
case engine_type_gig: |
case Engine::type_gig: |
62 |
pNewEngine = new gig::Engine; |
pNewEngine = new gig::Engine; |
63 |
break; |
break; |
64 |
default: |
default: |
65 |
throw LinuxSamplerException("Unknown engine type"); |
throw LinuxSamplerException("Unknown engine type"); |
66 |
} |
} |
67 |
|
|
68 |
|
// dereference midi input port. |
69 |
|
MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort); |
70 |
// disconnect old engine |
// disconnect old engine |
71 |
if (pEngine) { |
if (pEngine) { |
72 |
if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine); |
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine); |
73 |
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); |
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); |
74 |
delete pEngine; |
delete pEngine; |
75 |
} |
} |
76 |
|
|
77 |
// connect new engine |
// connect new engine |
78 |
pEngine = pNewEngine; |
pEngine = pNewEngine; |
79 |
if (pMidiInputDevice) pMidiInputDevice->Connect(pNewEngine, (MidiInputDevice::midi_chan_t) Index()); |
if (pMidiInputPort) pMidiInputPort->Connect(pNewEngine, this->midiChannel); |
80 |
if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine); |
if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine); |
81 |
dmsg(1,("SamplerChannel: Engine loaded.\n")); |
dmsg(2,("OK\n")); |
82 |
} |
} |
83 |
|
|
84 |
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); |
|
|
|
|
85 |
// disconnect old device |
// disconnect old device |
86 |
if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine); |
if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine); |
|
|
|
87 |
// connect new device |
// connect new device |
88 |
pAudioOutputDevice = pDevice; |
pAudioOutputDevice = pDevice; |
89 |
if (pEngine) pAudioOutputDevice->Connect(pEngine); |
if (pEngine) pAudioOutputDevice->Connect(pEngine); |
90 |
} |
} |
91 |
|
|
92 |
void SamplerChannel::SetMidiInputDevice(midi_input_type_t MidiType, MidiInputDevice::midi_chan_t MidiChannel) { |
void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) { |
93 |
// get / create desired midi device |
SetMidiInput(pDevice, this->midiPort, this->midiChannel); |
94 |
MidiInputDevice* pDevice = pSampler->GetMidiInputDevice(MidiType); |
} |
|
if (!pDevice) pDevice = pSampler->CreateMidiInputDevice(MidiType); |
|
95 |
|
|
96 |
// disconnect old device |
void SamplerChannel::SetMidiInputPort(int MidiPort) { |
97 |
if (pMidiInputDevice && pEngine) pMidiInputDevice->Disconnect(pEngine); |
SetMidiInput(pMidiInputDevice, MidiPort, this->midiChannel); |
98 |
|
} |
99 |
|
|
100 |
// connect new device |
void SamplerChannel::SetMidiInputChannel(MidiInputDevice::MidiInputPort::midi_chan_t MidiChannel) { |
101 |
|
SetMidiInput(pMidiInputDevice, this->midiPort, MidiChannel); |
102 |
|
} |
103 |
|
|
104 |
|
void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int MidiPort, MidiInputDevice::MidiInputPort::midi_chan_t MidiChannel) { |
105 |
|
// dereference old midi input port. |
106 |
|
MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort); |
107 |
|
// disconnect old device port |
108 |
|
if (pMidiInputPort && pEngine) pMidiInputPort->Disconnect(pEngine); |
109 |
|
// new device, port and channel |
110 |
pMidiInputDevice = pDevice; |
pMidiInputDevice = pDevice; |
111 |
if (pEngine) pMidiInputDevice->Connect(pEngine, MidiChannel); |
this->midiPort = MidiPort; |
112 |
|
this->midiChannel = MidiChannel; |
113 |
|
// connect new device port |
114 |
|
pMidiInputPort = GetMidiInputDevicePort(this->midiPort); |
115 |
|
if (pMidiInputPort && pEngine) pMidiInputPort->Connect(pEngine, MidiChannel); |
116 |
|
// Ooops. |
117 |
|
if (pMidiInputPort == NULL) |
118 |
|
throw LinuxSamplerException("There is no MIDI input port with index " + ToString(MidiPort) + "."); |
119 |
} |
} |
120 |
|
|
121 |
Engine* SamplerChannel::GetEngine() { |
Engine* SamplerChannel::GetEngine() { |
122 |
return pEngine; |
return pEngine; |
123 |
} |
} |
124 |
|
|
125 |
MidiInputDevice* SamplerChannel::GetMidiInputDevice() { |
MidiInputDevice::MidiInputPort::midi_chan_t SamplerChannel::GetMidiInputChannel() { |
126 |
return pMidiInputDevice; |
return this->midiChannel; |
127 |
|
} |
128 |
|
|
129 |
|
int SamplerChannel::GetMidiInputPort() { |
130 |
|
MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort); |
131 |
|
return (pMidiInputPort ? (int) pMidiInputPort->GetPortNumber() : -1); |
132 |
} |
} |
133 |
|
|
134 |
AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() { |
AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() { |
135 |
return pAudioOutputDevice; |
return pAudioOutputDevice; |
136 |
} |
} |
137 |
|
|
138 |
|
MidiInputDevice* SamplerChannel::GetMidiInputDevice() { |
139 |
|
return pMidiInputDevice; |
140 |
|
} |
141 |
|
|
142 |
uint SamplerChannel::Index() { |
uint SamplerChannel::Index() { |
143 |
if (iIndex >= 0) return iIndex; |
if (iIndex >= 0) return iIndex; |
144 |
|
|
153 |
throw LinuxSamplerException("SamplerChannel index not found"); |
throw LinuxSamplerException("SamplerChannel index not found"); |
154 |
} |
} |
155 |
|
|
156 |
|
MidiInputDevice::MidiInputPort* SamplerChannel::GetMidiInputDevicePort(int MidiPort) { |
157 |
|
MidiInputDevice::MidiInputPort *pMidiInputPort = NULL; |
158 |
|
if (pMidiInputDevice) |
159 |
|
pMidiInputPort = pMidiInputDevice->GetPort(MidiPort); |
160 |
|
return pMidiInputPort; |
161 |
|
} |
162 |
|
|
163 |
// ****************************************************************** |
// ****************************************************************** |
164 |
// * Sampler |
// * Sampler |
175 |
|
|
176 |
// delete midi input devices |
// delete midi input devices |
177 |
{ |
{ |
178 |
MidiInputDeviceMap::iterator iter = MidiInputDevices.begin(); |
MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); |
179 |
for (; iter != MidiInputDevices.end(); iter++) { |
for (; iter != mMidiInputDevices.end(); iter++) { |
180 |
MidiInputDevice* pDevice = iter->second; |
MidiInputDevice* pDevice = iter->second; |
181 |
pDevice->StopListen(); |
pDevice->StopListen(); |
182 |
delete pDevice; |
delete pDevice; |
185 |
|
|
186 |
// delete audio output devices |
// delete audio output devices |
187 |
{ |
{ |
188 |
AudioOutputDeviceMap::iterator iter = AudioOutputDevices.begin(); |
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
189 |
for (; iter != AudioOutputDevices.end(); iter++) { |
for (; iter != mAudioOutputDevices.end(); iter++) { |
190 |
AudioOutputDevice* pDevice = iter->second; |
AudioOutputDevice* pDevice = iter->second; |
191 |
pDevice->Stop(); |
pDevice->Stop(); |
192 |
delete pDevice; |
delete pDevice; |
195 |
} |
} |
196 |
|
|
197 |
uint Sampler::SamplerChannels() { |
uint Sampler::SamplerChannels() { |
198 |
vSamplerChannels.size(); |
return vSamplerChannels.size(); |
199 |
} |
} |
200 |
|
|
201 |
SamplerChannel* Sampler::AddSamplerChannel() { |
SamplerChannel* Sampler::AddSamplerChannel() { |
226 |
RemoveSamplerChannel(pChannel); |
RemoveSamplerChannel(pChannel); |
227 |
} |
} |
228 |
|
|
229 |
AudioOutputDevice* Sampler::CreateAudioOutputDevice(audio_output_type_t AudioType) { |
std::vector<String> Sampler::AvailableAudioOutputDrivers() { |
230 |
// check if device already created |
return AudioOutputDeviceFactory::AvailableDrivers(); |
231 |
AudioOutputDevice* pDevice = GetAudioOutputDevice(AudioType); |
} |
|
if (pDevice) return pDevice; |
|
232 |
|
|
233 |
|
AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) { |
234 |
// create new device |
// create new device |
235 |
switch (AudioType) { |
AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters); |
236 |
case audio_output_type_alsa: |
|
237 |
pDevice = new AudioOutputDeviceAlsa; |
// add new audio device to the audio device list |
238 |
break; |
for (uint i = 0; ; i++) { // seek for a free place starting from the beginning |
239 |
case audio_output_type_jack: |
if (!mAudioOutputDevices[i]) { |
240 |
pDevice = new AudioOutputDeviceJack; |
mAudioOutputDevices[i] = pDevice; |
241 |
break; |
break; |
242 |
default: |
} |
|
throw LinuxSamplerException("Unknown audio output device type"); |
|
243 |
} |
} |
244 |
|
|
|
// activate device |
|
|
pDevice->Play(); |
|
|
|
|
245 |
return pDevice; |
return pDevice; |
246 |
} |
} |
247 |
|
|
248 |
AudioOutputDevice* Sampler::GetAudioOutputDevice(audio_output_type_t AudioType) { |
uint Sampler::AudioOutputDevices() { |
249 |
AudioOutputDeviceMap::iterator iter = AudioOutputDevices.find(AudioType); |
return mAudioOutputDevices.size(); |
250 |
return (iter != AudioOutputDevices.end()) ? iter->second : NULL; |
} |
251 |
|
|
252 |
|
uint Sampler::MidiInputDevices() { |
253 |
|
return mMidiInputDevices.size(); |
254 |
} |
} |
255 |
|
|
256 |
MidiInputDevice* Sampler::CreateMidiInputDevice(midi_input_type_t MidiType) { |
std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() { |
257 |
// check if device already created |
return mAudioOutputDevices; |
258 |
MidiInputDevice* pDevice = GetMidiInputDevice(MidiType); |
} |
|
if (pDevice) return pDevice; |
|
259 |
|
|
260 |
// create new device |
std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() { |
261 |
switch (MidiType) { |
return mMidiInputDevices; |
262 |
case midi_input_type_alsa: |
} |
263 |
pDevice = new MidiInputDeviceAlsa; |
|
264 |
break; |
void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (LinuxSamplerException) { |
265 |
default: |
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
266 |
throw LinuxSamplerException("Unknown audio output device type"); |
for (; iter != mAudioOutputDevices.end(); iter++) { |
267 |
|
if (iter->second == pDevice) { |
268 |
|
// check if there are still sampler engines connected to this device |
269 |
|
for (uint i = 0; i < SamplerChannels(); i++) |
270 |
|
if (GetSamplerChannel(i)->GetAudioOutputDevice() == pDevice) throw LinuxSamplerException("Sampler channel " + ToString(i) + " is still connected to the audio output device."); |
271 |
|
|
272 |
|
// disable device |
273 |
|
pDevice->Stop(); |
274 |
|
|
275 |
|
// remove device from the device list |
276 |
|
mAudioOutputDevices.erase(iter); |
277 |
|
|
278 |
|
// destroy and free device from memory |
279 |
|
delete pDevice; |
280 |
|
} |
281 |
} |
} |
282 |
|
} |
283 |
|
|
284 |
// activate device |
void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (LinuxSamplerException) { |
285 |
pDevice->Listen(); |
MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); |
286 |
|
for (; iter != mMidiInputDevices.end(); iter++) { |
287 |
|
if (iter->second == pDevice) { |
288 |
|
// check if there are still sampler engines connected to this device |
289 |
|
for (uint i = 0; i < SamplerChannels(); i++) |
290 |
|
if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw LinuxSamplerException("Sampler channel " + ToString(i) + " is still connected to the midi input device."); |
291 |
|
|
292 |
return pDevice; |
// disable device |
293 |
|
pDevice->StopListen(); |
294 |
|
|
295 |
|
// remove device from the device list |
296 |
|
mMidiInputDevices.erase(iter); |
297 |
|
|
298 |
|
// destroy and free device from memory |
299 |
|
delete pDevice; |
300 |
|
} |
301 |
|
} |
302 |
} |
} |
303 |
|
|
304 |
MidiInputDevice* Sampler::GetMidiInputDevice(midi_input_type_t MidiType) { |
MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) { |
305 |
MidiInputDeviceMap::iterator iter = MidiInputDevices.find(MidiType); |
// create new device |
306 |
return (iter != MidiInputDevices.end()) ? iter->second : NULL; |
MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters); |
307 |
|
|
308 |
|
// add new device to the midi device list |
309 |
|
for (uint i = 0; ; i++) { // seek for a free place starting from the beginning |
310 |
|
if (!mMidiInputDevices[i]) { |
311 |
|
mMidiInputDevices[i] = pDevice; |
312 |
|
break; |
313 |
|
} |
314 |
|
} |
315 |
|
|
316 |
|
return pDevice; |
317 |
} |
} |
318 |
|
|
319 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |