Audaspace  1.3.0
A high level audio library.
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 
29 #include "devices/IDevice.h"
30 #include "devices/IHandle.h"
31 #include "devices/I3DDevice.h"
32 #include "devices/I3DHandle.h"
34 #include "util/Buffer.h"
35 
36 #include <al.h>
37 #include <alc.h>
38 #include <list>
39 #include <mutex>
40 #include <thread>
41 #include <string>
42 
44 
49 {
50 private:
52  class OpenALHandle : public IHandle, public I3DHandle
53  {
54  private:
55  friend class OpenALDevice;
56 
57  static const int CYCLE_BUFFERS = 3;
58 
60  bool m_isBuffered;
61 
63  std::shared_ptr<IReader> m_reader;
64 
66  bool m_keep;
67 
69  ALenum m_format;
70 
72  ALuint m_source;
73 
75  ALuint m_buffers[CYCLE_BUFFERS];
76 
78  int m_current;
79 
81  bool m_eos;
82 
84  int m_loopcount;
85 
87  stopCallback m_stop;
88 
90  void* m_stop_data;
91 
93  Quaternion m_orientation;
94 
96  Status m_status;
97 
99  OpenALDevice* m_device;
100 
101  AUD_LOCAL bool pause(bool keep);
102 
103  // delete copy constructor and operator=
104  OpenALHandle(const OpenALHandle&) = delete;
105  OpenALHandle& operator=(const OpenALHandle&) = delete;
106 
107  public:
108 
116  OpenALHandle(OpenALDevice* device, ALenum format, std::shared_ptr<IReader> reader, bool keep);
117 
118  virtual ~OpenALHandle() {}
119  virtual bool pause();
120  virtual bool resume();
121  virtual bool stop();
122  virtual bool getKeep();
123  virtual bool setKeep(bool keep);
124  virtual bool seek(float position);
125  virtual float getPosition();
126  virtual Status getStatus();
127  virtual float getVolume();
128  virtual bool setVolume(float volume);
129  virtual float getPitch();
130  virtual bool setPitch(float pitch);
131  virtual int getLoopCount();
132  virtual bool setLoopCount(int count);
133  virtual bool setStopCallback(stopCallback callback = 0, void* data = 0);
134 
135  virtual Vector3 getLocation();
136  virtual bool setLocation(const Vector3& location);
137  virtual Vector3 getVelocity();
138  virtual bool setVelocity(const Vector3& velocity);
139  virtual Quaternion getOrientation();
140  virtual bool setOrientation(const Quaternion& orientation);
141  virtual bool isRelative();
142  virtual bool setRelative(bool relative);
143  virtual float getVolumeMaximum();
144  virtual bool setVolumeMaximum(float volume);
145  virtual float getVolumeMinimum();
146  virtual bool setVolumeMinimum(float volume);
147  virtual float getDistanceMaximum();
148  virtual bool setDistanceMaximum(float distance);
149  virtual float getDistanceReference();
150  virtual bool setDistanceReference(float distance);
151  virtual float getAttenuation();
152  virtual bool setAttenuation(float factor);
153  virtual float getConeAngleOuter();
154  virtual bool setConeAngleOuter(float angle);
155  virtual float getConeAngleInner();
156  virtual bool setConeAngleInner(float angle);
157  virtual float getConeVolumeOuter();
158  virtual bool setConeVolumeOuter(float volume);
159  };
160 
164  ALCdevice* m_device;
165 
169  ALCcontext* m_context;
170 
174  DeviceSpecs m_specs;
175 
179  bool m_useMC;
180 
184  std::list<std::shared_ptr<OpenALHandle> > m_playingSounds;
185 
189  std::list<std::shared_ptr<OpenALHandle> > m_pausedSounds;
190 
194  std::recursive_mutex m_mutex;
195 
199  std::thread m_thread;
200 
204  bool m_playing;
205 
209  int m_buffersize;
210 
214  Buffer m_buffer;
215 
219  Quaternion m_orientation;
220 
222  DefaultSynchronizer m_synchronizer;
223 
228  AUD_LOCAL void start();
229 
233  AUD_LOCAL void updateStreams();
234 
241  AUD_LOCAL bool getFormat(ALenum &format, Specs specs);
242 
243  // delete copy constructor and operator=
244  OpenALDevice(const OpenALDevice&) = delete;
245  OpenALDevice& operator=(const OpenALDevice&) = delete;
246 
247 public:
257  OpenALDevice(DeviceSpecs specs, int buffersize = AUD_DEFAULT_BUFFER_SIZE, std::string name = "");
258 
259  virtual ~OpenALDevice();
260 
261  virtual DeviceSpecs getSpecs() const;
262  virtual std::shared_ptr<IHandle> play(std::shared_ptr<IReader> reader, bool keep = false);
263  virtual std::shared_ptr<IHandle> play(std::shared_ptr<ISound> sound, bool keep = false);
264  virtual void stopAll();
265  virtual void lock();
266  virtual void unlock();
267  virtual float getVolume() const;
268  virtual void setVolume(float volume);
269  virtual ISynchronizer* getSynchronizer();
270 
271  virtual Vector3 getListenerLocation() const;
272  virtual void setListenerLocation(const Vector3& location);
273  virtual Vector3 getListenerVelocity() const;
274  virtual void setListenerVelocity(const Vector3& velocity);
275  virtual Quaternion getListenerOrientation() const;
276  virtual void setListenerOrientation(const Quaternion& orientation);
277  virtual float getSpeedOfSound() const;
278  virtual void setSpeedOfSound(float speed);
279  virtual float getDopplerFactor() const;
280  virtual void setDopplerFactor(float factor);
281  virtual DistanceModel getDistanceModel() const;
282  virtual void setDistanceModel(DistanceModel model);
283 
288  static std::list<std::string> getDeviceNames();
289 
293  static void registerPlugin();
294 };
295 
This class represents a quaternion used for 3D rotations.
Definition: Math3D.h:205
This device plays through OpenAL.
Definition: OpenALDevice.h:48
#define AUD_NAMESPACE_BEGIN
Opens the audaspace namespace aud.
Definition: Audaspace.h:116
The IDevice interface.
virtual void stopAll()
Stops all playing sounds.
virtual void setSpeedOfSound(float speed)
Sets the speed of sound.
virtual std::shared_ptr< IHandle > play(std::shared_ptr< IReader > reader, bool keep=false)
Plays a sound source.
virtual float getVolume() const
Retrieves the overall device volume.
virtual void setVolume(float volume)
Sets the overall device volume.
This class enables global synchronization of several audio applications if supported.
Definition: ISynchronizer.h:38
virtual void setListenerOrientation(const Quaternion &orientation)
Sets the listener orientation.
virtual DistanceModel getDistanceModel() const
Retrieves the distance model.
virtual void lock()
Locks the device.
virtual void unlock()
Unlocks the previously locked device.
Specification of a sound source.
Definition: Specification.h:109
The I3DHandle interface represents a playback handle for 3D sources.
Definition: I3DHandle.h:38
#define AUD_DEFAULT_BUFFER_SIZE
The default playback buffer size of a device.
Definition: Audaspace.h:103
static void registerPlugin()
Registers this plugin.
virtual Quaternion getListenerOrientation() const
Retrieves the listener orientation.
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:33
virtual DeviceSpecs getSpecs() const
Returns the specification of the device.
Specification of a sound device.
Definition: Specification.h:119
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.
The I3DDevice interface represents an output device for 3D sound.
Definition: I3DDevice.h:52
virtual float getDopplerFactor() const
Retrieves the doppler factor.
virtual Vector3 getListenerLocation() const
Retrieves the listener location.
Defines the IHandle interface as well as possible states of the handle.
The DefaultSynchronizer class.
#define AUD_NAMESPACE_END
Closes the audaspace namespace aud.
Definition: Audaspace.h:119
The IHandle interface represents a playback handles of a specific device.
Definition: IHandle.h:48
The I3DHandle interface.
Defines the I3DDevice interface as well as the different distance models.
#define AUD_PLUGIN_API
Used for exporting symbols in the shared library.
Definition: Audaspace.h:94
The IDevice interface represents an output device for sound sources.
Definition: IDevice.h:46
This class represents a 3 dimensional vector.
Definition: Math3D.h:35
#define AUD_LOCAL
Used for hiding symbols from export in the shared library.
Definition: Audaspace.h:80
This class is a default ISynchronizer implementation that actually does no synchronization and is int...
Definition: DefaultSynchronizer.h:33
The Buffer class.
virtual void setListenerVelocity(const Vector3 &velocity)
Sets the listener velocity.
virtual void setDistanceModel(DistanceModel model)
Sets the distance model.
virtual void setDopplerFactor(float factor)
Sets the doppler factor.
Status
Status of a playback handle.
Definition: IHandle.h:30
virtual Vector3 getListenerVelocity() const
Retrieves the listener velocity.
DistanceModel
Possible distance models for the 3D device.
Definition: I3DDevice.h:34
virtual ISynchronizer * getSynchronizer()
Retrieves the synchronizer for this device, which enables accurate synchronization between audio play...
virtual float getSpeedOfSound() const
Retrieves the speed of sound.