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 * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* This program 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 "audiodriver/AudioOutputDeviceFactory.h" |
#include "engines/EngineChannelFactory.h" |
29 |
#include "mididriver/MidiInputDeviceAlsa.h" |
#include "drivers/audio/AudioOutputDeviceFactory.h" |
30 |
#include "engines/gig/Engine.h" |
#include "drivers/midi/MidiInputDeviceFactory.h" |
31 |
|
#include "network/lscpserver.h" |
32 |
|
|
33 |
namespace LinuxSampler { |
namespace LinuxSampler { |
34 |
|
|
37 |
|
|
38 |
SamplerChannel::SamplerChannel(Sampler* pS) { |
SamplerChannel::SamplerChannel(Sampler* pS) { |
39 |
pSampler = pS; |
pSampler = pS; |
40 |
pEngine = NULL; |
pEngineChannel = NULL; |
41 |
pMidiInputDevice = NULL; |
pMidiInputDevice = NULL; |
42 |
pAudioOutputDevice = NULL; |
pAudioOutputDevice = NULL; |
43 |
|
midiPort = 0; |
44 |
|
midiChannel = MidiInputPort::midi_chan_all; |
45 |
iIndex = -1; |
iIndex = -1; |
46 |
} |
} |
47 |
|
|
48 |
SamplerChannel::~SamplerChannel() { |
SamplerChannel::~SamplerChannel() { |
49 |
if (pEngine) { |
if (pEngineChannel) { |
50 |
if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine); |
MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort); |
51 |
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); |
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel); |
52 |
delete pEngine; |
if (pEngineChannel) { |
53 |
|
if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice(); |
54 |
|
EngineChannelFactory::Destroy(pEngineChannel); |
55 |
|
} |
56 |
} |
} |
57 |
} |
} |
58 |
|
|
59 |
void SamplerChannel::LoadEngine(Engine::type_t EngineType) { |
void SamplerChannel::SetEngineType(String EngineType) throw (LinuxSamplerException) { |
60 |
dmsg(2,("SamplerChannel: Loading engine...")); |
dmsg(2,("SamplerChannel: Assigning engine type...")); |
61 |
|
|
62 |
// create new engine |
// create new engine channel |
63 |
Engine* pNewEngine = NULL; |
EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType); |
64 |
switch (EngineType) { |
if (!pNewEngineChannel) throw LinuxSamplerException("Unknown engine type"); |
|
case Engine::type_gig: |
|
|
pNewEngine = new gig::Engine; |
|
|
break; |
|
|
default: |
|
|
throw LinuxSamplerException("Unknown engine type"); |
|
|
} |
|
65 |
|
|
66 |
|
//FIXME: hack to allow fast retrieval of engine channel's sampler channel index |
67 |
|
pNewEngineChannel->iSamplerChannelIndex = Index(); |
68 |
|
|
69 |
|
// dereference midi input port. |
70 |
|
MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort); |
71 |
// disconnect old engine |
// disconnect old engine |
72 |
if (pEngine) { |
if (pEngineChannel) { |
73 |
if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine); |
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel); |
74 |
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); |
if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice(); |
75 |
delete pEngine; |
EngineChannelFactory::Destroy(pEngineChannel); |
76 |
} |
} |
77 |
|
|
78 |
// connect new engine |
// connect new engine channel |
79 |
pEngine = pNewEngine; |
pEngineChannel = pNewEngineChannel; |
80 |
if (pMidiInputDevice) pMidiInputDevice->Connect(pNewEngine, (MidiInputDevice::midi_chan_t) Index()); |
if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, this->midiChannel); |
81 |
if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine); |
if (pAudioOutputDevice) { |
82 |
|
pNewEngineChannel->Connect(pAudioOutputDevice); |
83 |
|
pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine()); |
84 |
|
} |
85 |
dmsg(2,("OK\n")); |
dmsg(2,("OK\n")); |
86 |
} |
} |
87 |
|
|
88 |
void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) { |
void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) { |
89 |
// disconnect old device |
// disconnect old device |
90 |
if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine); |
if (pAudioOutputDevice && pEngineChannel) pEngineChannel->DisconnectAudioOutputDevice(); |
|
|
|
91 |
// connect new device |
// connect new device |
92 |
pAudioOutputDevice = pDevice; |
pAudioOutputDevice = pDevice; |
93 |
if (pEngine) pAudioOutputDevice->Connect(pEngine); |
if (pEngineChannel) { |
94 |
|
pEngineChannel->Connect(pAudioOutputDevice); |
95 |
|
pAudioOutputDevice->Connect(pEngineChannel->GetEngine()); |
96 |
|
} |
97 |
} |
} |
98 |
|
|
99 |
void SamplerChannel::SetMidiInputDevice(MidiInputDevice::type_t MidiType, MidiInputDevice::midi_chan_t MidiChannel) { |
void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) { |
100 |
// get / create desired midi device |
SetMidiInput(pDevice, this->midiPort, this->midiChannel); |
101 |
MidiInputDevice* pDevice = pSampler->GetMidiInputDevice(MidiType); |
} |
|
if (!pDevice) pDevice = pSampler->CreateMidiInputDevice(MidiType); |
|
102 |
|
|
103 |
// disconnect old device |
void SamplerChannel::SetMidiInputPort(int MidiPort) { |
104 |
if (pMidiInputDevice && pEngine) pMidiInputDevice->Disconnect(pEngine); |
SetMidiInput(pMidiInputDevice, MidiPort, this->midiChannel); |
105 |
|
} |
106 |
|
|
107 |
// connect new device |
void SamplerChannel::SetMidiInputChannel(MidiInputPort::midi_chan_t MidiChannel) { |
108 |
|
SetMidiInput(pMidiInputDevice, this->midiPort, MidiChannel); |
109 |
|
} |
110 |
|
|
111 |
|
void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, MidiInputPort::midi_chan_t MidiChannel) { |
112 |
|
// dereference old midi input port. |
113 |
|
MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort); |
114 |
|
// disconnect old device port |
115 |
|
if (pMidiInputPort && pEngineChannel) pMidiInputPort->Disconnect(pEngineChannel); |
116 |
|
// new device, port and channel |
117 |
pMidiInputDevice = pDevice; |
pMidiInputDevice = pDevice; |
118 |
if (pEngine) pMidiInputDevice->Connect(pEngine, MidiChannel); |
this->midiPort = iMidiPort; |
119 |
|
this->midiChannel = MidiChannel; |
120 |
|
// connect new device port |
121 |
|
pMidiInputPort = GetMidiInputDevicePort(this->midiPort); |
122 |
|
if (pMidiInputPort && pEngineChannel) pMidiInputPort->Connect(pEngineChannel, MidiChannel); |
123 |
|
// Ooops. |
124 |
|
if (pMidiInputPort == NULL) |
125 |
|
throw LinuxSamplerException("There is no MIDI input port with index " + ToString(iMidiPort) + "."); |
126 |
} |
} |
127 |
|
|
128 |
Engine* SamplerChannel::GetEngine() { |
EngineChannel* SamplerChannel::GetEngineChannel() { |
129 |
return pEngine; |
return pEngineChannel; |
130 |
} |
} |
131 |
|
|
132 |
MidiInputDevice* SamplerChannel::GetMidiInputDevice() { |
MidiInputPort::midi_chan_t SamplerChannel::GetMidiInputChannel() { |
133 |
return pMidiInputDevice; |
return this->midiChannel; |
134 |
|
} |
135 |
|
|
136 |
|
int SamplerChannel::GetMidiInputPort() { |
137 |
|
MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort); |
138 |
|
return (pMidiInputPort ? (int) pMidiInputPort->GetPortNumber() : -1); |
139 |
} |
} |
140 |
|
|
141 |
AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() { |
AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() { |
142 |
return pAudioOutputDevice; |
return pAudioOutputDevice; |
143 |
} |
} |
144 |
|
|
145 |
|
MidiInputDevice* SamplerChannel::GetMidiInputDevice() { |
146 |
|
return pMidiInputDevice; |
147 |
|
} |
148 |
|
|
149 |
uint SamplerChannel::Index() { |
uint SamplerChannel::Index() { |
150 |
if (iIndex >= 0) return iIndex; |
if (iIndex >= 0) return iIndex; |
151 |
|
|
152 |
std::vector<SamplerChannel*>::iterator iter = pSampler->vSamplerChannels.begin(); |
Sampler::SamplerChannelMap::iterator iter = pSampler->mSamplerChannels.begin(); |
153 |
for (int i = 0; iter != pSampler->vSamplerChannels.end(); i++, iter++) { |
for (; iter != pSampler->mSamplerChannels.end(); iter++) { |
154 |
if (*iter == this) { |
if (iter->second == this) { |
155 |
iIndex = i; |
iIndex = iter->first; |
156 |
return i; |
return iIndex; |
157 |
} |
} |
158 |
} |
} |
159 |
|
|
160 |
throw LinuxSamplerException("SamplerChannel index not found"); |
throw LinuxSamplerException("Internal error: SamplerChannel index not found"); |
161 |
|
} |
162 |
|
|
163 |
|
MidiInputPort* SamplerChannel::GetMidiInputDevicePort(int iMidiPort) { |
164 |
|
MidiInputPort* pMidiInputPort = NULL; |
165 |
|
if (pMidiInputDevice) |
166 |
|
pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort); |
167 |
|
return pMidiInputPort; |
168 |
} |
} |
169 |
|
|
170 |
|
|
171 |
|
|
172 |
// ****************************************************************** |
// ****************************************************************** |
173 |
// * Sampler |
// * Sampler |
174 |
|
|
176 |
} |
} |
177 |
|
|
178 |
Sampler::~Sampler() { |
Sampler::~Sampler() { |
179 |
// delete sampler channels |
Reset(); |
|
{ |
|
|
std::vector<SamplerChannel*>::iterator iter = vSamplerChannels.begin(); |
|
|
for (; iter != vSamplerChannels.end(); iter++) delete *iter; |
|
|
} |
|
|
|
|
|
// delete midi input devices |
|
|
{ |
|
|
MidiInputDeviceMap::iterator iter = MidiInputDevices.begin(); |
|
|
for (; iter != MidiInputDevices.end(); iter++) { |
|
|
MidiInputDevice* pDevice = iter->second; |
|
|
pDevice->StopListen(); |
|
|
delete pDevice; |
|
|
} |
|
|
} |
|
|
|
|
|
// delete audio output devices |
|
|
{ |
|
|
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
|
|
for (; iter != mAudioOutputDevices.end(); iter++) { |
|
|
AudioOutputDevice* pDevice = iter->second; |
|
|
pDevice->Stop(); |
|
|
delete pDevice; |
|
|
} |
|
|
} |
|
180 |
} |
} |
181 |
|
|
182 |
uint Sampler::SamplerChannels() { |
uint Sampler::SamplerChannels() { |
183 |
return vSamplerChannels.size(); |
return mSamplerChannels.size(); |
184 |
} |
} |
185 |
|
|
186 |
SamplerChannel* Sampler::AddSamplerChannel() { |
SamplerChannel* Sampler::AddSamplerChannel() { |
187 |
|
// if there's no sampler channel yet |
188 |
|
if (!mSamplerChannels.size()) { |
189 |
|
SamplerChannel* pChannel = new SamplerChannel(this); |
190 |
|
mSamplerChannels[0] = pChannel; |
191 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, 1)); |
192 |
|
return pChannel; |
193 |
|
} |
194 |
|
|
195 |
|
// get the highest used sampler channel index |
196 |
|
uint lastIndex = (--(mSamplerChannels.end()))->first; |
197 |
|
|
198 |
|
// check if we reached the index limit |
199 |
|
if (lastIndex + 1 < lastIndex) { |
200 |
|
// search for an unoccupied sampler channel index starting from 0 |
201 |
|
for (uint i = 0; i < lastIndex; i++) { |
202 |
|
if (mSamplerChannels.find(i) != mSamplerChannels.end()) continue; |
203 |
|
// we found an unused index, so insert the new channel there |
204 |
|
SamplerChannel* pChannel = new SamplerChannel(this); |
205 |
|
mSamplerChannels[i] = pChannel; |
206 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, i)); |
207 |
|
return pChannel; |
208 |
|
} |
209 |
|
throw LinuxSamplerException("Internal error: could not find unoccupied sampler channel index."); |
210 |
|
} |
211 |
|
|
212 |
|
// we have not reached the index limit so we just add the channel past the highest index |
213 |
SamplerChannel* pChannel = new SamplerChannel(this); |
SamplerChannel* pChannel = new SamplerChannel(this); |
214 |
vSamplerChannels.push_back(pChannel); |
mSamplerChannels[lastIndex + 1] = pChannel; |
215 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, lastIndex + 1)); |
216 |
return pChannel; |
return pChannel; |
217 |
} |
} |
218 |
|
|
219 |
SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) { |
SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) { |
220 |
if (uiSamplerChannel >= SamplerChannels()) return NULL; |
return (mSamplerChannels.find(uiSamplerChannel) != mSamplerChannels.end()) ? mSamplerChannels[uiSamplerChannel] : NULL; |
221 |
return vSamplerChannels[uiSamplerChannel]; |
} |
222 |
|
|
223 |
|
std::map<uint, SamplerChannel*> Sampler::GetSamplerChannels() { |
224 |
|
return mSamplerChannels; |
225 |
} |
} |
226 |
|
|
227 |
void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) { |
void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) { |
228 |
std::vector<SamplerChannel*>::iterator iterChan = vSamplerChannels.begin(); |
SamplerChannelMap::iterator iterChan = mSamplerChannels.begin(); |
229 |
for (; iterChan != vSamplerChannels.end(); iterChan++) { |
for (; iterChan != mSamplerChannels.end(); iterChan++) { |
230 |
if (*iterChan == pSamplerChannel) { |
if (iterChan->second == pSamplerChannel) { |
231 |
vSamplerChannels.erase(iterChan); |
mSamplerChannels.erase(iterChan); |
232 |
delete pSamplerChannel; |
delete pSamplerChannel; |
233 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, mSamplerChannels.size())); |
234 |
return; |
return; |
235 |
} |
} |
236 |
} |
} |
250 |
// create new device |
// create new device |
251 |
AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters); |
AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters); |
252 |
|
|
|
// activate device |
|
|
pDevice->Play(); |
|
|
|
|
253 |
// add new audio device to the audio device list |
// add new audio device to the audio device list |
254 |
for (uint i = 0; ; i++) { // seek for a free place starting from the beginning |
for (uint i = 0; ; i++) { // seek for a free place starting from the beginning |
255 |
if (!mAudioOutputDevices[i]) { |
if (!mAudioOutputDevices[i]) { |
265 |
return mAudioOutputDevices.size(); |
return mAudioOutputDevices.size(); |
266 |
} |
} |
267 |
|
|
268 |
|
uint Sampler::MidiInputDevices() { |
269 |
|
return mMidiInputDevices.size(); |
270 |
|
} |
271 |
|
|
272 |
std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() { |
std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() { |
273 |
return mAudioOutputDevices; |
return mAudioOutputDevices; |
274 |
} |
} |
275 |
|
|
276 |
|
std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() { |
277 |
|
return mMidiInputDevices; |
278 |
|
} |
279 |
|
|
280 |
void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (LinuxSamplerException) { |
void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (LinuxSamplerException) { |
281 |
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
282 |
for (; iter != mAudioOutputDevices.end(); iter++) { |
for (; iter != mAudioOutputDevices.end(); iter++) { |
297 |
} |
} |
298 |
} |
} |
299 |
|
|
300 |
MidiInputDevice* Sampler::CreateMidiInputDevice(MidiInputDevice::type_t MidiType) { |
void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (LinuxSamplerException) { |
301 |
// check if device already created |
MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); |
302 |
MidiInputDevice* pDevice = GetMidiInputDevice(MidiType); |
for (; iter != mMidiInputDevices.end(); iter++) { |
303 |
if (pDevice) return pDevice; |
if (iter->second == pDevice) { |
304 |
|
// check if there are still sampler engines connected to this device |
305 |
|
for (uint i = 0; i < SamplerChannels(); i++) |
306 |
|
if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw LinuxSamplerException("Sampler channel " + ToString(i) + " is still connected to the midi input device."); |
307 |
|
|
308 |
// create new device |
// disable device |
309 |
switch (MidiType) { |
pDevice->StopListen(); |
310 |
case MidiInputDevice::type_alsa: |
|
311 |
pDevice = new MidiInputDeviceAlsa; |
// remove device from the device list |
312 |
break; |
mMidiInputDevices.erase(iter); |
313 |
default: |
|
314 |
throw LinuxSamplerException("Unknown audio output device type"); |
// destroy and free device from memory |
315 |
|
delete pDevice; |
316 |
|
} |
317 |
} |
} |
318 |
|
} |
319 |
|
|
320 |
// activate device |
MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) { |
321 |
pDevice->Listen(); |
// create new device |
322 |
|
MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this); |
323 |
|
|
324 |
// add new MIDI device to the MIDI device list |
// add new device to the midi device list |
325 |
MidiInputDevices[MidiType] = pDevice; |
for (uint i = 0; ; i++) { // seek for a free place starting from the beginning |
326 |
|
if (!mMidiInputDevices[i]) { |
327 |
|
mMidiInputDevices[i] = pDevice; |
328 |
|
break; |
329 |
|
} |
330 |
|
} |
331 |
|
|
332 |
return pDevice; |
return pDevice; |
333 |
} |
} |
334 |
|
|
335 |
MidiInputDevice* Sampler::GetMidiInputDevice(MidiInputDevice::type_t MidiType) { |
void Sampler::Reset() { |
336 |
MidiInputDeviceMap::iterator iter = MidiInputDevices.find(MidiType); |
// delete sampler channels |
337 |
return (iter != MidiInputDevices.end()) ? iter->second : NULL; |
try { |
338 |
|
while (true) { |
339 |
|
SamplerChannelMap::iterator iter = mSamplerChannels.begin(); |
340 |
|
if (iter == mSamplerChannels.end()) break; |
341 |
|
RemoveSamplerChannel(iter->second); |
342 |
|
} |
343 |
|
} |
344 |
|
catch(...) { |
345 |
|
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all sampler channels, exiting.\n" << std::flush; |
346 |
|
exit(EXIT_FAILURE); |
347 |
|
} |
348 |
|
|
349 |
|
// delete midi input devices |
350 |
|
try { |
351 |
|
while (true) { |
352 |
|
MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); |
353 |
|
if (iter == mMidiInputDevices.end()) break; |
354 |
|
DestroyMidiInputDevice(iter->second); |
355 |
|
} |
356 |
|
} |
357 |
|
catch(...) { |
358 |
|
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI input devices, exiting.\n" << std::flush; |
359 |
|
exit(EXIT_FAILURE); |
360 |
|
} |
361 |
|
|
362 |
|
// delete audio output devices |
363 |
|
try { |
364 |
|
while (true) { |
365 |
|
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
366 |
|
if (iter == mAudioOutputDevices.end()) break; |
367 |
|
DestroyAudioOutputDevice(iter->second); |
368 |
|
} |
369 |
|
} |
370 |
|
catch(...) { |
371 |
|
std::cerr << "Sampler::Reset(): Exception occured while trying to delete all audio output devices, exiting.\n" << std::flush; |
372 |
|
exit(EXIT_FAILURE); |
373 |
|
} |
374 |
} |
} |
375 |
|
|
376 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |