Skip to content

Commit f0cd90b

Browse files
committed
SoundDeviceNetwork: Fix writing wrong number of silence after a long underrun
This happens due to samples vs. frames issue.
1 parent 4aca12d commit f0cd90b

2 files changed

Lines changed: 12 additions & 14 deletions

File tree

src/soundio/sounddevicenetwork.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ void SoundDeviceNetwork::workerWriteProcess(NetworkOutputStreamWorkerPtr pWorker
318318
// << "Catch up with silence:" << writeExpected - copyCount
319319
// << "streamTime" << pWorker->getStreamTimeFrames();;
320320
// catch up by filling buffer until we are synced
321-
workerWriteSilence(pWorker, writeExpected - copyCount);
321+
workerWriteSilence(pWorker, (writeExpected - copyCount) / m_numOutputChannels);
322322
m_pSoundManager->underflowHappened(24);
323323
} else if (writeExpected - copyCount > outChunkSize / 2) {
324324
// try to keep PAs buffer filled up to 0.5 chunks
@@ -327,7 +327,7 @@ void SoundDeviceNetwork::workerWriteProcess(NetworkOutputStreamWorkerPtr pWorker
327327
// kLogger.debug() << "workerWriteProcess() duplicate one frame"
328328
// << (float)writeExpected / outChunkSize
329329
// << (float)readAvailable / outChunkSize;
330-
workerWrite(pWorker, dataPtr1, m_numOutputChannels);
330+
workerWrite(pWorker, dataPtr1, 1);
331331
} else {
332332
pWorker->setOutputDrift(true);
333333
}
@@ -349,9 +349,9 @@ void SoundDeviceNetwork::workerWriteProcess(NetworkOutputStreamWorkerPtr pWorker
349349
pWorker->setOutputDrift(false);
350350
}
351351

352-
workerWrite(pWorker, dataPtr1, size1);
352+
workerWrite(pWorker, dataPtr1, size1 / m_numOutputChannels);
353353
if (size2 > 0) {
354-
workerWrite(pWorker, dataPtr2, size2);
354+
workerWrite(pWorker, dataPtr2, size2 / m_numOutputChannels);
355355
}
356356

357357
QSharedPointer<FIFO<CSAMPLE>> pFifo = pWorker->getOutputFifo();
@@ -368,17 +368,16 @@ void SoundDeviceNetwork::workerWriteProcess(NetworkOutputStreamWorkerPtr pWorker
368368
}
369369

370370
void SoundDeviceNetwork::workerWrite(NetworkOutputStreamWorkerPtr pWorker,
371-
const CSAMPLE* buffer,
372-
SINT samples) {
371+
const CSAMPLE* buffer, int frames) {
373372
if (!pWorker->threadWaiting()) {
374-
pWorker->addFramesWritten(samples / m_numOutputChannels);
373+
pWorker->addFramesWritten(frames);
375374
return;
376375
}
377376

378377
QSharedPointer<FIFO<CSAMPLE>> pFifo = pWorker->getOutputFifo();
379378
if (pFifo) {
380379
int writeAvailable = pFifo->writeAvailable();
381-
int writeRequired = samples;
380+
int writeRequired = frames * m_numOutputChannels;
382381
if (writeAvailable < writeRequired) {
383382
kLogger.warning() << "write: worker buffer full, losing samples";
384383
pWorker->incOverflowCount();
@@ -396,16 +395,16 @@ void SoundDeviceNetwork::workerWrite(NetworkOutputStreamWorkerPtr pWorker,
396395
}
397396
}
398397

399-
void SoundDeviceNetwork::workerWriteSilence(NetworkOutputStreamWorkerPtr pWorker, SINT samples) {
398+
void SoundDeviceNetwork::workerWriteSilence(NetworkOutputStreamWorkerPtr pWorker, int frames) {
400399
if (!pWorker->threadWaiting()) {
401-
pWorker->addFramesWritten(samples / m_numOutputChannels);
400+
pWorker->addFramesWritten(frames);
402401
return;
403402
}
404403

405404
QSharedPointer<FIFO<CSAMPLE>> pFifo = pWorker->getOutputFifo();
406405
if (pFifo) {
407406
int writeAvailable = pFifo->writeAvailable();
408-
int writeRequired = samples;
407+
int writeRequired = frames * m_numOutputChannels;
409408
if (writeAvailable < writeRequired) {
410409
kLogger.warning() << "writeSilence: worker buffer full, losing samples";
411410
pWorker->incOverflowCount();

src/soundio/sounddevicenetwork.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,8 @@ class SoundDeviceNetwork : public SoundDevice {
5252
CSAMPLE* dataPtr1, ring_buffer_size_t size1,
5353
CSAMPLE* dataPtr2, ring_buffer_size_t size2);
5454
void workerWrite(NetworkOutputStreamWorkerPtr pWorker,
55-
const CSAMPLE* buffer,
56-
SINT samples);
57-
void workerWriteSilence(NetworkOutputStreamWorkerPtr pWorker, SINT samples);
55+
const CSAMPLE* buffer, int frames);
56+
void workerWriteSilence(NetworkOutputStreamWorkerPtr pWorker, int frames);
5857

5958
QSharedPointer<EngineNetworkStream> m_pNetworkStream;
6059
std::unique_ptr<FIFO<CSAMPLE>> m_outputFifo;

0 commit comments

Comments
 (0)