audio fixes

This commit is contained in:
MihailRis
2024-03-09 12:15:11 +03:00
parent 3ee350fc0c
commit 8ac51752d3
12 changed files with 49 additions and 7 deletions
+16 -1
View File
@@ -38,6 +38,11 @@ ALStream::ALStream(ALAudio* al, std::shared_ptr<PCMStream> source, bool keepSour
ALStream::~ALStream() {
bindSpeaker(0);
source = nullptr;
while (!unusedBuffers.empty()) {
al->freeBuffer(unusedBuffers.front());
unusedBuffers.pop();
}
}
std::shared_ptr<PCMStream> ALStream::getSource() const {
@@ -135,6 +140,11 @@ void ALStream::update(double delta) {
return;
}
ALSpeaker* alspeaker = dynamic_cast<ALSpeaker*>(speaker);
if (alspeaker->stopped) {
speaker = 0;
return;
}
uint alsource = alspeaker->source;
unqueueBuffers(alsource);
@@ -194,6 +204,8 @@ ALSpeaker::~ALSpeaker() {
}
void ALSpeaker::update(const Channel* channel, float masterVolume) {
if (source == 0)
return;
float gain = this->volume * channel->getVolume()*masterVolume;
AL_CHECK(alSourcef(source, AL_GAIN, gain));
@@ -265,7 +277,9 @@ void ALSpeaker::stop() {
AL_CHECK(alSourceUnqueueBuffers(source, 1, &buffer));
al->freeBuffer(buffer);
}
AL_CHECK(alSourcei(source, AL_BUFFER, 0));
al->freeSource(source);
source = 0;
}
}
@@ -395,8 +409,9 @@ uint ALAudio::getFreeSource(){
}
ALuint id;
alGenSources(1, &id);
if (!AL_GET_ERROR())
if (!AL_GET_ERROR()) {
return 0;
}
allsources.push_back(id);
return id;
}
+8 -1
View File
@@ -17,7 +17,8 @@
#define AL_CHECK(STATEMENT) STATEMENT; AL::check_errors(__FILE__, __LINE__)
#define AL_GET_ERROR() AL::check_errors(__FILE__, __LINE__)
namespace AL {
namespace AL {
/// @return true if no errors
bool check_errors(const std::string& filename, const std::uint_fast32_t line);
/// @brief alGetSourcef wrapper
@@ -27,6 +28,8 @@ namespace AL {
/// @return field value or default
inline float getSourcef(uint source, ALenum field, float def=0.0f) {
float value = def;
if (source == 0)
return def;
AL_CHECK(alGetSourcef(source, field, &value));
return value;
}
@@ -38,6 +41,8 @@ namespace AL {
/// @return field value or default
inline glm::vec3 getSource3f(uint source, ALenum field, glm::vec3 def={}) {
glm::vec3 value = def;
if (source == 0)
return def;
AL_CHECK(alGetSource3f(source, field, &value.x, &value.y, &value.z));
return value;
}
@@ -49,6 +54,8 @@ namespace AL {
/// @return field value or default
inline float getSourcei(uint source, ALenum field, int def=0) {
int value = def;
if (source == 0)
return def;
AL_CHECK(alGetSourcei(source, field, &value));
return value;
}
+4
View File
@@ -368,6 +368,10 @@ size_t audio::count_speakers() {
return speakers.size();
}
size_t audio::count_streams() {
return streams.size();
}
void audio::update(double delta) {
backend->update(delta);
+3
View File
@@ -484,6 +484,9 @@ namespace audio {
/// @brief Get alive speakers number (including paused)
extern size_t count_speakers();
/// @brief Get playing streams number (including paused)
extern size_t count_streams();
/// @brief Update audio streams and sound instanced
/// @param delta time elapsed since the last update (seconds)
extern void update(double delta);