Trimming the middle part of the quiet segments so that the quiet segments do not exceed 1.5 seconds

Hi!

I’m trying (unsuccessfully) to create a Nyquist plugin which would trim the middle part of the quiet segments so that the quiet segments do not exceed 1.5 seconds. (Trimming the middle part is useful when the start/end of the “silent” segment may contain actual sound)

The output is the original length of sound (for some reason, not trimmed quiet parts), but the output is not original sound – it is -74…-71dB white noise.
Cannot figure out what is wrong with it :frowning:

Any help appreciated!

EDIT: because there were no comments yet I made some corrections (Nov-24)

(setq threshold -45)
(setq segment-length 0.1)
(defun peak-amp (sound) (linear-to-db (peak sound ny:all))) 

(defun process-track (track)
  (setf length-in-samples (snd-length track ny:all))
  (setf duration (/ length-in-samples *sound-srate*))
  (let ((processed-track (s-rest 1))
        (segments (make-array 16)))
    (dotimes (i 16)
      (setf (aref segments i) (s-rest segment-length)))
    (do ((i 0 (+ i 1)))
        ((>= (* i segment-length) duration))
      (let ((start-time (* i segment-length))
            (stop-time (* (+ i 1) segment-length)))
        (let ((current-segment (extract start-time stop-time track)))
          (setf (aref segments 0) current-segment)
          (let ((all-below-threshold t))
            (if (> (peak-amp (aref segments 15)) threshold) (setf all-below-threshold nil))
            (if (> (peak-amp (aref segments 14)) threshold) (setf all-below-threshold nil))
            (if (> (peak-amp (aref segments 13)) threshold) (setf all-below-threshold nil))
            (if (> (peak-amp (aref segments 12)) threshold) (setf all-below-threshold nil))
            (if (> (peak-amp (aref segments 11)) threshold) (setf all-below-threshold nil))
            (if (> (peak-amp (aref segments 10)) threshold) (setf all-below-threshold nil))
            (if (> (peak-amp (aref segments 9)) threshold) (setf all-below-threshold nil))
            (if (> (peak-amp (aref segments 8)) threshold) (setf all-below-threshold nil))
            (if (> (peak-amp (aref segments 7)) threshold) (setf all-below-threshold nil))
            (if (> (peak-amp (aref segments 6)) threshold) (setf all-below-threshold nil))
            (if (> (peak-amp (aref segments 5)) threshold) (setf all-below-threshold nil))
            (if (> (peak-amp (aref segments 4)) threshold) (setf all-below-threshold nil))
            (if (> (peak-amp (aref segments 3)) threshold) (setf all-below-threshold nil))
            (if (> (peak-amp (aref segments 2)) threshold) (setf all-below-threshold nil))
            (if (> (peak-amp (aref segments 1)) threshold) (setf all-below-threshold nil))
            (if (> (peak-amp (aref segments 0)) threshold) (setf all-below-threshold nil))
            (if all-below-threshold
              (progn
                (setf (aref segments 7) (aref segments 6))
                (setf (aref segments 6) (aref segments 5))
                (setf (aref segments 5) (aref segments 4))
                (setf (aref segments 4) (aref segments 3))
                (setf (aref segments 3) (aref segments 2))
                (setf (aref segments 2) (aref segments 1))
                (setf (aref segments 1) (aref segments 0)))
              (progn
                (setf processed-track (seq processed-track (aref segments 15)))
                (setf (aref segments 15) (aref segments 14))
                (setf (aref segments 14) (aref segments 13))
                (setf (aref segments 13) (aref segments 12))
                (setf (aref segments 12) (aref segments 11))
                (setf (aref segments 11) (aref segments 10))
                (setf (aref segments 10) (aref segments 9))
                (setf (aref segments 9) (aref segments 8))
                (setf (aref segments 8) (aref segments 7))
                (setf (aref segments 7) (aref segments 6))
                (setf (aref segments 6) (aref segments 5))
                (setf (aref segments 5) (aref segments 4))
                (setf (aref segments 4) (aref segments 3))
                (setf (aref segments 3) (aref segments 2))
                (setf (aref segments 2) (aref segments 1))
                (setf (aref segments 1) (aref segments 0))
              )
            )
          )
        )
      )
    )
  processed-track)
)
(setf *track* (process-track *track*))