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