mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
fix(audio-source): improve state, volume, and const correctness
This commit is contained in:
@@ -20,7 +20,7 @@ public:
|
|||||||
float duration() const;
|
float duration() const;
|
||||||
float current_time() const;
|
float current_time() const;
|
||||||
float volume() const;
|
float volume() const;
|
||||||
AudioState state();
|
AudioState state() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ALuint m_source = 0;
|
ALuint m_source = 0;
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
#include "Cubed/audio/audio_source.hpp"
|
#include "Cubed/audio/audio_source.hpp"
|
||||||
|
|
||||||
#include "Cubed/tools/log.hpp"
|
#include <algorithm>
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
namespace Cubed {
|
namespace Cubed {
|
||||||
@@ -14,7 +13,7 @@ AudioSource::~AudioSource() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void AudioSource::set_buffer_2d(const AudioBuffer& buffer) {
|
void AudioSource::set_buffer_2d(const AudioBuffer& buffer) {
|
||||||
if (state() == AudioState::PLAYING) {
|
if (state() != AudioState::STOPPED && state() != AudioState::INITIAL) {
|
||||||
stop();
|
stop();
|
||||||
}
|
}
|
||||||
m_duration = buffer.duration();
|
m_duration = buffer.duration();
|
||||||
@@ -32,10 +31,7 @@ void AudioSource::set_loop(bool on) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void AudioSource::set_volume(float volume) {
|
void AudioSource::set_volume(float volume) {
|
||||||
if (volume > 1.0f) {
|
volume = std::clamp(volume, 0.0f, 1.0f);
|
||||||
Logger::error("Volume {} is too large", volume);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
m_volume = volume;
|
m_volume = volume;
|
||||||
alSourcef(m_source, AL_GAIN, volume);
|
alSourcef(m_source, AL_GAIN, volume);
|
||||||
}
|
}
|
||||||
@@ -53,7 +49,7 @@ float AudioSource::current_time() const {
|
|||||||
}
|
}
|
||||||
float AudioSource::volume() const { return m_volume; }
|
float AudioSource::volume() const { return m_volume; }
|
||||||
|
|
||||||
AudioState AudioSource::state() {
|
AudioState AudioSource::state() const {
|
||||||
ALint state;
|
ALint state;
|
||||||
alGetSourcei(m_source, AL_SOURCE_STATE, &state);
|
alGetSourcei(m_source, AL_SOURCE_STATE, &state);
|
||||||
switch (state) {
|
switch (state) {
|
||||||
@@ -65,6 +61,8 @@ AudioState AudioSource::state() {
|
|||||||
return AudioState::STOPPED;
|
return AudioState::STOPPED;
|
||||||
case AL_PAUSED:
|
case AL_PAUSED:
|
||||||
return AudioState::PAUSED;
|
return AudioState::PAUSED;
|
||||||
|
default:
|
||||||
|
throw std::runtime_error("Invalid OpenAL source state");
|
||||||
}
|
}
|
||||||
throw std::runtime_error("Invaild state");
|
throw std::runtime_error("Invaild state");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user