Skip to content

Commit f9d83d0

Browse files
committed
Fix OSX crash
When compensating for underrun from our ring buffer, the calculation for number of silent samples was incorrect. This led to smashing whatever followed PortAudio's buffer.
1 parent 1c060d3 commit f9d83d0

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/audio/OSAudio.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void OSAudio::addBuffer(HulaRingBuffer *rb)
4949
*/
5050
void OSAudio::startRecord()
5151
{
52-
hlDebug() << "Start record called" << std::endl;
52+
hlDebug() << "OSAudio: Start record called." << std::endl;
5353

5454
// Prevent other state changes
5555
this->stateSem.wait();
@@ -61,6 +61,7 @@ void OSAudio::startRecord()
6161
}
6262
else
6363
{
64+
hlDebug() << "OSAudio: Start record fell through." << std::endl;
6465
this->stateSem.notify();
6566
}
6667
}
@@ -110,7 +111,7 @@ void OSAudio::removeBuffer(HulaRingBuffer *rb)
110111
*/
111112
void OSAudio::endRecord()
112113
{
113-
hlDebug() << "End record called" << std::endl;
114+
hlDebug() << "OSAudio: End record called." << std::endl;
114115

115116
this->stateSem.wait();
116117

@@ -281,6 +282,9 @@ void OSAudio::backgroundPlayback()
281282
*/
282283
void OSAudio::startPlayback()
283284
{
285+
hlDebug() << "OSAudio: Start playback called." << std::endl;
286+
287+
284288
this->stateSem.wait();
285289

286290
if(this->endPlay.load() && this->endCapture.load())
@@ -291,6 +295,7 @@ void OSAudio::startPlayback()
291295
}
292296
else
293297
{
298+
hlDebug() << "OSAudio: Start playback fell through." << std::endl;
294299
this->stateSem.notify();
295300
}
296301
}
@@ -319,6 +324,8 @@ ring_buffer_size_t OSAudio::playbackCopyToBuffers(const float *samples, ring_buf
319324
*/
320325
void OSAudio::endPlayback()
321326
{
327+
hlDebug() << "OSAudio: End playback called." << std::endl;
328+
322329
this->stateSem.wait();
323330

324331
this->endPlay.store(true);
@@ -377,9 +384,9 @@ static int paPlayCallback(const void *inputBuffer, void *outputBuffer,
377384
{
378385
hlDebug() << "Playback: Ring buffer underrun. Received " << samplesRead << " of " << elementsToRead << std::endl;
379386
hlDebug() << "Writing " << elementsToRead - samplesRead << " samples of silence." << std::endl;
380-
for (int i = 0; i < elementsToRead; i++)
387+
for (int i = samplesRead; i < elementsToRead; i++)
381388
{
382-
wptr[i + size1 + size2] = 0;
389+
wptr[i] = 0;
383390
}
384391
}
385392

src/control/Playback.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ void Playback::player()
6060
SNDFILE *sndFile = sf_open(files[fileIndex].c_str(), SFM_READ, &sfinfo);
6161

6262
hlDebug() << "Opened file #" << fileIndex << std::endl;
63+
hlDebug() << "Location: " << files[fileIndex] << std::endl;
6364

6465
sf_count_t samplesRead = 0;
6566
while(!this->endPlay.load())
@@ -85,7 +86,7 @@ void Playback::player()
8586
if (samplesRead == 0)
8687
{
8788
// Open the next file or end the playback
88-
if (fileIndex != files.size() - 1)
89+
if (fileIndex < files.size() - 1)
8990
{
9091
sf_close(sndFile);
9192

0 commit comments

Comments
 (0)