Search code examples
androidc++audiooboe

How to give blank data for Oboe library to play as sound?


Sometimes I want my audio output in Oboe to play nothing, but I dont want it to stop, I just want it to be silent while no data arrives. I tried:

static void writeBlankData(float* pointer, int numFrames) {
    std::fill_n(pointer, numFrames, 0);
}
oboe::DataCallbackResult PlayRecordingCallback::onAudioReady(
        oboe::AudioStream *audioStream,
        void *audioData,
        int numFrames) {
    float *floatData = (float *) audioData;
    writeBlankData(floatData, numFrames);
    return oboe::DataCallbackResult::Continue;
}

but I hear a buzzing on the audio output instead of silence. Shouldn't an array of 0s be silence? I tried -1.0f also which gives a different buzzing.


Solution

  • The most likely cause is that the stream is in stereo so has 2 samples per frame. Your current code assumes a mono stream.

    Try changing:

    writeBlankData(floatData, numFrames);
    

    To:

    writeBlankData(floatData, numFrames * audioStream->getChannelCount());