From 4f886574f8ba6cf5eb02658a6bf04e921ff4d417 Mon Sep 17 00:00:00 2001 From: cat Date: Sun, 21 Sep 2025 18:31:33 +0300 Subject: [PATCH] Added basic error handling --- CMakeLists.txt | 5 +++-- src/ChargeAudio.hpp | 3 +++ src/Engine.cpp | 24 ++++-------------------- src/ErrorHandling.cpp | 12 ++++++++++++ src/Sound.cpp | 18 +++++++++--------- 5 files changed, 31 insertions(+), 31 deletions(-) create mode 100644 src/ErrorHandling.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d98e2f..cfe2a88 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,8 +20,9 @@ pkg_check_modules(AVUTIL REQUIRED libavutil) pkg_check_modules(SWRESAMPLE REQUIRED libswresample) add_library( - ChargeAudio SHARED "src/ChargeAudio.hpp" "src/Engine.cpp" "src/Sound.cpp" - "src/Listener.cpp" "lib/miniaudio/miniaudio.c") + ChargeAudio SHARED + "src/ChargeAudio.hpp" "src/Engine.cpp" "src/Sound.cpp" "src/Listener.cpp" + "src/ErrorHandling.cpp" "lib/miniaudio/miniaudio.c") target_link_libraries( ChargeAudio diff --git a/src/ChargeAudio.hpp b/src/ChargeAudio.hpp index 1dff2d0..7bbb31d 100644 --- a/src/ChargeAudio.hpp +++ b/src/ChargeAudio.hpp @@ -114,5 +114,8 @@ private: friend class Sound; }; +void ThrowOnRuntimeError(std::string message, + ma_result errorType = ma_result::MA_ERROR); + } // namespace ChargeAudio #endif diff --git a/src/Engine.cpp b/src/Engine.cpp index 9e240ec..e3af11e 100644 --- a/src/Engine.cpp +++ b/src/Engine.cpp @@ -1,9 +1,7 @@ #include "ChargeAudio.hpp" #include "miniaudio/miniaudio.h" -#include #include -#include using namespace ChargeAudio; using namespace Corrade; @@ -13,11 +11,8 @@ Engine::Engine(uint32_t sampleRate, uint32_t channels) { maConfig.sampleRate = sampleRate; maConfig.channels = channels; - if ((maResponse = ma_engine_init(&maConfig, &maEngine)) != MA_SUCCESS) { - Utility::Error{} << "Could not init miniaudio (" << maResponse << ")"; - throw new std::runtime_error( - "Failed to init miniaudio engine. Check STDERR for more info."); - } + ThrowOnRuntimeError("Failed to init miniaudio engine", + ma_engine_init(&maConfig, &maEngine)); } Engine::~Engine() { ma_engine_uninit(&maEngine); } @@ -34,12 +29,7 @@ SoundContainer Engine::CreateSound(int bufferLengthInSeconds) { ma_result result = ma_pcm_rb_init( ma_format_s32, channels, sampleRate * channels * length, nullptr, nullptr, &sound->maRingBuffer); - if (result != MA_SUCCESS) { - Utility::Error{} << "Failed to create a new ring buffer!" << " (" - << result << ")"; - throw new std::runtime_error("Failed to create a new ring buffer! " - "Check STDERR for more info."); - } + ThrowOnRuntimeError("Failed to create a new ring buffer!", result); }, Sound::SoundType::StreamedRawPCM, "Failed to create the sound from ring buffer: ")); @@ -56,13 +46,7 @@ SoundContainer Engine::CreateSound(uint8_t *data, int length) { ma_result result = ma_audio_buffer_init_copy(&config, &sound->maAudioBuffer); - if (result != MA_SUCCESS) { - Utility::Error{} << "Failed to create a new audio buffer!" << " (" - << result << ")"; - throw new std::runtime_error("Failed to create a new audio buffer! " - "Check STDERR for more info."); - } - + ThrowOnRuntimeError("Failed to create a new audio buffer!", result); sound->maConfig.pDataSource = &sound->maAudioBuffer; }, Sound::SoundType::RawPCM, diff --git a/src/ErrorHandling.cpp b/src/ErrorHandling.cpp new file mode 100644 index 0000000..3d12d37 --- /dev/null +++ b/src/ErrorHandling.cpp @@ -0,0 +1,12 @@ +#include "ChargeAudio.hpp" +#include + +void ChargeAudio::ThrowOnRuntimeError(std::string message, + ma_result errorType) { + if (errorType == MA_SUCCESS) { + return; + } + + Utility::Error{} << message << " (" << errorType << ")"; + throw new std::runtime_error(message + ". Check STDERR for more info.\n"); +} diff --git a/src/Sound.cpp b/src/Sound.cpp index ca6b682..95c7b24 100644 --- a/src/Sound.cpp +++ b/src/Sound.cpp @@ -1,7 +1,6 @@ #include "ChargeAudio.hpp" #include -#include using namespace ChargeAudio; Sound::Sound(Engine *engine, std::function setupFunction, @@ -13,13 +12,8 @@ Sound::Sound(Engine *engine, std::function setupFunction, setupFunction(this); 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); - } + ThrowOnRuntimeError("Failed to create a new sound. " + additionalErrorMessage, + maResponse); type = soundType; } @@ -78,7 +72,13 @@ void Sound::Pause() { state = Sound::SoundState::Paused; } -void Sound::Reset() { ma_sound_seek_to_pcm_frame(&maSound, 0); } +void Sound::Reset() { + if (type == SoundType::StreamedRawPCM) { + ThrowOnRuntimeError("You cannot reset on Streamed RawPCM sounds!"); + } + + ma_sound_seek_to_pcm_frame(&maSound, 0); +} void Sound::SetPosition(Magnum::Vector3 position) { ma_sound_set_position(&maSound, position.x(), position.y(), position.z());