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 "engines/EngineFactory.h" |
29 |
|
#include "engines/EngineChannelFactory.h" |
30 |
#include "drivers/audio/AudioOutputDeviceFactory.h" |
#include "drivers/audio/AudioOutputDeviceFactory.h" |
31 |
#include "drivers/midi/MidiInputDeviceFactory.h" |
#include "drivers/midi/MidiInputDeviceFactory.h" |
32 |
#include "engines/gig/Engine.h" |
#include "network/lscpserver.h" |
33 |
|
|
34 |
namespace LinuxSampler { |
namespace LinuxSampler { |
35 |
|
|
38 |
|
|
39 |
SamplerChannel::SamplerChannel(Sampler* pS) { |
SamplerChannel::SamplerChannel(Sampler* pS) { |
40 |
pSampler = pS; |
pSampler = pS; |
41 |
pEngine = NULL; |
pEngineChannel = NULL; |
|
pMidiInputDevice = NULL; |
|
42 |
pAudioOutputDevice = NULL; |
pAudioOutputDevice = NULL; |
43 |
midiPort = 0; |
pMidiInputDevice = NULL; |
44 |
midiChannel = MidiInputDevice::MidiInputPort::midi_chan_all; |
iMidiPort = 0; |
45 |
|
midiChannel = midi_chan_all; |
46 |
iIndex = -1; |
iIndex = -1; |
47 |
} |
} |
48 |
|
|
49 |
SamplerChannel::~SamplerChannel() { |
SamplerChannel::~SamplerChannel() { |
50 |
if (pEngine) { |
if (pEngineChannel) { |
51 |
MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort); |
MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : __GetMidiInputDevicePort(GetMidiInputChannel()); |
52 |
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine); |
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel); |
53 |
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); |
if (pEngineChannel) { |
54 |
delete pEngine; |
if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice(); |
55 |
|
EngineChannelFactory::Destroy(pEngineChannel); |
56 |
|
} |
57 |
} |
} |
58 |
} |
} |
59 |
|
|
60 |
void SamplerChannel::LoadEngine(Engine::type_t EngineType) { |
void SamplerChannel::SetEngineType(String EngineType) throw (LinuxSamplerException) { |
61 |
dmsg(2,("SamplerChannel: Loading engine...")); |
dmsg(2,("SamplerChannel: Assigning engine type...")); |
62 |
|
|
63 |
// create new engine |
// create new engine channel |
64 |
Engine* pNewEngine = NULL; |
EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType); |
65 |
switch (EngineType) { |
if (!pNewEngineChannel) throw LinuxSamplerException("Unknown engine type"); |
66 |
case Engine::type_gig: |
|
67 |
pNewEngine = new gig::Engine; |
//FIXME: hack to allow fast retrieval of engine channel's sampler channel index |
68 |
break; |
pNewEngineChannel->iSamplerChannelIndex = Index(); |
|
default: |
|
|
throw LinuxSamplerException("Unknown engine type"); |
|
|
} |
|
69 |
|
|
70 |
// dereference midi input port. |
// dereference midi input port. |
71 |
MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort); |
MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort()); |
72 |
// disconnect old engine |
// disconnect old engine channel |
73 |
if (pEngine) { |
if (pEngineChannel) { |
74 |
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine); |
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel); |
75 |
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); |
if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice(); |
76 |
delete pEngine; |
EngineChannelFactory::Destroy(pEngineChannel); |
77 |
} |
} |
78 |
|
|
79 |
|
// connect new engine channel |
80 |
|
if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, GetMidiInputChannel()); |
81 |
|
if (pAudioOutputDevice) { |
82 |
|
pNewEngineChannel->Connect(pAudioOutputDevice); |
83 |
|
pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine()); |
84 |
|
} |
85 |
|
pEngineChannel = pNewEngineChannel; |
86 |
|
|
87 |
|
// from now on get MIDI device and port from EngineChannel object |
88 |
|
this->pMidiInputDevice = NULL; |
89 |
|
this->iMidiPort = 0; |
90 |
|
|
91 |
|
pEngineChannel->StatusChanged(true); |
92 |
|
|
|
// connect new engine |
|
|
pEngine = pNewEngine; |
|
|
if (pMidiInputPort) pMidiInputPort->Connect(pNewEngine, this->midiChannel); |
|
|
if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine); |
|
93 |
dmsg(2,("OK\n")); |
dmsg(2,("OK\n")); |
94 |
} |
} |
95 |
|
|
96 |
void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) { |
void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) { |
97 |
// disconnect old device |
// disconnect old device |
98 |
if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine); |
if (pAudioOutputDevice && pEngineChannel) pEngineChannel->DisconnectAudioOutputDevice(); |
99 |
// connect new device |
// connect new device |
100 |
pAudioOutputDevice = pDevice; |
pAudioOutputDevice = pDevice; |
101 |
if (pEngine) pAudioOutputDevice->Connect(pEngine); |
if (pEngineChannel) { |
102 |
|
pEngineChannel->Connect(pAudioOutputDevice); |
103 |
|
pAudioOutputDevice->Connect(pEngineChannel->GetEngine()); |
104 |
|
} |
105 |
} |
} |
106 |
|
|
107 |
void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) { |
void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) { |
108 |
SetMidiInput(pDevice, this->midiPort, this->midiChannel); |
SetMidiInput(pDevice, 0, GetMidiInputChannel()); |
109 |
} |
} |
110 |
|
|
111 |
void SamplerChannel::SetMidiInputPort(int MidiPort) { |
void SamplerChannel::SetMidiInputPort(int MidiPort) { |
112 |
SetMidiInput(pMidiInputDevice, MidiPort, this->midiChannel); |
SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel()); |
113 |
} |
} |
114 |
|
|
115 |
void SamplerChannel::SetMidiInputChannel(MidiInputDevice::MidiInputPort::midi_chan_t MidiChannel) { |
void SamplerChannel::SetMidiInputChannel(midi_chan_t MidiChannel) { |
116 |
SetMidiInput(pMidiInputDevice, this->midiPort, MidiChannel); |
SetMidiInput(GetMidiInputDevice(), GetMidiInputPort(), MidiChannel); |
117 |
} |
} |
118 |
|
|
119 |
void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int MidiPort, MidiInputDevice::MidiInputPort::midi_chan_t MidiChannel) { |
void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) { |
120 |
// dereference old midi input port. |
if (!pDevice) throw LinuxSamplerException("No MIDI input device assigned."); |
121 |
MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort); |
|
122 |
|
// get old and new midi input port |
123 |
|
MidiInputPort* pOldMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort()); |
124 |
|
MidiInputPort* pNewMidiInputPort = pDevice->GetPort(iMidiPort); |
125 |
|
|
126 |
// disconnect old device port |
// disconnect old device port |
127 |
if (pMidiInputPort && pEngine) pMidiInputPort->Disconnect(pEngine); |
if (pOldMidiInputPort && pEngineChannel) pOldMidiInputPort->Disconnect(pEngineChannel); |
128 |
// new device, port and channel |
// remember new device, port and channel if not engine channel yet created |
129 |
pMidiInputDevice = pDevice; |
if (!pEngineChannel) { |
130 |
this->midiPort = MidiPort; |
this->pMidiInputDevice = pDevice; |
131 |
this->midiChannel = MidiChannel; |
this->iMidiPort = iMidiPort; |
132 |
|
this->midiChannel = MidiChannel; |
133 |
|
} |
134 |
|
|
135 |
// connect new device port |
// connect new device port |
136 |
pMidiInputPort = GetMidiInputDevicePort(this->midiPort); |
if (pNewMidiInputPort && pEngineChannel) pNewMidiInputPort->Connect(pEngineChannel, MidiChannel); |
|
if (pMidiInputPort && pEngine) pMidiInputPort->Connect(pEngine, MidiChannel); |
|
137 |
// Ooops. |
// Ooops. |
138 |
if (pMidiInputPort == NULL) |
if (pNewMidiInputPort == NULL) |
139 |
throw LinuxSamplerException("There is no MIDI input port with index " + ToString(MidiPort) + "."); |
throw LinuxSamplerException("There is no MIDI input port with index " + ToString(iMidiPort) + "."); |
140 |
} |
} |
141 |
|
|
142 |
Engine* SamplerChannel::GetEngine() { |
EngineChannel* SamplerChannel::GetEngineChannel() { |
143 |
return pEngine; |
return pEngineChannel; |
144 |
} |
} |
145 |
|
|
146 |
MidiInputDevice::MidiInputPort::midi_chan_t SamplerChannel::GetMidiInputChannel() { |
midi_chan_t SamplerChannel::GetMidiInputChannel() { |
147 |
|
if (pEngineChannel) this->midiChannel = pEngineChannel->MidiChannel(); |
148 |
return this->midiChannel; |
return this->midiChannel; |
149 |
} |
} |
150 |
|
|
151 |
int SamplerChannel::GetMidiInputPort() { |
int SamplerChannel::GetMidiInputPort() { |
152 |
MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort); |
MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : NULL; |
153 |
return (pMidiInputPort ? (int) pMidiInputPort->GetPortNumber() : -1); |
if (pMidiInputPort) this->iMidiPort = (int) pMidiInputPort->GetPortNumber(); |
154 |
|
return iMidiPort; |
155 |
} |
} |
156 |
|
|
157 |
AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() { |
AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() { |
159 |
} |
} |
160 |
|
|
161 |
MidiInputDevice* SamplerChannel::GetMidiInputDevice() { |
MidiInputDevice* SamplerChannel::GetMidiInputDevice() { |
162 |
|
if (pEngineChannel) |
163 |
|
pMidiInputDevice = (pEngineChannel->GetMidiInputPort()) ? pEngineChannel->GetMidiInputPort()->GetDevice() : NULL; |
164 |
return pMidiInputDevice; |
return pMidiInputDevice; |
165 |
} |
} |
166 |
|
|
178 |
throw LinuxSamplerException("Internal error: SamplerChannel index not found"); |
throw LinuxSamplerException("Internal error: SamplerChannel index not found"); |
179 |
} |
} |
180 |
|
|
181 |
MidiInputDevice::MidiInputPort* SamplerChannel::GetMidiInputDevicePort(int MidiPort) { |
MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) { |
182 |
MidiInputDevice::MidiInputPort *pMidiInputPort = NULL; |
MidiInputPort* pMidiInputPort = NULL; |
183 |
|
MidiInputDevice* pMidiInputDevice = GetMidiInputDevice(); |
184 |
if (pMidiInputDevice) |
if (pMidiInputDevice) |
185 |
pMidiInputPort = pMidiInputDevice->GetPort(MidiPort); |
pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort); |
186 |
return pMidiInputPort; |
return pMidiInputPort; |
187 |
} |
} |
188 |
|
|
207 |
if (!mSamplerChannels.size()) { |
if (!mSamplerChannels.size()) { |
208 |
SamplerChannel* pChannel = new SamplerChannel(this); |
SamplerChannel* pChannel = new SamplerChannel(this); |
209 |
mSamplerChannels[0] = pChannel; |
mSamplerChannels[0] = pChannel; |
210 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, 1)); |
211 |
return pChannel; |
return pChannel; |
212 |
} |
} |
213 |
|
|
222 |
// we found an unused index, so insert the new channel there |
// we found an unused index, so insert the new channel there |
223 |
SamplerChannel* pChannel = new SamplerChannel(this); |
SamplerChannel* pChannel = new SamplerChannel(this); |
224 |
mSamplerChannels[i] = pChannel; |
mSamplerChannels[i] = pChannel; |
225 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, i)); |
226 |
return pChannel; |
return pChannel; |
227 |
} |
} |
228 |
throw LinuxSamplerException("Internal error: could not find unoccupied sampler channel index."); |
throw LinuxSamplerException("Internal error: could not find unoccupied sampler channel index."); |
231 |
// we have not reached the index limit so we just add the channel past the highest index |
// we have not reached the index limit so we just add the channel past the highest index |
232 |
SamplerChannel* pChannel = new SamplerChannel(this); |
SamplerChannel* pChannel = new SamplerChannel(this); |
233 |
mSamplerChannels[lastIndex + 1] = pChannel; |
mSamplerChannels[lastIndex + 1] = pChannel; |
234 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, lastIndex + 1)); |
235 |
return pChannel; |
return pChannel; |
236 |
} |
} |
237 |
|
|
249 |
if (iterChan->second == pSamplerChannel) { |
if (iterChan->second == pSamplerChannel) { |
250 |
mSamplerChannels.erase(iterChan); |
mSamplerChannels.erase(iterChan); |
251 |
delete pSamplerChannel; |
delete pSamplerChannel; |
252 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, mSamplerChannels.size())); |
253 |
return; |
return; |
254 |
} |
} |
255 |
} |
} |
307 |
// disable device |
// disable device |
308 |
pDevice->Stop(); |
pDevice->Stop(); |
309 |
|
|
|
// remove device from the device list |
|
|
mAudioOutputDevices.erase(iter); |
|
|
|
|
310 |
// destroy and free device from memory |
// destroy and free device from memory |
311 |
delete pDevice; |
delete pDevice; |
312 |
} |
} |
313 |
} |
} |
314 |
|
// remove devices from the device list |
315 |
|
mAudioOutputDevices.clear(); |
316 |
} |
} |
317 |
|
|
318 |
void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (LinuxSamplerException) { |
void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (LinuxSamplerException) { |
326 |
// disable device |
// disable device |
327 |
pDevice->StopListen(); |
pDevice->StopListen(); |
328 |
|
|
|
// remove device from the device list |
|
|
mMidiInputDevices.erase(iter); |
|
|
|
|
329 |
// destroy and free device from memory |
// destroy and free device from memory |
330 |
delete pDevice; |
delete pDevice; |
331 |
} |
} |
332 |
} |
} |
333 |
|
// remove devices from the device list |
334 |
|
mMidiInputDevices.clear(); |
335 |
} |
} |
336 |
|
|
337 |
MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) { |
MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) { |
338 |
// create new device |
// create new device |
339 |
MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters); |
MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this); |
340 |
|
|
341 |
// add new device to the midi device list |
// add new device to the midi device list |
342 |
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 |
349 |
return pDevice; |
return pDevice; |
350 |
} |
} |
351 |
|
|
352 |
|
int Sampler::GetVoiceCount() { |
353 |
|
int count = 0; |
354 |
|
std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin(); |
355 |
|
|
356 |
|
for(; it != EngineFactory::EngineInstances().end(); it++) { |
357 |
|
count += (*it)->VoiceCount(); |
358 |
|
} |
359 |
|
|
360 |
|
return count; |
361 |
|
} |
362 |
|
|
363 |
void Sampler::Reset() { |
void Sampler::Reset() { |
364 |
// delete sampler channels |
// delete sampler channels |
365 |
try { |
try { |
366 |
SamplerChannelMap::iterator iter = mSamplerChannels.begin(); |
while (true) { |
367 |
for (; iter != mSamplerChannels.end(); iter++) { |
SamplerChannelMap::iterator iter = mSamplerChannels.begin(); |
368 |
RemoveSamplerChannel(iter->second); |
if (iter == mSamplerChannels.end()) break; |
369 |
|
RemoveSamplerChannel(iter->second); |
370 |
} |
} |
371 |
} |
} |
372 |
catch(...) { |
catch(...) { |
376 |
|
|
377 |
// delete midi input devices |
// delete midi input devices |
378 |
try { |
try { |
379 |
MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); |
while (true) { |
380 |
for (; iter != mMidiInputDevices.end(); iter++) { |
MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); |
381 |
DestroyMidiInputDevice(iter->second); |
if (iter == mMidiInputDevices.end()) break; |
382 |
|
DestroyMidiInputDevice(iter->second); |
383 |
} |
} |
384 |
} |
} |
385 |
catch(...) { |
catch(...) { |
389 |
|
|
390 |
// delete audio output devices |
// delete audio output devices |
391 |
try { |
try { |
392 |
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
while (true) { |
393 |
for (; iter != mAudioOutputDevices.end(); iter++) { |
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
394 |
DestroyAudioOutputDevice(iter->second); |
if (iter == mAudioOutputDevices.end()) break; |
395 |
|
DestroyAudioOutputDevice(iter->second); |
396 |
} |
} |
397 |
} |
} |
398 |
catch(...) { |
catch(...) { |