Added WarnIfTrue also warns for using some functions with StreamedRawPCM

This commit is contained in:
2025-10-05 15:14:15 +03:00
parent 69fd698da1
commit 020f9c5eef
3 changed files with 48 additions and 8 deletions

View File

@@ -33,6 +33,7 @@ public:
const SoundState GetState(); const SoundState GetState();
const SoundType GetSoundType(); const SoundType GetSoundType();
const uint64_t GetPlayedSampleCount();
const float GetPlaybackTime(); const float GetPlaybackTime();
bool SetPlaybackTime(float time); bool SetPlaybackTime(float time);
@@ -119,8 +120,13 @@ private:
friend class Sound; friend class Sound;
}; };
// For MA
void ThrowOnRuntimeError(std::string message, void ThrowOnRuntimeError(std::string message,
ma_result errorType = ma_result::MA_ERROR); 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 } // namespace ChargeAudio
#endif #endif

View File

@@ -9,7 +9,24 @@ void ChargeAudio::ThrowOnRuntimeError(std::string message,
return; return;
} }
Utility::Error{} << message.c_str() Utility::Error{} << Utility::Debug::color(Utility::Debug::Color::Red)
<< magic_enum::enum_name(errorType).data(); << "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"); 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.";
}
}

View File

@@ -40,12 +40,20 @@ const Sound::SoundState Sound::GetState() { return state; }
const Sound::SoundType Sound::GetSoundType() { return type; } const Sound::SoundType Sound::GetSoundType() { return type; }
const float Sound::GetDuration() { 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; float time;
ma_sound_get_length_in_seconds(&this->maSound, &time); ma_sound_get_length_in_seconds(&this->maSound, &time);
return time; return time;
} }
const float Sound::GetPlaybackTime() { 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; float time;
ma_sound_get_cursor_in_seconds(&this->maSound, &time); ma_sound_get_cursor_in_seconds(&this->maSound, &time);
return time; return time;
@@ -53,6 +61,12 @@ const float Sound::GetPlaybackTime() {
// true or false depending on if the playback was set // true or false depending on if the playback was set
bool Sound::SetPlaybackTime(float time) { 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 // Better to just catch it from the start
if (time < 0) { if (time < 0) {
return false; return false;
@@ -78,13 +92,16 @@ void Sound::Pause() {
} }
void Sound::Reset() { void Sound::Reset() {
if (type == SoundType::StreamedRawPCM) {
ThrowOnRuntimeError( ThrowOnRuntimeError(
"You cannot reset on Streamed RawPCM sounds! Since the buffer is a " "You cannot reset on Streamed RawPCM sounds! Since the buffer is a "
"ring buffer there isn't a \"start\" to return to."); "ring buffer there isn't a \"start\" to return to.",
type == SoundType::StreamedRawPCM);
ma_sound_seek_to_pcm_frame(&maSound, 0);
} }
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) { void Sound::SetPosition(Magnum::Vector3 position) {