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" |
|
#include "audiodriver/AudioOutputDeviceJack.h" |
|
28 |
#include "mididriver/MidiInputDeviceAlsa.h" |
#include "mididriver/MidiInputDeviceAlsa.h" |
29 |
#include "engines/gig/Engine.h" |
#include "engines/gig/Engine.h" |
30 |
|
|
49 |
} |
} |
50 |
} |
} |
51 |
|
|
52 |
void SamplerChannel::LoadEngine(engine_type_t EngineType) { |
void SamplerChannel::LoadEngine(Engine::type_t EngineType) { |
53 |
dmsg(1,("SamplerChannel: Loading engine\n")); |
dmsg(2,("SamplerChannel: Loading engine...")); |
54 |
|
|
55 |
// create new engine |
// create new engine |
56 |
Engine* pNewEngine = NULL; |
Engine* pNewEngine = NULL; |
57 |
switch (EngineType) { |
switch (EngineType) { |
58 |
case engine_type_gig: |
case Engine::type_gig: |
59 |
pNewEngine = new gig::Engine; |
pNewEngine = new gig::Engine; |
60 |
break; |
break; |
61 |
default: |
default: |
73 |
pEngine = pNewEngine; |
pEngine = pNewEngine; |
74 |
if (pMidiInputDevice) pMidiInputDevice->Connect(pNewEngine, (MidiInputDevice::midi_chan_t) Index()); |
if (pMidiInputDevice) pMidiInputDevice->Connect(pNewEngine, (MidiInputDevice::midi_chan_t) Index()); |
75 |
if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine); |
if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine); |
76 |
dmsg(1,("SamplerChannel: Engine loaded.\n")); |
dmsg(2,("OK\n")); |
77 |
} |
} |
78 |
|
|
79 |
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); |
|
|
|
|
80 |
// disconnect old device |
// disconnect old device |
81 |
if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine); |
if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine); |
82 |
|
|
85 |
if (pEngine) pAudioOutputDevice->Connect(pEngine); |
if (pEngine) pAudioOutputDevice->Connect(pEngine); |
86 |
} |
} |
87 |
|
|
88 |
void SamplerChannel::SetMidiInputDevice(midi_input_type_t MidiType, MidiInputDevice::midi_chan_t MidiChannel) { |
void SamplerChannel::SetMidiInputDevice(MidiInputDevice::type_t MidiType, MidiInputDevice::midi_chan_t MidiChannel) { |
89 |
// get / create desired midi device |
// get / create desired midi device |
90 |
MidiInputDevice* pDevice = pSampler->GetMidiInputDevice(MidiType); |
MidiInputDevice* pDevice = pSampler->GetMidiInputDevice(MidiType); |
91 |
if (!pDevice) pDevice = pSampler->CreateMidiInputDevice(MidiType); |
if (!pDevice) pDevice = pSampler->CreateMidiInputDevice(MidiType); |
150 |
|
|
151 |
// delete audio output devices |
// delete audio output devices |
152 |
{ |
{ |
153 |
AudioOutputDeviceMap::iterator iter = AudioOutputDevices.begin(); |
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
154 |
for (; iter != AudioOutputDevices.end(); iter++) { |
for (; iter != mAudioOutputDevices.end(); iter++) { |
155 |
AudioOutputDevice* pDevice = iter->second; |
AudioOutputDevice* pDevice = iter->second; |
156 |
pDevice->Stop(); |
pDevice->Stop(); |
157 |
delete pDevice; |
delete pDevice; |
160 |
} |
} |
161 |
|
|
162 |
uint Sampler::SamplerChannels() { |
uint Sampler::SamplerChannels() { |
163 |
vSamplerChannels.size(); |
return vSamplerChannels.size(); |
164 |
} |
} |
165 |
|
|
166 |
SamplerChannel* Sampler::AddSamplerChannel() { |
SamplerChannel* Sampler::AddSamplerChannel() { |
191 |
RemoveSamplerChannel(pChannel); |
RemoveSamplerChannel(pChannel); |
192 |
} |
} |
193 |
|
|
194 |
AudioOutputDevice* Sampler::CreateAudioOutputDevice(audio_output_type_t AudioType) { |
std::vector<String> Sampler::AvailableAudioOutputDrivers() { |
195 |
// check if device already created |
return AudioOutputDeviceFactory::AvailableDrivers(); |
196 |
AudioOutputDevice* pDevice = GetAudioOutputDevice(AudioType); |
} |
|
if (pDevice) return pDevice; |
|
197 |
|
|
198 |
|
AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) { |
199 |
// create new device |
// create new device |
200 |
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"); |
|
|
} |
|
201 |
|
|
202 |
// activate device |
// activate device |
203 |
pDevice->Play(); |
pDevice->Play(); |
204 |
|
|
205 |
|
// add new audio device to the audio device list |
206 |
|
for (uint i = 0; ; i++) { // seek for a free place starting from the beginning |
207 |
|
if (!mAudioOutputDevices[i]) { |
208 |
|
mAudioOutputDevices[i] = pDevice; |
209 |
|
break; |
210 |
|
} |
211 |
|
} |
212 |
|
|
213 |
return pDevice; |
return pDevice; |
214 |
} |
} |
215 |
|
|
216 |
AudioOutputDevice* Sampler::GetAudioOutputDevice(audio_output_type_t AudioType) { |
uint Sampler::AudioOutputDevices() { |
217 |
AudioOutputDeviceMap::iterator iter = AudioOutputDevices.find(AudioType); |
return mAudioOutputDevices.size(); |
218 |
return (iter != AudioOutputDevices.end()) ? iter->second : NULL; |
} |
219 |
|
|
220 |
|
std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() { |
221 |
|
return mAudioOutputDevices; |
222 |
} |
} |
223 |
|
|
224 |
MidiInputDevice* Sampler::CreateMidiInputDevice(midi_input_type_t MidiType) { |
void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (LinuxSamplerException) { |
225 |
|
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
226 |
|
for (; iter != mAudioOutputDevices.end(); iter++) { |
227 |
|
if (iter->second == pDevice) { |
228 |
|
// check if there are still sampler engines connected to this device |
229 |
|
for (uint i = 0; i < SamplerChannels(); i++) |
230 |
|
if (GetSamplerChannel(i)->GetAudioOutputDevice() == pDevice) throw LinuxSamplerException("Sampler channel " + ToString(i) + " is still connected to the audio output device."); |
231 |
|
|
232 |
|
// disable device |
233 |
|
pDevice->Stop(); |
234 |
|
|
235 |
|
// remove device from the device list |
236 |
|
mAudioOutputDevices.erase(iter); |
237 |
|
|
238 |
|
// destroy and free device from memory |
239 |
|
delete pDevice; |
240 |
|
} |
241 |
|
} |
242 |
|
} |
243 |
|
|
244 |
|
MidiInputDevice* Sampler::CreateMidiInputDevice(MidiInputDevice::type_t MidiType) { |
245 |
// check if device already created |
// check if device already created |
246 |
MidiInputDevice* pDevice = GetMidiInputDevice(MidiType); |
MidiInputDevice* pDevice = GetMidiInputDevice(MidiType); |
247 |
if (pDevice) return pDevice; |
if (pDevice) return pDevice; |
248 |
|
|
249 |
// create new device |
// create new device |
250 |
switch (MidiType) { |
switch (MidiType) { |
251 |
case midi_input_type_alsa: |
case MidiInputDevice::type_alsa: |
252 |
pDevice = new MidiInputDeviceAlsa; |
pDevice = new MidiInputDeviceAlsa; |
253 |
break; |
break; |
254 |
default: |
default: |
258 |
// activate device |
// activate device |
259 |
pDevice->Listen(); |
pDevice->Listen(); |
260 |
|
|
261 |
|
// add new MIDI device to the MIDI device list |
262 |
|
MidiInputDevices[MidiType] = pDevice; |
263 |
|
|
264 |
return pDevice; |
return pDevice; |
265 |
} |
} |
266 |
|
|
267 |
MidiInputDevice* Sampler::GetMidiInputDevice(midi_input_type_t MidiType) { |
MidiInputDevice* Sampler::GetMidiInputDevice(MidiInputDevice::type_t MidiType) { |
268 |
MidiInputDeviceMap::iterator iter = MidiInputDevices.find(MidiType); |
MidiInputDeviceMap::iterator iter = MidiInputDevices.find(MidiType); |
269 |
return (iter != MidiInputDevices.end()) ? iter->second : NULL; |
return (iter != MidiInputDevices.end()) ? iter->second : NULL; |
270 |
} |
} |