Audaspace  1.4.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 
77 
79  bool m_keep;
80 
82  float m_user_pitch;
83 
86 
88  float m_user_pan;
89 
91  float m_volume;
92 
94  float m_old_volume;
95 
98 
101 
104 
107 
110 
113 
116 
119 
122 
125 
128 
131 
134 
136  int m_flags;
137 
140 
142  void* m_stop_data;
143 
146 
149 
155  bool pause(bool keep);
156 
157  public:
167  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);
168 
172  void update();
173 
178  void setSpecs(Specs specs);
179 
180  virtual ~SoftwareHandle() {}
181  virtual bool pause();
182  virtual bool resume();
183  virtual bool stop();
184  virtual bool getKeep();
185  virtual bool setKeep(bool keep);
186  virtual bool seek(double position);
187  virtual double getPosition();
188  virtual Status getStatus();
189  virtual float getVolume();
190  virtual bool setVolume(float volume);
191  virtual float getPitch();
192  virtual bool setPitch(float pitch);
193  virtual int getLoopCount();
194  virtual bool setLoopCount(int count);
195  virtual bool setStopCallback(stopCallback callback = 0, void* data = 0);
196 
197  virtual Vector3 getLocation();
198  virtual bool setLocation(const Vector3& location);
199  virtual Vector3 getVelocity();
200  virtual bool setVelocity(const Vector3& velocity);
201  virtual Quaternion getOrientation();
202  virtual bool setOrientation(const Quaternion& orientation);
203  virtual bool isRelative();
204  virtual bool setRelative(bool relative);
205  virtual float getVolumeMaximum();
206  virtual bool setVolumeMaximum(float volume);
207  virtual float getVolumeMinimum();
208  virtual bool setVolumeMinimum(float volume);
209  virtual float getDistanceMaximum();
210  virtual bool setDistanceMaximum(float distance);
211  virtual float getDistanceReference();
212  virtual bool setDistanceReference(float distance);
213  virtual float getAttenuation();
214  virtual bool setAttenuation(float factor);
215  virtual float getConeAngleOuter();
216  virtual bool setConeAngleOuter(float angle);
217  virtual float getConeAngleInner();
218  virtual bool setConeAngleInner(float angle);
219  virtual float getConeVolumeOuter();
220  virtual bool setConeVolumeOuter(float volume);
221  };
222 
227 
231  std::shared_ptr<Mixer> m_mixer;
232 
236  bool m_quality;
237 
241  void create();
242 
246  void destroy();
247 
253  void mix(data_t* buffer, int length);
254 
260  virtual void playing(bool playing)=0;
261 
266  void setSpecs(Specs specs);
267 
272  void setSpecs(DeviceSpecs specs);
273 
278  SoftwareDevice();
279 
280 private:
284  Buffer m_buffer;
285 
289  std::list<std::shared_ptr<SoftwareHandle> > m_playingSounds;
290 
294  std::list<std::shared_ptr<SoftwareHandle> > m_pausedSounds;
295 
299  bool m_playback;
300 
304  std::recursive_mutex m_mutex;
305 
309  float m_volume;
310 
312  Vector3 m_location;
313 
315  Vector3 m_velocity;
316 
318  Quaternion m_orientation;
319 
321  float m_speed_of_sound;
322 
324  float m_doppler_factor;
325 
327  DistanceModel m_distance_model;
328 
330  int m_flags;
331 
333  DefaultSynchronizer m_synchronizer;
334 
335  // delete copy constructor and operator=
336  SoftwareDevice(const SoftwareDevice&) = delete;
337  SoftwareDevice& operator=(const SoftwareDevice&) = delete;
338 
339 public:
340 
346  static void setPanning(IHandle* handle, float pan);
347 
352  void setQuality(bool quality);
353 
354  virtual DeviceSpecs getSpecs() const;
355  virtual std::shared_ptr<IHandle> play(std::shared_ptr<IReader> reader, bool keep = false);
356  virtual std::shared_ptr<IHandle> play(std::shared_ptr<ISound> sound, bool keep = false);
357  virtual void stopAll();
358  virtual void lock();
359  virtual void unlock();
360  virtual float getVolume() const;
361  virtual void setVolume(float volume);
362  virtual ISynchronizer* getSynchronizer();
363 
364  virtual Vector3 getListenerLocation() const;
365  virtual void setListenerLocation(const Vector3& location);
366  virtual Vector3 getListenerVelocity() const;
367  virtual void setListenerVelocity(const Vector3& velocity);
368  virtual Quaternion getListenerOrientation() const;
369  virtual void setListenerOrientation(const Quaternion& orientation);
370  virtual float getSpeedOfSound() const;
371  virtual void setSpeedOfSound(float speed);
372  virtual float getDopplerFactor() const;
373  virtual void setDopplerFactor(float factor);
374  virtual DistanceModel getDistanceModel() const;
375  virtual void setDistanceModel(DistanceModel model);
376 };
377 
This class represents a quaternion used for 3D rotations.
Definition: Math3D.h:205
bool m_keep
Whether to keep the source if end of it is reached.
Definition: SoftwareDevice.h:79
#define AUD_NAMESPACE_BEGIN
Opens the audaspace namespace aud.
Definition: Audaspace.h:116
This abstract class is able to mix audiosignals with same channel count and sample rate and convert i...
Definition: Mixer.h:39
std::shared_ptr< Mixer > m_mixer
The mixer.
Definition: SoftwareDevice.h:231
virtual bool stop()=0
Stops a played back or paused sound.
The IDevice interface.
virtual bool setKeep(bool keep)=0
Sets the behaviour of the device for a played back sound when the sound doesn&#39;t return any more sampl...
virtual bool seek(double position)=0
Seeks in a played back sound.
virtual float getPitch()=0
Retrieves the pitch of a playing sound.
void * m_stop_data
Stop callback data.
Definition: SoftwareDevice.h:142
float m_volume_min
Minimum volume.
Definition: SoftwareDevice.h:115
float m_old_volume
The previous calculated final volume of the source.
Definition: SoftwareDevice.h:94
virtual float getConeVolumeOuter()=0
Retrieves the outer volume of the cone of a source.
virtual float getVolumeMaximum()=0
Retrieves the maximum volume of a source.
This class enables global synchronization of several audio applications if supported.
Definition: ISynchronizer.h:38
Saves the data for playback.
Definition: SoftwareDevice.h:55
virtual bool setStopCallback(stopCallback callback=0, void *data=0)=0
Sets the callback function that&#39;s called when the end of a playing sound is reached.
float m_distance_reference
Reference distance;.
Definition: SoftwareDevice.h:121
#define AUD_API
Used for exporting symbols in the shared library.
Definition: Audaspace.h:93
float m_cone_angle_outer
Cone outer angle.
Definition: SoftwareDevice.h:127
Vector3 m_velocity
Velocity in 3D Space.
Definition: SoftwareDevice.h:103
virtual Status getStatus()=0
Returns the status of a played back sound.
This class maps a sound source&#39;s channels to a specific output channel count.
Definition: ChannelMapperReader.h:34
unsigned char data_t
Sample data type (format samples)
Definition: Audaspace.h:129
Specification of a sound source.
Definition: Specification.h:109
float m_cone_angle_inner
Cone inner angle.
Definition: SoftwareDevice.h:130
virtual bool setPitch(float pitch)=0
Sets the pitch of a playing sound.
bool m_relative
Whether the position to the listener is relative or absolute.
Definition: SoftwareDevice.h:109
The I3DHandle interface represents a playback handle for 3D sources.
Definition: I3DHandle.h:38
int m_flags
Rendering flags.
Definition: SoftwareDevice.h:136
std::shared_ptr< ResampleReader > m_resampler
The resample reader in between.
Definition: SoftwareDevice.h:70
virtual float getDistanceReference()=0
Retrieves the reference distance of a source.
DeviceSpecs m_specs
The specification of the device.
Definition: SoftwareDevice.h:226
virtual bool getKeep()=0
Gets the behaviour of the device for a played back sound when the sound doesn&#39;t return any more sampl...
float m_attenuation
Attenuation.
Definition: SoftwareDevice.h:124
virtual bool setOrientation(const Quaternion &orientation)=0
Sets the orientation of the source.
virtual bool setDistanceReference(float distance)=0
Sets the reference distance of a source.
virtual int getLoopCount()=0
Retrieves the loop count of a playing sound.
void(* stopCallback)(void *)
The stopCallback is called when a handle reaches the end of the stream and thus gets stopped...
Definition: IHandle.h:42
virtual bool setDistanceMaximum(float distance)=0
Sets the maximum distance of a source.
float m_volume
The calculated final volume of the source.
Definition: SoftwareDevice.h:91
float m_user_pitch
The user set pitch of the source.
Definition: SoftwareDevice.h:82
This class is a simple buffer in RAM which is 32 Byte aligned and provides resize functionality...
Definition: Buffer.h:33
std::shared_ptr< IReader > m_reader
The reader source.
Definition: SoftwareDevice.h:64
Status m_status
Current status of the handle.
Definition: SoftwareDevice.h:145
virtual bool setVolumeMinimum(float volume)=0
Sets the minimum volume of a source.
Specification of a sound device.
Definition: Specification.h:119
float m_user_volume
The user set volume of the source.
Definition: SoftwareDevice.h:85
virtual float getConeAngleInner()=0
Retrieves the inner opening angle of the cone of a source.
Quaternion m_orientation
Orientation in 3D Space.
Definition: SoftwareDevice.h:106
virtual Vector3 getVelocity()=0
Retrieves the velocity of the source.
virtual float getDistanceMaximum()=0
Retrieves the maximum distance of a source.
float m_cone_volume_outer
Cone outer volume.
Definition: SoftwareDevice.h:133
bool m_first_reading
Whether the source is being read for the first time.
Definition: SoftwareDevice.h:76
virtual float getConeAngleOuter()=0
Retrieves the outer opening angle of the cone of a source.
virtual float getAttenuation()=0
Retrieves the attenuation of a source.
float m_volume_max
Maximum volume.
Definition: SoftwareDevice.h:112
The I3DDevice interface represents an output device for 3D sound.
Definition: I3DDevice.h:52
virtual bool isRelative()=0
Checks whether the source location, velocity and orientation are relative to the listener.
Defines the IHandle interface as well as possible states of the handle.
SoftwareDevice * m_device
Own device.
Definition: SoftwareDevice.h:148
float m_distance_max
Maximum distance.
Definition: SoftwareDevice.h:118
virtual bool setRelative(bool relative)=0
Sets whether the source location, velocity and orientation are relative to the listener.
virtual bool setVolumeMaximum(float volume)=0
Sets the maximum volume of a source.
virtual bool setConeAngleInner(float angle)=0
Sets the inner opening angle of the cone of a source.
virtual Vector3 getLocation()=0
Retrieves the location of the source.
float m_user_pan
The user set panning for non-3D sources.
Definition: SoftwareDevice.h:88
The DefaultSynchronizer class.
virtual float getVolume()=0
Retrieves the volume of a playing sound.
virtual bool pause()=0
Pauses a played back sound.
This is the base class for all resampling readers.
Definition: ResampleReader.h:32
#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.
virtual bool setLoopCount(int count)=0
Sets the loop count of a playing sound.
Defines the I3DDevice interface as well as the different distance models.
std::shared_ptr< PitchReader > m_pitch
The pitch reader in between.
Definition: SoftwareDevice.h:67
The IDevice interface represents an output device for sound sources.
Definition: IDevice.h:46
virtual bool setVolume(float volume)=0
Sets the volume of a playing sound.
The software device is a generic device with software mixing.
Definition: SoftwareDevice.h:51
bool m_quality
Whether to do high or low quality resampling.
Definition: SoftwareDevice.h:236
This class represents a 3 dimensional vector.
Definition: Math3D.h:35
virtual float getVolumeMinimum()=0
Retrieves the minimum volume of a source.
This class is a default ISynchronizer implementation that actually does no synchronization and is int...
Definition: DefaultSynchronizer.h:33
virtual double getPosition()=0
Retrieves the current playback position of a sound.
The Buffer class.
virtual bool setConeVolumeOuter(float volume)=0
Sets the outer volume of the cone of a source.
int m_loopcount
The loop count of the source.
Definition: SoftwareDevice.h:97
This class reads another reader and changes it&#39;s pitch.
Definition: PitchReader.h:32
virtual bool setAttenuation(float factor)=0
Sets the attenuation of a source.
virtual Quaternion getOrientation()=0
Retrieves the orientation of the source.
Status
Status of a playback handle.
Definition: IHandle.h:30
DistanceModel
Possible distance models for the 3D device.
Definition: I3DDevice.h:34
Vector3 m_location
Location in 3D Space.
Definition: SoftwareDevice.h:100
std::shared_ptr< ChannelMapperReader > m_mapper
The channel mapper reader in between.
Definition: SoftwareDevice.h:73
virtual bool setVelocity(const Vector3 &velocity)=0
Sets the velocity of the source.
stopCallback m_stop
The stop callback.
Definition: SoftwareDevice.h:139
virtual bool setConeAngleOuter(float angle)=0
Sets the outer opening angle of the cone of a source.
virtual bool setLocation(const Vector3 &location)=0
Sets the location of the source.
virtual bool resume()=0
Resumes a paused sound.