Added basic error handling

This commit is contained in:
2025-09-21 18:31:33 +03:00
parent 64a5d19464
commit 4f886574f8
5 changed files with 31 additions and 31 deletions

View File

@@ -20,8 +20,9 @@ pkg_check_modules(AVUTIL REQUIRED libavutil)
pkg_check_modules(SWRESAMPLE REQUIRED libswresample) pkg_check_modules(SWRESAMPLE REQUIRED libswresample)
add_library( add_library(
ChargeAudio SHARED "src/ChargeAudio.hpp" "src/Engine.cpp" "src/Sound.cpp" ChargeAudio SHARED
"src/Listener.cpp" "lib/miniaudio/miniaudio.c") "src/ChargeAudio.hpp" "src/Engine.cpp" "src/Sound.cpp" "src/Listener.cpp"
"src/ErrorHandling.cpp" "lib/miniaudio/miniaudio.c")
target_link_libraries( target_link_libraries(
ChargeAudio ChargeAudio

View File

@@ -114,5 +114,8 @@ private:
friend class Sound; friend class Sound;
}; };
void ThrowOnRuntimeError(std::string message,
ma_result errorType = ma_result::MA_ERROR);
} // namespace ChargeAudio } // namespace ChargeAudio
#endif #endif

View File

@@ -1,9 +1,7 @@
#include "ChargeAudio.hpp" #include "ChargeAudio.hpp"
#include "miniaudio/miniaudio.h" #include "miniaudio/miniaudio.h"
#include <Corrade/Utility/Debug.h>
#include <cstdint> #include <cstdint>
#include <stdexcept>
using namespace ChargeAudio; using namespace ChargeAudio;
using namespace Corrade; using namespace Corrade;
@@ -13,11 +11,8 @@ Engine::Engine(uint32_t sampleRate, uint32_t channels) {
maConfig.sampleRate = sampleRate; maConfig.sampleRate = sampleRate;
maConfig.channels = channels; maConfig.channels = channels;
if ((maResponse = ma_engine_init(&maConfig, &maEngine)) != MA_SUCCESS) { ThrowOnRuntimeError("Failed to init miniaudio engine",
Utility::Error{} << "Could not init miniaudio (" << maResponse << ")"; ma_engine_init(&maConfig, &maEngine));
throw new std::runtime_error(
"Failed to init miniaudio engine. Check STDERR for more info.");
}
} }
Engine::~Engine() { ma_engine_uninit(&maEngine); } Engine::~Engine() { ma_engine_uninit(&maEngine); }
@@ -34,12 +29,7 @@ SoundContainer Engine::CreateSound(int bufferLengthInSeconds) {
ma_result result = ma_pcm_rb_init( ma_result result = ma_pcm_rb_init(
ma_format_s32, channels, sampleRate * channels * length, nullptr, ma_format_s32, channels, sampleRate * channels * length, nullptr,
nullptr, &sound->maRingBuffer); nullptr, &sound->maRingBuffer);
if (result != MA_SUCCESS) { ThrowOnRuntimeError("Failed to create a new ring buffer!", result);
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.");
}
}, },
Sound::SoundType::StreamedRawPCM, Sound::SoundType::StreamedRawPCM,
"Failed to create the sound from ring buffer: ")); "Failed to create the sound from ring buffer: "));
@@ -56,13 +46,7 @@ SoundContainer Engine::CreateSound(uint8_t *data, int length) {
ma_result result = ma_result result =
ma_audio_buffer_init_copy(&config, &sound->maAudioBuffer); ma_audio_buffer_init_copy(&config, &sound->maAudioBuffer);
if (result != MA_SUCCESS) { ThrowOnRuntimeError("Failed to create a new audio buffer!", result);
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.");
}
sound->maConfig.pDataSource = &sound->maAudioBuffer; sound->maConfig.pDataSource = &sound->maAudioBuffer;
}, },
Sound::SoundType::RawPCM, Sound::SoundType::RawPCM,

12
src/ErrorHandling.cpp Normal file
View File

@@ -0,0 +1,12 @@
#include "ChargeAudio.hpp"
#include <stdexcept>
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");
}

View File

@@ -1,7 +1,6 @@
#include "ChargeAudio.hpp" #include "ChargeAudio.hpp"
#include <functional> #include <functional>
#include <stdexcept>
using namespace ChargeAudio; using namespace ChargeAudio;
Sound::Sound(Engine *engine, std::function<void(Sound *)> setupFunction, Sound::Sound(Engine *engine, std::function<void(Sound *)> setupFunction,
@@ -13,13 +12,8 @@ Sound::Sound(Engine *engine, std::function<void(Sound *)> setupFunction,
setupFunction(this); setupFunction(this);
ma_result maResponse = ma_result maResponse =
ma_sound_init_ex(&baseEngine->maEngine, &maConfig, &maSound); ma_sound_init_ex(&baseEngine->maEngine, &maConfig, &maSound);
if (maResponse != MA_SUCCESS) { ThrowOnRuntimeError("Failed to create a new sound. " + additionalErrorMessage,
Utility::Error{} << "Failed to create a new sound" << " (" << maResponse maResponse);
<< ")";
throw new std::runtime_error(
"Failed to create a new sound. Check STDERR for more info.\n" +
additionalErrorMessage);
}
type = soundType; type = soundType;
} }
@@ -78,7 +72,13 @@ void Sound::Pause() {
state = Sound::SoundState::Paused; 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) { void Sound::SetPosition(Magnum::Vector3 position) {
ma_sound_set_position(&maSound, position.x(), position.y(), position.z()); ma_sound_set_position(&maSound, position.x(), position.y(), position.z());