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 "AudioOutputDeviceFactory.h" |
24 |
#include "AudioOutputDevice.h" |
25 |
|
26 |
namespace LinuxSampler { |
27 |
|
28 |
// *************** ParameterActive *************** |
29 |
// * |
30 |
|
31 |
AudioOutputDevice::ParameterActive::ParameterActive() : DeviceCreationParameterBool() { |
32 |
InitWithDefault(); |
33 |
} |
34 |
|
35 |
AudioOutputDevice::ParameterActive::ParameterActive(String s) : DeviceCreationParameterBool(s) { |
36 |
} |
37 |
|
38 |
String AudioOutputDevice::ParameterActive::Description() { |
39 |
return "Enable / disable device"; |
40 |
} |
41 |
|
42 |
bool AudioOutputDevice::ParameterActive::Fix() { |
43 |
return false; |
44 |
} |
45 |
|
46 |
bool AudioOutputDevice::ParameterActive::Mandatory() { |
47 |
return false; |
48 |
} |
49 |
|
50 |
std::map<String,DeviceCreationParameter*> AudioOutputDevice::ParameterActive::DependsAsParameters() { |
51 |
return std::map<String,DeviceCreationParameter*>(); |
52 |
} |
53 |
|
54 |
optional<bool> AudioOutputDevice::ParameterActive::DefaultAsBool(std::map<String,String> Parameters) { |
55 |
return true; |
56 |
} |
57 |
|
58 |
void AudioOutputDevice::ParameterActive::OnSetValue(bool b) throw (LinuxSamplerException) { |
59 |
if (b) ((AudioOutputDevice*)pDevice)->Play(); |
60 |
else ((AudioOutputDevice*)pDevice)->Stop(); |
61 |
} |
62 |
|
63 |
String AudioOutputDevice::ParameterActive::Name() { |
64 |
return "ACTIVE"; |
65 |
} |
66 |
|
67 |
|
68 |
|
69 |
// *************** ParameterSampleRate *************** |
70 |
// * |
71 |
|
72 |
AudioOutputDevice::ParameterSampleRate::ParameterSampleRate() : DeviceCreationParameterInt() { |
73 |
InitWithDefault(); |
74 |
} |
75 |
|
76 |
AudioOutputDevice::ParameterSampleRate::ParameterSampleRate(String s) : DeviceCreationParameterInt(s) { |
77 |
} |
78 |
|
79 |
String AudioOutputDevice::ParameterSampleRate::Description() { |
80 |
return "Output sample rate"; |
81 |
} |
82 |
|
83 |
bool AudioOutputDevice::ParameterSampleRate::Fix() { |
84 |
return true; |
85 |
} |
86 |
|
87 |
bool AudioOutputDevice::ParameterSampleRate::Mandatory() { |
88 |
return false; |
89 |
} |
90 |
|
91 |
std::map<String,DeviceCreationParameter*> AudioOutputDevice::ParameterSampleRate::DependsAsParameters() { |
92 |
return std::map<String,DeviceCreationParameter*>(); |
93 |
} |
94 |
|
95 |
optional<int> AudioOutputDevice::ParameterSampleRate::DefaultAsInt(std::map<String,String> Parameters) { |
96 |
return 44100; |
97 |
} |
98 |
|
99 |
optional<int> AudioOutputDevice::ParameterSampleRate::RangeMinAsInt(std::map<String,String> Parameters) { |
100 |
return optional<int>::nothing; |
101 |
} |
102 |
|
103 |
optional<int> AudioOutputDevice::ParameterSampleRate::RangeMaxAsInt(std::map<String,String> Parameters) { |
104 |
return optional<int>::nothing; |
105 |
} |
106 |
|
107 |
std::vector<int> AudioOutputDevice::ParameterSampleRate::PossibilitiesAsInt(std::map<String,String> Parameters) { |
108 |
return std::vector<int>(); |
109 |
} |
110 |
|
111 |
void AudioOutputDevice::ParameterSampleRate::OnSetValue(int i) throw (LinuxSamplerException) { |
112 |
/* cannot happen, as parameter is fix */ |
113 |
} |
114 |
|
115 |
String AudioOutputDevice::ParameterSampleRate::Name() { |
116 |
return "SAMPLERATE"; |
117 |
} |
118 |
|
119 |
|
120 |
|
121 |
// *************** ParameterChannels *************** |
122 |
// * |
123 |
|
124 |
AudioOutputDevice::ParameterChannels::ParameterChannels() : DeviceCreationParameterInt() { |
125 |
InitWithDefault(); |
126 |
} |
127 |
|
128 |
AudioOutputDevice::ParameterChannels::ParameterChannels(String s) : DeviceCreationParameterInt(s) { |
129 |
} |
130 |
|
131 |
String AudioOutputDevice::ParameterChannels::Description() { |
132 |
return "Number of output channels"; |
133 |
} |
134 |
|
135 |
bool AudioOutputDevice::ParameterChannels::Fix() { |
136 |
return false; |
137 |
} |
138 |
|
139 |
bool AudioOutputDevice::ParameterChannels::Mandatory() { |
140 |
return false; |
141 |
} |
142 |
|
143 |
std::map<String,DeviceCreationParameter*> AudioOutputDevice::ParameterChannels::DependsAsParameters() { |
144 |
return std::map<String,DeviceCreationParameter*>(); |
145 |
} |
146 |
|
147 |
optional<int> AudioOutputDevice::ParameterChannels::DefaultAsInt(std::map<String,String> Parameters) { |
148 |
return 2; |
149 |
} |
150 |
|
151 |
optional<int> AudioOutputDevice::ParameterChannels::RangeMinAsInt(std::map<String,String> Parameters) { |
152 |
return optional<int>::nothing; |
153 |
} |
154 |
|
155 |
optional<int> AudioOutputDevice::ParameterChannels::RangeMaxAsInt(std::map<String,String> Parameters) { |
156 |
return optional<int>::nothing; |
157 |
} |
158 |
|
159 |
std::vector<int> AudioOutputDevice::ParameterChannels::PossibilitiesAsInt(std::map<String,String> Parameters) { |
160 |
return std::vector<int>(); |
161 |
} |
162 |
|
163 |
void AudioOutputDevice::ParameterChannels::OnSetValue(int i) throw (LinuxSamplerException) { |
164 |
((AudioOutputDevice*)pDevice)->AcquireChannels(i); |
165 |
} |
166 |
|
167 |
String AudioOutputDevice::ParameterChannels::Name() { |
168 |
return "CHANNELS"; |
169 |
} |
170 |
|
171 |
|
172 |
|
173 |
// *************** AudioOutputDevice *************** |
174 |
// * |
175 |
|
176 |
AudioOutputDevice::AudioOutputDevice(std::map<String,DeviceCreationParameter*> DriverParameters) { |
177 |
this->Parameters = DriverParameters; |
178 |
} |
179 |
|
180 |
AudioOutputDevice::~AudioOutputDevice() { |
181 |
// delete all audio channels |
182 |
{ |
183 |
std::vector<AudioChannel*>::iterator iter = Channels.begin(); |
184 |
while (iter != Channels.end()) { |
185 |
Channels.erase(iter); |
186 |
delete *iter; |
187 |
iter++; |
188 |
} |
189 |
|
190 |
} |
191 |
|
192 |
// delete all device parameters |
193 |
{ |
194 |
std::map<String,DeviceCreationParameter*>::iterator iter = Parameters.begin(); |
195 |
while (iter != Parameters.end()) { |
196 |
Parameters.erase(iter); |
197 |
delete iter->second; |
198 |
iter++; |
199 |
} |
200 |
} |
201 |
} |
202 |
|
203 |
void AudioOutputDevice::Connect(Engine* pEngine) { |
204 |
if (Engines.find(pEngine) == Engines.end()) { |
205 |
pEngine->Connect(this); |
206 |
Engines.insert(pEngine); |
207 |
} |
208 |
} |
209 |
|
210 |
void AudioOutputDevice::Disconnect(Engine* pEngine) { |
211 |
if (Engines.find(pEngine) != Engines.end()) { // if clause to prevent disconnect loop |
212 |
Engines.erase(pEngine); |
213 |
pEngine->DisconnectAudioOutputDevice(); |
214 |
} |
215 |
} |
216 |
|
217 |
AudioChannel* AudioOutputDevice::Channel(uint ChannelIndex) { |
218 |
return (ChannelIndex < Channels.size()) ? Channels[ChannelIndex] : NULL; |
219 |
} |
220 |
|
221 |
void AudioOutputDevice::AcquireChannels(uint Channels) { |
222 |
if (Channels > this->Channels.size()) { |
223 |
for (int c = this->Channels.size(); c < Channels; c++) { |
224 |
this->Channels.push_back(CreateChannel(c)); |
225 |
} |
226 |
} |
227 |
} |
228 |
|
229 |
std::map<String,DeviceCreationParameter*> AudioOutputDevice::DeviceParameters() { |
230 |
return Parameters; |
231 |
} |
232 |
|
233 |
int AudioOutputDevice::RenderAudio(uint Samples) { |
234 |
if (Channels.empty()) return 0; |
235 |
|
236 |
// reset all channels with silence |
237 |
{ |
238 |
std::vector<AudioChannel*>::iterator iterChannels = Channels.begin(); |
239 |
std::vector<AudioChannel*>::iterator end = Channels.end(); |
240 |
for (; iterChannels != end; iterChannels++) |
241 |
(*iterChannels)->Clear(); // zero out audio buffer |
242 |
} |
243 |
|
244 |
int result = 0; |
245 |
|
246 |
// let all connected engines render audio for the current audio fragment cycle |
247 |
#if USE_EXCEPTIONS |
248 |
try |
249 |
#endif // USE_EXCEPTIONS |
250 |
{ |
251 |
std::set<Engine*>::iterator iterEngine = Engines.begin(); |
252 |
std::set<Engine*>::iterator end = Engines.end(); |
253 |
for (; iterEngine != end; iterEngine++) { |
254 |
int res = (*iterEngine)->RenderAudio(Samples); |
255 |
if (res != 0) result = res; |
256 |
} |
257 |
} |
258 |
#if USE_EXCEPTIONS |
259 |
catch (std::runtime_error se) { |
260 |
std::cerr << "std::runtime_error: " << se.what() << std::endl << std::flush; |
261 |
exit(EXIT_FAILURE); |
262 |
} |
263 |
#endif // USE_EXCEPTIONS |
264 |
|
265 |
return result; |
266 |
} |
267 |
|
268 |
int AudioOutputDevice::RenderSilence(uint Samples) { |
269 |
if (Channels.empty()) return 0; |
270 |
|
271 |
// reset all channels with silence |
272 |
{ |
273 |
std::vector<AudioChannel*>::iterator iterChannels = Channels.begin(); |
274 |
std::vector<AudioChannel*>::iterator end = Channels.end(); |
275 |
for (; iterChannels != end; iterChannels++) |
276 |
(*iterChannels)->Clear(); // zero out audio buffer |
277 |
} |
278 |
|
279 |
return 0; |
280 |
} |
281 |
|
282 |
} // namespace LinuxSampler |