1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 - 2007 Christian Schoenebeck * |
7 |
* * |
8 |
* 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 * |
10 |
* the Free Software Foundation; either version 2 of the License, or * |
11 |
* (at your option) any later version. * |
12 |
* * |
13 |
* This program is distributed in the hope that it will be useful, * |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
* GNU General Public License for more details. * |
17 |
* * |
18 |
* You should have received a copy of the GNU General Public License * |
19 |
* along with this program; if not, write to the Free Software * |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
23 |
|
24 |
#include "AudioOutputDeviceFactory.h" |
25 |
#include "AudioOutputDevice.h" |
26 |
#include "../../common/global_private.h" |
27 |
|
28 |
namespace LinuxSampler { |
29 |
|
30 |
// *************** ParameterActive *************** |
31 |
// * |
32 |
|
33 |
AudioOutputDevice::ParameterActive::ParameterActive() : DeviceCreationParameterBool() { |
34 |
InitWithDefault(); |
35 |
} |
36 |
|
37 |
AudioOutputDevice::ParameterActive::ParameterActive(String s) : DeviceCreationParameterBool(s) { |
38 |
} |
39 |
|
40 |
String AudioOutputDevice::ParameterActive::Description() { |
41 |
return "Enable / disable device"; |
42 |
} |
43 |
|
44 |
bool AudioOutputDevice::ParameterActive::Fix() { |
45 |
return false; |
46 |
} |
47 |
|
48 |
bool AudioOutputDevice::ParameterActive::Mandatory() { |
49 |
return false; |
50 |
} |
51 |
|
52 |
std::map<String,DeviceCreationParameter*> AudioOutputDevice::ParameterActive::DependsAsParameters() { |
53 |
return std::map<String,DeviceCreationParameter*>(); |
54 |
} |
55 |
|
56 |
optional<bool> AudioOutputDevice::ParameterActive::DefaultAsBool(std::map<String,String> Parameters) { |
57 |
return true; |
58 |
} |
59 |
|
60 |
void AudioOutputDevice::ParameterActive::OnSetValue(bool b) throw (Exception) { |
61 |
if (b) ((AudioOutputDevice*)pDevice)->Play(); |
62 |
else ((AudioOutputDevice*)pDevice)->Stop(); |
63 |
} |
64 |
|
65 |
String AudioOutputDevice::ParameterActive::Name() { |
66 |
return "ACTIVE"; |
67 |
} |
68 |
|
69 |
|
70 |
|
71 |
// *************** ParameterSampleRate *************** |
72 |
// * |
73 |
|
74 |
AudioOutputDevice::ParameterSampleRate::ParameterSampleRate() : DeviceCreationParameterInt() { |
75 |
InitWithDefault(); |
76 |
} |
77 |
|
78 |
AudioOutputDevice::ParameterSampleRate::ParameterSampleRate(String s) : DeviceCreationParameterInt(s) { |
79 |
} |
80 |
|
81 |
String AudioOutputDevice::ParameterSampleRate::Description() { |
82 |
return "Output sample rate"; |
83 |
} |
84 |
|
85 |
bool AudioOutputDevice::ParameterSampleRate::Fix() { |
86 |
return true; |
87 |
} |
88 |
|
89 |
bool AudioOutputDevice::ParameterSampleRate::Mandatory() { |
90 |
return false; |
91 |
} |
92 |
|
93 |
std::map<String,DeviceCreationParameter*> AudioOutputDevice::ParameterSampleRate::DependsAsParameters() { |
94 |
return std::map<String,DeviceCreationParameter*>(); |
95 |
} |
96 |
|
97 |
optional<int> AudioOutputDevice::ParameterSampleRate::DefaultAsInt(std::map<String,String> Parameters) { |
98 |
return 44100; |
99 |
} |
100 |
|
101 |
optional<int> AudioOutputDevice::ParameterSampleRate::RangeMinAsInt(std::map<String,String> Parameters) { |
102 |
return optional<int>::nothing; |
103 |
} |
104 |
|
105 |
optional<int> AudioOutputDevice::ParameterSampleRate::RangeMaxAsInt(std::map<String,String> Parameters) { |
106 |
return optional<int>::nothing; |
107 |
} |
108 |
|
109 |
std::vector<int> AudioOutputDevice::ParameterSampleRate::PossibilitiesAsInt(std::map<String,String> Parameters) { |
110 |
return std::vector<int>(); |
111 |
} |
112 |
|
113 |
void AudioOutputDevice::ParameterSampleRate::OnSetValue(int i) throw (Exception) { |
114 |
/* cannot happen, as parameter is fix */ |
115 |
} |
116 |
|
117 |
String AudioOutputDevice::ParameterSampleRate::Name() { |
118 |
return "SAMPLERATE"; |
119 |
} |
120 |
|
121 |
|
122 |
|
123 |
// *************** ParameterChannels *************** |
124 |
// * |
125 |
|
126 |
AudioOutputDevice::ParameterChannels::ParameterChannels() : DeviceCreationParameterInt() { |
127 |
InitWithDefault(); |
128 |
} |
129 |
|
130 |
AudioOutputDevice::ParameterChannels::ParameterChannels(String s) : DeviceCreationParameterInt(s) { |
131 |
} |
132 |
|
133 |
String AudioOutputDevice::ParameterChannels::Description() { |
134 |
return "Number of output channels"; |
135 |
} |
136 |
|
137 |
bool AudioOutputDevice::ParameterChannels::Fix() { |
138 |
return false; |
139 |
} |
140 |
|
141 |
bool AudioOutputDevice::ParameterChannels::Mandatory() { |
142 |
return false; |
143 |
} |
144 |
|
145 |
std::map<String,DeviceCreationParameter*> AudioOutputDevice::ParameterChannels::DependsAsParameters() { |
146 |
return std::map<String,DeviceCreationParameter*>(); |
147 |
} |
148 |
|
149 |
optional<int> AudioOutputDevice::ParameterChannels::DefaultAsInt(std::map<String,String> Parameters) { |
150 |
return 2; |
151 |
} |
152 |
|
153 |
optional<int> AudioOutputDevice::ParameterChannels::RangeMinAsInt(std::map<String,String> Parameters) { |
154 |
return 1; |
155 |
} |
156 |
|
157 |
optional<int> AudioOutputDevice::ParameterChannels::RangeMaxAsInt(std::map<String,String> Parameters) { |
158 |
return 100; |
159 |
} |
160 |
|
161 |
std::vector<int> AudioOutputDevice::ParameterChannels::PossibilitiesAsInt(std::map<String,String> Parameters) { |
162 |
return std::vector<int>(); |
163 |
} |
164 |
|
165 |
void AudioOutputDevice::ParameterChannels::OnSetValue(int i) throw (Exception) { |
166 |
((AudioOutputDevice*)pDevice)->AcquireChannels(i); |
167 |
} |
168 |
|
169 |
String AudioOutputDevice::ParameterChannels::Name() { |
170 |
return "CHANNELS"; |
171 |
} |
172 |
|
173 |
|
174 |
|
175 |
// *************** AudioOutputDevice *************** |
176 |
// * |
177 |
|
178 |
AudioOutputDevice::AudioOutputDevice(std::map<String,DeviceCreationParameter*> DriverParameters) |
179 |
: EnginesReader(Engines) { |
180 |
this->Parameters = DriverParameters; |
181 |
} |
182 |
|
183 |
AudioOutputDevice::~AudioOutputDevice() { |
184 |
// delete all audio channels |
185 |
{ |
186 |
std::vector<AudioChannel*>::iterator iter = Channels.begin(); |
187 |
while (iter != Channels.end()) { |
188 |
delete *iter; |
189 |
iter++; |
190 |
} |
191 |
Channels.clear(); |
192 |
} |
193 |
|
194 |
// delete all device parameters |
195 |
{ |
196 |
std::map<String,DeviceCreationParameter*>::iterator iter = Parameters.begin(); |
197 |
while (iter != Parameters.end()) { |
198 |
delete iter->second; |
199 |
iter++; |
200 |
} |
201 |
Parameters.clear(); |
202 |
} |
203 |
} |
204 |
|
205 |
void AudioOutputDevice::Connect(Engine* pEngine) { |
206 |
std::set<Engine*>& engines = Engines.GetConfigForUpdate(); |
207 |
if (engines.find(pEngine) == engines.end()) { |
208 |
engines.insert(pEngine); |
209 |
Engines.SwitchConfig().insert(pEngine); |
210 |
// make sure the engine knows about the connection |
211 |
//pEngine->Connect(this); |
212 |
} |
213 |
} |
214 |
|
215 |
void AudioOutputDevice::Disconnect(Engine* pEngine) { |
216 |
std::set<Engine*>& engines = Engines.GetConfigForUpdate(); |
217 |
if (engines.find(pEngine) != engines.end()) { // if clause to prevent disconnect loop |
218 |
engines.erase(pEngine); |
219 |
Engines.SwitchConfig().erase(pEngine); |
220 |
// make sure the engine knows about the disconnection |
221 |
//pEngine->DisconnectAudioOutputDevice(); |
222 |
} |
223 |
} |
224 |
|
225 |
AudioChannel* AudioOutputDevice::Channel(uint ChannelIndex) { |
226 |
return (ChannelIndex < Channels.size()) ? Channels[ChannelIndex] : NULL; |
227 |
} |
228 |
|
229 |
void AudioOutputDevice::AcquireChannels(uint Channels) { |
230 |
if (Channels > this->Channels.size()) { |
231 |
for (int c = this->Channels.size(); c < Channels; c++) { |
232 |
this->Channels.push_back(CreateChannel(c)); |
233 |
} |
234 |
} |
235 |
} |
236 |
|
237 |
uint AudioOutputDevice::ChannelCount() { |
238 |
return Channels.size(); |
239 |
} |
240 |
|
241 |
std::map<String,DeviceCreationParameter*> AudioOutputDevice::DeviceParameters() { |
242 |
return Parameters; |
243 |
} |
244 |
|
245 |
int AudioOutputDevice::RenderAudio(uint Samples) { |
246 |
if (Channels.empty()) return 0; |
247 |
|
248 |
// reset all channels with silence |
249 |
{ |
250 |
std::vector<AudioChannel*>::iterator iterChannels = Channels.begin(); |
251 |
std::vector<AudioChannel*>::iterator end = Channels.end(); |
252 |
for (; iterChannels != end; iterChannels++) |
253 |
(*iterChannels)->Clear(); // zero out audio buffer |
254 |
} |
255 |
|
256 |
int result = 0; |
257 |
|
258 |
// let all connected engines render audio for the current audio fragment cycle |
259 |
const std::set<Engine*>& engines = EnginesReader.Lock(); |
260 |
#if CONFIG_RT_EXCEPTIONS |
261 |
try |
262 |
#endif // CONFIG_RT_EXCEPTIONS |
263 |
{ |
264 |
std::set<Engine*>::iterator iterEngine = engines.begin(); |
265 |
std::set<Engine*>::iterator end = engines.end(); |
266 |
for (; iterEngine != end; iterEngine++) { |
267 |
int res = (*iterEngine)->RenderAudio(Samples); |
268 |
if (res != 0) result = res; |
269 |
} |
270 |
} |
271 |
#if CONFIG_RT_EXCEPTIONS |
272 |
catch (std::runtime_error se) { |
273 |
std::cerr << "std::runtime_error: " << se.what() << std::endl << std::flush; |
274 |
exit(EXIT_FAILURE); |
275 |
} |
276 |
#endif // CONFIG_RT_EXCEPTIONS |
277 |
|
278 |
EnginesReader.Unlock(); |
279 |
return result; |
280 |
} |
281 |
|
282 |
int AudioOutputDevice::RenderSilence(uint Samples) { |
283 |
if (Channels.empty()) return 0; |
284 |
|
285 |
// reset all channels with silence |
286 |
{ |
287 |
std::vector<AudioChannel*>::iterator iterChannels = Channels.begin(); |
288 |
std::vector<AudioChannel*>::iterator end = Channels.end(); |
289 |
for (; iterChannels != end; iterChannels++) |
290 |
(*iterChannels)->Clear(); // zero out audio buffer |
291 |
} |
292 |
|
293 |
return 0; |
294 |
} |
295 |
|
296 |
} // namespace LinuxSampler |