mirror of
https://github.com/rwengine/openrw.git
synced 2024-10-06 09:07:19 +02:00
Wrap more openAl code with error checking and unqueue buffers before
delete them
This commit is contained in:
parent
1d6290f605
commit
1b588da1d1
@ -1,4 +1,4 @@
|
|||||||
#include "audio/SoundBufferStreamed.hpp"
|
#include "audio/SoundBufferStreamed.hpp"
|
||||||
|
|
||||||
#include <rw/types.hpp>
|
#include <rw/types.hpp>
|
||||||
|
|
||||||
@ -18,13 +18,14 @@ SoundBufferStreamed::SoundBufferStreamed() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SoundBufferStreamed::~SoundBufferStreamed() {
|
SoundBufferStreamed::~SoundBufferStreamed() {
|
||||||
|
alCheck(alSourceUnqueueBuffers(source, kNrBuffersStreaming, buffers.data()));
|
||||||
alCheck(alDeleteBuffers(kNrBuffersStreaming, buffers.data()));
|
alCheck(alDeleteBuffers(kNrBuffersStreaming, buffers.data()));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SoundBufferStreamed::bufferData(SoundSource &soundSource) {
|
bool SoundBufferStreamed::bufferData(SoundSource &soundSource) {
|
||||||
/* Rewind the source position and clear the buffer queue */
|
/* Rewind the source position and clear the buffer queue */
|
||||||
alSourceRewind(source);
|
alCheck(alSourceRewind(source));
|
||||||
alSourcei(source, AL_BUFFER, 0);
|
alCheck(alSourcei(source, AL_BUFFER, 0));
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lock(soundSource.mutex);
|
std::lock_guard<std::mutex> lock(soundSource.mutex);
|
||||||
/* Fill the buffer queue */
|
/* Fill the buffer queue */
|
||||||
@ -43,7 +44,7 @@ bool SoundBufferStreamed::bufferData(SoundSource &soundSource) {
|
|||||||
streamedData++;
|
streamedData++;
|
||||||
}
|
}
|
||||||
|
|
||||||
alSourceQueueBuffers(source, kNrBuffersStreaming, buffers.data());
|
alCheck(alSourceQueueBuffers(source, kNrBuffersStreaming, buffers.data()));
|
||||||
|
|
||||||
this->soundSource = &soundSource;
|
this->soundSource = &soundSource;
|
||||||
|
|
||||||
@ -65,8 +66,8 @@ void SoundBufferStreamed::updateBuffers() {
|
|||||||
ALint processed, state;
|
ALint processed, state;
|
||||||
|
|
||||||
/* Get relevant source info */
|
/* Get relevant source info */
|
||||||
alGetSourcei(source, AL_SOURCE_STATE, &state);
|
alCheck(alGetSourcei(source, AL_SOURCE_STATE, &state));
|
||||||
alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
|
alCheck(alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed));
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lock(soundSource->mutex);
|
std::lock_guard<std::mutex> lock(soundSource->mutex);
|
||||||
bool bufferedData = false;
|
bool bufferedData = false;
|
||||||
@ -81,18 +82,19 @@ void SoundBufferStreamed::updateBuffers() {
|
|||||||
soundSource->data.size() -
|
soundSource->data.size() -
|
||||||
static_cast<size_t>(kSizeOfChunk) * streamedData);
|
static_cast<size_t>(kSizeOfChunk) * streamedData);
|
||||||
|
|
||||||
alSourceUnqueueBuffers(source, 1, &bufid);
|
alCheck(alSourceUnqueueBuffers(source, 1, &bufid));
|
||||||
processed--;
|
processed--;
|
||||||
|
|
||||||
if (sizeOfNextChunk > 0) {
|
if (sizeOfNextChunk > 0) {
|
||||||
alBufferData(bufid,
|
alCheck(alBufferData(
|
||||||
soundSource->channels == 1 ? AL_FORMAT_MONO16
|
bufid,
|
||||||
: AL_FORMAT_STEREO16,
|
soundSource->channels == 1 ? AL_FORMAT_MONO16
|
||||||
&soundSource->data[streamedData * kSizeOfChunk],
|
: AL_FORMAT_STEREO16,
|
||||||
sizeOfNextChunk * sizeof(int16_t),
|
&soundSource->data[streamedData * kSizeOfChunk],
|
||||||
soundSource->sampleRate);
|
sizeOfNextChunk * sizeof(int16_t),
|
||||||
|
soundSource->sampleRate));
|
||||||
streamedData++;
|
streamedData++;
|
||||||
alSourceQueueBuffers(source, 1, &bufid);
|
alCheck(alSourceQueueBuffers(source, 1, &bufid));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,10 +103,10 @@ void SoundBufferStreamed::updateBuffers() {
|
|||||||
ALint queued;
|
ALint queued;
|
||||||
|
|
||||||
/* If no buffers are queued, playback is finished */
|
/* If no buffers are queued, playback is finished */
|
||||||
alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);
|
alCheck(alGetSourcei(source, AL_BUFFERS_QUEUED, &queued));
|
||||||
if (queued == 0) return;
|
if (queued == 0) return;
|
||||||
|
|
||||||
alSourcePlay(source);
|
alCheck(alSourcePlay(source));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user