home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 February / MAC_easy_02_2010.iso / Software / Multimedia / audacity-macosx-ub-1.3.11.dmg / plug-ins / SilenceMarker.ny < prev    next >
Encoding:
Audacity Nyquits plug-in  |  2010-01-16  |  4.5 KB  |  108 lines

  1. ;nyquist plug-in
  2. ;version 1
  3. ;type analyze
  4. ;categories "http://lv2plug.in/ns/lv2core#AnalyserPlugin"
  5. ;name "Silence Finder..."
  6. ;action "Finding silence..."
  7. ;info "Written by Alex S. Brown, PMP (http://www.alexsbrown.com) \nReleased under terms of the GNU General Public License version 2\nAdds point labels in areas of silence according to the specified\nlevel and duration of silence. If too many silences are detected,\nincrease the silence level and duration; if too few are detected,\nreduce the level and duration."
  8. ;control sil-lev "Treat audio below this level as silence [ -dB]" real "" 26 0 100
  9. ;control sil-dur "Minimum duration of silence [seconds]" real "" 1.0 0.1 5.0
  10. ;control labelbeforedur "Label placement [seconds before silence ends]" real "" 0.3 0.0 1.0
  11.  
  12. ;Create a function to make the sum the two channels if they are stereo
  13. (defun mono-s (s-in) (if (arrayp s-in) (snd-add (aref s-in 0) (aref s-in 1))
  14. s-in))
  15.  
  16. ;Create a function to reduce the sample rate and prepare the signal for
  17. ;analysis. RMS is good to monitor volume the way humans hear it, but is not
  18. ;available in Audacity. Used a peak-calculating function instead.
  19. ;NOTE: this is the place to add any processing to improve the quality of the
  20. ;signal. Noise filters could improve the quality of matches for noisy signals.
  21. ;PERFORMANCE vs. ACCURACY
  22. ;Reducing the samples per second should improve the performance and decrease
  23. ;the accuracy of the labels. Increasing the samples per second will do the
  24. ;opposite. The more samples checked, the longer it takes. The more samples
  25. ;checked, the more precisely the program can place the silence labels.
  26. ;my-srate-ratio determines the number of samples in my-s. Set the number after (snd-srate s)
  27. ;higher to increase the number of samples.
  28.  
  29. (defun my-s (s-in)
  30.  (setq my-srate-ratio (truncate (/ (snd-srate (mono-s s-in)) 100)))
  31.  (snd-avg (mono-s s-in) my-srate-ratio my-srate-ratio OP-PEAK)
  32. )
  33.  
  34. ;Set the silence threshold level (convert it to a linear form)
  35. (setq thres (db-to-linear (* -1 sil-lev)))
  36. ;Store the sample rate of the sound
  37. (setq s1-srate (snd-srate (my-s s)))
  38. ;Initialize the variable that will hold the length of the sound.
  39. ;Do not calculate it now with snd-length, because it would waste memory.
  40. ;We will calculate it later.
  41. (setq s1-length 0)
  42. ;Initialize the silence counter and the labels variable
  43. (setq sil-c 0)
  44. (setq l NIL)
  45. ;Convert the silence duration in seconds to a length in samples
  46. (setq sil-length (* sil-dur s1-srate))
  47.  
  48. ;Define a function to add new items to the list of labels
  49. (defun add-label (l-time l-text)
  50.  (setq l (cons (list l-time l-text) l))
  51. )
  52.  
  53. ;The main working part of the program, it counts
  54. ;the number of sequential samples with volume under
  55. ;the threshold. It adds to a list of markers ever time
  56. ;there is a longer period of silence than the silence
  57. ;duration amount.
  58.  
  59. ;It runs through a loop, adding to the list of markers (l)
  60. ;each time it finds silence.
  61. (let (s1) ;Define s1 as a local variable to allow efficient memory use
  62.  ; Get the sample into s1, then free s to save memory
  63.  (setq s1 (my-s s))
  64.  (setq s nil)
  65.  ;Capture the result of this "do" loop, because we need the sountd's legnth
  66.  ;in samples.
  67.  (setq s1-length
  68.   ;Keep repeating, incrementing the counter and getting another sample
  69.   ;each time through the loop.
  70.   (do ((n 1 (+ n 1)) (v (snd-fetch s1) (setq v (snd-fetch s1))))
  71.    ;Exit when we run out of samples (v is nil) and return the number of
  72.    ;samples processed (n)
  73.    ((not v) n)
  74.    ;Start the execution part of the do loop
  75.    ;if found silence, increment the silence counter
  76.    (if (< v thres) (setq sil-c (+ sil-c 1)))
  77.  
  78.    ;If this sample is NOT silent and the previous samples were silent
  79.    ;then mark the passage.
  80.    (if (and (> v thres) (> sil-c sil-length))
  81.        ;Mark the user-set number of seconds BEFORE this point to avoid clipping the start
  82.        ;of the material.
  83.     (add-label (- (/ n s1-srate) labelbeforedur) "S")
  84.    )
  85.    ;If this sample is NOT silent, then reset the silence counter
  86.    (if (> v thres)
  87.     (setq sil-c 0)
  88.    )
  89.   )
  90.  )
  91. )
  92.  
  93. ;Check for a long period of silence at the end
  94. ;of the sample. If so, then mark it.
  95. (if (> sil-c sil-length)
  96.  ;If found, add a label
  97.  ;Label time is the time the silence began plus the silence duration target
  98.  ;amount. We calculate the time the silence began as the end-time minus the
  99.  ;final value of the silence counter
  100.  (add-label (+ (/ (- s1-length sil-c) s1-srate) sil-dur) "S")
  101. )
  102.  
  103. ;If no silence markers were found, return a message
  104. (if (null l)
  105.  (setq l "No silences found. Try reducing the silence\nlevel and minimum silence duration.")
  106. )
  107. l
  108.