57 const glm::vec3& position)
59 if (!
Init())
return false;
63 s_LastError =
"AudioSource has no clip.";
66 const auto path = ResolveClip(source.
ClipPath);
67 if (!std::filesystem::exists(path))
69 s_LastError =
"Audio clip not found: " + path.string();
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)
79 s_LastError =
"Could not load audio clip: " + path.string();
82 runtime->Initialized =
true;
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));
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)
96 s_LastError =
"Could not start audio clip: " + path.string();
99 s_Sounds[entityHandle] = std::move(runtime);
115 const glm::vec3& position,
const glm::vec3& velocity)
117 auto it = s_Sounds.find(entityHandle);
118 if (it == s_Sounds.end())
return;
119 if (it->second->Path != source.
ClipPath)
121 Play(entityHandle, source, position);
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));
132 ma_sound_set_doppler_factor(&it->second->Sound, std::max(0.0f, source.
DopplerFactor));
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);
142 const glm::vec3& forward,
144 const glm::vec3& velocity)
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);