Audaspace 1.7.0
A high level audio library.
Loading...
Searching...
No Matches
OpenALDevice.h
Go to the documentation of this file.
1/*******************************************************************************
2 * Copyright 2009-2016 Jörg Müller
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 ******************************************************************************/
16
17#pragma once
18
19#ifdef OPENAL_PLUGIN
20#define AUD_BUILD_PLUGIN
21#endif
22
28
29#include "devices/IDevice.h"
30#include "devices/IHandle.h"
31#include "devices/I3DDevice.h"
32#include "devices/I3DHandle.h"
33#include "util/Buffer.h"
34
35#include <al.h>
36#include <alc.h>
37#include <list>
38#include <mutex>
39#include <thread>
40#include <string>
41
43
47class AUD_PLUGIN_API OpenALDevice : public IDevice, public I3DDevice
48{
49private:
51 class OpenALHandle : public IHandle, public I3DHandle
52 {
53 private:
54 friend class OpenALDevice;
55
56 static const int CYCLE_BUFFERS = 3;
57
59 bool m_isBuffered;
60
62 std::shared_ptr<IReader> m_reader;
63
65 bool m_keep;
66
68 ALenum m_format;
69
71 ALuint m_source;
72
74 ALuint m_buffers[CYCLE_BUFFERS];
75
77 int m_current;
78
80 bool m_eos;
81
83 int m_loopcount;
84
86 stopCallback m_stop;
87
89 void* m_stop_data;
90
92 Quaternion m_orientation;
93
95 Status m_status;
96
98 ALint m_relative;
99
101 OpenALDevice* m_device;
102
103 AUD_LOCAL bool pause(bool keep);
104
105 AUD_LOCAL bool reinitialize();
106
107 // delete copy constructor and operator=
108 OpenALHandle(const OpenALHandle&) = delete;
109 OpenALHandle& operator=(const OpenALHandle&) = delete;
110
111 public:
112
120 OpenALHandle(OpenALDevice* device, ALenum format, std::shared_ptr<IReader> reader, bool keep);
121
122 virtual ~OpenALHandle() {}
123 virtual bool pause();
124 virtual bool resume();
125 virtual bool stop();
126 virtual bool getKeep();
127 virtual bool setKeep(bool keep);
128 virtual bool seek(double position);
129 virtual double getPosition();
130 virtual Status getStatus();
131 virtual float getVolume();
132 virtual bool setVolume(float volume);
133 virtual float getPitch();
134 virtual bool setPitch(float pitch);
135 virtual int getLoopCount();
136 virtual bool setLoopCount(int count);
137 virtual bool setStopCallback(stopCallback callback = 0, void* data = 0);
138
139 virtual Vector3 getLocation();
140 virtual bool setLocation(const Vector3& location);
141 virtual Vector3 getVelocity();
142 virtual bool setVelocity(const Vector3& velocity);
143 virtual Quaternion getOrientation();
144 virtual bool setOrientation(const Quaternion& orientation);
145 virtual bool isRelative();
146 virtual bool setRelative(bool relative);
147 virtual float getVolumeMaximum();
148 virtual bool setVolumeMaximum(float volume);
149 virtual float getVolumeMinimum();
150 virtual bool setVolumeMinimum(float volume);
151 virtual float getDistanceMaximum();
152 virtual bool setDistanceMaximum(float distance);
153 virtual float getDistanceReference();
154 virtual bool setDistanceReference(float distance);
155 virtual float getAttenuation();
156 virtual bool setAttenuation(float factor);
157 virtual float getConeAngleOuter();
158 virtual bool setConeAngleOuter(float angle);
159 virtual float getConeAngleInner();
160 virtual bool setConeAngleInner(float angle);
161 virtual float getConeVolumeOuter();
162 virtual bool setConeVolumeOuter(float volume);
163 };
164
168 ALCdevice* m_device;
169
173 ALCcontext* m_context;
174
178 DeviceSpecs m_specs;
179
183 std::string m_name;
184
188 bool m_useMC;
189
193 bool m_checkDisconnect;
194
198 std::list<std::shared_ptr<OpenALHandle> > m_playingSounds;
199
203 std::list<std::shared_ptr<OpenALHandle> > m_pausedSounds;
204
208 std::recursive_mutex m_mutex;
209
213 std::thread m_thread;
214
218 bool m_playing;
219
223 int m_buffersize;
224
228 Buffer m_buffer;
229
233 Quaternion m_orientation;
234
236 uint64_t m_synchronizerPosition{0};
237 std::shared_ptr<IHandle> m_silenceHandle;
238
243 AUD_LOCAL void start();
244
248 AUD_LOCAL void updateStreams();
249
256 AUD_LOCAL bool getFormat(ALenum &format, Specs specs);
257
258 // delete copy constructor and operator=
259 OpenALDevice(const OpenALDevice&) = delete;
260 OpenALDevice& operator=(const OpenALDevice&) = delete;
261
262public:
272 OpenALDevice(DeviceSpecs specs, int buffersize = AUD_DEFAULT_BUFFER_SIZE, const std::string &name = "");
273
274 virtual ~OpenALDevice();
275
276 virtual DeviceSpecs getSpecs() const;
277 virtual std::shared_ptr<IHandle> play(std::shared_ptr<IReader> reader, bool keep = false);
278 virtual std::shared_ptr<IHandle> play(std::shared_ptr<ISound> sound, bool keep = false);
279 virtual void stopAll();
280 virtual void lock();
281 virtual void unlock();
282 virtual float getVolume() const;
283 virtual void setVolume(float volume);
284
285 virtual void seekSynchronizer(double time);
286 virtual double getSynchronizerPosition();
287 virtual void playSynchronizer();
288 virtual void stopSynchronizer();
289 virtual void setSyncCallback(syncFunction function, void* data);
290 virtual int isSynchronizerPlaying();
291
293 virtual void setListenerLocation(const Vector3& location);
295 virtual void setListenerVelocity(const Vector3& velocity);
297 virtual void setListenerOrientation(const Quaternion& orientation);
298 virtual float getSpeedOfSound() const;
299 virtual void setSpeedOfSound(float speed);
300 virtual float getDopplerFactor() const;
301 virtual void setDopplerFactor(float factor);
303 virtual void setDistanceModel(DistanceModel model);
304
309 static std::list<std::string> getDeviceNames();
310
314 static void registerPlugin();
315};
316
#define AUD_DEFAULT_BUFFER_SIZE
The default playback buffer size of a device.
Definition Audaspace.h:103
#define AUD_NAMESPACE_END
Closes the audaspace namespace aud.
Definition Audaspace.h:119
#define AUD_LOCAL
Used for hiding symbols from export in the shared library.
Definition Audaspace.h:80
#define AUD_NAMESPACE_BEGIN
Opens the audaspace namespace aud.
Definition Audaspace.h:116
#define AUD_PLUGIN_API
Used for exporting symbols in the shared library.
Definition Audaspace.h:94
The Buffer class.
Defines the I3DDevice interface as well as the different distance models.
DistanceModel
Possible distance models for the 3D device.
Definition I3DDevice.h:35
The I3DHandle interface.
The IDevice interface.
Defines the IHandle interface as well as possible states of the handle.
Status
Status of a playback handle.
Definition IHandle.h:31
void(* stopCallback)(void *)
The stopCallback is called when a handle reaches the end of the stream and thus gets stopped.
Definition IHandle.h:42
This class is a simple buffer in RAM which is 32 Byte aligned and provides resize functionality.
Definition Buffer.h:34
The I3DDevice interface represents an output device for 3D sound.
Definition I3DDevice.h:53
The I3DHandle interface represents a playback handle for 3D sources.
Definition I3DHandle.h:39
The IDevice interface represents an output device for sound sources.
Definition IDevice.h:46
void(* syncFunction)(void *, int, float)
The syncFunction is called when a synchronization event happens.
Definition IDevice.h:120
The IHandle interface represents a playback handles of a specific device.
Definition IHandle.h:49
virtual void setListenerLocation(const Vector3 &location)
Sets the listener location.
static std::list< std::string > getDeviceNames()
Retrieves a list of available hardware devices to open with OpenAL.
static void registerPlugin()
Registers this plugin.
virtual void unlock()
Unlocks the previously locked device.
virtual void setDistanceModel(DistanceModel model)
Sets the distance model.
virtual std::shared_ptr< IHandle > play(std::shared_ptr< IReader > reader, bool keep=false)
Plays a sound source.
virtual Vector3 getListenerVelocity() const
Retrieves the listener velocity.
virtual void lock()
Locks the device.
virtual DistanceModel getDistanceModel() const
Retrieves the distance model.
virtual void setListenerOrientation(const Quaternion &orientation)
Sets the listener orientation.
OpenALDevice(DeviceSpecs specs, int buffersize=AUD_DEFAULT_BUFFER_SIZE, const std::string &name="")
Opens the OpenAL audio device for playback.
virtual Vector3 getListenerLocation() const
Retrieves the listener location.
virtual Quaternion getListenerOrientation() const
Retrieves the listener orientation.
virtual float getSpeedOfSound() const
Retrieves the speed of sound.
virtual void stopAll()
Stops all playing sounds.
virtual void setDopplerFactor(float factor)
Sets the doppler factor.
virtual void setVolume(float volume)
Sets the overall device volume.
virtual float getVolume() const
Retrieves the overall device volume.
virtual DeviceSpecs getSpecs() const
Returns the specification of the device.
virtual std::shared_ptr< IHandle > play(std::shared_ptr< ISound > sound, bool keep=false)
Plays a sound source.
virtual void setSpeedOfSound(float speed)
Sets the speed of sound.
virtual float getDopplerFactor() const
Retrieves the doppler factor.
virtual void setListenerVelocity(const Vector3 &velocity)
Sets the listener velocity.
This class represents a quaternion used for 3D rotations.
Definition Math3D.h:206
This class represents a 3 dimensional vector.
Definition Math3D.h:36
Specification of a sound device.
Definition Specification.h:129
Specification of a sound source.
Definition Specification.h:119