Stretching audio to length

Audacity includes an excellent “Change Tempo” effect which can stretch the length of the selected audio without altering the pitch. The effect includes the ability to specify the required new length, but this option is not available when using the effect in a Macro, or with Scripting.

Setting the target length in a Macro

Audacity does not directly support setting the target length for Change Tempo in Macros, but fortunately we can work around this limitation using a Nyquist plug-in.

The Change Tempo effect requires the amount of stretch to be set as a percentage. The percentage is calculated as:

The task of calculating the percentage change is made a little bit tricky as the plug-in needs to be a ;type tool plug-in because we will be modifying the project with Audacity’s effects rather than modifying with Nyquist’s return value. Unfortunately, for ;type tool effects the *TRACK* variable, and other variables associated with the selection, such as LEN are unavailable, and even the GET-DURATION function cannot be used. Fortunately there is a workaround, which is that we can still use the *SELECTION* property list.

Calculating the percentage change

Once we have the length of the original selection, we can calculate the required percentage stretch:

Applying the Change Tempo effect

We can then apply the Change Tempo effect using AUD-DO, or (better) importing the AUD commands and using aud-ChangeTempo.

The full code may be seen by opening the plug-in in a plain text editor.

2 thoughts on “Stretching audio to length”

    1. The “Change Tempo” effect is a built-in effect in Audacity and is listed in the “Effect” menu. The exact position in that menu depends on which version of Audacity and which menu layout preferences you use.

      The “Change Tempo To Length” effect must be downloaded (from this page), installed (see “Installing Nyquist plugins” for installation instructions – https://support.audacityteam.org/basics/customizing-audacity/installing-plugins), and enabled. It will then be listed in Audacity’s “Tools” menu.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.