### 28.2.10 Trimming silence

You can find leading and/or trailing silence and remove it by using the trim command (see Section 5.2.5, Section 21.2.2 and Section 28.1.9 for other usages of trim).

- trim takes one mandatory argument and up to four optional arguments:
  - A, an audio clip object.
  - Optionally, threshold, a negative real number specifying the silence threshold in dB (by default, threshold=−30).
  - Optionally, mavglen, a nonnegative real number specifying the time interval (in seconds) for a moving-average filter (by default, mavglen=0.001).
  - Optionally, either left or right, the symbol specifiying one-sided trimming.
  - Optionally, index, the symbol.
- trim(A ⟨,threshold ⟨,mavglen ⟩ ⟩ ⟨,left|right ⟩ ⟨,index ⟩) finds the start l of the first nonsilent portion of A and the start u of the trailing silence (in samples). Audio is considered silent at offset k if the instantaneous energy at k is below threshold decibels. The energy vector is smoothed out by applying a moving-average filter with length n=⌊ samplerate(A)·mavglen+1/2⌋ (i.e. the length in samples corresponding to mavglen seconds). To disable the filtering, set mavglen to 0.

  If index is given, then the return value is either l if left is given, u if right is given, or (l,u−l) otherwise (the last sequence contains the start and length of the nonsilent part, in samples). If index is omitted, then the return value is an audio clip obtained from A by removing either the leading silence if left is given, trailing silence if right is given, or both leading and trailing silence otherwise.

#### Example

With the file gs-16b-2c-44100hz.wav downloaded from here (an excerpt from a piece of band music):

clip1:=readwav("/home/luka/Downloads/gs-16b-2c-44100hz.wav")

a sound clip with 698194 samples at 44100 Hz (16 bit, stereo) |  |  |  |  |  |  |  |  |  |

plotwav(clip1)

clip2:=trim(clip1)

a sound clip with 538796 samples at 44100 Hz (16 bit, stereo) |  |  |  |  |  |  |  |  |  |

plotwav(clip2)