ASW Lib
A.D.S. Games SDL Wrapper Library. A library targeted at Allegro4 users who want to switch to SDL3 and use modern c++.
Loading...
Searching...
No Matches
sound.h
Go to the documentation of this file.
1
8
9#ifndef ASW_SOUND_H
10#define ASW_SOUND_H
11
12#include <cstdint>
13
14#include "./geometry.h"
15#include "./types.h"
16
17namespace asw::sound {
18
22enum class Bus : uint8_t {
23 Sfx,
24 Music,
25 Ambient,
26 Ui,
27 Voice,
28 Count,
29};
30
33enum class Rolloff : uint8_t {
35 Linear,
37 Inverse,
40};
41
44enum class SpatialMode : uint8_t {
46 Stereo,
50};
51
56 float min_distance { 100.0F };
57
59 float max_distance { 1000.0F };
60
63};
64
69 float volume { 1.0F };
70
72 float pan { 0.0F };
73
75 bool loop { false };
76
78 float pitch { 1.0F };
79
82 float pitch_variation { 0.0F };
83
85 float volume_variation { 0.0F };
86
89
92 int priority { 0 };
93
95 float fade_in_s { 0.0F };
96
99};
100
101struct HandleAccess;
102
110public:
113 SoundHandle() = default;
114
119 bool is_playing() const;
120
125 void stop(float fade_out_s = 0.0F) const;
126
129 void pause() const;
130
133 void resume() const;
134
139 void set_volume(float volume) const;
140
145 void set_pitch(float pitch) const;
146
151 void set_pan(float pan) const;
152
157 void set_position(const Vec2<float>& position) const;
158
163 void set_velocity(const Vec2<float>& velocity) const;
164
169 void set_occlusion(float amount) const;
170
175 void set_attenuation(const Attenuation& attenuation) const;
176
177private:
178 friend struct HandleAccess;
179
180 SoundHandle(int voice, uint32_t generation);
181
182 int voice { -1 };
183 uint32_t generation { 0 };
184};
185
190bool _init();
191
195void _update();
196
199void _shutdown();
200
205MIX_Mixer* get_mixer();
206
214uint32_t _get_session();
215
226SoundHandle play(
227 const asw::Sample& sample, float volume = 1.0F, float pan = 0.0F, bool loop = false);
228
235SoundHandle play(const asw::Sample& sample, const PlayOptions& options);
236
246SoundHandle play_positional(
247 const asw::Sample& sample, const Vec2<float>& position, const PlayOptions& options = {});
248
258SoundHandle play_at(const asw::Sample& sample, float x, float volume = 1.0F);
259
270SoundHandle play_at(const asw::Sample& sample, float x, const PlayOptions& options);
271
277void set_listener(const Vec2<float>& position, const Vec2<float>& velocity = { 0.0F, 0.0F });
278
283Vec2<float> get_listener();
284
290
297void set_doppler(float factor, float speed_of_sound = 3000.0F);
298
304void set_bus_volume(Bus bus, float volume);
305
311float get_bus_volume(Bus bus);
312
322void duck(Bus bus, float gain, float hold_s, float fade_s = 0.2F);
323
330void play_music(const asw::Music& sample, float volume = 1.0F, float fade_in_s = 0.0F);
331
334void stop_music(float fade_out_s = 0.0F);
335
338void pause_music();
339
342void resume_music();
343
348bool is_music_playing();
349
354bool is_music_paused();
355
360void set_master_volume(float volume);
361
366void set_sfx_volume(float volume);
367
372void set_music_volume(float volume);
373
378float get_master_volume();
379
384float get_sfx_volume();
385
390float get_music_volume();
391
392} // namespace asw::sound
393
394#endif // ASW_SOUND_H
A 2D vector in space.
Definition geometry.h:27
A handle to a playing sound, used to change it while it plays.
Definition sound.h:109
void set_occlusion(float amount) const
Muffle the sound, as if heard through a wall.
Definition sound.cpp:420
void set_velocity(const Vec2< float > &velocity) const
Set how fast the sound is moving, for doppler pitch shift.
Definition sound.cpp:412
void set_attenuation(const Attenuation &attenuation) const
Change the distance fall off.
Definition sound.cpp:428
void set_position(const Vec2< float > &position) const
Set where the sound is in the world. Makes the sound positional.
Definition sound.cpp:403
bool is_playing() const
Check if the sound is still playing or paused.
Definition sound.cpp:351
void stop(float fade_out_s=0.0F) const
Stop the sound.
Definition sound.cpp:356
void set_pitch(float pitch) const
Set the playback speed and pitch.
Definition sound.cpp:386
SoundHandle()=default
Create a handle that refers to nothing.
void pause() const
Pause the sound.
Definition sound.cpp:364
void set_pan(float pan) const
Set the panning. Makes a positional sound non positional.
Definition sound.cpp:394
void resume() const
Resume a paused sound.
Definition sound.cpp:371
void set_volume(float volume) const
Set the volume.
Definition sound.cpp:378
Common geometry types.
bool is_music_paused()
Check if music is paused.
Definition sound.cpp:750
void set_doppler(float factor, float speed_of_sound=3000.0F)
Set how strong doppler pitch shift is.
Definition sound.cpp:679
void pause_music()
Pause the currently playing music.
Definition sound.cpp:735
float get_master_volume()
Get the current master volume multiplier.
Definition sound.cpp:772
void play_music(const asw::Music &sample, float volume=1.0F, float fade_in_s=0.0F)
Play a music sample.
Definition sound.cpp:713
bool is_music_playing()
Check if music is currently playing.
Definition sound.cpp:745
void set_sfx_volume(float volume)
Set the SFX volume multiplier. Same as the Sfx bus volume.
Definition sound.cpp:761
float get_music_volume()
Get the current music volume multiplier.
Definition sound.cpp:782
void resume_music()
Resume paused music.
Definition sound.cpp:740
Bus
Mix buses. Each sound plays on one bus, and each bus has its own volume and can be ducked.
Definition sound.h:22
void _shutdown()
Shut down the sound module. Called automatically by asw::core::shutdown().
Definition sound.cpp:444
MIX_Mixer * get_mixer()
Get the SDL mixer device.
Definition sound.cpp:439
float get_sfx_volume()
Get the current SFX volume multiplier.
Definition sound.cpp:777
void set_listener(const Vec2< float > &position, const Vec2< float > &velocity={ 0.0F, 0.0F })
Set where the listener is, usually the player or camera centre.
Definition sound.cpp:663
void set_music_volume(float volume)
Set the music volume multiplier. Same as the Music bus volume.
Definition sound.cpp:766
void set_master_volume(float volume)
Set the master volume multiplier (affects all audio).
Definition sound.cpp:755
void set_bus_volume(Bus bus, float volume)
Set a bus volume.
Definition sound.cpp:685
void stop_music(float fade_out_s=0.0F)
Stop the currently playing music.
Definition sound.cpp:727
SoundHandle play(const asw::Sample &sample, float volume=1.0F, float pan=0.0F, bool loop=false)
Play a sample.
Definition sound.cpp:609
bool _init()
Initialize the sound module. Called automatically by asw::core::init().
Definition sound.cpp:465
SpatialMode
How positional sounds are placed on the speakers.
Definition sound.h:44
@ Stereo
Left/right panning. Best for headphones and side on games.
Vec2< float > get_listener()
Get the listener position.
Definition sound.cpp:669
void duck(Bus bus, float gain, float hold_s, float fade_s=0.2F)
Turn a bus down for a while, for example music under dialogue or everything under a big explosion....
Definition sound.cpp:701
Rolloff
How volume falls off with distance for positional sounds.
Definition sound.h:33
@ Linear
Straight line from full volume at min_distance to silent at max_distance.
@ Inverse
Natural sounding, loud close up and a long quiet tail.
@ InverseSquare
Like Inverse but drops faster.
float get_bus_volume(Bus bus)
Get a bus volume.
Definition sound.cpp:693
uint32_t _get_session()
Get the number of times the sound module has been shut down.
Definition sound.cpp:460
void set_spatial_mode(SpatialMode mode)
Choose how positional sounds are placed on the speakers.
Definition sound.cpp:674
SoundHandle play_at(const asw::Sample &sample, float x, float volume=1.0F)
Play a sample panned by where it happens on screen. Sounds pan towards the side they are on and fade ...
Definition sound.cpp:631
SoundHandle play_positional(const asw::Sample &sample, const Vec2< float > &position, const PlayOptions &options={})
Play a sample at a point in the world. It is panned, faded and pitch shifted relative to the listener...
Definition sound.cpp:619
void _update()
Update fades, ducking and positional sounds. Called automatically by asw::core::update().
Definition sound.cpp:511
std::shared_ptr< MIX_Audio > Sample
Alias for a shared pointer to an MIX_Audio.
Definition types.h:82
std::shared_ptr< MIX_Audio > Music
Alias for a shared pointer to an MIX_Audio.
Definition types.h:85
Distance settings for positional sounds, in world units (pixels).
Definition sound.h:54
float min_distance
Full volume at this distance or closer.
Definition sound.h:56
float max_distance
Silent at this distance or further.
Definition sound.h:59
Rolloff rolloff
Shape of the fall off between the two.
Definition sound.h:62
Options for playing a sound.
Definition sound.h:67
Bus bus
Bus to play on.
Definition sound.h:88
float fade_in_s
Fade in time in seconds.
Definition sound.h:95
bool loop
Loop forever.
Definition sound.h:75
float pitch
Playback speed and pitch. 1.0 is normal, 2.0 is an octave up.
Definition sound.h:78
float volume_variation
Random volume change per play, as a fraction.
Definition sound.h:85
float pan
Panning (-1.0 left - 1.0 right). Ignored for positional sounds.
Definition sound.h:72
float volume
Playback volume (0.0 - 1.0).
Definition sound.h:69
Attenuation attenuation
Distance fall off, used by positional sounds.
Definition sound.h:98
Types used throughout the ASW library.