NoJobEngine 2.0.7
Loading...
Searching...
No Matches
AudioEngine.cpp
Go to the documentation of this file.
4#include <miniaudio.h>
5#include <algorithm>
6#include <filesystem>
7#include <memory>
8#include <unordered_map>
9
10namespace NoJob
11{
12 namespace
13 {
14 struct RuntimeSound
15 {
16 ma_sound Sound{};
17 bool Initialized = false;
18 std::string Path;
19 ~RuntimeSound() { if (Initialized) ma_sound_uninit(&Sound); }
20 };
21
22 ma_engine s_Engine{};
23 bool s_Initialized = false;
24 std::unordered_map<std::uint32_t, std::unique_ptr<RuntimeSound>> s_Sounds;
25 std::string s_LastError;
26
27 std::filesystem::path ResolveClip(const std::string& clip)
28 {
29 return AssetManager::ResolveProjectPath(std::filesystem::path(clip));
30 }
31 }
32
34 {
35 if (s_Initialized) return true;
36 const ma_result result = ma_engine_init(nullptr, &s_Engine);
37 if (result != MA_SUCCESS)
38 {
39 s_LastError = "miniaudio engine initialization failed (" + std::to_string(result) + ")";
40 return false;
41 }
42 s_Initialized = true;
43 s_LastError.clear();
44 return true;
45 }
46
48 {
49 StopAll();
50 if (s_Initialized) ma_engine_uninit(&s_Engine);
51 s_Initialized = false;
52 }
53
54 bool AudioEngine::IsInitialized() { return s_Initialized; }
55
56 bool AudioEngine::Play(std::uint32_t entityHandle, const AudioSourceComponent& source,
57 const glm::vec3& position)
58 {
59 if (!Init()) return false;
60 Stop(entityHandle);
61 if (source.ClipPath.empty())
62 {
63 s_LastError = "AudioSource has no clip.";
64 return false;
65 }
66 const auto path = ResolveClip(source.ClipPath);
67 if (!std::filesystem::exists(path))
68 {
69 s_LastError = "Audio clip not found: " + path.string();
70 return false;
71 }
72
73 auto runtime = std::make_unique<RuntimeSound>();
74 const ma_result result = ma_sound_init_from_file(
75 &s_Engine, path.string().c_str(), 0,
76 nullptr, nullptr, &runtime->Sound);
77 if (result != MA_SUCCESS)
78 {
79 s_LastError = "Could not load audio clip: " + path.string();
80 return false;
81 }
82 runtime->Initialized = true;
83 runtime->Path = source.ClipPath;
84 ma_sound_set_volume(&runtime->Sound, std::clamp(source.Volume, 0.0f, 4.0f));
85 ma_sound_set_pitch(&runtime->Sound, std::clamp(source.Pitch, 0.01f, 4.0f));
86 ma_sound_set_looping(&runtime->Sound, source.Loop ? MA_TRUE : MA_FALSE);
87 ma_sound_set_position(&runtime->Sound, position.x, position.y, position.z);
88 ma_sound_set_spatialization_enabled(&runtime->Sound, source.SpatialBlend > 0.001f ? MA_TRUE : MA_FALSE);
89 ma_sound_set_min_distance(&runtime->Sound, std::max(0.01f, source.MinDistance));
90 ma_sound_set_max_distance(&runtime->Sound, std::max(source.MinDistance + 0.01f, source.MaxDistance));
91 ma_sound_set_doppler_factor(&runtime->Sound, std::max(0.0f, source.DopplerFactor));
92 ma_sound_set_pan(&runtime->Sound, 0.0f);
93 ma_sound_set_pan_mode(&runtime->Sound, ma_pan_mode_balance);
94 if (ma_sound_start(&runtime->Sound) != MA_SUCCESS)
95 {
96 s_LastError = "Could not start audio clip: " + path.string();
97 return false;
98 }
99 s_Sounds[entityHandle] = std::move(runtime);
100 s_LastError.clear();
101 return true;
102 }
103
104 void AudioEngine::Stop(std::uint32_t entityHandle)
105 {
106 auto it = s_Sounds.find(entityHandle);
107 if (it == s_Sounds.end()) return;
108 if (it->second->Initialized) ma_sound_stop(&it->second->Sound);
109 s_Sounds.erase(it);
110 }
111
112 void AudioEngine::StopAll() { s_Sounds.clear(); }
113
114 void AudioEngine::Update(std::uint32_t entityHandle, const AudioSourceComponent& source,
115 const glm::vec3& position, const glm::vec3& velocity)
116 {
117 auto it = s_Sounds.find(entityHandle);
118 if (it == s_Sounds.end()) return;
119 if (it->second->Path != source.ClipPath)
120 {
121 Play(entityHandle, source, position);
122 return;
123 }
124 ma_sound_set_volume(&it->second->Sound, std::clamp(source.Volume, 0.0f, 4.0f));
125 ma_sound_set_pitch(&it->second->Sound, std::clamp(source.Pitch, 0.01f, 4.0f));
126 ma_sound_set_looping(&it->second->Sound, source.Loop ? MA_TRUE : MA_FALSE);
127 ma_sound_set_position(&it->second->Sound, position.x, position.y, position.z);
128 ma_sound_set_velocity(&it->second->Sound, velocity.x, velocity.y, velocity.z);
129 ma_sound_set_spatialization_enabled(&it->second->Sound, source.SpatialBlend > 0.001f ? MA_TRUE : MA_FALSE);
130 ma_sound_set_min_distance(&it->second->Sound, std::max(0.01f, source.MinDistance));
131 ma_sound_set_max_distance(&it->second->Sound, std::max(source.MinDistance + 0.01f, source.MaxDistance));
132 ma_sound_set_doppler_factor(&it->second->Sound, std::max(0.0f, source.DopplerFactor));
133 // miniaudio exposes spatialization as an enable/disable switch. Blend is
134 // represented by smoothly reducing the 3D contribution while preserving
135 // the source's user volume.
136 const float spatialGain = std::clamp(source.SpatialBlend, 0.0f, 1.0f);
137 if (spatialGain > 0.001f && spatialGain < 0.999f)
138 ma_sound_set_positioning(&it->second->Sound, ma_positioning_absolute);
139 }
140
141 void AudioEngine::SetListener(const glm::vec3& position,
142 const glm::vec3& forward,
143 const glm::vec3& up,
144 const glm::vec3& velocity)
145 {
146 if (!Init()) return;
147 ma_engine_listener_set_position(&s_Engine, 0, position.x, position.y, position.z);
148 ma_engine_listener_set_direction(&s_Engine, 0, forward.x, forward.y, forward.z);
149 ma_engine_listener_set_world_up(&s_Engine, 0, up.x, up.y, up.z);
150 ma_engine_listener_set_velocity(&s_Engine, 0, velocity.x, velocity.y, velocity.z);
151 }
152
153 bool AudioEngine::IsPlaying(std::uint32_t entityHandle)
154 {
155 auto it = s_Sounds.find(entityHandle);
156 return it != s_Sounds.end() && it->second->Initialized &&
157 ma_sound_is_playing(&it->second->Sound) == MA_TRUE;
158 }
159
160 const std::string& AudioEngine::GetLastError() { return s_LastError; }
161}
AssetManager declarations for the Asset subsystem.
AudioEngine declarations for the Audio subsystem.
Components declarations for the Scenes and Native Scripting subsystem.
static std::filesystem::path ResolveProjectPath(const std::filesystem::path &path)
static bool IsPlaying(std::uint32_t entityHandle)
static bool Init()
static bool Play(std::uint32_t entityHandle, const AudioSourceComponent &source, const glm::vec3 &position=glm::vec3(0.0f))
static void Stop(std::uint32_t entityHandle)
static void StopAll()
static void SetListener(const glm::vec3 &position, const glm::vec3 &forward, const glm::vec3 &up, const glm::vec3 &velocity=glm::vec3(0.0f))
static const std::string & GetLastError()
static void Shutdown()
static bool IsInitialized()
static void Update(std::uint32_t entityHandle, const AudioSourceComponent &source, const glm::vec3 &position=glm::vec3(0.0f), const glm::vec3 &velocity=glm::vec3(0.0f))