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:
1 2 |
percent = ((original_length - target_length) / target_length) * 100 |
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.
1 2 3 4 5 |
(defun get-selected-length () (let ((start (get '*selection* 'start)) (end (get '*selection* 'end))) (- end start))) |
Calculating the percentage change
Once we have the length of the original selection, we can calculate the required percentage stretch:
1 2 3 4 5 |
(let ((trk-len (get-selected-length))) (if (<= TARGET-LEN 0) (throw 'err "Error.\nTarget length too short.")) (setf percent (* 100 (/ (- trk-len TARGET-LEN) TARGET-LEN))) |
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.
1 2 3 |
(aud-import-commands) (aud-ChangeTempo :percentage percent :sbsms HQ) |
The full code may be seen by opening the plug-in in a plain text editor.
Hello.
I can’t find this effect on My effect menu of Audacity. Where to find It?
Thank You.
Marco Oros
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.