Audaspace  1.3.0
A high level audio library.
SoftwareDevice.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 
25 #include "devices/IDevice.h"
26 #include "devices/IHandle.h"
27 #include "devices/I3DDevice.h"
28 #include "devices/I3DHandle.h"
30 #include "util/Buffer.h"
31 
32 #include <list>
33 #include <mutex>
34 
36 
37 class Mixer;
38 class PitchReader;
39 class ResampleReader;
41 
51 class AUD_API SoftwareDevice : public IDevice, public I3DDevice
52 {
53 protected:
55  class AUD_API SoftwareHandle : public IHandle, public I3DHandle
56  {
57  private:
58  // delete copy constructor and operator=
59  SoftwareHandle(const SoftwareHandle&) = delete;
60  SoftwareHandle& operator=(const SoftwareHandle&) = delete;
61 
62  public:
64  std::shared_ptr<IReader> m_reader;
65 
67  std::shared_ptr<PitchReader> m_pitch;
68 
70  std::shared_ptr<ResampleReader> m_resampler;
71 
73  std::shared_ptr<ChannelMapperReader> m_mapper;
74 
76  bool m_keep;
77 
79  float m_user_pitch;
80 
83 
85  float m_user_pan;
86 
88  float m_volume;
89 
91  float m_old_volume;
92 
95 
98 
101 
104 
107 
110 
113 
116 
119 
122 
125 
128 
131 
133  int m_flags;
134 
137 
139  void* m_stop_data;
140 
143 
146 
152  bool pause(bool keep);
153 
154  public:
164  SoftwareHandle(SoftwareDevice* device, std::shared_ptr<IReader> reader, std::shared_ptr<PitchReader> pitch, std::shared_ptr<ResampleReader> resampler, std::shared_ptr<ChannelMapperReader> mapper, bool keep);
165 
169  void update();
170 
175  void setSpecs(Specs specs);
176 
177  virtual ~SoftwareHandle() {}
178  virtual bool pause();
179  virtual bool resume();
180  virtual bool stop();
181  virtual bool getKeep();
182  virtual bool setKeep(bool keep);
183  virtual bool seek(float position);
184  virtual float getPosition();
185  virtual Status getStatus();
186  virtual float getVolume();
187  virtual bool setVolume(float volume);
188  virtual float getPitch();
189  virtual bool setPitch(float pitch);
190  virtual int getLoopCount();
191  virtual bool setLoopCount(int count);
192  virtual bool setStopCallback(stopCallback callback = 0, void* data = 0);
193 
194  virtual Vector3 getLocation();
195  virtual bool setLocation(const Vector3& location);
196  virtual Vector3 getVelocity();
197  virtual bool setVelocity(const Vector3& velocity);
199  virtual bool setOrientation(const Quaternion& orientation);
200  virtual bool isRelative();
201  virtual bool setRelative(bool relative);
202  virtual float getVolumeMaximum();
203  virtual bool setVolumeMaximum(float volume);
204  virtual float getVolumeMinimum();
205  virtual bool setVolumeMinimum(float volume);
206  virtual float getDistanceMaximum();
207  virtual bool setDistanceMaximum(float distance);
208  virtual float getDistanceReference();
209  virtual bool setDistanceReference(float distance);
210  virtual float getAttenuation();
211  virtual bool setAttenuation(float factor);
212  virtual float getConeAngleOuter();
213  virtual bool setConeAngleOuter(float angle);
214  virtual float getConeAngleInner();
215  virtual bool setConeAngleInner(float angle);
216  virtual float getConeVolumeOuter();
217  virtual bool setConeVolumeOuter(float volume);
218  };
219 
224 
228  std::shared_ptr<Mixer> m_mixer;
229 
233  bool m_quality;
234 
238  void create();
239 
243  void destroy();
244 
250  void mix(data_t* buffer, int length);
251 
256  virtual void playing(bool playing)=0;
257 
262  void setSpecs(Specs specs);
263 
269 
270 private:
274  Buffer m_buffer;
275 
279  std::list<std::shared_ptr<SoftwareHandle> > m_playingSounds;
280 
284  std::list<std::shared_ptr<SoftwareHandle> > m_pausedSounds;
285 
289  bool m_playback;
290 
294  std::recursive_mutex m_mutex;
295 
299  float m_volume;
300 
302  Vector3 m_location;
303 
305  Vector3 m_velocity;
306 
308  Quaternion m_orientation;
309 
311  float m_speed_of_sound;
312 
314  float m_doppler_factor;
315 
317  DistanceModel m_distance_model;
318 
320  int m_flags;
321 
323  DefaultSynchronizer m_synchronizer;
324 
325  // delete copy constructor and operator=
326  SoftwareDevice(const SoftwareDevice&) = delete;
327  SoftwareDevice& operator=(const SoftwareDevice&) = delete;
328 
329 public:
330 
336  static void setPanning(IHandle* handle, float pan);
337 
342  void setQuality(bool quality);
343 
344  virtual DeviceSpecs getSpecs() const;
345  virtual std::shared_ptr<IHandle> play(std::shared_ptr<IReader> reader, bool keep = false);
346  virtual std::shared_ptr<IHandle> play(std::shared_ptr<ISound> sound, bool keep = false);
347  virtual void stopAll();
348  virtual void lock();
349  virtual void unlock();
350  virtual float getVolume() const;
351  virtual void setVolume(float volume);
353 
354  virtual Vector3 getListenerLocation() const;
355  virtual void setListenerLocation(const Vector3& location);
356  virtual Vector3 getListenerVelocity() const;
357  virtual void setListenerVelocity(const Vector3& velocity);
359  virtual void setListenerOrientation(const Quaternion& orientation);
360  virtual float getSpeedOfSound() const;
361  virtual void setSpeedOfSound(float speed);
362  virtual float getDopplerFactor() const;
363  virtual void setDopplerFactor(float factor);
365  virtual void setDistanceModel(DistanceModel model);
366 };
367 
Specs
Specification of a sound source.
Definition: Specification.h:110
SoftwareDevice::SoftwareHandle::getConeAngleInner
virtual float getConeAngleInner()
Retrieves the inner opening angle of the cone of a source.
SoftwareDevice::play
virtual std::shared_ptr< IHandle > play(std::shared_ptr< IReader > reader, bool keep=false)
Plays a sound source.
SoftwareDevice::SoftwareHandle::getAttenuation
virtual float getAttenuation()
Retrieves the attenuation of a source.
SoftwareDevice::m_mixer
std::shared_ptr< Mixer > m_mixer
The mixer.
Definition: SoftwareDevice.h:228
SoftwareDevice::SoftwareHandle::m_resampler
std::shared_ptr< ResampleReader > m_resampler
The resample reader in between.
Definition: SoftwareDevice.h:70
I3DDevice.h
Defines the I3DDevice interface as well as the different distance models.
SoftwareDevice::setQuality
void setQuality(bool quality)
Sets the resampling quality.
SoftwareDevice::getSpecs
virtual DeviceSpecs getSpecs() const
Returns the specification of the device.
SoftwareDevice::SoftwareHandle::seek
virtual bool seek(float position)
Seeks in a played back sound.
SoftwareDevice::SoftwareHandle::m_pitch
std::shared_ptr< PitchReader > m_pitch
The pitch reader in between.
Definition: SoftwareDevice.h:67
SoftwareDevice::SoftwareHandle::m_loopcount
int m_loopcount
The loop count of the source.
Definition: SoftwareDevice.h:94
SoftwareDevice::SoftwareHandle::setDistanceMaximum
virtual bool setDistanceMaximum(float distance)
Sets the maximum distance of a source.
SoftwareDevice::SoftwareHandle::setOrientation
virtual bool setOrientation(const Quaternion &orientation)
Sets the orientation of the source.
Mixer
This abstract class is able to mix audiosignals with same channel count and sample rate and convert i...
Definition: Mixer.h:40
SoftwareDevice::unlock
virtual void unlock()
Unlocks the previously locked device.
SoftwareDevice::SoftwareHandle::update
void update()
Updates the handle's playback parameters.
SoftwareDevice::setSpecs
void setSpecs(Specs specs)
Sets the audio output specification of the device.
SoftwareDevice::getSynchronizer
virtual ISynchronizer * getSynchronizer()
Retrieves the synchronizer for this device, which enables accurate synchronization between audio play...
SoftwareDevice::SoftwareHandle::m_stop_data
void * m_stop_data
Stop callback data.
Definition: SoftwareDevice.h:139
SoftwareDevice::SoftwareHandle::m_status
Status m_status
Current status of the handle.
Definition: SoftwareDevice.h:142
PitchReader
This class reads another reader and changes it's pitch.
Definition: PitchReader.h:33
SoftwareDevice::setVolume
virtual void setVolume(float volume)
Sets the overall device volume.
SoftwareDevice::setListenerLocation
virtual void setListenerLocation(const Vector3 &location)
Sets the listener location.
SoftwareDevice::setSpeedOfSound
virtual void setSpeedOfSound(float speed)
Sets the speed of sound.
I3DHandle.h
The I3DHandle interface.
stopCallback
void(* stopCallback)(void *)
The stopCallback is called when a handle reaches the end of the stream and thus gets stopped.
Definition: IHandle.h:42
ResampleReader
This is the base class for all resampling readers.
Definition: ResampleReader.h:33
SoftwareDevice::SoftwareHandle::setRelative
virtual bool setRelative(bool relative)
Sets whether the source location, velocity and orientation are relative to the listener.
DeviceSpecs
Specification of a sound device.
Definition: Specification.h:120
SoftwareDevice::SoftwareHandle::setVelocity
virtual bool setVelocity(const Vector3 &velocity)
Sets the velocity of the source.
DistanceModel
DistanceModel
Possible distance models for the 3D device.
Definition: I3DDevice.h:35
ISynchronizer
This class enables global synchronization of several audio applications if supported.
Definition: ISynchronizer.h:39
SoftwareDevice::SoftwareHandle::getKeep
virtual bool getKeep()
Gets the behaviour of the device for a played back sound when the sound doesn't return any more sampl...
SoftwareDevice::getDistanceModel
virtual DistanceModel getDistanceModel() const
Retrieves the distance model.
SoftwareDevice::SoftwareHandle::m_keep
bool m_keep
Whether to keep the source if end of it is reached.
Definition: SoftwareDevice.h:76
SoftwareDevice::SoftwareHandle::m_user_pitch
float m_user_pitch
The user set pitch of the source.
Definition: SoftwareDevice.h:79
SoftwareDevice::SoftwareHandle::getConeVolumeOuter
virtual float getConeVolumeOuter()
Retrieves the outer volume of the cone of a source.
SoftwareDevice::SoftwareDevice
SoftwareDevice()
Empty default constructor.
SoftwareDevice::playing
virtual void playing(bool playing)=0
This function tells the device, to start or pause playback.
SoftwareDevice::setListenerVelocity
virtual void setListenerVelocity(const Vector3 &velocity)
Sets the listener velocity.
DefaultSynchronizer
This class is a default ISynchronizer implementation that actually does no synchronization and is int...
Definition: DefaultSynchronizer.h:34
SoftwareDevice::SoftwareHandle::pause
virtual bool pause()
Pauses a played back sound.
ChannelMapperReader
This class maps a sound source's channels to a specific output channel count.
Definition: ChannelMapperReader.h:35
DefaultSynchronizer.h
The DefaultSynchronizer class.
SoftwareDevice::SoftwareHandle::setLoopCount
virtual bool setLoopCount(int count)
Sets the loop count of a playing sound.
SoftwareDevice::SoftwareHandle::getConeAngleOuter
virtual float getConeAngleOuter()
Retrieves the outer opening angle of the cone of a source.
SoftwareDevice::create
void create()
Initializes member variables.
SoftwareDevice::SoftwareHandle::m_flags
int m_flags
Rendering flags.
Definition: SoftwareDevice.h:133
SoftwareDevice::SoftwareHandle::getVolume
virtual float getVolume()
Retrieves the volume of a playing sound.
SoftwareDevice::SoftwareHandle::setVolume
virtual bool setVolume(float volume)
Sets the volume of a playing sound.
SoftwareDevice::SoftwareHandle::m_stop
stopCallback m_stop
The stop callback.
Definition: SoftwareDevice.h:136
SoftwareDevice::SoftwareHandle::setSpecs
void setSpecs(Specs specs)
Sets the audio output specification of the readers.
SoftwareDevice::SoftwareHandle::m_device
SoftwareDevice * m_device
Own device.
Definition: SoftwareDevice.h:145
SoftwareDevice::m_specs
DeviceSpecs m_specs
The specification of the device.
Definition: SoftwareDevice.h:223
I3DHandle
The I3DHandle interface represents a playback handle for 3D sources.
Definition: I3DHandle.h:39
SoftwareDevice::SoftwareHandle::m_distance_max
float m_distance_max
Maximum distance.
Definition: SoftwareDevice.h:115
SoftwareDevice::SoftwareHandle::setDistanceReference
virtual bool setDistanceReference(float distance)
Sets the reference distance of a source.
SoftwareDevice::SoftwareHandle::getDistanceReference
virtual float getDistanceReference()
Retrieves the reference distance of a source.
SoftwareDevice::SoftwareHandle::setVolumeMaximum
virtual bool setVolumeMaximum(float volume)
Sets the maximum volume of a source.
Vector3
This class represents a 3 dimensional vector.
Definition: Math3D.h:36
SoftwareDevice::SoftwareHandle::isRelative
virtual bool isRelative()
Checks whether the source location, velocity and orientation are relative to the listener.
SoftwareDevice::SoftwareHandle::m_old_volume
float m_old_volume
The previous calculated final volume of the source.
Definition: SoftwareDevice.h:91
SoftwareDevice::SoftwareHandle::m_cone_angle_inner
float m_cone_angle_inner
Cone inner angle.
Definition: SoftwareDevice.h:127
SoftwareDevice::SoftwareHandle::setConeVolumeOuter
virtual bool setConeVolumeOuter(float volume)
Sets the outer volume of the cone of a source.
SoftwareDevice::SoftwareHandle::setKeep
virtual bool setKeep(bool keep)
Sets the behaviour of the device for a played back sound when the sound doesn't return any more sampl...
SoftwareDevice::lock
virtual void lock()
Locks the device.
Buffer.h
The Buffer class.
IDevice.h
The IDevice interface.
SoftwareDevice::SoftwareHandle::m_reader
std::shared_ptr< IReader > m_reader
The reader source.
Definition: SoftwareDevice.h:64
SoftwareDevice::SoftwareHandle::m_volume_min
float m_volume_min
Minimum volume.
Definition: SoftwareDevice.h:112
SoftwareDevice::SoftwareHandle::m_cone_angle_outer
float m_cone_angle_outer
Cone outer angle.
Definition: SoftwareDevice.h:124
SoftwareDevice::SoftwareHandle::setLocation
virtual bool setLocation(const Vector3 &location)
Sets the location of the source.
SoftwareDevice::SoftwareHandle
Saves the data for playback.
Definition: SoftwareDevice.h:56
SoftwareDevice::getSpeedOfSound
virtual float getSpeedOfSound() const
Retrieves the speed of sound.
SoftwareDevice::SoftwareHandle::getDistanceMaximum
virtual float getDistanceMaximum()
Retrieves the maximum distance of a source.
SoftwareDevice::SoftwareHandle::m_user_pan
float m_user_pan
The user set panning for non-3D sources.
Definition: SoftwareDevice.h:85
SoftwareDevice::SoftwareHandle::m_relative
bool m_relative
Whether the position to the listener is relative or absolute.
Definition: SoftwareDevice.h:106
SoftwareDevice::SoftwareHandle::getVelocity
virtual Vector3 getVelocity()
Retrieves the velocity of the source.
SoftwareDevice::SoftwareHandle::pause
bool pause(bool keep)
This method is for internal use only.
Quaternion
This class represents a quaternion used for 3D rotations.
Definition: Math3D.h:206
Buffer
This class is a simple buffer in RAM which is 32 Byte aligned and provides resize functionality.
Definition: Buffer.h:34
data_t
unsigned char data_t
Sample data type (format samples)
Definition: Audaspace.h:129
SoftwareDevice::SoftwareHandle::resume
virtual bool resume()
Resumes a paused sound.
SoftwareDevice::m_quality
bool m_quality
Whether to do high or low quality resampling.
Definition: SoftwareDevice.h:233
SoftwareDevice::SoftwareHandle::getPosition
virtual float getPosition()
Retrieves the current playback position of a sound.
SoftwareDevice::stopAll
virtual void stopAll()
Stops all playing sounds.
SoftwareDevice::SoftwareHandle::m_user_volume
float m_user_volume
The user set volume of the source.
Definition: SoftwareDevice.h:82
IDevice
The IDevice interface represents an output device for sound sources.
Definition: IDevice.h:47
I3DDevice
The I3DDevice interface represents an output device for 3D sound.
Definition: I3DDevice.h:53
SoftwareDevice::SoftwareHandle::getVolumeMinimum
virtual float getVolumeMinimum()
Retrieves the minimum volume of a source.
AUD_NAMESPACE_END
#define AUD_NAMESPACE_END
Closes the audaspace namespace aud.
Definition: Audaspace.h:119
SoftwareDevice::SoftwareHandle::setStopCallback
virtual bool setStopCallback(stopCallback callback=0, void *data=0)
Sets the callback function that's called when the end of a playing sound is reached.
SoftwareDevice::SoftwareHandle::m_distance_reference
float m_distance_reference
Reference distance;.
Definition: SoftwareDevice.h:118
SoftwareDevice::SoftwareHandle::m_attenuation
float m_attenuation
Attenuation.
Definition: SoftwareDevice.h:121
SoftwareDevice::mix
void mix(data_t *buffer, int length)
Mixes the next samples into the buffer.
SoftwareDevice::setDistanceModel
virtual void setDistanceModel(DistanceModel model)
Sets the distance model.
SoftwareDevice::SoftwareHandle::stop
virtual bool stop()
Stops a played back or paused sound.
SoftwareDevice::SoftwareHandle::getPitch
virtual float getPitch()
Retrieves the pitch of a playing sound.
SoftwareDevice::setDopplerFactor
virtual void setDopplerFactor(float factor)
Sets the doppler factor.
SoftwareDevice::SoftwareHandle::getLoopCount
virtual int getLoopCount()
Retrieves the loop count of a playing sound.
SoftwareDevice::SoftwareHandle::getStatus
virtual Status getStatus()
Returns the status of a played back sound.
SoftwareDevice::getVolume
virtual float getVolume() const
Retrieves the overall device volume.
SoftwareDevice::SoftwareHandle::getVolumeMaximum
virtual float getVolumeMaximum()
Retrieves the maximum volume of a source.
SoftwareDevice::SoftwareHandle::m_volume
float m_volume
The calculated final volume of the source.
Definition: SoftwareDevice.h:88
IHandle
The IHandle interface represents a playback handles of a specific device.
Definition: IHandle.h:49
SoftwareDevice::getListenerLocation
virtual Vector3 getListenerLocation() const
Retrieves the listener location.
SoftwareDevice::getDopplerFactor
virtual float getDopplerFactor() const
Retrieves the doppler factor.
SoftwareDevice::SoftwareHandle::setVolumeMinimum
virtual bool setVolumeMinimum(float volume)
Sets the minimum volume of a source.
SoftwareDevice::setListenerOrientation
virtual void setListenerOrientation(const Quaternion &orientation)
Sets the listener orientation.
SoftwareDevice::getListenerVelocity
virtual Vector3 getListenerVelocity() const
Retrieves the listener velocity.
SoftwareDevice::SoftwareHandle::m_velocity
Vector3 m_velocity
Velocity in 3D Space.
Definition: SoftwareDevice.h:100
SoftwareDevice::SoftwareHandle::SoftwareHandle
SoftwareHandle(SoftwareDevice *device, std::shared_ptr< IReader > reader, std::shared_ptr< PitchReader > pitch, std::shared_ptr< ResampleReader > resampler, std::shared_ptr< ChannelMapperReader > mapper, bool keep)
Creates a new software handle.
SoftwareDevice::SoftwareHandle::setConeAngleInner
virtual bool setConeAngleInner(float angle)
Sets the inner opening angle of the cone of a source.
SoftwareDevice::SoftwareHandle::setAttenuation
virtual bool setAttenuation(float factor)
Sets the attenuation of a source.
SoftwareDevice::SoftwareHandle::m_volume_max
float m_volume_max
Maximum volume.
Definition: SoftwareDevice.h:109
AUD_NAMESPACE_BEGIN
#define AUD_NAMESPACE_BEGIN
Opens the audaspace namespace aud.
Definition: Audaspace.h:116
IHandle.h
Defines the IHandle interface as well as possible states of the handle.
AUD_API
#define AUD_API
Used for exporting symbols in the shared library.
Definition: Audaspace.h:93
SoftwareDevice::SoftwareHandle::m_cone_volume_outer
float m_cone_volume_outer
Cone outer volume.
Definition: SoftwareDevice.h:130
SoftwareDevice::play
virtual std::shared_ptr< IHandle > play(std::shared_ptr< ISound > sound, bool keep=false)
Plays a sound source.
SoftwareDevice::SoftwareHandle::setConeAngleOuter
virtual bool setConeAngleOuter(float angle)
Sets the outer opening angle of the cone of a source.
SoftwareDevice::SoftwareHandle::setPitch
virtual bool setPitch(float pitch)
Sets the pitch of a playing sound.
SoftwareDevice::destroy
void destroy()
Uninitializes member variables.
SoftwareDevice
The software device is a generic device with software mixing.
Definition: SoftwareDevice.h:52
SoftwareDevice::setPanning
static void setPanning(IHandle *handle, float pan)
Sets the panning of a specific handle.
SoftwareDevice::SoftwareHandle::getOrientation
virtual Quaternion getOrientation()
Retrieves the orientation of the source.
SoftwareDevice::SoftwareHandle::m_location
Vector3 m_location
Location in 3D Space.
Definition: SoftwareDevice.h:97
SoftwareDevice::SoftwareHandle::m_mapper
std::shared_ptr< ChannelMapperReader > m_mapper
The channel mapper reader in between.
Definition: SoftwareDevice.h:73
SoftwareDevice::SoftwareHandle::getLocation
virtual Vector3 getLocation()
Retrieves the location of the source.
SoftwareDevice::getListenerOrientation
virtual Quaternion getListenerOrientation() const
Retrieves the listener orientation.
SoftwareDevice::SoftwareHandle::m_orientation
Quaternion m_orientation
Orientation in 3D Space.
Definition: SoftwareDevice.h:103
Status
Status
Status of a playback handle.
Definition: IHandle.h:31