2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * Copyright 2009-2011 Jörg Hermann Müller
6 * This file is part of AudaSpace.
8 * Audaspace 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.
13 * AudaSpace 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.
18 * You should have received a copy of the GNU General Public License
19 * along with Audaspace; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * ***** END GPL LICENSE BLOCK *****
25 /** \file audaspace/OpenAL/AUD_OpenALDevice.h
30 #ifndef AUD_OPENALDEVICE
31 #define AUD_OPENALDEVICE
33 #include "AUD_IDevice.h"
34 #include "AUD_IHandle.h"
35 #include "AUD_I3DDevice.h"
36 #include "AUD_I3DHandle.h"
37 #include "AUD_Buffer.h"
38 //struct AUD_OpenALBufferedFactory;
46 * This device plays through OpenAL.
48 class AUD_OpenALDevice : public AUD_IDevice, public AUD_I3DDevice
51 /// Saves the data for playback.
52 class AUD_OpenALHandle : public AUD_IHandle, public AUD_I3DHandle
55 static const int CYCLE_BUFFERS = 3;
57 /// Whether it's a buffered or a streamed source.
60 /// The reader source.
61 AUD_Reference<AUD_IReader> m_reader;
63 /// Whether to keep the source if end of it is reached.
66 /// OpenAL sample format.
73 ALuint m_buffers[CYCLE_BUFFERS];
75 /// The first buffer to be read next.
78 /// Whether the stream doesn't return any more data.
81 /// The loop count of the source.
84 /// The stop callback.
87 /// Stop callback data.
91 AUD_Quaternion m_orientation;
93 /// Current status of the handle
97 AUD_OpenALDevice* m_device;
102 * Creates a new OpenAL handle.
103 * \param device The OpenAL device the handle belongs to.
104 * \param format The AL format.
105 * \param reader The reader this handle plays.
106 * \param keep Whether to keep the handle alive when the reader ends.
108 AUD_OpenALHandle(AUD_OpenALDevice* device, ALenum format, AUD_Reference<AUD_IReader> reader, bool keep);
110 virtual ~AUD_OpenALHandle() {}
111 virtual bool pause();
112 virtual bool resume();
114 virtual bool getKeep();
115 virtual bool setKeep(bool keep);
116 virtual bool seek(float position);
117 virtual float getPosition();
118 virtual AUD_Status getStatus();
119 virtual float getVolume();
120 virtual bool setVolume(float volume);
121 virtual float getPitch();
122 virtual bool setPitch(float pitch);
123 virtual int getLoopCount();
124 virtual bool setLoopCount(int count);
125 virtual bool setStopCallback(stopCallback callback = 0, void* data = 0);
127 virtual AUD_Vector3 getSourceLocation();
128 virtual bool setSourceLocation(const AUD_Vector3& location);
129 virtual AUD_Vector3 getSourceVelocity();
130 virtual bool setSourceVelocity(const AUD_Vector3& velocity);
131 virtual AUD_Quaternion getSourceOrientation();
132 virtual bool setSourceOrientation(const AUD_Quaternion& orientation);
133 virtual bool isRelative();
134 virtual bool setRelative(bool relative);
135 virtual float getVolumeMaximum();
136 virtual bool setVolumeMaximum(float volume);
137 virtual float getVolumeMinimum();
138 virtual bool setVolumeMinimum(float volume);
139 virtual float getDistanceMaximum();
140 virtual bool setDistanceMaximum(float distance);
141 virtual float getDistanceReference();
142 virtual bool setDistanceReference(float distance);
143 virtual float getAttenuation();
144 virtual bool setAttenuation(float factor);
145 virtual float getConeAngleOuter();
146 virtual bool setConeAngleOuter(float angle);
147 virtual float getConeAngleInner();
148 virtual bool setConeAngleInner(float angle);
149 virtual float getConeVolumeOuter();
150 virtual bool setConeVolumeOuter(float volume);
153 typedef std::list<AUD_Reference<AUD_OpenALHandle> >::iterator AUD_HandleIterator;
156 * The OpenAL device handle.
161 * The OpenAL context.
163 ALCcontext* m_context;
166 * The specification of the device.
168 AUD_DeviceSpecs m_specs;
171 * Whether the device has the AL_EXT_MCFORMATS extension.
176 * The list of sounds that are currently playing.
178 std::list<AUD_Reference<AUD_OpenALHandle> > m_playingSounds;
181 * The list of sounds that are currently paused.
183 std::list<AUD_Reference<AUD_OpenALHandle> > m_pausedSounds;
186 * The list of buffered factories.
188 //std::list<AUD_OpenALBufferedFactory*>* m_bufferedFactories;
191 * The mutex for locking.
193 pthread_mutex_t m_mutex;
196 * The streaming thread.
201 * The condition for streaming thread wakeup.
218 AUD_Quaternion m_orientation;
221 * Starts the streaming thread.
222 * \param Whether the previous thread should be joined.
224 void start(bool join = true);
227 * Gets the format according to the specs.
228 * \param format The variable to put the format into.
229 * \param specs The specs to read the channel count from.
230 * \return Whether the format is valid or not.
232 bool getFormat(ALenum &format, AUD_Specs specs);
234 // hide copy constructor and operator=
235 AUD_OpenALDevice(const AUD_OpenALDevice&);
236 AUD_OpenALDevice& operator=(const AUD_OpenALDevice&);
240 * Opens the OpenAL audio device for playback.
241 * \param specs The wanted audio specification.
242 * \param buffersize The size of the internal buffer.
243 * \note The specification really used for opening the device may differ.
244 * \note The buffersize will be multiplicated by three for this device.
245 * \exception AUD_Exception Thrown if the audio device cannot be opened.
247 AUD_OpenALDevice(AUD_DeviceSpecs specs,
248 int buffersize = AUD_DEFAULT_BUFFER_SIZE);
251 * Streaming thread main function.
253 void updateStreams();
255 virtual ~AUD_OpenALDevice();
257 virtual AUD_DeviceSpecs getSpecs() const;
258 virtual AUD_Reference<AUD_IHandle> play(AUD_Reference<AUD_IReader> reader, bool keep = false);
259 virtual AUD_Reference<AUD_IHandle> play(AUD_Reference<AUD_IFactory> factory, bool keep = false);
260 virtual void stopAll();
262 virtual void unlock();
263 virtual float getVolume() const;
264 virtual void setVolume(float volume);
266 virtual AUD_Vector3 getListenerLocation() const;
267 virtual void setListenerLocation(const AUD_Vector3& location);
268 virtual AUD_Vector3 getListenerVelocity() const;
269 virtual void setListenerVelocity(const AUD_Vector3& velocity);
270 virtual AUD_Quaternion getListenerOrientation() const;
271 virtual void setListenerOrientation(const AUD_Quaternion& orientation);
272 virtual float getSpeedOfSound() const;
273 virtual void setSpeedOfSound(float speed);
274 virtual float getDopplerFactor() const;
275 virtual void setDopplerFactor(float factor);
276 virtual AUD_DistanceModel getDistanceModel() const;
277 virtual void setDistanceModel(AUD_DistanceModel model);
280 #endif //AUD_OPENALDEVICE