Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to obtain latency of a stream #69

Closed
philburk opened this issue Mar 9, 2018 · 2 comments
Closed

Add method to obtain latency of a stream #69

philburk opened this issue Mar 9, 2018 · 2 comments
Labels
enhancement P1 high priority

Comments

@philburk
Copy link
Collaborator

philburk commented Mar 9, 2018

This was moved from an internal Android bug tracker.

For many developers knowing the latency of a stream is more important than obtaining very low latency.

For example, in karaoke apps, the latency of the recording and playback streams must be known in order to synchronise the user's recorded voice over the backing track.

Another example is recording in Audio Workstation apps (DAWs), the recorded audio must be synchronised with the current track. Doing this requires knowing the latency of the recording and playback streams.

We could add the following method to a utility.

int32_t getLatency()

which returns the latency of the stream in microseconds or nanoseconds (milliseconds does not have sufficient precision).

@philburk
Copy link
Collaborator Author

philburk commented Mar 9, 2018

The latency varies depending on how many frames are buffered between the app and the DAC. If the app writes N frames to AAudio then the latency will jump up immediately by N frames. The latency will then go down smoothly by sampleRate frames per second until something underflows or the app writes more data.

If the app runs with a nearly full buffer then the latency will be higher than if it runs with a nearly empty buffer. We can calculate a maximum latency that assumes full buffers all the way down. But the instantaneous latency will generally be lower.

If we add a function to Oboe then a good name might be calculateInstantaneousLatency() or calculateCurrentLatency().

@philburk
Copy link
Collaborator Author

philburk commented Mar 9, 2018

Here is code to measure latency using timestamps.

    {
        int64_t presentationFrame;
        int64_t presentationTime;
        result = AAudioStream_getTimestamp(aaudioStream,
                                           CLOCK_MONOTONIC,
                                           &presentationFrame,
                                           &presentationTime
                                           );
        if (result == AAUDIO_OK) {
            int64_t elapsedNanos = getNanoseconds() - presentationTime;
            int64_t elapsedFrames = actualSampleRate * elapsedNanos / NANOS_PER_SECOND;
            int64_t currentFrame = presentationFrame + elapsedFrames;
            int64_t framesWritten = AAudioStream_getFramesWritten(aaudioStream);
            int64_t estimatedLatencyFrames = framesWritten - currentFrame;
            int64_t estimatedLatencyMillis = estimatedLatencyFrames * 1000 / actualSampleRate;
            printf("estimatedLatencyMillis %d\n", (int)estimatedLatencyMillis);
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement P1 high priority
Projects
None yet
Development

No branches or pull requests

1 participant