Search code examples
androidaudioaudio-recording

Audio recording delay


I'm writting an application for Android where my goal is to record the user singing. After the recording, I play the record synced with the instrumental one (which is not a problem).

The problem is when I start recording at the same time as playing the audio I suspect there is a delay for the recording to start: (it is noticeable when I try to play the recording later when the recording recorded the instrumental too). The delay might be around 300ms on nexus 5.

My inquiries are:

  • is the delay constant on all Android devices (4.0 +)?
  • how to calculate delay without involving complex methods?
  • is it acceptable on mobile device, to open the audio recording and scale it to instrumental (which may involve audio decoding/encoding)?

How to get rid of the delay?


Solution

  • is the delay constant on all Android devices (4.0 +)?

    The delay is not constant, because at least the audio round-trip latency is not constant.

    You can be sure that the delay is not constant because it also depends on:

    how to calculate delay without involving complex methods? You can't. (without complex methods).

    The audio latency reported by the AudioTrack API is only a software latency. And measuring the latency is not a simple task, as you can see in the official documentation.

    how to get rid of the delay?

    This is the good news here: you probably don't need to get rid of the delay, as long as you start playing once the recording is really started.

    Jeffrey's solution is a great start. Note that you need to read() the data or your callbacks won't get called.

    is it acceptable on mobile device, to open the audio recording and scale it to instrumental (which may involve audio decoding/encoding)?

    AudioTrack.setVolume() gives you a simple way to scale your tracks. Finding the scaling factor automatically can be quite tricky: you will need to estimate the loudness of the recording, and also the loudness of your instrumental.

    I suggest you use a much simpler solution: provide a slider on your UI which allows your user to adjust the mix between the instrumental and the voice recording (use AudioTrack.setVolume in the background). This way, you can be sure the instrumental/voice ratio really fits the user's taste.