1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* * |
7 |
* This program is free software; you can redistribute it and/or modify * |
8 |
* it under the terms of the GNU General Public License as published by * |
9 |
* the Free Software Foundation; either version 2 of the License, or * |
10 |
* (at your option) any later version. * |
11 |
* * |
12 |
* This program is distributed in the hope that it will be useful, * |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
15 |
* GNU General Public License for more details. * |
16 |
* * |
17 |
* You should have received a copy of the GNU General Public License * |
18 |
* along with this program; if not, write to the Free Software * |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
20 |
* MA 02111-1307 USA * |
21 |
***************************************************************************/ |
22 |
|
23 |
#include "Sampler.h" |
24 |
|
25 |
#include "audiodriver/AudioOutputDeviceAlsa.h" |
26 |
#include "audiodriver/AudioOutputDeviceJack.h" |
27 |
#include "mididriver/MidiInputDeviceAlsa.h" |
28 |
#include "engines/gig/Engine.h" |
29 |
|
30 |
namespace LinuxSampler { |
31 |
|
32 |
// ****************************************************************** |
33 |
// * SamplerChannel |
34 |
|
35 |
SamplerChannel::SamplerChannel(Sampler* pS) { |
36 |
pSampler = pS; |
37 |
pEngine = NULL; |
38 |
pMidiInputDevice = NULL; |
39 |
pAudioOutputDevice = NULL; |
40 |
iIndex = -1; |
41 |
} |
42 |
|
43 |
SamplerChannel::~SamplerChannel() { |
44 |
if (pEngine) { |
45 |
if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine); |
46 |
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); |
47 |
delete pEngine; |
48 |
} |
49 |
} |
50 |
|
51 |
void SamplerChannel::LoadEngine(Engine::type_t EngineType) { |
52 |
dmsg(2,("SamplerChannel: Loading engine...")); |
53 |
|
54 |
// create new engine |
55 |
Engine* pNewEngine = NULL; |
56 |
switch (EngineType) { |
57 |
case Engine::type_gig: |
58 |
pNewEngine = new gig::Engine; |
59 |
break; |
60 |
default: |
61 |
throw LinuxSamplerException("Unknown engine type"); |
62 |
} |
63 |
|
64 |
// disconnect old engine |
65 |
if (pEngine) { |
66 |
if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine); |
67 |
if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine); |
68 |
delete pEngine; |
69 |
} |
70 |
|
71 |
// connect new engine |
72 |
pEngine = pNewEngine; |
73 |
if (pMidiInputDevice) pMidiInputDevice->Connect(pNewEngine, (MidiInputDevice::midi_chan_t) Index()); |
74 |
if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine); |
75 |
dmsg(2,("OK\n")); |
76 |
} |
77 |
|
78 |
void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice::type_t AudioType) { |
79 |
// get / create desired audio device |
80 |
AudioOutputDevice* pDevice = pSampler->GetAudioOutputDevice(AudioType); |
81 |
if (!pDevice) pDevice = pSampler->CreateAudioOutputDevice(AudioType); |
82 |
|
83 |
// disconnect old device |
84 |
if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine); |
85 |
|
86 |
// connect new device |
87 |
pAudioOutputDevice = pDevice; |
88 |
if (pEngine) pAudioOutputDevice->Connect(pEngine); |
89 |
} |
90 |
|
91 |
void SamplerChannel::SetMidiInputDevice(MidiInputDevice::type_t MidiType, MidiInputDevice::midi_chan_t MidiChannel) { |
92 |
// get / create desired midi device |
93 |
MidiInputDevice* pDevice = pSampler->GetMidiInputDevice(MidiType); |
94 |
if (!pDevice) pDevice = pSampler->CreateMidiInputDevice(MidiType); |
95 |
|
96 |
// disconnect old device |
97 |
if (pMidiInputDevice && pEngine) pMidiInputDevice->Disconnect(pEngine); |
98 |
|
99 |
// connect new device |
100 |
pMidiInputDevice = pDevice; |
101 |
if (pEngine) pMidiInputDevice->Connect(pEngine, MidiChannel); |
102 |
} |
103 |
|
104 |
Engine* SamplerChannel::GetEngine() { |
105 |
return pEngine; |
106 |
} |
107 |
|
108 |
MidiInputDevice* SamplerChannel::GetMidiInputDevice() { |
109 |
return pMidiInputDevice; |
110 |
} |
111 |
|
112 |
AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() { |
113 |
return pAudioOutputDevice; |
114 |
} |
115 |
|
116 |
uint SamplerChannel::Index() { |
117 |
if (iIndex >= 0) return iIndex; |
118 |
|
119 |
std::vector<SamplerChannel*>::iterator iter = pSampler->vSamplerChannels.begin(); |
120 |
for (int i = 0; iter != pSampler->vSamplerChannels.end(); i++, iter++) { |
121 |
if (*iter == this) { |
122 |
iIndex = i; |
123 |
return i; |
124 |
} |
125 |
} |
126 |
|
127 |
throw LinuxSamplerException("SamplerChannel index not found"); |
128 |
} |
129 |
|
130 |
|
131 |
// ****************************************************************** |
132 |
// * Sampler |
133 |
|
134 |
Sampler::Sampler() { |
135 |
} |
136 |
|
137 |
Sampler::~Sampler() { |
138 |
// delete sampler channels |
139 |
{ |
140 |
std::vector<SamplerChannel*>::iterator iter = vSamplerChannels.begin(); |
141 |
for (; iter != vSamplerChannels.end(); iter++) delete *iter; |
142 |
} |
143 |
|
144 |
// delete midi input devices |
145 |
{ |
146 |
MidiInputDeviceMap::iterator iter = MidiInputDevices.begin(); |
147 |
for (; iter != MidiInputDevices.end(); iter++) { |
148 |
MidiInputDevice* pDevice = iter->second; |
149 |
pDevice->StopListen(); |
150 |
delete pDevice; |
151 |
} |
152 |
} |
153 |
|
154 |
// delete audio output devices |
155 |
{ |
156 |
AudioOutputDeviceMap::iterator iter = AudioOutputDevices.begin(); |
157 |
for (; iter != AudioOutputDevices.end(); iter++) { |
158 |
AudioOutputDevice* pDevice = iter->second; |
159 |
pDevice->Stop(); |
160 |
delete pDevice; |
161 |
} |
162 |
} |
163 |
} |
164 |
|
165 |
uint Sampler::SamplerChannels() { |
166 |
return vSamplerChannels.size(); |
167 |
} |
168 |
|
169 |
SamplerChannel* Sampler::AddSamplerChannel() { |
170 |
SamplerChannel* pChannel = new SamplerChannel(this); |
171 |
vSamplerChannels.push_back(pChannel); |
172 |
return pChannel; |
173 |
} |
174 |
|
175 |
SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) { |
176 |
if (uiSamplerChannel >= SamplerChannels()) return NULL; |
177 |
return vSamplerChannels[uiSamplerChannel]; |
178 |
} |
179 |
|
180 |
void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) { |
181 |
std::vector<SamplerChannel*>::iterator iterChan = vSamplerChannels.begin(); |
182 |
for (; iterChan != vSamplerChannels.end(); iterChan++) { |
183 |
if (*iterChan == pSamplerChannel) { |
184 |
vSamplerChannels.erase(iterChan); |
185 |
delete pSamplerChannel; |
186 |
return; |
187 |
} |
188 |
} |
189 |
} |
190 |
|
191 |
void Sampler::RemoveSamplerChannel(uint uiSamplerChannel) { |
192 |
SamplerChannel* pChannel = GetSamplerChannel(uiSamplerChannel); |
193 |
if (!pChannel) return; |
194 |
RemoveSamplerChannel(pChannel); |
195 |
} |
196 |
|
197 |
AudioOutputDevice* Sampler::CreateAudioOutputDevice(AudioOutputDevice::type_t AudioType) { |
198 |
// check if device already created |
199 |
AudioOutputDevice* pDevice = GetAudioOutputDevice(AudioType); |
200 |
if (pDevice) return pDevice; |
201 |
|
202 |
// create new device |
203 |
switch (AudioType) { |
204 |
case AudioOutputDevice::type_alsa: |
205 |
pDevice = new AudioOutputDeviceAlsa; |
206 |
break; |
207 |
case AudioOutputDevice::type_jack: |
208 |
pDevice = new AudioOutputDeviceJack; |
209 |
break; |
210 |
default: |
211 |
throw LinuxSamplerException("Unknown audio output device type"); |
212 |
} |
213 |
|
214 |
// activate device |
215 |
pDevice->Play(); |
216 |
|
217 |
// add new audio device to the audio device list |
218 |
AudioOutputDevices[AudioType] = pDevice; |
219 |
|
220 |
return pDevice; |
221 |
} |
222 |
|
223 |
AudioOutputDevice* Sampler::GetAudioOutputDevice(AudioOutputDevice::type_t AudioType) { |
224 |
AudioOutputDeviceMap::iterator iter = AudioOutputDevices.find(AudioType); |
225 |
return (iter != AudioOutputDevices.end()) ? iter->second : NULL; |
226 |
} |
227 |
|
228 |
MidiInputDevice* Sampler::CreateMidiInputDevice(MidiInputDevice::type_t MidiType) { |
229 |
// check if device already created |
230 |
MidiInputDevice* pDevice = GetMidiInputDevice(MidiType); |
231 |
if (pDevice) return pDevice; |
232 |
|
233 |
// create new device |
234 |
switch (MidiType) { |
235 |
case MidiInputDevice::type_alsa: |
236 |
pDevice = new MidiInputDeviceAlsa; |
237 |
break; |
238 |
default: |
239 |
throw LinuxSamplerException("Unknown audio output device type"); |
240 |
} |
241 |
|
242 |
// activate device |
243 |
pDevice->Listen(); |
244 |
|
245 |
// add new MIDI device to the MIDI device list |
246 |
MidiInputDevices[MidiType] = pDevice; |
247 |
|
248 |
return pDevice; |
249 |
} |
250 |
|
251 |
MidiInputDevice* Sampler::GetMidiInputDevice(MidiInputDevice::type_t MidiType) { |
252 |
MidiInputDeviceMap::iterator iter = MidiInputDevices.find(MidiType); |
253 |
return (iter != MidiInputDevices.end()) ? iter->second : NULL; |
254 |
} |
255 |
|
256 |
} // namespace LinuxSampler |