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.)
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.
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.
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.
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.
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):
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.
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).
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.
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:
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.
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:
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.
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.
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.
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:
;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)
(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)))
(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))))
;; 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
(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)))
;; 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)))
(mult (- 1 mix) s)
(mult mix (- 0.35 (* time 0.02))
(reverb (stereo s width) time)
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.