Robin Lobel
Robin Lobel

Reputation: 780

How do you find the audio latency ? (Windows/OSX)

I'm writing a music application, but I have no clue how applications such as Ableton/Cubase/etc find the audio latency of a system (do they ?) so they can compensate for time difference when recording/playing. Meaning, the audio input latency (from mic to useable buffer) and the audio output latency (from a buffer to sound in speakers).

It seems more complex than just a matter of buffer size, since an internal chain of events occurs in-between the analogic audio and the digital data the software has access to.

Any idea how to (gu)estimate that ?

Upvotes: 2

Views: 736

Answers (2)

Robin Lobel
Robin Lobel

Reputation: 780

CoreAudio:

property.mSelector = kAudioDevicePropertyLatency;
if ( AudioObjectHasProperty( id, &property ) == true ) {
    result = AudioObjectGetPropertyData( id, &property, 0, NULL, &dataSize, &latency );
}

WASAPI:

IAudioClient::GetStreamLatency
IAudioClient*& captureAudioClient->GetStreamLatency( ( long long* ) &stream_.latency[mode] );
IAudioClient*& renderAudioClient->GetStreamLatency( ( long long* ) &stream_.latency[mode] );

ASIO:

long inputLatency, outputLatency;
ASIOGetLatencies( &inputLatency, &outputLatency );

ALSA:

snd_pcm_sframes_t frames;
snd_pcm_delay( handle, &frames );

OpenSL:

AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
Method m = am.getClass().getMethod("getOutputLatency", int.class);
latency = (Integer)m.invoke(am, AudioManager.STREAM_MUSIC);

see also:

uint32_t afLatency;
android::AudioSystem::getOutputLatency(&afLatency, ANDROID_DEFAULT_OUTPUT_STREAM_TYPE);

https://android.googlesource.com/platform/system/media/+/4f924ff768d761f53db6fa2dbfb794ba7a65e776/opensles/libopensles/android_AudioPlayer.cpp https://android.googlesource.com/platform/frameworks/av/+/006ceacb82f62a22945c7702c4c0d78f31eb2290/media/libmedia/AudioSystem.cpp

Upvotes: 4

FOP
FOP

Reputation: 1022

I'm not sure, but the output is the same as input, with a delay.

If you find the FFT of the input signal and the output one, at the same time, both must to be the very similar of the input.

Make a correlation of both signals, and you must find a pulse. More tall and thin pulse, more similar input and output.

The time from 0 to the pulse, is the delay time between signals.

The correlation of the FFT of the same signal is a pulse at 0.

The correlation of the FFT of the same signal delayed 1 seg is a pulse at 1s. So on

Check this link https://www.dsprelated.com/showarticle/26.php

Upvotes: 0

Related Questions