25 |
#include "Sampler.h" |
#include "Sampler.h" |
26 |
|
|
27 |
#include "audiodriver/AudioOutputDeviceFactory.h" |
#include "audiodriver/AudioOutputDeviceFactory.h" |
28 |
#include "mididriver/MidiInputDeviceAlsa.h" |
#include "mididriver/MidiInputDeviceFactory.h" |
29 |
#include "engines/gig/Engine.h" |
#include "engines/gig/Engine.h" |
30 |
|
|
31 |
namespace LinuxSampler { |
namespace LinuxSampler { |
36 |
SamplerChannel::SamplerChannel(Sampler* pS) { |
SamplerChannel::SamplerChannel(Sampler* pS) { |
37 |
pSampler = pS; |
pSampler = pS; |
38 |
pEngine = NULL; |
pEngine = NULL; |
39 |
pMidiInputDevice = NULL; |
pMidiInputPort = NULL; |
40 |
pAudioOutputDevice = NULL; |
pAudioOutputDevice = NULL; |
41 |
iIndex = -1; |
iIndex = -1; |
42 |
|
midiChannel = MidiInputDevice::MidiInputPort::midi_chan_all; |
43 |
} |
} |
44 |
|
|
45 |
SamplerChannel::~SamplerChannel() { |
SamplerChannel::~SamplerChannel() { |
46 |
if (pEngine) { |
if (pEngine) { |
47 |
if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine); |
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine); |
48 |
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); |
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); |
49 |
delete pEngine; |
delete pEngine; |
50 |
} |
} |
65 |
|
|
66 |
// disconnect old engine |
// disconnect old engine |
67 |
if (pEngine) { |
if (pEngine) { |
68 |
if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine); |
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine); |
69 |
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); |
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); |
70 |
delete pEngine; |
delete pEngine; |
71 |
} |
} |
72 |
|
|
73 |
// connect new engine |
// connect new engine |
74 |
pEngine = pNewEngine; |
pEngine = pNewEngine; |
75 |
if (pMidiInputDevice) pMidiInputDevice->Connect(pNewEngine, (MidiInputDevice::midi_chan_t) Index()); |
if (pMidiInputPort) pMidiInputPort->Connect(pNewEngine, (MidiInputDevice::MidiInputPort::midi_chan_t) Index()); |
76 |
if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine); |
if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine); |
77 |
dmsg(2,("OK\n")); |
dmsg(2,("OK\n")); |
78 |
} |
} |
86 |
if (pEngine) pAudioOutputDevice->Connect(pEngine); |
if (pEngine) pAudioOutputDevice->Connect(pEngine); |
87 |
} |
} |
88 |
|
|
89 |
void SamplerChannel::SetMidiInputDevice(MidiInputDevice::type_t MidiType, MidiInputDevice::midi_chan_t MidiChannel) { |
void SamplerChannel::SetMidiInputPort(MidiInputDevice* pDevice, int midiPort, MidiInputDevice::MidiInputPort::midi_chan_t MidiChannel) { |
90 |
// get / create desired midi device |
// disconnect old port |
91 |
MidiInputDevice* pDevice = pSampler->GetMidiInputDevice(MidiType); |
if (pMidiInputPort && pEngine) pMidiInputPort->Disconnect(pEngine); |
92 |
if (!pDevice) pDevice = pSampler->CreateMidiInputDevice(MidiType); |
|
93 |
|
// connect new port |
94 |
// disconnect old device |
pMidiInputPort = pDevice->GetPort(midiPort); |
95 |
if (pMidiInputDevice && pEngine) pMidiInputDevice->Disconnect(pEngine); |
if (!pMidiInputPort) |
96 |
|
throw LinuxSamplerException("MIDI port doesn't exist"); |
97 |
// connect new device |
if (pEngine) { |
98 |
pMidiInputDevice = pDevice; |
pMidiInputPort->Connect(pEngine, MidiChannel); |
99 |
if (pEngine) pMidiInputDevice->Connect(pEngine, MidiChannel); |
this->midiChannel = MidiChannel; |
100 |
|
} |
101 |
} |
} |
102 |
|
|
103 |
Engine* SamplerChannel::GetEngine() { |
Engine* SamplerChannel::GetEngine() { |
104 |
return pEngine; |
return pEngine; |
105 |
} |
} |
106 |
|
|
107 |
MidiInputDevice* SamplerChannel::GetMidiInputDevice() { |
MidiInputDevice::MidiInputPort* SamplerChannel::GetMidiInputPort() { |
108 |
return pMidiInputDevice; |
return pMidiInputPort; |
109 |
} |
} |
110 |
|
|
111 |
AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() { |
AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() { |
112 |
return pAudioOutputDevice; |
return pAudioOutputDevice; |
113 |
} |
} |
114 |
|
|
115 |
|
MidiInputDevice* SamplerChannel::GetMidiInputDevice() { |
116 |
|
MidiInputDevice::MidiInputPort* port = GetMidiInputPort(); |
117 |
|
if (!port) |
118 |
|
return NULL; |
119 |
|
return port->GetDevice(); |
120 |
|
} |
121 |
|
|
122 |
uint SamplerChannel::Index() { |
uint SamplerChannel::Index() { |
123 |
if (iIndex >= 0) return iIndex; |
if (iIndex >= 0) return iIndex; |
124 |
|
|
149 |
|
|
150 |
// delete midi input devices |
// delete midi input devices |
151 |
{ |
{ |
152 |
MidiInputDeviceMap::iterator iter = MidiInputDevices.begin(); |
MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); |
153 |
for (; iter != MidiInputDevices.end(); iter++) { |
for (; iter != mMidiInputDevices.end(); iter++) { |
154 |
MidiInputDevice* pDevice = iter->second; |
MidiInputDevice* pDevice = iter->second; |
155 |
pDevice->StopListen(); |
pDevice->StopListen(); |
156 |
delete pDevice; |
delete pDevice; |
226 |
return mAudioOutputDevices.size(); |
return mAudioOutputDevices.size(); |
227 |
} |
} |
228 |
|
|
229 |
|
uint Sampler::MidiInputDevices() { |
230 |
|
return mMidiInputDevices.size(); |
231 |
|
} |
232 |
|
|
233 |
std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() { |
std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() { |
234 |
return mAudioOutputDevices; |
return mAudioOutputDevices; |
235 |
} |
} |
236 |
|
|
237 |
|
std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() { |
238 |
|
return mMidiInputDevices; |
239 |
|
} |
240 |
|
|
241 |
void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (LinuxSamplerException) { |
void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (LinuxSamplerException) { |
242 |
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
243 |
for (; iter != mAudioOutputDevices.end(); iter++) { |
for (; iter != mAudioOutputDevices.end(); iter++) { |
258 |
} |
} |
259 |
} |
} |
260 |
|
|
261 |
MidiInputDevice* Sampler::CreateMidiInputDevice(MidiInputDevice::type_t MidiType) { |
void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (LinuxSamplerException) { |
262 |
// check if device already created |
MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); |
263 |
MidiInputDevice* pDevice = GetMidiInputDevice(MidiType); |
for (; iter != mMidiInputDevices.end(); iter++) { |
264 |
if (pDevice) return pDevice; |
if (iter->second == pDevice) { |
265 |
|
// check if there are still sampler engines connected to this device |
266 |
|
for (uint i = 0; i < SamplerChannels(); i++) |
267 |
|
if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw LinuxSamplerException("Sampler channel " + ToString(i) + " is still connected to the midi input device."); |
268 |
|
|
269 |
|
// disable device |
270 |
|
pDevice->StopListen(); |
271 |
|
|
272 |
// create new device |
// remove device from the device list |
273 |
switch (MidiType) { |
mMidiInputDevices.erase(iter); |
274 |
case MidiInputDevice::type_alsa: |
|
275 |
pDevice = new MidiInputDeviceAlsa; |
// destroy and free device from memory |
276 |
break; |
delete pDevice; |
277 |
default: |
} |
|
throw LinuxSamplerException("Unknown audio output device type"); |
|
278 |
} |
} |
279 |
|
} |
280 |
|
|
281 |
|
MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) { |
282 |
|
// create new device |
283 |
|
MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters); |
284 |
|
|
285 |
// activate device |
// activate device |
286 |
pDevice->Listen(); |
pDevice->Listen(); |
287 |
|
|
288 |
// add new MIDI device to the MIDI device list |
// add new device to the midi device list |
289 |
MidiInputDevices[MidiType] = pDevice; |
for (uint i = 0; ; i++) { // seek for a free place starting from the beginning |
290 |
|
if (!mMidiInputDevices[i]) { |
291 |
|
mMidiInputDevices[i] = pDevice; |
292 |
|
break; |
293 |
|
} |
294 |
|
} |
295 |
|
|
296 |
return pDevice; |
return pDevice; |
297 |
} |
} |
298 |
|
|
|
MidiInputDevice* Sampler::GetMidiInputDevice(MidiInputDevice::type_t MidiType) { |
|
|
MidiInputDeviceMap::iterator iter = MidiInputDevices.find(MidiType); |
|
|
return (iter != MidiInputDevices.end()) ? iter->second : NULL; |
|
|
} |
|
|
|
|
299 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |