vkmEngine 1.0.15
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
audio_device.h
1#pragma once
2
3#include <algorithm>
4#include <cmath>
5#include <cstdint>
6#include <memory>
7
8#include <glm/glm.hpp>
9
10#include "ecs/component/audio/audio_source.h"
11
12namespace Vkm::Engine {
13
17using VoiceId = uint32_t;
18
24inline constexpr size_t MAX_ACTIVE_VOICES = 128;
25
28inline constexpr size_t MAX_HELD_VOICES = 2 * MAX_ACTIVE_VOICES;
29
42 float volume = 1.0f;
43 float pitch = 1.0f;
44 bool loop = false;
45 bool spatial = true;
46 glm::vec3 position = {0.0f, 0.0f, 0.0f};
47 float minDistance = 1.0f;
48 float maxDistance = 50.0f;
49 AudioRolloff rolloff = AudioRolloff::Linear;
51 int priority = 0;
52
58 float heardVolume() const { return std::isfinite(volume) ? std::max(0.0f, volume) : 0.0f; }
59
67 float heardMinDistance() const { return std::isfinite(minDistance) ? std::max(0.0f, minDistance) : 0.0f; }
68
74 float heardMaxDistance() const { return std::isfinite(maxDistance) ? std::max(0.0f, maxDistance) : 0.0f; }
75
81 bool hasFar() const { return heardMaxDistance() > heardMinDistance(); }
82};
83
88inline constexpr float INVERSE_ROLLOFF_MIN_DISTANCE = 0.01f;
89
98inline float inverseRolloffGain(const VoiceParams& params, float distance) {
99 if (!params.hasFar()) return 1.0f;
100 const float lo = std::max(params.heardMinDistance(), INVERSE_ROLLOFF_MIN_DISTANCE);
101 const float hi = params.heardMaxDistance();
102 if (!(distance > lo)) return 1.0f;
103 if (distance >= hi) return 0.0f;
104 const float floor = lo / hi;
105 return (lo / distance - floor) / (1.0f - floor);
106}
107
117class AudioDevice {
118 public:
119 AudioDevice();
120 ~AudioDevice();
121
122 AudioDevice(const AudioDevice& other) = delete;
123 AudioDevice& operator=(const AudioDevice& other) = delete;
124
125 AudioDevice(AudioDevice && other) = delete;
126 AudioDevice& operator=(AudioDevice && other) = delete;
127
128 public:
136 bool open();
137
147 bool openOffline(uint32_t sampleRate, uint32_t channels);
148
152 void close();
153
164 uint64_t render(float* frames, uint64_t frameCount);
165
178 VoiceId play(const AudioClipAsset& clip, const VoiceParams& params);
179
186 void updateVoice(VoiceId voice, const VoiceParams& params);
187
197 bool isVoiceActive(VoiceId voice) const;
198
207 void pauseVoice(VoiceId voice);
208
216 void resumeVoice(VoiceId voice);
217
224 bool isVoicePaused(VoiceId voice) const;
225
236 float voiceCursor(VoiceId voice) const;
237
248 void seekVoice(VoiceId voice, float seconds);
249
258 void stopVoice(VoiceId voice);
259
267
275
283
290
296 size_t voiceCount() const;
297
306 void setListener(const glm::vec3& position, const glm::vec3& forward, const glm::vec3& up);
307
315 void setListenerActive(bool active);
316
322 void setMasterVolume(float volume);
323
324 public:
325 bool isOpen() const noexcept { return m_backend != nullptr; }
326
332 float masterVolume() const noexcept { return m_masterVolume; }
333
334 private:
335 // ma_engine, the voice table and each voice's sound; held by pointer so
336 // miniaudio's header stays out of this one.
337 struct Backend;
338
339 private:
346 void reopenLostDevice();
347
348 private:
349 std::unique_ptr<Backend> m_backend;
350 float m_masterVolume = 1.0f;
351
353 bool m_voiceBudgetSpent = false;
354};
355
356} // namespace Vkm::Engine
uint64_t render(float *frames, uint64_t frameCount)
Pull mixed frames from an offline mixer into frames.
void updateVoice(VoiceId voice, const VoiceParams &params)
Push a voice's parameters to the mixer; an unknown or finished voice is ignored.
size_t voiceCount() const
Number of voices the mixer currently holds.
void setListener(const glm::vec3 &position, const glm::vec3 &forward, const glm::vec3 &up)
Place the ear.
void setListenerActive(bool active)
Whether anything is listening.
void setMasterVolume(float volume)
Master gain applied to the whole mix.
void seekVoice(VoiceId voice, float seconds)
Move voice's cursor to seconds into its clip.
void reapFinishedVoices()
Release every voice that has played to its end, or been ramped out by stopVoice().
void resumeAllVoices()
Let every voice pauseAllVoices() is still holding play on.
void pauseVoice(VoiceId voice)
Hold voice at its cursor, silently, without ending it.
void pauseAllVoices()
Hold every voice that is sounding right now.
bool openOffline(uint32_t sampleRate, uint32_t channels)
Open the mixer with no device, to be pumped by render().
float masterVolume() const noexcept
The master gain the mix is running at, after setMasterVolume's sanitising.
Definition audio_device.h:332
void stopAllVoices()
Cut and release every voice at once, leaving the device open.
VoiceId play(const AudioClipAsset &clip, const VoiceParams &params)
Start playing clip and return the voice driving it.
bool isVoiceActive(VoiceId voice) const
Whether voice is still there with audio left to play.
bool open()
Open the default playback device and start its mixer thread.
void close()
Stop every voice and shut the mixer down; idempotent.
bool isVoicePaused(VoiceId voice) const
Whether voice is being held by a pause rather than playing.
float voiceCursor(VoiceId voice) const
How far into its clip voice has played, in seconds.
void resumeVoice(VoiceId voice)
Let voice play on from where it was held, ramped back in.
void stopVoice(VoiceId voice)
Ramp voice to silence and let it go.
A sound: fully decoded interleaved PCM, plus the rate and layout it was written at.
Definition audio_clip_asset.h:58
Everything the mixer needs to know about one voice, pushed every frame.
Definition audio_device.h:35
float heardVolume() const
The gain as the mixer takes it.
Definition audio_device.h:58
int priority
Rank when the mixer is full; see AudioDevice::play.
Definition audio_device.h:51
float heardMinDistance() const
The near distance as the mixer takes it.
Definition audio_device.h:67
bool hasFar() const
Whether the voice fades with distance and so has a range past which it is silent.
Definition audio_device.h:81
float heardMaxDistance() const
The far distance as the mixer takes it.
Definition audio_device.h:74
float volume
Linear gain for this voice, multiplied into the master.
Definition audio_device.h:42