2

I am trying to delete silence from an audio file using Audacity. There is a Nyquist plugin called Trim Silence which deletes silence from the start and end of a file, but not the middle. I would like to invert this, and delete silence from everywhere except the start and end.

I think the function below is the relevant part of the plugin. How should I change it to get a truncate-internal-silences function? (I don't know any Nyquist, or Lisp, so I'm struggling to understand what it currently does, let alone change it.)

Entirely different approaches also welcome - this is just my current best guess at how to edit my many audio files.

(defun trim-silence ()
  ;; Nyquist plug-ins cannot return 'no audio', so trap as error.
  (if (< (get '*selection* 'peak-level) threshold)
      (throw 'error (format nil "Error.~%All selected audio in the ~a selected track~%~
                                is below the silence threshold.~%~%~
                                Try setting the threshold to a~%~
                                lower (more negative) dB level."
                                (add-num-suffix (get '*track* 'index)))))
  (if (> len (* limit *sound-srate*)) ;max length in samples
      (throw 'error (format nil "Error.\nMax RAM usage by Trim Silence is set to ~a GB.~%This allows a maximum duration ~
                                for a ~a~%track at ~a Hz of ~a.~%Selected track is ~a.~%"
                                RAM-limit
                                (if (arrayp *track*) "stereo" "mono")
                                (round *sound-srate*)
                                (to-hhmmss limit)
                                (to-hhmmss (get-duration 1)))))
  (let* (;; ratio provides tighter trimming for short selections
         ;; while maintaining reasonable speed for long selections
         (ratio (max 10 (min 200 (round (/ len 100000.0)))))
         (my-srate (/ *sound-srate* ratio))
         (mysound (convert *track* ratio))
         (limits (get-clip-limits))   ; (list start, end) times of audio clips
         (clip-start (if (first limits)
                         (abs-to-relative-time (nth 0 limits))))  ; nil id invalid
         (clip-end (if (second limits)
                       (abs-to-relative-time (nth 1 limits)))))  ; nil if invalid 
    ;loop through samples and mark start and end
    (setf result (find-sil mysound clip-start clip-end))
    (let ((start (if clip-start
                     (max clip-start
                          (- (/ (first result) my-srate) min-start-silence))
                      0))
          (end (if clip-end
                   (min (+ (- (get-duration 1) (/ (second result) my-srate))
                           min-end-silence)
                        clip-end)
                   (get '*selection* 'end))))
      ;; ensure at least 1 sample remains
      ;; This should never happen.
      (if (>= start end)
        (setq start (- end (/ *sound-srate*))))
      ; trim
      (multichan-expand #'extract-abs start end (cue *track*)))))
Ollyver
  • 359
  • 2
  • 14

0 Answers0