vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
audio_system.h
1#pragma once
2
3#include <cstdint>
4#include <string>
5#include <unordered_map>
6#include <unordered_set>
7#include <vector>
8
9#include "core/event/bus.h"
10#include "core/system.h"
11#include "ecs/entity.h"
12#include "resource/asset/audio_clip_asset.h"
13#include "system/audio/audio_device.h"
14#include "system/audio/audio_events.h"
15
16namespace Vkm::Engine {
17
18class EventBus;
19struct AudioSource;
20
29class AudioSystem : public System {
30 public:
31 AudioSystem() = default;
32 ~AudioSystem() override = default;
33
34 AudioSystem(const AudioSystem& other) = delete;
35 AudioSystem& operator=(const AudioSystem& other) = delete;
36
37 AudioSystem(AudioSystem && other) = delete;
38 AudioSystem& operator=(AudioSystem && other) = delete;
39
40 public:
41 void init(FrameContext& ctx) override;
42 void update(FrameContext& ctx) override;
43 void shutdown() override;
44
50 AudioDevice& device() { return m_device; }
51
57 void setSilent(bool silent = true) { m_silent = silent; }
58
69 VoiceId voiceOf(EntityId entity) const;
70
79
80 private:
88 void updateListener(FrameContext& ctx);
89
99 void reconcileSource(
100 FrameContext& ctx,
101 EntityId entity,
102 AudioSource& source,
103 const glm::vec3& worldPosition,
104 bool simRunning
105 );
106
116 void startPendingRequests(FrameContext& ctx);
117
126 void warnIfNoListener(bool spatial);
127
137 void warnIfStereoSpatial(const AudioClipAsset& asset, bool spatial);
138
139 private:
146 struct ActiveVoice {
147 EntityId entity;
148 AudioClipHandle clip;
149 VoiceId voice = 0;
150 uint64_t seenOnFrame = 0;
151 };
152
153 private:
154 AudioDevice m_device;
155 bool m_silent = false;
156
158 std::unordered_map<uint32_t, ActiveVoice> m_voices;
159
161 std::vector<PlaySoundEvent> m_pending;
162
164 std::vector<PlaySoundEvent> m_starting;
165
167 EventBus* m_events = nullptr;
168
170 ListenerId m_playListener = 0;
171
173 uint64_t m_assetEpoch = 0;
174
176 uint64_t m_frame = 0;
177
179 bool m_hasListener = false;
181 glm::vec3 m_listenerPosition{0.0f};
182
184 bool m_warnedNoListener = false;
185
187 std::unordered_set<std::string> m_warnedStereoClips;
188};
189
190} // namespace Vkm::Engine
The engine's whole surface onto the audio backend.
Definition audio_device.h:80
VoiceId voiceOf(EntityId entity) const
The voice entity's AudioSource is being heard through.
void init(FrameContext &ctx) override
Called once after all systems are registered, before the first update.
void shutdown() override
Called once on engine shutdown.
AudioDevice & device()
The device, for a tool (auditioning, a transport pause), not a game.
Definition audio_system.h:50
void setSilent(bool silent=true)
Run without opening a device, for a host that plays to nobody; set before init().
Definition audio_system.h:57
void update(FrameContext &ctx) override
Execute this system for the current frame.
void stopEverything()
Stop every voice and forget them.
Typed pub/sub event dispatcher.
Definition event_bus.h:22
A sound: fully decoded interleaved PCM, plus the rate and layout it was written at.
Definition audio_clip_asset.h:58
A sound emitter: one clip, how loud, and where it is heard from.
Definition audio_source.h:17
Names one entity of one Scene, for as long as that slot holds it.
Definition entity.h:23
Per-frame state bundle passed to every system.
Definition system.h:55