/[svn]/linuxsampler/trunk/src/audiodriver/AudioOutputDevice.h
ViewVC logotype

Contents of /linuxsampler/trunk/src/audiodriver/AudioOutputDevice.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 139 - (show annotations) (download) (as text)
Sun Jun 20 23:42:44 2004 UTC (19 years, 10 months ago) by senkov
File MIME type: text/x-c++hdr
File size: 15187 byte(s)
* Fixes and hacks to make CREATE AUDIO_OUTPUT_DEVICE work

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

  ViewVC Help
Powered by ViewVC