3

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.

PPP
  • 1,279
  • 1
  • 28
  • 71

1 Answers1

3

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());
donturner
  • 17,867
  • 8
  • 59
  • 81
  • Hello Don, does writing blank data cause any overheads? Like battery consumption etc In my app i only want to play the audio when user touches a button, to get the minimal latency should I keep the stream open and only write non blank data when user presses the button? – cs guy Nov 04 '20 at 14:49
  • 1
    There will be some extra power consumption associated with keeping a stream open. I don't have any figures on how much extra power is used but I don't imagine it to be significant when compared to keeping the screen on. – donturner Nov 10 '20 at 14:41