Rogare
Rogare

Reputation: 3274

Audio buffer error when headphones plugged in

I'm using Novocaine to play audio within an app, and I'm consistently getting a crash after the following steps:

  1. Open app on iPhone with no headphones plugged in
  2. Begin audio playback in app
  3. Plug in headphones

The app crashes on the line outData[i*stride] = mData[whichChannel][idx]; in the following function:

void RingBuffer::FetchData(float *outData, SInt64 numFrames, SInt64 whichChannel, SInt64 stride)
{
    int idx;
    for (int i=0; i < numFrames; ++i) {
        idx = (int)((mLastReadIndex[whichChannel] + i) % (mSizeOfBuffer));
        outData[i*stride] = mData[whichChannel][idx];
    }

    mLastReadIndex[whichChannel] = (mLastReadIndex[whichChannel] + numFrames) % (mSizeOfBuffer);

    mNumUnreadFrames[whichChannel] -= numFrames;
    if (mNumUnreadFrames[whichChannel] <= 0) mNumUnreadFrames[whichChannel] = 0;

}

In the header file, mData is declared as float **mData; and the error message in the editor is "AURemoteIO::IOThread(14): EXC_BAD_ACCESS (code=1, address=0x0)."

If I type po mData[whichChannel][idx] in the console, I get the message "error: Couldn't apply expression side effects : Couldn't dematerialize a result variable: couldn't read its memory."

What's happening here and how can I avoid it? As an Objective-C/Swift developer, these are strange waters for me, so any help would be much appreciated. Thanks for reading!

EDIT: Additional debugging information at the time of crash:

numFrames=1024
whichChannel=1
stride=2
idx=6 // though it's been several different numbers before at the time of crash

Upvotes: 1

Views: 229

Answers (1)

Yasir Ali
Yasir Ali

Reputation: 154

Tthe variable whichChannel is the culprit, when we play a song on speakers and connect the headphones afterward, it crashes as soon as whichChannel becomes 1, i tried to hardcode it to 0 for such scenario and the crash stopped but the audio became distorted! in novocaine.mm, change line 901 and 902 to this

// self.numOutputChannels = newNumChannels; self.numOutputChannels = 1;

this will fix the issue :)

Upvotes: 2

Related Questions