From 020f9c5eef9e983441f87f3c6a2efef276c1facb Mon Sep 17 00:00:00 2001 From: cat Date: Sun, 5 Oct 2025 15:14:15 +0300 Subject: [PATCH] Added WarnIfTrue also warns for using some functions with StreamedRawPCM --- src/ChargeAudio.hpp | 6 ++++++ src/ErrorHandling.cpp | 21 +++++++++++++++++++-- src/Sound.cpp | 29 +++++++++++++++++++++++------ 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/src/ChargeAudio.hpp b/src/ChargeAudio.hpp index a67c368..c77a45d 100644 --- a/src/ChargeAudio.hpp +++ b/src/ChargeAudio.hpp @@ -33,6 +33,7 @@ public: const SoundState GetState(); const SoundType GetSoundType(); + const uint64_t GetPlayedSampleCount(); const float GetPlaybackTime(); bool SetPlaybackTime(float time); @@ -119,8 +120,13 @@ private: friend class Sound; }; +// For MA void ThrowOnRuntimeError(std::string message, ma_result errorType = ma_result::MA_ERROR); +// Generalised +void ThrowOnRuntimeError(std::string message, bool comparison); +void WarnIfTrue(std::string message, bool comparison = true); + } // namespace ChargeAudio #endif diff --git a/src/ErrorHandling.cpp b/src/ErrorHandling.cpp index 88baf42..f2ffc58 100644 --- a/src/ErrorHandling.cpp +++ b/src/ErrorHandling.cpp @@ -9,7 +9,24 @@ void ChargeAudio::ThrowOnRuntimeError(std::string message, return; } - Utility::Error{} << message.c_str() - << magic_enum::enum_name(errorType).data(); + Utility::Error{} << Utility::Debug::color(Utility::Debug::Color::Red) + << "ERROR:" << Utility::Debug::resetColor << message.c_str() + << magic_enum::enum_name(errorType).data() + << "Execution will be stopped!\n"; throw new std::runtime_error(message + " Check STDERR for more info.\n"); } + +void ChargeAudio::ThrowOnRuntimeError(std::string message, bool comparison) { + if (!comparison) { + return; + } + ThrowOnRuntimeError(message); +} + +void ChargeAudio::WarnIfTrue(std::string message, bool comparison) { + if (comparison) { + Utility::Debug{} << Utility::Debug::color(Utility::Debug::Color::Yellow) + << "WARNING:" << Utility::Debug::resetColor + << message.c_str() << "Execution will continue."; + } +} diff --git a/src/Sound.cpp b/src/Sound.cpp index fb407dc..ddff644 100644 --- a/src/Sound.cpp +++ b/src/Sound.cpp @@ -40,12 +40,20 @@ const Sound::SoundState Sound::GetState() { return state; } const Sound::SoundType Sound::GetSoundType() { return type; } const float Sound::GetDuration() { + WarnIfTrue("You are using StreamedRawPCM sound! GetDuration() will not work " + "since the PCM data is in a fixed ring buffer.", + type == Sound::SoundType::StreamedRawPCM); float time; ma_sound_get_length_in_seconds(&this->maSound, &time); return time; } const float Sound::GetPlaybackTime() { + WarnIfTrue( + "You are using StreamedRawPCM sound! GetPlaybackTime() will not work " + "since the PCM data is in a fixed ring buffer. However you can do " + "GetPlayedSampleCount()/SAMPLE_RATE to get time elapsed.", + type == Sound::SoundType::StreamedRawPCM); float time; ma_sound_get_cursor_in_seconds(&this->maSound, &time); return time; @@ -53,6 +61,12 @@ const float Sound::GetPlaybackTime() { // true or false depending on if the playback was set bool Sound::SetPlaybackTime(float time) { + ThrowOnRuntimeError( + "You cannot set playback time on a StreamedRawPCM as it uses a ring " + "buffer. If you wanted to skip forward or backward. Empty the buffer and " + "then write what you want.", + type == Sound::SoundType::StreamedRawPCM); + // Better to just catch it from the start if (time < 0) { return false; @@ -78,15 +92,18 @@ void Sound::Pause() { } 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."); - } - + ThrowOnRuntimeError( + "You cannot reset on Streamed RawPCM sounds! Since the buffer is a " + "ring buffer there isn't a \"start\" to return to.", + type == SoundType::StreamedRawPCM); ma_sound_seek_to_pcm_frame(&maSound, 0); } +// Used with Streamed PCM but works with anything +const uint64_t Sound::GetPlayedSampleCount() { + return ma_sound_get_time_in_pcm_frames(&maSound); +} + void Sound::SetPosition(Magnum::Vector3 position) { ma_sound_set_position(&maSound, position.x(), position.y(), position.z()); }