Detection for SoundState::Finished

This commit is contained in:
2025-09-05 17:30:12 +03:00
parent 76b53b5f71
commit 64b5a8af29
3 changed files with 17 additions and 5 deletions

View File

@@ -27,7 +27,9 @@ public:
float GetVolume(); float GetVolume();
private: private:
Sound(); Sound(class Engine *engine);
static void onSoundFinish(void *customData, ma_sound *);
class Engine *baseEngine; class Engine *baseEngine;
ma_sound maSound; ma_sound maSound;
SoundState state = SoundState::Idle; SoundState state = SoundState::Idle;
@@ -65,6 +67,7 @@ private:
ma_result maResponse; ma_result maResponse;
ma_uint64 listenerCounter = 0; ma_uint64 listenerCounter = 0;
friend class Listener; friend class Listener;
friend class Sound;
}; };
} // namespace ChargeAudio } // namespace ChargeAudio

View File

@@ -21,17 +21,19 @@ Engine::Engine() {
Engine::~Engine() { ma_engine_uninit(&maEngine); } Engine::~Engine() { ma_engine_uninit(&maEngine); }
Containers::Pointer<Sound> Engine::CreateSound(std::string filepath) { Containers::Pointer<Sound> Engine::CreateSound(std::string filepath) {
auto sound = Containers::Pointer<Sound>(new Sound()); auto sound = Containers::Pointer<Sound>(new Sound(this));
sound->baseEngine = this;
maResponse = ma_sound_init_from_file(&maEngine, filepath.c_str(), 0, NULL, maResponse = ma_sound_init_from_file(&maEngine, filepath.c_str(), 0, NULL,
NULL, &sound->maSound); NULL, &sound->maSound);
sound->maSound.endCallback = Sound::onSoundFinish;
sound->maSound.pEndCallbackUserData = sound.get();
if (maResponse != MA_SUCCESS) { if (maResponse != MA_SUCCESS) {
Utility::Error{} << "Failed to create the sound from the file: " Utility::Error{} << "Failed to create the sound from the file: "
<< filepath.c_str() << " (" << maResponse << ")"; << filepath.c_str() << " (" << maResponse << ")";
throw new std::runtime_error( throw new std::runtime_error(
"Failed to create the sound from file. Check STDERR for more info."); "Failed to create the sound from file. Check STDERR for more info.");
} }
return sound; return sound;
} }

View File

@@ -3,7 +3,8 @@
#include <Magnum/Math/Vector3.h> #include <Magnum/Math/Vector3.h>
using namespace ChargeAudio; using namespace ChargeAudio;
Sound::Sound() {} Sound::Sound(Engine *engine) : baseEngine(engine) {}
Sound::~Sound() { ma_sound_uninit(&maSound); } Sound::~Sound() { ma_sound_uninit(&maSound); }
Sound::SoundState Sound::GetState() { return state; } Sound::SoundState Sound::GetState() { return state; }
@@ -30,3 +31,9 @@ Magnum::Vector3 Sound::GetPosition() {
} }
void Sound::SetVolume(float value) { ma_sound_set_volume(&maSound, value); } void Sound::SetVolume(float value) { ma_sound_set_volume(&maSound, value); }
float Sound::GetVolume() { return ma_sound_get_volume(&maSound); } float Sound::GetVolume() { return ma_sound_get_volume(&maSound); }
// STATICs
void Sound::onSoundFinish(void *customData, ma_sound *) {
auto sound = static_cast<Sound *>(customData);
sound->state = SoundState::Finished;
}