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