;nyquist plug-in ;version 4 ;type analyze ;name "Silence Finder" ;action "Finding silence..." ;author "Alex S. Brown and Steve Daulton" ;release 3.0.2-0 ;copyright "Released under terms of the GNU General Public License version 2" ;; This version by Steve Daulton (https://audionyq.com). ;; Based on SilenceMarker by Alex S. Brown, PMP (http://www.alexsbrown.com) ;; ;; Adds point labels in areas of silence according to the specified ;; level and minimum duration of silence. ;; If too many silences are detected, lower the ;; silence level or increase the minimum duration. ;; If too few are detected, increase the level or ;; reduce the minimum duration. ;; Released under terms of the GNU General Public License version 2 or later. ;; http://www.gnu.org/licenses/old-licenses/gpl-2.0.html ;control sil-lev "Treat audio below this level as silence [dB]" real "" -26 -100 0 ;control sil-dur "Minimum duration of silence [seconds]" real "" 1.0 0.1 5.0 ;control labelbeforedur "Label placement [seconds before silence ends]" real "" 0.3 0.0 1.0 (setf thresh (db-to-linear sil-lev)) ;"labels" is an empty list with global scope. (setf labels ()) ;;; If stereo, convert to mono. (defun to-mono (sig) (if (arrayp sig) (s-max (snd-abs (aref sig 0)) (snd-abs (aref sig 1))) sig)) ;;; Reduce sample rate to about 100 Hz. ;;; Note: If the original sample rate is not divisible ;;; by 100, the new sample rate will not be exactly 100 Hz. ;;; Less samples require less processing time but ;;; reduces accuracy. (defun reduce-rate (sig) (let* ((target-rate 100) (step (truncate (/ (snd-srate (to-mono sig)) 100)))) (snd-avg (to-mono sig) step step OP-PEAK))) ;;; Add label to 'labels'. (defun add-label (samples rate) (let ((time (/ samples rate))) (push (list time "S") labels))) ;; s1 created as local variable to allow garbage collection. (let ((s1 (reduce-rate *track*)) (sil-count 0) (count 0)) (setf *track* nil) ;To allow samples to be released from RAM. (setf s1-rate (snd-srate s1)) ;may not be exactly 100 Hz. (setf min-dur (* sil-dur s1-rate)) ;minimum silence duration in samples. (setf offset (* -1 labelbeforedur s1-rate)) ;offset in samples ;; Main loop counts the number of samples below the ;; threshold, and adds a label whenever the count ;; exceeds the specified minimum silence duration. (do ((val (snd-fetch s1) (snd-fetch s1))) ((not val)) ;return when no more samples. (cond ((< val thresh) (incf sil-count)) ;; Now above silence threshold level. ;; If silence count > min-dur, add label and reset sil-count, ;; else just reset sil-count. (t (when (> sil-count min-dur) (add-label (+ count offset) s1-rate)) (setf sil-count 0))) (incf count)) ;; If selection ends in silence: (when (> sil-count min-dur) (setf count (- count sil-count)) ;end of last sound. ;; Add label "Minimum silence length" after sound. (add-label (+ count min-dur) s1-rate)) ;; If no silence markers were found, return a message (if labels labels (format nil "No silences found.~%~ Try raising the silence level or~%~ reducing the minimum silence duration.")))