Set Latency Compensation


In some cases, it is important to record in a perfect synch with the existing audio. There is an instruction on how to do the Latency test and then set the Latency Compensation (Latency Test).

In attachment, you can find two macros that automatically set the latency compensation for you. Important rule is to allow mic to listen to the audio. Enable Overdub and make sure that the headphones are close to the mic or that the loudspeakers are enabled. Yes, you can do this test just with the loudspeakers/monitor speakers.

Why there are two macros?
Good question. The very true answer is because Audacity crashes when a plugin contains the command (aud-do-command “Undo”). I was not able to overcome this problem. So instead of plugin I had to switch to the macro (to undo the temporary steps) and instead of having one macro I had to split the code into two macros (only the second one must contain the undo commands).

How to use it

  1. Run the macro Set Latency Compensation 1.
  2. Be silent and wait 2 seconds until the recording is done.
  3. Do not click/select/deselect anything. Immediately after the recording stops, run the macro Set Latency Compensation 2.

Note: This macros were developed in Audacity 2.4.2.

Any comments or feedback?
Set Latency Compensation 2.txt (3.65 KB)
Set Latency Compensation 1.txt (656 Bytes)

This was developed with a slightly more recent version of Audacity, but I think it should still work in 2.4.2:

Instead of lots of “Undo’s”, I used:


The slight downside is that it leaves some undo data in Audacity’s temp folder until Audacity is closed, but as it’s only a few seconds of audio, that should not be an issue.

I did not notice that post :astonished:
I tested in v2.4.2 and it does not work for me. I had the same problem that the macro does not wait until the recording is done. It continues with the next steps and this interrupts the recording. Can I do something with this problem?


This works for an empty project. I was thinking to allow to set latency compensation also in the project with some data on tracks. I need to mute all existing tracks. And then return it back. Now I got an idea - I can store the mute state of all tracks and at the end of the macro to restore the mute state of every track. This is not difficult.

I just went through your macro code. It is very interesting to see different, but similar approach to solve the same problem :slight_smile:
Of course, my level of Nyquist programming is still at kindergarten :smiley: , when I compare it to your level.

I have a questions: Why do you store the value directly into scratch? Is it safe? What if I have another tool that stores something in the defined variable in scratch?

As they say “Great minds …” :smiley:

That was a problem in older versions of Audacity. I don’t recall exactly when it was fixed - I thought it was fixed in 2.4.2, but perhaps 3.0.0?

You could always open a new Audacity window (“Ctrl + N”).
Personally, I wouldn’t want to be messing around with setting up latency compensation (either manually or via a macro) while I have valuable unsaved data in the project.

Hmm, let me refer to the code …

So long as the macro runs to completion, yes it’s safe. After scratch has been used, the macro cleans up after itself:

(when (boundp '*scratch*)
  (setf *scratch* '*unbound*))

When using scratch, because scratch persists until Audacity is closed, the developer needs to take steps to avoid data collision.
Usually this is done by using property list with property names that are extremely unlikely to conflict. Well behaved plug-ins should clean up after themselves when possible, and certainly should not leave large amounts of data in RAM.

In the case of this macro, scratch is short-lived as its value is deleted at the end, and it’s a very small amount of data.
Note that the macro also cleans scratch before use with the same code as it uses to clean up at the end:

(when (boundp '*scratch*)
  (setf *scratch* '*unbound*))

If you’ve not already seen it, this tutorial in the Audacity wiki is worth a look: Missing features - Audacity Support

and here’s unbound in the XLISP manual: XLISP *unbound*