Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
audio.hpp
Go to the documentation of this file.
1
3#pragma once
4
5#include <aegis2d/audio/export.hpp>
7
8#include <algorithm>
9#include <cmath>
10#include <cstddef>
11#include <cstdint>
12#include <filesystem>
13#include <memory>
14#include <stdexcept>
15#include <utility>
16
17namespace aegis2d::audio {
18
22class AEGIS2D_AUDIO_EXPORT AudioError : public std::runtime_error {
23public:
24 using std::runtime_error::runtime_error;
25};
26
28enum class Channel : std::uint8_t {
30 Effects,
32 Music,
33};
34
36using SoundId = std::size_t;
37
38struct Volumes;
39
54class AEGIS2D_AUDIO_EXPORT AudioDevice {
55public:
57 static constexpr std::size_t max_voices = 32;
58
63
64 AudioDevice(const AudioDevice&) = delete;
67 AudioDevice(AudioDevice&& other) noexcept;
70 AudioDevice& operator=(AudioDevice&& other) noexcept;
73
79 SoundId load(const std::filesystem::path& path);
80
83 [[nodiscard]] std::size_t sounds() const noexcept;
84
92 void play(SoundId sound, Channel channel = Channel::Effects);
93
105 void play(SoundId sound, Channel channel, float pan, float gain_scale = 1.0F);
106
116 void play_music(SoundId sound, bool loop = true);
117
119 void stop_music() noexcept;
120
123 [[nodiscard]] bool music_playing() const noexcept;
124
127 void stop(Channel channel) noexcept;
128
130 void stop_all() noexcept;
131
136 void set_volumes(const Volumes& volumes) noexcept;
137
140 [[nodiscard]] Volumes volumes() const noexcept;
141
143 void update() noexcept;
144
147 [[nodiscard]] std::size_t playing() const noexcept;
148
152 [[nodiscard]] std::size_t playing(Channel channel) const noexcept;
153
154private:
155 struct Impl;
156 std::unique_ptr<Impl> impl_;
157};
158
176[[nodiscard]] inline std::pair<float, float> positional(Vec2 position, Vec2 camera_center,
177 float view_half_width) noexcept {
178 const Vec2 offset = position - camera_center;
179 const float pan = std::clamp(offset.x / view_half_width, -1.0F, 1.0F);
180 const float gain =
181 std::clamp(1.0F - std::hypot(offset.x, offset.y) / view_half_width, 0.0F, 1.0F);
182 return {pan, gain};
183}
184
185} // namespace aegis2d::audio
The default audio output and the sounds it plays.
Definition audio.hpp:54
AudioDevice & operator=(AudioDevice &&other) noexcept
Closes this device and takes over the device of other.
std::size_t sounds() const noexcept
Number of sounds loaded.
AudioDevice()
Opens the default audio output of the system.
AudioDevice & operator=(const AudioDevice &)=delete
AudioDevice(const AudioDevice &)=delete
~AudioDevice()
Stops every sound and closes the device.
AudioDevice(AudioDevice &&other) noexcept
Takes over the device of other, which must not be used afterwards.
SoundId load(const std::filesystem::path &path)
Loads a sound from a WAV file.
Thrown when the audio device cannot be opened or a sound cannot be loaded.
Definition audio.hpp:22
Vector and matrix types of the engine (GLM behind engine names).
Sound effects and music on top of SDL3 (client only).
Definition audio.hpp:17
Channel
Group of sounds whose volume is set together.
Definition audio.hpp:28
@ Music
Background music.
@ Effects
Short sounds of the game: shots, explosions, menus.
std::size_t SoundId
Identifier of a sound loaded by an AudioDevice, given by AudioDevice::load().
Definition audio.hpp:36
glm::vec2 Vec2
2D vector of floats: positions, velocities, sizes (glm::vec2).
Definition math.hpp:19
Definition endpoint.hpp:85
Positions of the three volume sliders of a settings menu, from 0 (silent) to 1.
Definition volume.hpp:18