Compare commits

...

2 Commits

Author SHA1 Message Date
cat
5291b08330 Added magic_enum as a dependency. Tweaked ErrorHandling 2025-09-21 18:55:41 +03:00
cat
4f886574f8 Added basic error handling 2025-09-21 18:31:33 +03:00
7 changed files with 46 additions and 33 deletions

3
.gitmodules vendored
View File

@@ -1,3 +1,6 @@
[submodule "lib/miniaudio"]
path = lib/miniaudio
url = https://github.com/mackron/miniaudio
[submodule "lib/magic_enum"]
path = lib/magic_enum
url = https://github.com/Neargye/magic_enum

View File

@@ -9,7 +9,9 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-subobject-linkage")
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=address")
endif()
add_subdirectory(lib/miniaudio)
add_subdirectory(lib/magic_enum)
find_package(Corrade REQUIRED Main)
find_package(Magnum REQUIRED)
@@ -20,8 +22,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

1
lib/magic_enum Submodule

Submodule lib/magic_enum added at ec5734e491

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,

15
src/ErrorHandling.cpp Normal file
View File

@@ -0,0 +1,15 @@
#include "ChargeAudio.hpp"
#include "magic_enum/include/magic_enum/magic_enum.hpp"
#include <Corrade/Utility/Debug.h>
#include <stdexcept>
void ChargeAudio::ThrowOnRuntimeError(std::string message,
ma_result errorType) {
if (errorType == MA_SUCCESS) {
return;
}
Utility::Error{} << message.c_str()
<< magic_enum::enum_name(errorType).data();
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,
@@ -10,16 +9,13 @@ Sound::Sound(Engine *engine, std::function<void(Sound *)> setupFunction,
maConfig = ma_sound_config_init_2(&baseEngine->maEngine);
maConfig.endCallback = Sound::onSoundFinish;
maConfig.pEndCallbackUserData = this;
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;
}
@@ -69,16 +65,24 @@ bool Sound::SetPlaybackTime(float time) {
// Controls
void Sound::Play() {
ma_sound_start(&maSound);
ThrowOnRuntimeError("Failed to start the sound.", ma_sound_start(&maSound));
state = Sound::SoundState::Playing;
}
void Sound::Pause() {
ma_sound_stop(&maSound);
ThrowOnRuntimeError("Failed to pause the sound.", ma_sound_stop(&maSound));
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! Since the buffer is a "
"ring buffer there isn't a \"start\" to return to.");
}
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());