Added WarnIfTrue also warns for using some functions with StreamedRawPCM
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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.";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user