Reinitialize stream if format params change.

This commit is contained in:
casey langen 2022-12-29 20:23:05 -08:00
parent 96be500dd1
commit 4c8123390c
2 changed files with 22 additions and 0 deletions

View File

@ -242,6 +242,8 @@ void PortAudioOut::Stop() {
std::unique_lock<decltype(this->mutex)> lock(this->mutex);
if (this->paStream) {
logPaError("Pa_AbortStream", Pa_AbortStream(this->paStream));
logPaError("Pa_AbortStream", Pa_CloseStream(this->paStream));
this->paStream = nullptr;
}
this->state = StateStopped;
this->buffers.swap(swap);
@ -313,6 +315,11 @@ OutputState PortAudioOut::Play(IBuffer *buffer, IBufferProvider *provider) {
return static_cast<OutputState>(retryMs);
}
if (!this->formatContext.IsSame(buffer)) {
this->Stop();
this->formatContext.From(buffer);
}
if (!this->paStream) {
auto device = static_cast<PortAudioDevice*>(this->GetDefaultDevice());
if (device) {

View File

@ -84,6 +84,20 @@ class PortAudioOut : public IOutput {
StateDraining,
};
struct FormatContext {
bool IsSame(IBuffer* buffer) {
return
this->sampleRate == buffer->SampleRate() &&
this->channels == buffer->Channels();
}
void From(IBuffer* buffer) {
this->sampleRate = buffer->SampleRate();
this->channels = buffer->Channels();
}
long sampleRate { 0 };
long channels { 0 };
};
struct BufferContext {
BufferContext(IBuffer* buffer, IBufferProvider* provider) {
this->buffer = buffer;
@ -102,6 +116,7 @@ class PortAudioOut : public IOutput {
PaStream* paStream { nullptr };
IDeviceList* deviceList { nullptr };
std::deque<std::shared_ptr<BufferContext>> buffers;
FormatContext formatContext;
State state;
double volume;
};