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

Annotation of /linuxsampler/trunk/src/drivers/audio/AudioOutputDevice.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1934 - (hide annotations) (download) (as text)
Sun Jul 12 10:35:55 2009 UTC (14 years, 9 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 14464 byte(s)
* bugfix: don't allow to create or destroy audio devices and MIDI devices
  of host plugin implementations (e.g VST, AU, DSSI, LV2) on their own,
  as they only exist in the context of the plugin instance and would
  otherwise crash the application

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

  ViewVC Help
Powered by ViewVC