Audio recording delay

I am writing an Android application where my goal is to record a user's singing. After recording, I play back the recording in sync with the instrumental (which is not a problem).

The problem is that I start recording at the same time as the audio is playing, which I suspect is a delay to start recording: (this is noticeable when I try to replay the recording later when the recording was also recorded by the instrument) The delay can be around 300ms at communication 5.

My queries:

  • - persistent lag on all android devices (4.0+)?
  • how to calculate the delay without using complex methods ?
  • acceptable on a mobile device to open an audio recording and scale it to instrumental (which might include audio decoding / encoding)?

How to get rid of the delay?

+3


source to share


2 answers


- persistent lag on all android devices (4.0+)?

The latency is not constant because at least the audio feedback latency is not constant .

You can rest assured that the delay is not constant, as it also depends on:

how to calculate latency without complicated methods? You can not. (no complicated methods).

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



how to get rid of the delay?

That's the good news here: you probably don't need to get rid of the lag while you start playing as soon as the recording starts.

Jeffrey's solution is a great start. Note that you need to read () the data, or your callbacks will not be called.

acceptable on a mobile device to open an audio recording and scale it to instrumental (which might include audio decoding / encoding)?

AudioTrack.setVolume () gives you an easy way to scale your tracks. Finding the scaling factor automatically can be quite tricky: you will need to estimate the volume of the recording as well as the volume of your instrument.

I suggest you use a much simpler solution: provide a slider in the UI that allows your user to customize the combination between instrument and voice recording (use AudioTrack.setVolume

in the background). This way, you can be sure that the signal / voice ratio really suits the user's taste.

+3


source


As @Lukos mentioned, it is very likely that latency is dependent on processing speed. It takes some time for the recorder to start up.

I believe that the best solution would be to start the recorder first and then start playing audio as soon as you know the recorder is up. This way you will have a slight delay in recording, but at least you are in sync. You have to make sure that playback has been initialized so that it can be started as quickly as possible.

This can be achieved using AudioRecorder callbacks.



// Assuming you have a recorder objects defined somewhere
AudioRecord recorder;

recorder.setNotificationMarkerPosition(1);
recorder.setRecordPositionUpdateListener(new AudioRecord.OnRecordPositionUpdateListener() {
    @Override
    public void onMarkerReached(AudioRecord recorder) {
        // start audio playback
    }

    @Override
    public void onPeriodicNotification(AudioRecord recorder) {

    }
});

      

Please note that I am not sure about the position of the marker. You might also be able to use 0, not sure if this will give you a notification right after the recording starts, or if it actually started. You should do some tests there.

+4


source







All Articles