1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 - 2008 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 |
#ifndef __LS_AUDIOOUTPUTDEVICE_H__ |
25 |
#define __LS_AUDIOOUTPUTDEVICE_H__ |
26 |
|
27 |
#include <set> |
28 |
#include <map> |
29 |
#include <vector> |
30 |
#include <stdexcept> |
31 |
|
32 |
#include "../../common/global.h" |
33 |
#include "../../common/Exception.h" |
34 |
#include "../Device.h" |
35 |
#include "../DeviceParameter.h" |
36 |
#include "../../engines/Engine.h" |
37 |
#include "AudioChannel.h" |
38 |
#include "../../common/SynchronizedConfig.h" |
39 |
#include "../../effects/EffectChain.h" |
40 |
|
41 |
namespace LinuxSampler { |
42 |
|
43 |
// just symbol prototyping |
44 |
class Engine; |
45 |
|
46 |
/** Abstract base class for audio output drivers in LinuxSampler |
47 |
* |
48 |
* This class will be derived by specialized classes which implement the |
49 |
* connection to a specific audio output system (e.g. Alsa, Jack, |
50 |
* CoreAudio). |
51 |
*/ |
52 |
class AudioOutputDevice : public Device { |
53 |
public: |
54 |
|
55 |
///////////////////////////////////////////////////////////////// |
56 |
// Device parameters |
57 |
|
58 |
/** Device Parameter 'ACTIVE' |
59 |
* |
60 |
* Used to activate / deactivate the audio output device. |
61 |
*/ |
62 |
class ParameterActive : public DeviceCreationParameterBool { |
63 |
public: |
64 |
ParameterActive(); |
65 |
ParameterActive(String s); |
66 |
virtual String Description(); |
67 |
virtual bool Fix(); |
68 |
virtual bool Mandatory(); |
69 |
virtual std::map<String,DeviceCreationParameter*> DependsAsParameters(); |
70 |
virtual optional<bool> DefaultAsBool(std::map<String,String> Parameters); |
71 |
virtual void OnSetValue(bool b) throw (Exception); |
72 |
static String Name(); |
73 |
}; |
74 |
|
75 |
/** Device Parameter 'SAMPLERATE' |
76 |
* |
77 |
* Used to set the sample rate of the audio output device. |
78 |
*/ |
79 |
class ParameterSampleRate : public DeviceCreationParameterInt { |
80 |
public: |
81 |
ParameterSampleRate(); |
82 |
ParameterSampleRate(String s); |
83 |
virtual String Description(); |
84 |
virtual bool Fix(); |
85 |
virtual bool Mandatory(); |
86 |
virtual std::map<String,DeviceCreationParameter*> DependsAsParameters(); |
87 |
virtual optional<int> DefaultAsInt(std::map<String,String> Parameters); |
88 |
virtual optional<int> RangeMinAsInt(std::map<String,String> Parameters); |
89 |
virtual optional<int> RangeMaxAsInt(std::map<String,String> Parameters); |
90 |
virtual std::vector<int> PossibilitiesAsInt(std::map<String,String> Parameters); |
91 |
virtual int ValueAsInt(); |
92 |
virtual void OnSetValue(int i) throw (Exception); |
93 |
static String Name(); |
94 |
}; |
95 |
|
96 |
/** Device Parameters 'CHANNELS' |
97 |
* |
98 |
* Used to increase / decrease the number of audio channels of |
99 |
* audio output device. |
100 |
*/ |
101 |
class ParameterChannels : public DeviceCreationParameterInt { |
102 |
public: |
103 |
ParameterChannels(); |
104 |
ParameterChannels(String s); |
105 |
virtual String Description(); |
106 |
virtual bool Fix(); |
107 |
virtual bool Mandatory(); |
108 |
virtual std::map<String,DeviceCreationParameter*> DependsAsParameters(); |
109 |
virtual optional<int> DefaultAsInt(std::map<String,String> Parameters); |
110 |
virtual optional<int> RangeMinAsInt(std::map<String,String> Parameters); |
111 |
virtual optional<int> RangeMaxAsInt(std::map<String,String> Parameters); |
112 |
virtual std::vector<int> PossibilitiesAsInt(std::map<String,String> Parameters); |
113 |
virtual void OnSetValue(int i) throw (Exception); |
114 |
static String Name(); |
115 |
}; |
116 |
|
117 |
|
118 |
|
119 |
///////////////////////////////////////////////////////////////// |
120 |
// abstract methods |
121 |
// (these have to be implemented by the descendant) |
122 |
|
123 |
/** |
124 |
* Start playback of audio signal on the audio device. It's the |
125 |
* responsibility of the implementing audio device to call the |
126 |
* RenderAudio(uint Samples) method of all connected engines. |
127 |
* This will cause the engines to continue to render 'Samples' |
128 |
* number of audio sample points and the engines will |
129 |
* automatically add their audio signals to the audio buffers of |
130 |
* the audio channels of this audio device. So the implementing |
131 |
* audio device just has to access the buffers of it's audio |
132 |
* channels. |
133 |
* |
134 |
* @throws AudioOutputException if playback can not be started |
135 |
* @see AudioChannel |
136 |
*/ |
137 |
virtual void Play() = 0; |
138 |
|
139 |
/** |
140 |
* Returns true if the audio device is currently playing back. |
141 |
*/ |
142 |
virtual bool IsPlaying() = 0; |
143 |
|
144 |
/** |
145 |
* Stop playback of audio signal on the audio device. The |
146 |
* implementing audio device will stop calling the RenderAudio() |
147 |
* method of all connected engines and close it's connection to |
148 |
* audio output system. |
149 |
*/ |
150 |
virtual void Stop() = 0; |
151 |
|
152 |
/** |
153 |
* Maximum amount of sample points the implementing audio |
154 |
* device will ever demand the sampler engines to write in one |
155 |
* fragment cycle / period. Simple audio device drivers usually |
156 |
* have a fixed fragment size, so those devices just would return |
157 |
* the fragment size in this method. |
158 |
* |
159 |
* @returns max. amount of sample points ever |
160 |
*/ |
161 |
virtual uint MaxSamplesPerCycle() = 0; |
162 |
|
163 |
/** |
164 |
* Playback samplerate the audio device uses. The sampler engines |
165 |
* currently assume this to be a constant value for the whole |
166 |
* life time of an instance of the implementing audio device. |
167 |
* |
168 |
* @returns sample rate in Hz |
169 |
*/ |
170 |
virtual uint SampleRate() = 0; |
171 |
|
172 |
/** |
173 |
* Return the audio output device driver name. |
174 |
*/ |
175 |
virtual String Driver() = 0; |
176 |
|
177 |
/** |
178 |
* Create new audio channel. This will be called by |
179 |
* AcquireChannels(). Each driver must implement it. |
180 |
*/ |
181 |
virtual AudioChannel* CreateChannel(uint ChannelNr) = 0; |
182 |
|
183 |
|
184 |
|
185 |
///////////////////////////////////////////////////////////////// |
186 |
// normal methods |
187 |
// (usually not to be overriden by descendant) |
188 |
|
189 |
/** |
190 |
* Connect given sampler engine to this audio output device. The |
191 |
* engine will be added to the Engines container of this audio |
192 |
* device and the engine will also automatically be informed |
193 |
* about the connection. |
194 |
* |
195 |
* @param pEngine - sampler engine |
196 |
*/ |
197 |
void Connect(Engine* pEngine); |
198 |
|
199 |
/** |
200 |
* Disconnect given sampler engine from this audio output device. |
201 |
* Removes given sampler engine reference from the Engines |
202 |
* container of this audio device. |
203 |
* |
204 |
* @param pEngine - sampler engine |
205 |
*/ |
206 |
void Disconnect(Engine* pEngine); |
207 |
|
208 |
/** |
209 |
* Returns audio channel with index \a ChannelIndex or NULL if |
210 |
* index out of bounds. |
211 |
*/ |
212 |
AudioChannel* Channel(uint ChannelIndex); |
213 |
|
214 |
/** |
215 |
* This method will usually be called by the sampler engines that |
216 |
* are connected to this audio device to inform the audio device |
217 |
* how much audio channels the engine(s) need. It's the |
218 |
* responsibility of the audio device to offer that amount of |
219 |
* audio channels - again: this is not an option this is a must! |
220 |
* The engines will assume to be able to access those audio |
221 |
* channels right after. If the audio driver is not able to offer |
222 |
* that much channels, it can simply create mix channels which |
223 |
* are then just mixed to the 'real' audio channels. See |
224 |
* AudioChannel.h for details about channels and mix channels. |
225 |
* |
226 |
* @param Channels - amount of output channels required by |
227 |
* a sampler engine |
228 |
* @throws AudioOutputException if desired amount of channels |
229 |
* cannot be offered |
230 |
* @see AudioChannel |
231 |
*/ |
232 |
void AcquireChannels(uint Channels); |
233 |
|
234 |
/** |
235 |
* Returns the amount of audio channels (including the so called |
236 |
* "mix channels") the device is currently providing. |
237 |
*/ |
238 |
uint ChannelCount(); |
239 |
|
240 |
/** |
241 |
* Returns all device parameter settings. |
242 |
*/ |
243 |
std::map<String,DeviceCreationParameter*> DeviceParameters(); |
244 |
|
245 |
/** |
246 |
* Add a chain of master effects to this AudioOutputDevice. |
247 |
* You actually have to add effects to that chain afterwards. |
248 |
*/ |
249 |
EffectChain* AddMasterEffectChain(); |
250 |
|
251 |
/** |
252 |
* Remove the master effect chain given by @a iChain . |
253 |
* |
254 |
* @throws Exception - if given master effect chain doesn't exist |
255 |
*/ |
256 |
void RemoveMasterEffectChain(uint iChain) throw (Exception); |
257 |
|
258 |
/** |
259 |
* Returns master effect chain given by @a iChain or @c NULL if |
260 |
* there's no such effect chain. |
261 |
*/ |
262 |
EffectChain* MasterEffectChain(uint iChain) const; |
263 |
|
264 |
/** |
265 |
* Returns amount of master effect chains this AudioOutputDevice |
266 |
* currently provides. |
267 |
*/ |
268 |
uint MasterEffectChainCount() const; |
269 |
|
270 |
protected: |
271 |
SynchronizedConfig<std::set<Engine*> > Engines; ///< All sampler engines that are connected to the audio output device. |
272 |
SynchronizedConfig<std::set<Engine*> >::Reader EnginesReader; ///< Audio thread access to Engines. |
273 |
std::vector<AudioChannel*> Channels; ///< All audio channels of the audio output device. This is just a container; the descendant has to create channels by himself. |
274 |
std::map<String,DeviceCreationParameter*> Parameters; ///< All device parameters. |
275 |
std::vector<EffectChain*> vEffectChains; |
276 |
|
277 |
AudioOutputDevice(std::map<String,DeviceCreationParameter*> DriverParameters); |
278 |
|
279 |
virtual ~AudioOutputDevice(); |
280 |
|
281 |
/** |
282 |
* This method should be called by the AudioOutputDevice |
283 |
* descendant to let all connected engines proceed to render the |
284 |
* given amount of sample points. The engines will place their |
285 |
* calculated audio data by themselfes into the buffers of the |
286 |
* respective AudioChannel objects, so the implementing audio |
287 |
* output device just has to copy the AudioChannel buffers to |
288 |
* the output buffer(s) of its audio system. |
289 |
* |
290 |
* @returns 0 on success or the last error return code of one |
291 |
* engine |
292 |
*/ |
293 |
int RenderAudio(uint Samples); |
294 |
|
295 |
/** |
296 |
* This can be called as an alternative to RenderAudio() for |
297 |
* just writing silence to the audio output buffers and not |
298 |
* calling the connected sampler engines for rendering audio, so |
299 |
* to provide a method to stop playback if the used audio output |
300 |
* system doesn't provide a better way. |
301 |
* |
302 |
* @returns 0 on success |
303 |
*/ |
304 |
int RenderSilence(uint Samples); |
305 |
|
306 |
friend class Sampler; // allow Sampler class to destroy audio devices |
307 |
|
308 |
}; |
309 |
|
310 |
/** |
311 |
* Audio output exception that should be thrown by the AudioOutputDevice |
312 |
* descendants in case initialization of the audio output system failed |
313 |
* (which should be done in the constructor of the AudioOutputDevice |
314 |
* descendant). |
315 |
*/ |
316 |
class AudioOutputException : public Exception { |
317 |
public: |
318 |
AudioOutputException(const std::string& msg) : Exception(msg) {} |
319 |
}; |
320 |
} |
321 |
|
322 |
#endif // __LS_AUDIOOUTPUTDEVICE_H__ |