Nebula
Loading...
Searching...
No Matches
audiodevice.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
18//------------------------------------------------------------------------------
19#include "core/refcounted.h"
20#include "core/singleton.h"
22#include "ids/idallocator.h"
23#include "audioclip.h"
24#include "debug/debugtimer.h"
25#include "debug/debugcounter.h"
26#include "math/point.h"
27#include "math/vector.h"
28#include "math/mat4.h"
29
30namespace SoLoud
31{
32class Wav;
33}
34
35namespace Audio
36{
37
39
41{
44
45public:
48
50 bool Open();
52 bool Close();
53
55 void OnFrame();
56
60 void UnloadClip(ClipId const id);
61
63 ClipInstanceId Play(ClipId clip, float volume, float pan, bool loop = false, float clock = 0.0f);
64
67 ClipId clip,
68 float volume,
69 Math::vec3 const& position,
70 Math::vec3 const& velocity,
71 float minDistance,
72 float maxDistance,
73 bool loop = false,
74 float clock = 0.0f
75 );
76
80 void Stop(ClipId id);
82 bool IsValid(ClipInstanceId id);
83
85 void UpdatePosition(ClipInstanceId id, Math::point const& pos);
87 void UpdateVelocity(ClipInstanceId id, Math::vector const& vel);
88
90 void SetListenerTransform(Math::mat4 const& transform);
92 void SetListenerVelocity(Math::vector const& velocity);
94 void ResetListener();
95
97 void SetSpeedOfSound(float value = 343.0f);
98
99
103 void DestroyAudioEmitter(AudioEmitterId const id);
105 ClipInstanceId Play(AudioEmitterId id, bool loop = false);
107 void SetSpatialize(AudioEmitterId id, bool value);
109 void SetPosition(AudioEmitterId id, Math::point const& pos);
111 void SetVelocity(AudioEmitterId id, Math::vector const& vel);
113 void SetMaxDistance(AudioEmitterId id, float value);
115 void SetMinDistance(AudioEmitterId id, float value);
117 void SetClock(AudioEmitterId id, float value);
119 void SetPan(AudioEmitterId id, float value);
120
121private:
123 {
124 CLIPID, // Clip that this emitter will be playing
125 POSITION, // spatial 3D position in world space coordinates
126 VELOCITY, // velocity used for doppler effect
127 VOLUME, // audio max volume
128 MINDISTANCE, // Min distance before audio attenuation starts
129 MAXDISTANCE, // Max audible distance
130 PAN, // 2D pan for non spatialized sounds. (L = -1.0, R = 1.0)
131 SPATIALIZE, // Set true if spatial position and velocity should be taken into account when playing sound
132 CLOCK, // Set this to > 0 if you need to delay the start of sounds so that rapidly launched sounds don't all get clumped to the start of the next outgoing sound buffer.
133 };
135
147
150
151 struct Listener
152 {
153 float forward[3] = {0, 0, 1};
154 float up[3] = {0, 1, 0};
155 float position[3] = {0, 0, 0};
156 float velocity[3] = {0, 0, 0};
158
159 _declare_timer(AudioOnFrameTime) _declare_counter(AudioNumberOfSoundsPlaying)
160};
161
162} // namespace Audio
Implements some audio clip types.
Central class of the core audio subsystem.
Definition audiodevice.h:41
void OnFrame()
Called per frame to update spatial positions.
Definition audiodevice.cc:69
ClipInstanceId PlaySpatial(ClipId clip, float volume, Math::vec3 const &position, Math::vec3 const &velocity, float minDistance, float maxDistance, bool loop=false, float clock=0.0f)
Play spatial audio clip that accounts for spatial position when playing. Returns the playing clip ins...
Definition audiodevice.cc:230
void SetClock(AudioEmitterId id, float value)
Set min delay between subsequent instances of an emitted sound (value of 0 disables)
Definition audiodevice.cc:372
void UnloadClip(ClipId const id)
Unload a soundfile.
Definition audiodevice.cc:125
void Stop(ClipId id)
Stop all instances of a clip.
Definition audiodevice.cc:318
Ids::IdAllocator< ClipId, Math::point, Math::vector, float, float, float, float, bool, float > emitterAllocator
Definition audiodevice.h:134
~AudioDevice()
Definition audiodevice.cc:27
Ids::IdAllocator< SoLoud::Wav, uint > wavs
Contains all wavs that are currently loaded.
Definition audiodevice.h:146
ClipId LoadClip(Resources::ResourceName const &name)
Load a soundfile into memory.
Definition audiodevice.cc:85
void SetMinDistance(AudioEmitterId id, float value)
Set the min audible distance of a sound.
Definition audiodevice.cc:363
void DestroyAudioEmitter(AudioEmitterId const id)
Destroy an audio emitter.
Definition audiodevice.cc:191
AudioEmitterId CreateAudioEmitter(Resources::ResourceName const &name)
Load a soundfile into memory.
Definition audiodevice.cc:143
void UpdateVelocity(ClipInstanceId id, Math::vector const &vel)
Update the spatial velocity of a sound instance.
Definition audiodevice.cc:399
void SetSpatialize(AudioEmitterId id, bool value)
Set whether or not to account for spatial position when playing a sound.
Definition audiodevice.cc:327
Util::Dictionary< Resources::ResourceName, ClipId > clipMap
resource -> clipid table
Definition audiodevice.h:149
void SetPosition(AudioEmitterId id, Math::point const &pos)
Set spatial position of a sound in world space.
Definition audiodevice.cc:336
__DeclareSingleton(AudioDevice)
ClipInstanceId Play(ClipId clip, float volume, float pan, bool loop=false, float clock=0.0f)
Play non-spatial audio clip. Returns the playing clip instance.
Definition audiodevice.cc:208
bool IsValid(ClipInstanceId id)
Check if an instance is valid.
Definition audiodevice.cc:473
WavAllocator
Definition audiodevice.h:137
@ REFCOUNT
Definition audiodevice.h:139
@ WAV
Definition audiodevice.h:138
struct Audio::AudioDevice::Listener listener
void SetPan(AudioEmitterId id, float value)
Set 2D pan for non spatialized sounds. (L = -1.0, R = 1.0)
Definition audiodevice.cc:381
void SetVelocity(AudioEmitterId id, Math::vector const &vel)
Set spatial velocity of a sound.
Definition audiodevice.cc:345
void SetMaxDistance(AudioEmitterId id, float value)
Set the max audible distance of a sound.
Definition audiodevice.cc:354
EmitterSlot
Definition audiodevice.h:123
@ SPATIALIZE
Definition audiodevice.h:131
@ CLOCK
Definition audiodevice.h:132
@ CLIPID
Definition audiodevice.h:124
@ POSITION
Definition audiodevice.h:125
@ VELOCITY
Definition audiodevice.h:126
@ PAN
Definition audiodevice.h:130
@ MINDISTANCE
Definition audiodevice.h:128
@ VOLUME
Definition audiodevice.h:127
@ MAXDISTANCE
Definition audiodevice.h:129
__DeclareClass(AudioDevice)
bool Close()
Shutdown the audio engine.
Definition audiodevice.cc:53
void StopInstance(ClipInstanceId id)
Stop an instance of a clip.
Definition audiodevice.cc:309
void SetSpeedOfSound(float value=343.0f)
Set the speed of sound constant. (default assumes 1 unit is 1 meter)
Definition audiodevice.cc:455
void UpdatePosition(ClipInstanceId id, Math::point const &pos)
Update the spatial position of a sound instance in world space.
Definition audiodevice.cc:390
void SetListenerVelocity(Math::vector const &velocity)
Set the listeners velocity.
Definition audiodevice.cc:443
void SetListenerTransform(Math::mat4 const &transform)
Set the listener transform in world space.
Definition audiodevice.cc:408
void ResetListener()
Reset listener.
Definition audiodevice.cc:464
bool Open()
Initialize the audio engine.
Definition audiodevice.cc:36
The common base class of Nebula.
Definition refcounted.h:38
An ID allocator associates an id with a slice in an N number of arrays.
Definition idallocator.h:39
A collection of key/value pairs with quick value retrieval by key at roughly O(log n).
Definition dictionary.h:34
A StringAtom.
Definition stringatom.h:22
#define _declare_counter(counter)
Definition debugcounter.h:31
#define _declare_timer(timer)
Definition debugtimer.h:35
#define ID_32_TYPE(x)
Definition id.h:16
Definition audioclip.h:15
Definition audiodevice.h:31
Definition audiodevice.h:152
float up[3]
Definition audiodevice.h:154
float position[3]
Definition audiodevice.h:155
float forward[3]
Definition audiodevice.h:153
float velocity[3]
Definition audiodevice.h:156
Definition audiodevice.h:38
Definition audioclip.h:17
Definition audioclip.h:18
A 4x4 single point precision float matrix.
Definition mat4.h:49
Represents a 3D point in space.
Definition point.h:22
A 3D vector.
Definition vec3.h:40
A vector is a 3D direction in space.
Definition vector.h:22