Fix synchronization issues and ensure we don't call Pa_WriteStream() if

the buffer is full, otherwise it'll block.
This commit is contained in:
casey langen 2022-12-29 17:43:29 -08:00
parent 4afc8af219
commit 04fe549bf5

View File

@ -204,6 +204,9 @@ IDevice* PortAudioOut::GetDefaultDevice() {
}
OutputState PortAudioOut::Play(IBuffer *buffer, IBufferProvider *provider) {
bool bufferWritten = false;
{
std::unique_lock<decltype(this->mutex)> lock(this->mutex);
if (this->state == StatePaused) {
@ -242,6 +245,12 @@ OutputState PortAudioOut::Play(IBuffer *buffer, IBufferProvider *provider) {
auto audio = buffer->BufferPointer();
auto const samples = buffer->Samples();
auto const frameCount = samples / buffer->Channels();
auto const writeAvailable = Pa_GetStreamWriteAvailable(this->paStream);
if (writeAvailable < frameCount) {
int retryMs = buffer->SampleRate() / samples;
return static_cast<OutputState>(retryMs);
}
if (volume != 1.0f) {
float gain = 0.0;
@ -259,12 +268,17 @@ OutputState PortAudioOut::Play(IBuffer *buffer, IBufferProvider *provider) {
this->paStream, buffer->BufferPointer(), frameCount);
if (result == paNoError) {
provider->OnBufferProcessed(buffer);
return OutputState::BufferWritten;
bufferWritten = true;
}
}
}
return OutputState::BufferFull;
if (bufferWritten) {
provider->OnBufferProcessed(buffer);
return OutputState::BufferWritten;
}
return OutputState::InvalidState;
}
double PortAudioOut::Latency() {