Stretch tempo to length

REQUIRES AUDACITY 2.3.1 (or later)

WARNING: This plug-in will crash Audacity 2.3.0

This is a “Nyquist Macro” plug-in.
It was written in response to a question here: https://forum.audacityteam.org/t/macro-to-change-tempo-to-specific-length/51523/1

Audacity’s “Change Tempo” macro only provides the “Percentage Change” option, but if you need to use “To Length”, then Nyquist can convert from lengths to percentage before calling the effect with AUD-DO.

To convert lengths to percent:
Percent change = 100 * ((current-duration - target-duration) / target-duration)

The AUD-DO command is:
(aud-do “ChangeTempo: Percentage= SBSMS=<0 or 1>”)
For the SBSMS (“high quality” option), 1 = true, 0 = false.

Here’s the full code:

;control target "Target length" float-text "seconds" 60 0 nil
;control sbsms "Use highquality stretching" choice "No,Yes" 0

(let* ((start (get '*selection* 'start))
       (end (get '*selection* 'end))
       (dur (- end start)))
  (if (= target 0)
      "Error.\nTarget length cannot be zero."
      (prog ((percent (* 100 (/ (- dur target) target))))
        (cond
          ((< percent -95)
            (return "Error.\nTarget length too large."))
          ((> percent 400)
            (return "Error.\nTarget length too small."))
          (t
            (setf command
                  (format nil
                          "ChangeTempo: Percentage=~a SBSMS=~a"
                          percent
                          sbsms))
            (aud-do command)
            (return ""))))))

As this code causes the selected audio to be processed, it should be declared as a “process” type plug-in (or a “tool process” type. See: https://wiki.audacityteam.org/wiki/Nyquist_Plug-in_Headers#type)

Here is the full plug-in: stretch-to-length.ny

Hello Steve. This plug-in you made is amazing. It runs really well as an effect.
However, when I add it into a ‘normal macro’ it didn’t respond. (I was using 2.3.1-alpha)
Is there anything I could do to make it work? I need to process a large batch of file. I’d be really grateful if you could help.
Thanks!

It works for me.
What commands are in your macro?
What happens when you run it?

My Macro :
01 Select All
02 Trim Silence min-end-silence=“0.05” min-start-silence=“0.05” threshold=“-10”
03 Select All
04 Stretch to length sbsms=“Yes” target=“0.33”
05 Select End=“0.01” Mode=“Set” RelativeTo=“ProjectStart” Start=“0”
06 Insert Silence
07 Select End=“0.01” Mode=“Set” RelativeTo=“ProjectEnd”
08 Silence
09 Select All
10 Normalize ApplyGain=“1” PeakLevel=“-10” RemoveDcOffset=“1” Stereolndependent=“0”
11 Export as WAV
12 -END-
It worked fine if I do these steps manually one by one.
Therefore, I created a ‘test’ Marco to check the steps one by one.
The step breakdown at 04.
Thank you!

If you make a macro with only the “Stretch to Length” command, and apply the macro to the current project, does that work for you?
How long is the track that you are processing?

Thank you for your prompt reply.
Tracks are on average 1 second long and usually 200ms - 500ms long after “Trim silence”.
If I only use “select all → stretch to length” or just “stretch to length”, nothing happens (nothing observable changed) except the track is selected (if “select all” goes first).
This only occurs when “stretch to length” is applied in Macro though, it works well as effect (double checked).

Thank you.

Could you look in your Macros folder, and find the macro file that you are using and I’ll try it here.

Yes, it is in the attachment, thank you.
experiment sound files.txt (458 Bytes)

Well I’ve tested Stretch-to-length in a macro in both Linux and Windows 10, and it works for me.
I’ve also tried installing TrimSilence and InsertSilence so that I can run your macro, and it runs.

Perhaps there’s a problem with one of the files that you are trying to apply the macro to (?)
I presume that you are using an unmodified version of “stretch-to-length.ny” from the first post (?)

Thank you very much for your effort and patience.

I tried it on multiple audio files one by one (.mp3) it didn’t work.

May I ask if I should get the right version of “stretch-to-length.ny” from the first post of this topic?

Does it have anything to do with the version of Audacity? I installed 2.3.0 before I get a zip version of 2.3.1-alpha-Oct 22 2018. But then I uninstalled 2.3.0 and only use 2.3.1 it still doesn’t work.

Please find the screenshots of before & after I apply ‘stretch to length’ as effect & applied as Macro.
Thank you.




Yes. Here’s a direct link: https://forum.audacityteam.org/download/file.php?id=22469

I’m not able to test, but it’s possible that your version of 2.3.1-alpha is too early. The version that I was using was from January.
There is due to be a “release candidate” for Audacity 2.3.1, hopefully in the next few days, so I’d suggest that you try that when it becomes available.

It has not yet been officially announced, but there is now a release candidate available here: Audacity devel download latest version

It worked when I used the latest version of 2.3.1!
thank you very much for your help! Saved me 60 hours of work :slight_smile:

I don’t know if this is still the best solution, but i just followed this thread and somehow got it working perfectly. just wanted to say thanks all these years later

I’m pleased to hear that it still works, though this plug-in isn’t really necessary now. I added a similar feature to Audacity’s “Change Tempo” effect, so you can now specify the required length directly into the Change Tempo effect without requiring additional plug-ins.