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