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)
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

View File

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

View File

@@ -1,9 +1,7 @@
#include "ChargeAudio.hpp"
#include "miniaudio/miniaudio.h"
#include <Corrade/Utility/Debug.h>
#include <cstdint>
#include <stdexcept>
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,

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 <functional>
#include <stdexcept>
using namespace ChargeAudio;
Sound::Sound(Engine *engine, std::function<void(Sound *)> setupFunction,
@@ -13,13 +12,8 @@ Sound::Sound(Engine *engine, std::function<void(Sound *)> 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());