From 146bd49b36835fb5fd1fa8168353f4e3d5bebc2d Mon Sep 17 00:00:00 2001 From: cat Date: Sun, 7 Sep 2025 22:39:17 +0300 Subject: [PATCH] Changing initing to be more standardised(needs to be refactored) --- src/ChargeAudio.hpp | 3 +++ src/Engine.cpp | 19 ++++++------------- src/Sound.cpp | 19 ++++++++++++++++++- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/ChargeAudio.hpp b/src/ChargeAudio.hpp index 1cd5938..dc3e23a 100644 --- a/src/ChargeAudio.hpp +++ b/src/ChargeAudio.hpp @@ -28,11 +28,14 @@ public: private: Sound(class Engine *engine); + void init(std::string additionalErrorMessage = ""); static void onSoundFinish(void *customData, ma_sound *); class Engine *baseEngine; ma_sound maSound; + ma_sound_config maConfig; SoundState state = SoundState::Idle; + friend class Engine; }; diff --git a/src/Engine.cpp b/src/Engine.cpp index e373d28..0477456 100644 --- a/src/Engine.cpp +++ b/src/Engine.cpp @@ -20,20 +20,13 @@ Engine::Engine() { Engine::~Engine() { ma_engine_uninit(&maEngine); } -Containers::Pointer Engine::CreateSound(std::string filepath) { +Containers::Pointer Engine::CreateSound(const std::string filepath) { auto sound = Containers::Pointer(new Sound(this)); - maResponse = ma_sound_init_from_file(&maEngine, filepath.c_str(), 0, NULL, - NULL, &sound->maSound); - sound->maSound.endCallback = Sound::onSoundFinish; - sound->maSound.pEndCallbackUserData = sound.get(); - - if (maResponse != MA_SUCCESS) { - Utility::Error{} << "Failed to create the sound from the file: " - << filepath.c_str() << " (" << maResponse << ")"; - throw new std::runtime_error( - "Failed to create the sound from file. Check STDERR for more info."); - } - + sound->maConfig.pFilePath = filepath.c_str(); + sound->maConfig.flags = 0; + sound->maConfig.pInitialAttachment = NULL; + sound->maConfig.pDoneFence = NULL; + sound->init("Failed to create the sound from the file: " + filepath); return sound; } diff --git a/src/Sound.cpp b/src/Sound.cpp index 9b118a9..b7ad0bd 100644 --- a/src/Sound.cpp +++ b/src/Sound.cpp @@ -1,14 +1,31 @@ #include "ChargeAudio.hpp" #include #include +#include using namespace ChargeAudio; -Sound::Sound(Engine *engine) : baseEngine(engine) {} +Sound::Sound(Engine *engine) : baseEngine(engine) { + maConfig = ma_sound_config_init_2(&baseEngine->maEngine); + maConfig.endCallback = Sound::onSoundFinish; + maConfig.pEndCallbackUserData = this; +} Sound::~Sound() { ma_sound_uninit(&maSound); } Sound::SoundState Sound::GetState() { return state; } +void Sound::init(std::string additionalErrorMessage) { + ma_result maResponse = + ma_sound_init_ex(&baseEngine->maEngine, &maConfig, &maSound); + if (maResponse != MA_SUCCESS) { + Utility::Error{} << "Failed to create a new sound" << " (" << maResponse + << ")"; + throw new std::runtime_error( + "Failed to create a new sound. Check STDERR for more info.\n" + + additionalErrorMessage); + } +} + // Controls void Sound::Play() { ma_sound_start(&maSound);