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/EngineChannelFactory.h" |
29 |
#include "drivers/audio/AudioOutputDeviceFactory.h" |
#include "drivers/audio/AudioOutputDeviceFactory.h" |
30 |
#include "drivers/midi/MidiInputDeviceFactory.h" |
#include "drivers/midi/MidiInputDeviceFactory.h" |
31 |
#include "engines/gig/Engine.h" |
#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; |
|
pMidiInputDevice = NULL; |
|
41 |
pAudioOutputDevice = NULL; |
pAudioOutputDevice = NULL; |
42 |
midiPort = 0; |
pMidiInputDevice = NULL; |
43 |
midiChannel = MidiInputPort::midi_chan_all; |
iMidiPort = 0; |
44 |
|
midiChannel = midi_chan_all; |
45 |
iIndex = -1; |
iIndex = -1; |
46 |
} |
} |
47 |
|
|
48 |
SamplerChannel::~SamplerChannel() { |
SamplerChannel::~SamplerChannel() { |
49 |
if (pEngine) { |
if (pEngineChannel) { |
50 |
MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort); |
MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : __GetMidiInputDevicePort(GetMidiInputChannel()); |
51 |
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine); |
if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel); |
52 |
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); |
if (pEngineChannel) { |
53 |
delete pEngine; |
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"); |
65 |
case Engine::type_gig: |
|
66 |
pNewEngine = new gig::Engine; |
//FIXME: hack to allow fast retrieval of engine channel's sampler channel index |
67 |
break; |
pNewEngineChannel->iSamplerChannelIndex = Index(); |
|
default: |
|
|
throw LinuxSamplerException("Unknown engine type"); |
|
|
} |
|
68 |
|
|
69 |
// dereference midi input port. |
// dereference midi input port. |
70 |
MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort); |
MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort()); |
71 |
// disconnect old engine |
// disconnect old engine channel |
72 |
if (pEngine) { |
if (pEngineChannel) { |
73 |
if (pMidiInputPort) pMidiInputPort->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 channel |
79 |
|
if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, GetMidiInputChannel()); |
80 |
|
if (pAudioOutputDevice) { |
81 |
|
pNewEngineChannel->Connect(pAudioOutputDevice); |
82 |
|
pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine()); |
83 |
|
} |
84 |
|
pEngineChannel = pNewEngineChannel; |
85 |
|
|
86 |
|
// from now on get MIDI device and port from EngineChannel object |
87 |
|
this->pMidiInputDevice = NULL; |
88 |
|
this->iMidiPort = 0; |
89 |
|
|
|
// connect new engine |
|
|
pEngine = pNewEngine; |
|
|
if (pMidiInputPort) pMidiInputPort->Connect(pNewEngine, this->midiChannel); |
|
|
if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine); |
|
90 |
dmsg(2,("OK\n")); |
dmsg(2,("OK\n")); |
91 |
} |
} |
92 |
|
|
93 |
void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) { |
void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) { |
94 |
// disconnect old device |
// disconnect old device |
95 |
if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine); |
if (pAudioOutputDevice && pEngineChannel) pEngineChannel->DisconnectAudioOutputDevice(); |
96 |
// connect new device |
// connect new device |
97 |
pAudioOutputDevice = pDevice; |
pAudioOutputDevice = pDevice; |
98 |
if (pEngine) pAudioOutputDevice->Connect(pEngine); |
if (pEngineChannel) { |
99 |
|
pEngineChannel->Connect(pAudioOutputDevice); |
100 |
|
pAudioOutputDevice->Connect(pEngineChannel->GetEngine()); |
101 |
|
} |
102 |
} |
} |
103 |
|
|
104 |
void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) { |
void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) { |
105 |
SetMidiInput(pDevice, this->midiPort, this->midiChannel); |
SetMidiInput(pDevice, 0, GetMidiInputChannel()); |
106 |
} |
} |
107 |
|
|
108 |
void SamplerChannel::SetMidiInputPort(int MidiPort) { |
void SamplerChannel::SetMidiInputPort(int MidiPort) { |
109 |
SetMidiInput(pMidiInputDevice, MidiPort, this->midiChannel); |
SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel()); |
110 |
} |
} |
111 |
|
|
112 |
void SamplerChannel::SetMidiInputChannel(MidiInputPort::midi_chan_t MidiChannel) { |
void SamplerChannel::SetMidiInputChannel(midi_chan_t MidiChannel) { |
113 |
SetMidiInput(pMidiInputDevice, this->midiPort, MidiChannel); |
SetMidiInput(GetMidiInputDevice(), GetMidiInputPort(), MidiChannel); |
114 |
} |
} |
115 |
|
|
116 |
void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, MidiInputPort::midi_chan_t MidiChannel) { |
void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) { |
117 |
// dereference old midi input port. |
if (!pDevice) throw LinuxSamplerException("No MIDI input device assigned."); |
118 |
MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort); |
|
119 |
|
// get old and new midi input port |
120 |
|
MidiInputPort* pOldMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort()); |
121 |
|
MidiInputPort* pNewMidiInputPort = pDevice->GetPort(iMidiPort); |
122 |
|
|
123 |
// disconnect old device port |
// disconnect old device port |
124 |
if (pMidiInputPort && pEngine) pMidiInputPort->Disconnect(pEngine); |
if (pOldMidiInputPort && pEngineChannel) pOldMidiInputPort->Disconnect(pEngineChannel); |
125 |
// new device, port and channel |
// remember new device, port and channel if not engine channel yet created |
126 |
pMidiInputDevice = pDevice; |
if (!pEngineChannel) { |
127 |
this->midiPort = iMidiPort; |
this->pMidiInputDevice = pDevice; |
128 |
this->midiChannel = MidiChannel; |
this->iMidiPort = iMidiPort; |
129 |
|
this->midiChannel = MidiChannel; |
130 |
|
} |
131 |
|
|
132 |
// connect new device port |
// connect new device port |
133 |
pMidiInputPort = GetMidiInputDevicePort(this->midiPort); |
if (pNewMidiInputPort && pEngineChannel) pNewMidiInputPort->Connect(pEngineChannel, MidiChannel); |
|
if (pMidiInputPort && pEngine) pMidiInputPort->Connect(pEngine, MidiChannel); |
|
134 |
// Ooops. |
// Ooops. |
135 |
if (pMidiInputPort == NULL) |
if (pNewMidiInputPort == NULL) |
136 |
throw LinuxSamplerException("There is no MIDI input port with index " + ToString(iMidiPort) + "."); |
throw LinuxSamplerException("There is no MIDI input port with index " + ToString(iMidiPort) + "."); |
137 |
} |
} |
138 |
|
|
139 |
Engine* SamplerChannel::GetEngine() { |
EngineChannel* SamplerChannel::GetEngineChannel() { |
140 |
return pEngine; |
return pEngineChannel; |
141 |
} |
} |
142 |
|
|
143 |
MidiInputPort::midi_chan_t SamplerChannel::GetMidiInputChannel() { |
midi_chan_t SamplerChannel::GetMidiInputChannel() { |
144 |
|
if (pEngineChannel) this->midiChannel = pEngineChannel->MidiChannel(); |
145 |
return this->midiChannel; |
return this->midiChannel; |
146 |
} |
} |
147 |
|
|
148 |
int SamplerChannel::GetMidiInputPort() { |
int SamplerChannel::GetMidiInputPort() { |
149 |
MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort); |
MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : NULL; |
150 |
return (pMidiInputPort ? (int) pMidiInputPort->GetPortNumber() : -1); |
if (pMidiInputPort) this->iMidiPort = (int) pMidiInputPort->GetPortNumber(); |
151 |
|
return iMidiPort; |
152 |
} |
} |
153 |
|
|
154 |
AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() { |
AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() { |
156 |
} |
} |
157 |
|
|
158 |
MidiInputDevice* SamplerChannel::GetMidiInputDevice() { |
MidiInputDevice* SamplerChannel::GetMidiInputDevice() { |
159 |
|
if (pEngineChannel) |
160 |
|
pMidiInputDevice = (pEngineChannel->GetMidiInputPort()) ? pEngineChannel->GetMidiInputPort()->GetDevice() : NULL; |
161 |
return pMidiInputDevice; |
return pMidiInputDevice; |
162 |
} |
} |
163 |
|
|
175 |
throw LinuxSamplerException("Internal error: SamplerChannel index not found"); |
throw LinuxSamplerException("Internal error: SamplerChannel index not found"); |
176 |
} |
} |
177 |
|
|
178 |
MidiInputPort* SamplerChannel::GetMidiInputDevicePort(int iMidiPort) { |
MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) { |
179 |
MidiInputPort* pMidiInputPort = NULL; |
MidiInputPort* pMidiInputPort = NULL; |
180 |
|
MidiInputDevice* pMidiInputDevice = GetMidiInputDevice(); |
181 |
if (pMidiInputDevice) |
if (pMidiInputDevice) |
182 |
pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort); |
pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort); |
183 |
return pMidiInputPort; |
return pMidiInputPort; |
204 |
if (!mSamplerChannels.size()) { |
if (!mSamplerChannels.size()) { |
205 |
SamplerChannel* pChannel = new SamplerChannel(this); |
SamplerChannel* pChannel = new SamplerChannel(this); |
206 |
mSamplerChannels[0] = pChannel; |
mSamplerChannels[0] = pChannel; |
207 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, 1)); |
208 |
return pChannel; |
return pChannel; |
209 |
} |
} |
210 |
|
|
219 |
// we found an unused index, so insert the new channel there |
// we found an unused index, so insert the new channel there |
220 |
SamplerChannel* pChannel = new SamplerChannel(this); |
SamplerChannel* pChannel = new SamplerChannel(this); |
221 |
mSamplerChannels[i] = pChannel; |
mSamplerChannels[i] = pChannel; |
222 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, i)); |
223 |
return pChannel; |
return pChannel; |
224 |
} |
} |
225 |
throw LinuxSamplerException("Internal error: could not find unoccupied sampler channel index."); |
throw LinuxSamplerException("Internal error: could not find unoccupied sampler channel index."); |
228 |
// 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 |
229 |
SamplerChannel* pChannel = new SamplerChannel(this); |
SamplerChannel* pChannel = new SamplerChannel(this); |
230 |
mSamplerChannels[lastIndex + 1] = pChannel; |
mSamplerChannels[lastIndex + 1] = pChannel; |
231 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, lastIndex + 1)); |
232 |
return pChannel; |
return pChannel; |
233 |
} |
} |
234 |
|
|
246 |
if (iterChan->second == pSamplerChannel) { |
if (iterChan->second == pSamplerChannel) { |
247 |
mSamplerChannels.erase(iterChan); |
mSamplerChannels.erase(iterChan); |
248 |
delete pSamplerChannel; |
delete pSamplerChannel; |
249 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, mSamplerChannels.size())); |
250 |
return; |
return; |
251 |
} |
} |
252 |
} |
} |
335 |
|
|
336 |
MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) { |
MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) { |
337 |
// create new device |
// create new device |
338 |
MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters); |
MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this); |
339 |
|
|
340 |
// add new device to the midi device list |
// add new device to the midi device list |
341 |
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 |
351 |
void Sampler::Reset() { |
void Sampler::Reset() { |
352 |
// delete sampler channels |
// delete sampler channels |
353 |
try { |
try { |
354 |
SamplerChannelMap::iterator iter = mSamplerChannels.begin(); |
while (true) { |
355 |
for (; iter != mSamplerChannels.end(); iter++) { |
SamplerChannelMap::iterator iter = mSamplerChannels.begin(); |
356 |
RemoveSamplerChannel(iter->second); |
if (iter == mSamplerChannels.end()) break; |
357 |
|
RemoveSamplerChannel(iter->second); |
358 |
} |
} |
359 |
} |
} |
360 |
catch(...) { |
catch(...) { |
364 |
|
|
365 |
// delete midi input devices |
// delete midi input devices |
366 |
try { |
try { |
367 |
MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); |
while (true) { |
368 |
for (; iter != mMidiInputDevices.end(); iter++) { |
MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin(); |
369 |
DestroyMidiInputDevice(iter->second); |
if (iter == mMidiInputDevices.end()) break; |
370 |
|
DestroyMidiInputDevice(iter->second); |
371 |
} |
} |
372 |
} |
} |
373 |
catch(...) { |
catch(...) { |
377 |
|
|
378 |
// delete audio output devices |
// delete audio output devices |
379 |
try { |
try { |
380 |
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
while (true) { |
381 |
for (; iter != mAudioOutputDevices.end(); iter++) { |
AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin(); |
382 |
DestroyAudioOutputDevice(iter->second); |
if (iter == mAudioOutputDevices.end()) break; |
383 |
|
DestroyAudioOutputDevice(iter->second); |
384 |
} |
} |
385 |
} |
} |
386 |
catch(...) { |
catch(...) { |