Change Speed - Stretch audio to a specified length

As there’s been several topic about this recently I’ve made a plug-in for the task.

Because this plug-in is a Nyquist effect it is quite a lot slower than the built-in “Change Speed” effect, but some users may find it easier to use.

The plug-in has 2 modes of operation:

Mode 1: Stretch audio to a specified length.

To use this mode, “Select input Control:” must be set to “New Duration” (default).
Type the required duration into the “New Duration” box as two numbers - minutes and seconds. (the plug-in will also accept the duration as just one number which will be the number of seconds.

Example: to stretch the audio to 2 minutes and 25.4 seconds, enter:
2 25.4

Example 2: to stretch the audio to 200 seconds (3 minutes and 20 seconds), any of the following will work:
200
200.000
3 20
3.0 20.0



Mode 2: New speed as a percentage

This is different and hopefully easier than the “% change” used in the built in effect.
To use this mode “Select input Control:” must be set to “as Percent”.
Use the “New Speed as Percent” slider (or type a number into the box next to the slider). The new speed is defined as a percentage of the original speed, so 100% is the same as the original speed.

To make the audio faster, the percentage must be increased:
200% is double the original speed.
300% is 3 times the original speed.
400% is 4 times the original speed.

Increasing the speed means that the audio plays faster (and higher pitch), so the duration will be shorter.
At 200% the audio will play twice as fast, so the duration will be 1/2 of the original.
At 300% the audio will play 3 times as fast, so the duration will be 1/3rd of the original.
At 400% the audio will play 4 times as fast, so the duration will be 1/4 of the original.

To make the audio slower, the percentage must be decreased:
50% is half the original speed.
25% is a quarter of the original speed.
10% is a tenth of the original speed.

The plug-in has two “stretching methods” - “High Quality” and “Low Quality”.
“High Quality” is the default.
Both methods are quite slow, but the Low Quality conversion is not quite as slow as High Quality conversion.
When slowing down audio, there is very little difference in sound quality, both methods are very good.
When speeding up audio, the Low Quality method may produce “aliasing distortion”.


Installing.
Download the plug-in and put it in the Audacity plug-ins folder.
Restart Audacity to make it appear in the Effect menu.
The plug-in will be called “Change Speed (ny)…” and it will appear below the divider in the Effect menu.

Obsolete version:
change-speed.ny (3.03 KB)
Current version: http://forum.audacityteam.org/download/file.php?id=10291

Quite so.


Gale

For users that only need to stretch the audio to a specified length, here is a simplified version of the effect:
StretchAudio.ny (583 Bytes)
Installing.
To install Nyquist plug-ins: http://wiki.audacityteam.org/wiki/Download_Nyquist_Plug-ins#Installing_Plug-ins
The plug-in will be called “Stretch…” and it will appear below the divider in the Effect menu.

It should be safe to use this effect on long tracks, but note that it is not very fast.

I prefer the version that includes “New speed as a percentage” and hope that version will find its way on to Wiki.

Gale

Thanks for the feedback Gale.

If they are free of errors/typos, then I’d be happy to move both of them to the wiki. Personally the one that I usually have installed is the simple one (StretchAudio.ny) though I agree that some of the additional features in the more complex (change-speed.ny) version may be useful for some users.

One thing that I like about the simple version is that the code is very simple, and so provides a good example of converting a Nyquist script that can be run in the Nyquist Prompt effect, into a full plug-in. If you open StretchAudio.ny in a plain text editor (such as NotePad or NotePad++ you will see that the actual processing code is just one line:

(force-srate (* *sound-srate* (/ target (get-duration 1))) s)

which is much like the Nyquist Prompt script that I’ve posted several times for people that need to make very small and accurate changes to the duration.

There is a disadvantage to the more complex “Change Speed (ny)…” effect, which is that in exceptional circumstances the output may be over 0 dB. An example of this is if you stretch white noise that has amplitude = 1 and use the “High Quality” setting. This is due to the anti-alias filter built into the high quality resampling code, combined with the extreme amount of high frequency content in white noise. On the other hand, the “Low Quality” will not produce output of higher amplitude than the input (though there will be more distortion in the frequency domain).

I’ve updated change-speed.ny to improve the program flow/readability but should otherwise be identical to the first version.
change-speed.ny (3.24 KB)

Steve, you could perhaps include high-quality resampling apart from the linear one.

Thanks for the feedback Robert, “High Quality” resampling is included in the “change-speed.ny” effect (in first post of this topic).

The “Lite” version, which I’ve called “StretchAudio.ny” is deliberately simple, and seems to produce very good results for making small and precise changes to the length of the audio, so it is a very easy and effective solution to the fairly common problem of synchronising tracks that have drifted slightly out of sync (such as when synchronising a video track made on a camcorder with an audio track recorded on an audio recorder). In this use case it is very unlikely that there will be any noticeable aliasing distortion so I expect that some users will prefer the simple version.

I’ll add a comment in the use notes when I upload them to the wiki, indicating the relative benefits of each.

“Stretch” as the name of “StretchAudio.ny” is a little misleading for non-technical users - you can use reduce the length. Why not call it “Adjust length”?

I think it would be better to add length boxes to the built-in Change Speed than have a “simple” length adjust plug-in on Wiki that has only one control. Length boxes in Change Speed has 19 votes on Feature Requests.

I suppose we need Change Speed (ny) given we won’t persuade the powers that be that Change Speed Percent Change should be a resulting speed percent (or factor). This has nine votes on Feature Requests.

Change Speed (ny) could look simpler with less text. I would not have any ;info text except "Change Speed (affecting Tempo and Pitch) by choosing new Speed or Duration.

Then something like:

  • Choose Speed or Duration
  • New Speed
    • New Duration
  • New Duration
  • New Speed as Percent
    50% = half, 200% = double
  • Quality
  • High (slower)
    • Low (faster)

Why is New Duration not a slider here?


Gale

If I recall correctly, “stretch” was the term used by the user that was asking how to “stretch” one track to synchronise with another. In non-technical realms, I think of “stretchy” like chewing gum, which can go longer or shorter. Some manufacturers use the analogy of a “rubber band”.

I agree that “stretching” implies “making longer”, whereas this effect can “make longer or shorter”, but I think I prefer “stretch” to “adjust” because it conveys the idea of “the same audio, but spread out wider (or shorter) than the original”, whereas “adjust length” may well be interpreted as “truncating” or “chopping bits out/adding bits in” or as an alternative to “Change Tempo”. “Change Speed” is probably the best name, but that is already in use, and many users miss the point that “Change Speed” affects the length.

As seen in the code, what the effect actually does is to “resample” the audio, but I think that term is too technical for these effects.


Looking at that another way, why should it matter what language “Change Speed” is written in?

With a few improvements to the Audacity/Nyquist interface, most of the built in effects could be written in Nyquist, which has the significant advantages of rapid development, terrific maintainability and simple modification/customisation.


I agree that is unlikely, and I agree that in most cases that would be much more user friendly, but on the other hand it is good that there is consistency between Change Speed, Change Tempo and Change Pitch.


I agree, though “optional” plug-ins have the difficulty that they are not documented in the manual. One of the feature’s of LL’s “version 4” plug-ins is that they can have built-in help, accessed via a “Help button”.

Unfortunately multiple text lines does not look good (squashed up), but if the ;info text is removed then I agree that “50% = half, 200% = double” still needs to be shown somewhere in the interface.

Because it can be entered as “min s” (minutes and seconds). Nyquist plug-ins do not yet have “time” controls.

Probably, but another user could be wanting to contract a track to synchronise with another.

More advanced users may see an inference to time stretching, which is not what this plug-in does.

Yes, perhaps, but I think on the whole it’s less open to misinterpretation than “Stretch”. Perhaps it could even just be called “Length”.

Hence the suggestion that built-in Change Speed should address that issue (IIRC, Vaughan was not strongly opposed to adding Length boxes when someone on feedback@ asked for this). Then we wouldn’t need “StretchAudio”.

Also, what proportion of users realise that extra Nyquist plug-ins are available, or want that inconvenience for such an obvious limitation in the built-in effect?

Aside: Perhaps we should have a “Download” button in Effects Preferences pointing to Wiki Download Nyquist Plug-ins?

Do you mean what coding language Change Speed is written in?

Is anyone planning Nyquist GUI in the near future?

I think we had agreed not to load the interface with text even in optional plug-ins. We can use Help screens (Nyquist error interface) or a bundled help text file until there is a proper Help button, allied with good choice of label names.

In “Change Speed (ny)”, the ;info text “Enter the required duration as two numbers” isn’t mandatory (you can enter one number to get seconds). So it is misleading to have that text.


Gale

Yes, I see the linguistic issue of “stretching something shorter” :smiley:

Hmm, resampling is very closely related to “time stretching” as “Change Speed/Tempo/Pitch” are all very closely related (at least “technically” so).

The (currently called) “Stretch…” plug-in is designed particularly for tweaking the length of a recording, and imho is well suited for that task in a simple and no nonsense way. I have even considered modifying it a little so that the user enters how much they want to stretch the selection by (rather than an absolute length.
Rather than manually working out what 1:35:15.4 is in seconds, the user could just say that they want to stretch the selection by 2.3 seconds (negative values for “compressing” the length). The default range could be something like +/- 10 seconds, with text entry for greater values.

I’m in favour of a time control for the Change Speed effect, but we don’t have that yet. One of the big advantages of Nyquist plug-ins is rapid development. For a simple plug-in like “Stretch…” I can make a working effect with a usable (if not perfect) interface in a matter of hours, or a working script for the Nyquist Prompt in a couple of minutes, whereas coding it in C++ would take considerably longer (for me, that would probably be days or weeks :wink:)

That sounds like a good idea.
It would also be “nice” if there were a built-in “installer” so that the user could browse (in an Audacity file browser dialog) for the .NY file and Audacity copy it to the plug-ins folder. Not terribly easy to do cross-platform, but very helpful for the less technical users.


or any of the plug-ins. As far as most users are concerned, whether a plug-in is written in C, C++, Fortran, Basic, or Lisp is irrelevant. What’s important is whether they can achieve what they want to achieve.


There has already been a working prototype for “version 4” plug-ins from Leland (and it’s terrific :smiley:) though of late he has been busy with the whole FFMpeg/GStreamer thing.


True, but the text is briefer than saying:
“Enter the required duration as one number (seconds) or two numbers (minutes and seconds). Either number may be a decimal, but if minutes are required then there must be a number entered for ‘seconds’ even if it is 0.”

See

Time stretching is the process of changing the speed or duration of an audio signal without affecting its pitch.

in Audio time stretching and pitch scaling - Wikipedia .

Length “from” and “to” boxes in Change Speed would fulfil that aim perfectly with no need for a new plug-in.

I agree if we must have the (currently called) “Stretch” in the meantime it would be useful to give it an option to extend/contract by. If you type in that box then it overrides the length slider for seconds. I’m not sure you should only have “extend/contract by”. The user may have a number of files of different length that they are trying to make the same length.

The specific request is Length “from” and “to” boxes. Are you in favour of that?

I don’t see that as a reason not to have a Bugzilla enhancement for it. I assume you know enough about C++ to determine how easily an experienced C++ developer could add Length boxes.

You mean, install Nyquist effects from within Effects Preferences, as we eventually plan to do for VST effects?

If this is a major problem there is no reason that instead of linking to a raw .NY file on Wiki Download Nyquist Plug-ins, you could build a Windows installer for it. If you ever group plug-ins into “packs” (for example, a pack for the Nyquist “Filters” group) then a Win installer could be justified.

Does it have a GUI, so that it could show a filter graphically?

Still misleading. :wink:


Gale

Good point.
The user still can’t select multiple track and extend them all to the same length in one go, due to the current limitation that Audacity does not tell Nyquist where clips start and end (white-space is treated as silence), but a user could select each track in turn and repeat the previous effect. So yes I agree that it would be better to have an option for stretching to or stretching by the set amount.

How about changing the name of “Stretch…” to “Adjust Speed…”

Any suggestions for the wording of the proposed new “by/to” choice control?


What would the “from” box do?
What happens if the user enters a value into the “from” box?
I was thinking that the “from” duration would be like we have in the “Repeat” effect.


+1


I’m not sure what the current proposal is for VST effects. Do you have a link to the discussion?


The GUI for the experimental “version4” plug-ins is far more flexible than the current GUI. It does not currently have a “graph window” like “Classic Filter”, “Plot Spectrum” or “Equalization”, but it does have most other features available in built-in effects.

It’s certainly better than “Stretch…” .

I’m not sure it’s better than "Adjust Length… " given (a) the controls mention length, not speed (b) we are targeting users who may not even be aware that speed will be changed or whose speed change may be inaudible (aligning two tracks).

If “Adjust Length…” implies cutting or inserting, how about “Set Length…”?

Could be:

Set: End time
     Length change (- or +)



They don’t - “from” would be greyed out as in Change Tempo.

Yes we could alternatively have the current duration as static text. The important thing is that displaying the current length of the selection makes it easy to work out what length you need if you are adjusting “by”.

Would it be more or less useful if the length boxes in Change Tempo and Change Speed (if added) displayed the current Selection Format?

It’s probably in the discussion for the “Install VST Effects” dialogue on -devel or -quality. When we have an “Effect Manager” the “Install VST Effects” dialogue would be done away with and VST effects would be installed or uninstalled from Audacity using the Effect Manager interface.


Gale

I think that depends on whether the user is familiar with the (documented in the manual) “Change Speed” effect.

I’m currently favouring “Adjust Length…” but I think that we may need a mention in either a Help screen or the main effect interface that it changes both length and speed.
I’m not a big fan of our current help screens in Nyquist plug-ins, but sometimes that’s the best option (currently) available.


I like that, except that if the track/selection does not start at zero, it will not be “End time” (because it is setting the “duration”), so it may be better to have:

Set: Duration
     Length change (- or +)



Yes I’m in favour of displaying the current length.


I’d prefer a proper “time control” widget as it allows use of the same units that the user is using in the Selection bar.
I think I’d ideally like the time format to be saved independent of the Selection Toolbar format, and likewise for the built-in Generate commands, but there are pros and cons for any system.


Sounds good. Yes, when we have that I’d like it to also be able to handle Nyquist plug-ins. Would that have “permissions” problems on Windows or Mac? On Linux, Nyquist plug-ins can be installed in “~/.audacity-files/plug-ins” which avoids permission problems (and is the most “Linux-like” way to do it).

The procedure for VST should be similar with an Effect Manager to now except there will be no restart of Audacity to add new VST’s.

I suppose Nyquist will have the same restrictions as now - if you add to the Audacity Plug-Ins folder on Windows or Mac you will have to give an admin password if you are not logged in as admin.


Gale

FWIW I tested that on Mac - Audacity doesn’t recognise Nyquist plug-ins in there.


Gale

I’m wondering if you would want to add functionality to make the “stretch” sample based. I often sync up multiple sources, and I’m expecting sample accuracy. I made a small change for my own use for that purpose. Here’s the meat of the code:

  ((= input 0)        ;Use string input
    (to-dur string)   ;get duration from string
    ;; calculate new-srate from duration
    (if (= (length err) 0)
      (let ((ratio (/ duration len)))
        (setq new-srate (* ratio *sound-srate*)))))

For that matter, does anyone know why Audacity’s own Change Speed isn’t sample accurate? You can enter a specific number of samples into the New Length field, but it seems that the calculation is based on the Speed Multiplier, as the results are almost never sample accurate.

It’s sample accurate for me unless the selection is extremely long.
Perhaps you’re using an old version of Audacity? The current version is 2.1.3 and is available here: Audacity ® | Downloads

Thanks for your contribution, but I think this plug-in is probably obsolete since the built-in effect was updated. (This plug-in was posted over 3 years ago, and Audacity has moved on a long way since then.)

Ah, thanks. I had been using 2.1.2. I hadn’t seen anything in the release notes about a change, but it would appear as if it’s working correctly now in 2.1.3.