Trimming almost-silence from the beginning and end of a file

As the title says: I have a bunch (over 100) of very short audio files, many of which have a second or two of almost empty space at the beginning and end of the file. I’m wondering if there’s any way to set something up to automatically remove those portions, while leaving alone anything in the middle (eg. pauses between words), thereby shortening the length of the audio file while leaving the core unchanged.

I saw something mention a “Truncate Silence” effect, which is very close to what I’m trying to get, but I only want silences at the very beginning and very end to be removed. (well, much is not complete silence, but is still quite soft.)

Anyone have any ideas? Thanks.

Are there “silent” parts within the recordings other than at the start/end? If so, how long are these pauses? Are they longer or shorter than the silences that you want to remove?

There are a few almost-silent parts in the middle of some, consisting of spaces between spoken words, usually ranging from about .1 to .2 seconds. The almost-silent parts at the beginning of the files are about the same length, but now that I look more closely at the individual files, what matters most is truncating the almost-silent end of the audio, which is more in the range of .5 to 2 seconds.

The length of each clip varies from around 3 seconds to 5 seconds.

If there was some option like “Truncate silence below X dB if above Y seconds” that would probably work well.

You’re in luck.

Use “Effect menu > Truncate Silence”
Set “Min silence duration” to set the minimum length that the silence needs to be in order for it to be truncated. (the shortest allowable resulting silence)
Set “Max silence duration” to set the new (target) length that you want the silence to be. (the longest allowable resulting silence)
Set “Silence compression” to “1” (no effect).
Set “Threshold for silence” just a little higher than the noise level of the silent parts that are to be removed.

This effect can be used as a Chain effect in Audacity 1.3.13
http://manual.audacityteam.org/man/Edit_Chains
http://manual.audacityteam.org/man/Apply_Chain
http://manual.audacityteam.org/man/Truncate_Silence

Very useful. Truncate Silence wasn’t in my “Effects” menu so I downloaded the most recent 1.3 beta and it seemed to work perfectly. By using Chains along with a moderate tempo speed-up, most of the files are now from 1 to 2 seconds, which is an excellent help.

Thanks!

Hi! I want to do exactly what was originally requested by the person who started this thread. Namely, delete silence from start and end of file NO MATTER WHAT IS IN THE REST OF THE FILE.

The reason - I don’t know whats in my 1000 or so voiceover files and I don’t want to risk Audacity corrupting stuff within the file by say closing a vocal pause.

Is that possible?

p.s. I’m looking for it to be batch of course!

Currently that is not possible in Audacity. However, if you are able to build Audacity from the source code there is a patch available that will make it possible.

Alternatively it would be possible to write a script to perform this task using SoX

Hi. Thanks. Surely there must be a tool that can do this (without learning a new scripting language) if Audacity can’t? It must be incredibly common…

Not tried it but a quick search shows this might do the job. BUT the batch processing version is not free:

http://www.mptrim.com

The problem with doing it in Audacity is not the trimming, but the fact that you want only trimming the ends even if there is long silence in the middle, and that you want to run it as a batch. Take away one or other of those constraints and it’s easy.

One of the highest rated feature requests is “Include features dependent on selecting regions (e.g. trim, fades)”.
I’ve added your vote.

Even with a batch feature that allowed selections you’ll be better off with a tool like MPTrim than Audacity anyway, because MPTrim won’t re-encode the MP3, but Audacity will.

Actually I can’t think of a free Windows GUI tool (or audio editor) either that does a batch processing length trim without re-encoding.

@Steve, is there a Nyquist plug-in that can trim the start and the end with only the whole track selected - is that what you meant by a patch?



Gale

Yes that’s what I meant.
I’ve got a plug-in somewhere that will trim a set duration (either in seconds, or as a percentage of the track duration) from the start/end of a selection. If used in a chain it would be applied to the entire track and so would trim the start and end of the track. This could be modified to trim only silence (or sound below a threshold level) from those regions.

If only a specific duration needed to be trimmed (for example, trim 0.5 second from the start and 2.5 seconds from the end), then that could already be done using the (undocumented and slightly buggy) feature of using the Nyquist Prompt in a chain. Trimming only silence is not quite so easy and is probably a bit too complex for running in the Nyquist Prompt.

The code for trimming the start and end (this will trim both audio and silence):

(setq start 0.5)
(setq end 2.5)
(multichan-expand #'extract-abs start (- (get-duration 1) end)(cue s))

The first number (0.5) is the number of seconds to trim from the start.
The second number (2.5) is the number of seconds to trim from the end.

Of course this does not get round the issue of re-encoding if the files are in a compressed format such as MP3.

OK, here’s a way that you can do it.
This requires Audacity 1.3 (preferably the latest version) and uses an undocumented and possibly buggy feature so only run this on copies of the files, not the originals, and avoid having other programs open just in case the whole thing crashes. It will not be very fast and is not suitable for long tracks as the track has to be loaded into memory, but considering that we’re doing something that is officially impossible let’s not complain. :wink:

See here for “Edit Chains” http://manual.audacityteam.org/man/Edit_Chains
and here for “Apply Chains” http://manual.audacityteam.org/man/Apply_Chain
we’ll be using these features.

  1. Create a new Chain
  2. In “Edit Chains”, click “Insert”. Then hit ENTER (the focus is on “Edit Parameters”). This opens a Nyquist Prompt
  3. copy and paste the following code into the Nyquist Prompt:
(setq thresh -48) ; silence threshold [dB]

(setq thresh (db-to-linear thresh))
(let* ((mysound (if (arrayp s)(snd-maxv (aref s 0)(aref s 1)) s))
			(start-count 0)
			(end-count 0)
			(flag 0)
			(my-srate (/ *sound-srate* 100.0))
			(mysound (snd-avg mysound 100 100 op-peak))
			(samples (snd-length mysound ny:all)))

	(dotimes (i samples)
		(setq new (snd-fetch mysound))
		(cond 
			((= flag 0)
				(if (<= new thresh)
					(setq start-count (1+ start-count))
					(setq flag 1)))
			(T
				(if (<= new thresh)
					(setq end-count (1+ end-count))
					(setq end-count 0)))))

	(let ((start (/ start-count my-srate))
				(end (-(get-duration 1)(/ end-count my-srate))))
		(multichan-expand #'extract-abs start end (cue s))))

Notice the “-48” in the first line. This is the threshold (in dB) for what will be considered to be “silence” at the beginning and end of the track. You can set it to a different value, but should be a negative number.
4. Add an Export step (for example “Export WAV”) to the chain after the “empty” Chain command.
5. Click OK to exit the Edit Chain dialogue
6. Now apply the Chain to your files (I recommend that you test it on just one or two files first).

And that’s it. Good luck.

Thanks, Steve. And if anyone just wanted to trim the start and end by a particular amount irrespective of how quiet it was, you can just enter Steve’s simpler code in Nyquist Prompt:

(setq start 0.5)
(setq end 2.5)
(multichan-expand #'extract-abs start (- (get-duration 1) end)(cue s))



Just to clarify that Audacity never overwrites the original files when exporting as a chain, but if there was a crash there is always a small risk to a file that was open at the time.

If anyone wants a build of Audacity 1.3.14 alpha for Windows 2000 and later that will add installed Nyquist “process” effects to the list of Chain commands (the patch Steve referred to), you can send me a Private Message. The more feedback we get for it the better chance we can commit it for the first Beta after 2.0. Obviously anyone using an alpha build realises it is not an official release and that “Nyquist plug-ins in Chain commands” isn’t yet officially approved code.



Gale

With the patch you can use this plug-in to trim silences from start and end a lot more easily and reliably.

Thanks for all the great feedback here. I’m SURE that an easy “trim silence from start and end” in Audacity will be a hugely valuable plug-in for many users - its critical for getting the most out of samples and web audio (my example case is 600 short speech files for a game).

Would the Nyquist approach also allow a distortion or more complex echo effect to be included in a chain? That’s been another stumbling block with using Audacity for this project.

By the way I have been attempting this with Sound Stage from Logic Studio which I’d hoped might help but so far Audacity is beating it hands down! Sound Stage just crashes when you use it with Apple Scripts (its approach to batch processing).

The method described in my previous post should do a good job with that. Short files are trimmed very close to start/end of the audio. However if the files are in a compressed format such as MP3, note that:

  1. Audacity only works on uncompressed data, so the MP3 would be decoded (automatically) when it is imported. If you then either export as MP3, or export as WAV and re-encode to MP3 with an external encoder, there will be some sound quality loss when it is encoded. It is always best (whenever possible) to work with uncompressed audio (such as WAV files). Encoding to a lossy format (such as MP3, WMA, Ogg…) should be done as the final step, and it is usually best to keep an uncompressed (WAV) backup of the file just in case you ever want to do more editing.
  2. The MP3 format always adds a little bit of silence at the beginning of the file. This is a limitation of the MP3 format and makes MP3s unsuitable for looping. Ogg format offers similar quality as MP3 (often slightly better) and does not have this problem.


Unfortunately, without my patch (mentioned previously) you are limited to the effects that are listed in the Edit Chains dialogue, and this does not include reverb or other complex echo effects. (For “distortion” you could use the “Leveller” effect on a heavy setting). There are some possible workarounds:

  1. You set up a Chain with the Nyquist Prompt (as already described) with “code to apply reverb” in the Nyquist prompt.
    Note that the major limitation of using the Nyquist Prompt in a chain is that the code in the Nyquist Prompt is not saved in the Chain. When you close Audacity the code is gone. If you use the Nyquist Prompt outside of the Chain, the code that is used in the Chain will also change. The Nyquist Prompt in the Chain will use whatever code is currently in the Nyquist Prompt window, so that means that you cannot add two different instances of Nyquist code.
  2. You could use multiple instances of the (simple) Echo effect in the same chain with different settings each time to create a more complex echo effect.
  3. You could compile Audacity from the source code with the patch (mentioned previously) that adds support for Nyquist (not easy on Windows).
    This would allow you to add a Nyquist reverb effect to the Chain.
  4. You could modify the code of the Nyquist Reverb effect so that the ;control settings are predefined variables, and that the output is assigned to a local variable, then add the “Trim Silence” code after the Reverb code, replacing “s” with the local variable. (not easy without learning a bit about Nyquist programming).

Here is some code for you to play with (from "Reverb1) that adds reverb. On stereo tracks it will produce a stereo reverb.
The first 5 lines (before the License information) are the reverb settings. I think they are self explanatory, but only change the number in those first 5 lines, any other changes are likely to break the code.

(setq time 2.5) ;Reverberation time in seconds
(setq damping 50) ;High frequency damping (%)
(setq low-cut 200) ;Low cut filter (Hz)
(setq width 40) ;Stereo Width of reverb
(setq mix 20) ;Reverb mix proportion (%)

;; An implementation of reverb.lsp by Roger Dannenberg.
;; reverb1.ny by Steve Daulton. July 2011.
;; Released under terms of the GNU General Public License version 2:
;; http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 

;limit values to valid range
(setq time (max 0.1 time))
(setq damping (max 0 (min 1 (/ damping 100.0))))
(setq low-cut (max 0 (min (/ *sound-srate* 2.0) low-cut)))
(setq width (max 0 (min 1 (/ width 100.0))))
(setq mix (max 0 (min 1 (/ mix 100.0))))


;; create new Left channel
(defun Lsplit (leftch df)
  (let((f1 (lp leftch 300)) ;Common Sub below 300
        (f2 (feedback-delay(lp(hp leftch 300)600)(* df 0.2) 0)) ;Low on LEFT 300-600
        (f3 (lp(hp leftch 600)1200)) ;Common MID - 600-1200
        (f4 (feedback-delay(lp(hp leftch 1200)2400)(* df 0.08) 0)) ;High Mid on LEFT 1200-2400
        (f5 (hp  leftch 2400))) ;Common Presence
    (sim f1 f2 f3 f4 f5))) ;mix sounds
    
;; create new Right channel
(defun Rsplit (rightch df)
  (let((f1 (lp rightch 300)) ;Common Sub below 300
        (f2 (feedback-delay(lp(hp rightch 300)600)(* df 0.14) 0)) ;Low Mid on RIGHT 300-600 Hz
        (f3 (lp(hp rightch 600)1200)) ;Common MID - 600-1200
        (f4 (feedback-delay(lp(hp rightch 1200)2400)(* df 0.06) 0)) ;Treble on RIGHT 1200-2400
        (f5 (hp rightch 2400))) ;Common Presence
    (sim f1 f2 f3 f4 f5))) ;mix sounds

(defun stereo (s-in w)
	(let ((dlay (+(* w 0.5)0.2)))
		(if (arrayp s-in)
		(vector
			(sim 
				(mult w (Lsplit (aref s-in 0) dlay))
				(mult w -0.5 (Rsplit (aref s-in 1) dlay))
				(mult (- 1 w) (aref s-in 0)))
			(sim 
				(mult w -0.5 (Lsplit (aref s-in 0) dlay))
				(mult w (Rsplit (aref s-in 1) dlay))
				(mult (- 1 w)(aref s-in 1))))
		s-in)))


;; Reverb function by Roger Dannenberg
(defun reverb (x time) 
  (multichan-expand #'reverb-mono x time))

(defun reverb-mono (ga irevfactor)
  (let (sr ilowpass idel ihz icsc acomball allp1 allp2 allp3 alow allp4 allp5
        arevout)
    (setf sr (snd-srate ga))

    (setf ilowpass 9000.000)       ; frequency of lowpass filter

    (setf idel (list ; list of frequency/delay values
                    (/ 1237.000 sr) (/  1381.000 sr) (/ 1607.000 sr)
                    (/ 1777.000 sr) (/ 1949.000 sr) (/  2063.000 sr)
                    (/ 307.000 sr) (/ 97.000 sr) (/ 71.000 sr)
                    (/ 53.000 sr) (/ 47.000 sr) (/ 37.000 sr)
                    (/ 31.000 sr)))
    ; Nyquist's comb filter uses Hz rather than delay as parameter,
    ; so take reciprocals to get Hz:
    (setf ihz (mapcar #'/ idel))

    (setf icsc (list ; list of delay times
                    (* irevfactor 0.822) (* irevfactor 0.802)
                    (* irevfactor 0.773) (* irevfactor 0.753)
                    (* irevfactor 0.753) (* irevfactor 0.753)
                    (* irevfactor 0.7)))

    (setf acomball (sum
                        (comb ga (nth 0 icsc) (nth 0 ihz))
                        (comb ga (nth 1 icsc) (nth 1 ihz))
                        (comb ga (nth 2 icsc) (nth 2 ihz))
                        (comb ga (nth 3 icsc) (nth 3 ihz))
                        (comb ga (nth 4 icsc) (nth 4 ihz))
                        (comb ga (nth 5 icsc) (nth 5 ihz))))

    (setf allp1 (alpass acomball (nth 6 icsc) (nth 6 ihz)))
    (setf allp2 (alpass allp1 (nth 6 icsc) (nth 7 ihz)))
    (setf allp3 (alpass allp2 (nth 6 icsc) (nth 8 ihz)))
    (setf alow  (lp allp3 ilowpass))
    (setf allp4 (alpass alow (nth 6 icsc) (nth 9 ihz)))
    (setf allp5 (alpass allp4 (nth 6 icsc) (nth 11 ihz)))

    allp5))
;; End of Reverb function.

(defun damp (s-in val)
				;make a mono copy of stereo input
	(let* ((mono (if (arrayp s-in)(mult 0.5(sum (aref s-in 0)(aref s-in 1))) s-in))
				;clip to 0 dB
				(mono (clip mono 1))
				;upper limit of filter
				(fmax (* 0.5 *sound-srate*))
				;lower limit of filter
				(fmin (+ 100(*(- 1 val)(- 1 val)(- fmax 100))))
				;make envelope for low-pass filter 
				(env (sum fmin (mult(- fmax fmin)(snd-avg mono 100 100 op-peak)))))
		(lp (lp (lp s-in fmin) env) env)))


(sim 
	(mult (- 1 mix) s)
	(mult mix (- 0.35 (* time 0.02))
		(highpass2
			(damp
				(reverb (stereo s width) time)
			damping)
		low-cut)))

Also I note that we are still in the “Audacity 1.2.x for Windows” section, but Chains are not available for Audacity 1.2.
I’ll move this to the Audio Processing section.

I need to do exactly what the original post says but this seems to have been lost many updates ago. Is there anyway to do this now?

In Audacity 2.3.0, “Chains” are now called “Macros”. See: https://manual.audacityteam.org/man/macros.html
There is now a Nyquist plug-in available for trimming silence / near silence from the start and/or end of the track: https://wiki.audacityteam.org/wiki/Nyquist_Effect_Plug-ins#Trim_Silence

If you have further questions about this, please start a new topic. As you point out, much of the information in this topic is out of date by many years.