How to test what my audio output latency is on MacBook?
I am trying right now to test my auditory reaction time using a script I wrote at the command line (using the classic echo -ne '\007'
beep).
I am getting reaction times always greater than 340 ms. When I try this with a visual prompt (echo go
) instead, I am getting about 280 ms.
Apparently human audio reaction time should be faster than visual reaction time (according to lots of published studies).
Of course, it could be that my visual reaction time is faster, but how can I check what my MacBook audio output latency actually is?
You really cannot measure the latency "through" the OS because of the overhead of queuing and managing the processes. To be "more accurate" you'd need to write a program that could directly access the kernel so that you could evaluate the actual time between events - the beep and the keypress.
However, you can get fairly close in bash by using the time
function. I've created a basic bash script that will prompt the user to press Enter after waiting for a beep to occur after a random period.
# generate a random number between 3 and 10 for seconds to wait for beep
t=$(( ( RANDOM % 10 ) + 3 ))
echo "When you hear the beep, press Enter!"
echo
echo
sleep $t
# beep the computer and get the time
time echo -ne '\007'
# wait for keypress and get the time
time read keypress
You'll get three time values twice. They are defined as:
- Real - the time start to finish of the call measured if you had a stopwatch in hand. This accounts for all processes running at the time.
- User - this is the time spent in the user space outside the kernel and is the actual CPU time spent executing the task
- Sys - this is the time spent inside the kernel and is also the actual time spent by the CPU.
For more information on this see Real, User and Sys process time statistics.
So, in order of accuracy, Real is in the ballpark, User is pretty darn close, and Sys is dead on balls accurate, as far as the CPU is concerned.
Sample Output:
When you hear the beep, press Enter!
real 0m0.000s
user 0m0.000s
sys 0m0.000s
real 0m0.812s
user 0m0.000s
sys 0m0.000s
In the above example it shows that the latency according to the computers "stopwatch", there was a .812s difference between when I heard the beep and pressed Enter. The resolution of the time utility is not granular enough to measure the time elapsed to execute each command so we assume it happened "immediately."
Running it again....
When you hear the beep, press Enter!
real 0m0.003s
user 0m0.001s
sys 0m0.002s
real 0m0.924s
user 0m0.001s
sys 0m0.002s
From this one, we can see that there was some overhead that caused the process to account for more time. If we subtract the top values from the bottom, we can get pretty close to how much time elapsed.
But is it accurate enough?
No. This is why you need a device called an Audiometer that is a purpose built piece of hardware that measures the time between the two events - sound and keypress. It's much more accurate because it removes the overhead of the CPU management and gives you a truly accurate elapsed time.