home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / audacity / nyquist / dspprims.lsp < prev    next >
Encoding:
Lisp/Scheme  |  2010-09-21  |  17.6 KB  |  578 lines

  1. ;; dspprims.lsp -- interface to dsp primitives
  2.  
  3. ;; ARESON - notch filter
  4. ;; 
  5. (defun areson (s c b &optional (n 0))
  6.   (multichan-expand #'nyq:areson s c b n))
  7.  
  8. (setf areson-implementations
  9.       (vector #'snd-areson #'snd-aresonvc #'snd-aresoncv #'snd-aresonvv))
  10.  
  11. ;; NYQ:ARESON - notch filter, single channel
  12. ;;
  13. (defun nyq:areson (signal center bandwidth normalize)
  14.   (select-implementation-1-2 areson-implementations 
  15.    signal center bandwidth normalize))
  16.  
  17.  
  18. ;; hp - highpass filter
  19. ;; 
  20. (defun hp (s c)
  21.   (multichan-expand #'nyq:hp s c))
  22.  
  23. (setf hp-implementations
  24.       (vector #'snd-atone #'snd-atonev))
  25.  
  26. ;; NYQ:hp - highpass filter, single channel
  27. ;;
  28. (defun nyq:hp (s c)
  29.   (select-implementation-1-1 hp-implementations s c))
  30.  
  31.  
  32. ;; comb-delay-from-hz -- compute the delay argument
  33. ;;
  34. (defun comb-delay-from-hz (hz caller)
  35.   (recip hz))
  36.  
  37. ;; comb-feedback-from-decay -- compute the feedback argument
  38. ;;
  39. (defun comb-feedback (decay delay)
  40.   (s-exp (mult -6.9087 delay (recip decay))))
  41.  
  42. ;; COMB - comb filter
  43. ;; 
  44. ;; this is just a feedback-delay with different arguments
  45. ;;
  46. (defun comb (snd decay hz)
  47.   (multichan-expand #'nyq:comb snd decay hz))
  48.  
  49. (defun nyq:comb (snd decay hz)
  50.   (let (delay feedback len d)
  51.     ; convert decay to feedback, iterate over array if necessary
  52.     (setf delay (comb-delay-from-hz hz "comb"))
  53.     (setf feedback (comb-feedback decay delay))
  54.     (nyq:feedback-delay snd delay feedback)))
  55.  
  56. ;; ALPASS - all-pass filter
  57. ;; 
  58. (defun alpass (snd decay hz &optional min-hz)
  59.   (multichan-expand #'nyq:alpass snd decay hz min-hz))
  60.   
  61.  
  62.  
  63. (defun nyq:alpass (snd decay hz min-hz)
  64.   (let (delay feedback len d)
  65.     ; convert decay to feedback, iterate over array if necessary
  66.     (setf delay (comb-delay-from-hz hz "alpass"))
  67.     (setf feedback (comb-feedback decay delay))
  68.     (nyq:alpass1 snd delay feedback min-hz)))
  69.  
  70.  
  71. ;; CONST -- a constant at control-srate
  72. ;;
  73. (defun const (value &optional (dur 1.0))
  74.   (let ((d (get-duration dur)))
  75.     (snd-const value *rslt* *CONTROL-SRATE* d)))
  76.  
  77.  
  78. ;; CONVOLVE - slow convolution
  79. ;; 
  80. (defun convolve (s r)
  81.   (multichan-expand #'snd-convolve s r))
  82.  
  83.  
  84. ;; FEEDBACK-DELAY -- (delay is quantized to sample period)
  85. ;;
  86. (defun feedback-delay (snd delay feedback)
  87.   (multichan-expand #'nyq:feedback-delay snd delay feedback))
  88.   
  89.  
  90. ;; SND-DELAY-ERROR -- report type error
  91. ;;
  92. (defun snd-delay-error (snd delay feedback)
  93.   (error "feedback-delay with variable delay is not implemented"))
  94.  
  95.  
  96. ;; NYQ::DELAYCV -- coerce sample rates and call snd-delaycv
  97. ;;
  98. (defun nyq:delaycv (the-snd delay feedback)
  99.   (display "delaycv" the-snd delay feedback)
  100.   (let ((the-snd-srate (snd-srate the-snd))
  101.         (feedback-srate (snd-srate feedback)))
  102.     (cond ((> the-snd-srate feedback-srate)
  103.            (setf feedback (snd-up the-snd-srate feedback)))
  104.           ((< the-snd-srate feedback-srate)
  105.            (format t "Warning: down-sampling feedback in feedback-delay/comb~%")
  106.            (setf feedback (snd-down the-snd-srate feedback))))
  107.     (snd-delaycv the-snd delay feedback)))
  108.  
  109. (setf feedback-delay-implementations
  110.       (vector #'snd-delay #'snd-delay-error #'nyq:delaycv #'snd-delay-error))
  111.  
  112.  
  113. ;; NYQ:FEEDBACK-DELAY -- single channel delay
  114. ;;
  115. (defun nyq:feedback-delay (snd delay feedback)
  116.   (select-implementation-1-2 feedback-delay-implementations 
  117.                              snd delay feedback))
  118.  
  119.  
  120. ;; SND-ALPASS-ERROR -- report type error
  121. ;;
  122. (defun snd-alpass-error (snd delay feedback)
  123.   (error "alpass with constant decay and variable hz is not implemented"))
  124.  
  125.  
  126. (if (not (fboundp 'snd-alpasscv))
  127.     (defun snd-alpasscv (snd delay feedback min-hz)
  128.       (error "snd-alpasscv (ALPASS with variable decay) is not implemented")))
  129. (if (not (fboundp 'snd-alpassvv))
  130.     (defun snd-alpassvv (snd delay feedback min-hz)
  131.       (error "snd-alpassvv (ALPASS with variable decay and feedback) is not implemented")))
  132.       
  133. (defun snd-alpass-4 (snd delay feedback min-hz)
  134.     (snd-alpass snd delay feedback))
  135.     
  136.  
  137. (defun snd-alpasscv-4 (the-snd delay feedback min-hz)
  138.     (display "snd-alpasscv-4" (snd-srate the-snd) (snd-srate feedback))
  139.     (let ((the-snd-srate (snd-srate the-snd))
  140.           (feedback-srate (snd-srate feedback)))
  141.       (cond ((> the-snd-srate feedback-srate)
  142.              (setf feedback (snd-up the-snd-srate feedback)))
  143.             ((< the-snd-srate feedback-srate)
  144.              (format t "Warning: down-sampling feedback in alpass~%")
  145.              (setf feedback (snd-down the-snd-srate feedback))))
  146.       ;(display "snd-alpasscv-4 after cond" (snd-srate the-snd) (snd-srate feedback))
  147.       (snd-alpasscv the-snd delay feedback)))
  148.  
  149.     
  150. (defun snd-alpassvv-4 (the-snd delay feedback min-hz)
  151.     ;(display "snd-alpassvv-4" (snd-srate the-snd) (snd-srate feedback))
  152.     (let ((the-snd-srate (snd-srate the-snd))
  153.           (delay-srate (snd-srate delay))
  154.           (feedback-srate (snd-srate feedback))
  155.           max-delay)
  156.       (cond ((or (not (numberp min-hz))
  157.                  (<= min-hz 0))
  158.              (error "alpass needs numeric (>0) 4th parameter (min-hz) when delay is variable")))
  159.       (setf max-delay (/ 1.0 min-hz))
  160.       ; make sure delay is between 0 and max-delay
  161.       ; use clip function, which is symetric, with an offset
  162.       (setf delay (snd-offset (clip (snd-offset delay (* max-delay -0.5))
  163.                                     (* max-delay 0.5))
  164.                               (* max-delay 0.5)))
  165.       ; now delay is between 0 and max-delay, so we won't crash nyquist when
  166.       ; we call snd-alpassvv, which doesn't test for out-of-range data
  167.       (cond ((> the-snd-srate feedback-srate)
  168.              (setf feedback (snd-up the-snd-srate feedback)))
  169.             ((< the-snd-srate feedback-srate)
  170.              (format t "Warning: down-sampling feedback in alpass~%")
  171.              (setf feedback (snd-down the-snd-srate feedback))))
  172.       (cond ((> the-snd-srate delay-srate)
  173.              (setf delay (snd-up the-snd-srate delay)))
  174.             ((< the-snd-srate delay-srate)
  175.              (format t "Warning: down-sampling delay in alpass~%")
  176.              (setf delay (snd-down the-snd-srate delay))))
  177.       (display "snd-alpassvv-4 after cond" (snd-srate the-snd) (snd-srate feedback))
  178.       (snd-alpassvv the-snd delay feedback max-delay)))
  179.  
  180. (setf alpass-implementations
  181.       (vector #'snd-alpass-4 #'snd-alpass-error
  182.               #'snd-alpasscv-4 #'snd-alpassvv-4))
  183.  
  184.  
  185.  
  186. ;; NYQ:ALPASS1 -- single channel alpass
  187. ;;
  188. (defun nyq:alpass1 (snd delay feedback min-hz)
  189.   (select-implementation-1-2 alpass-implementations
  190.                              snd delay feedback min-hz))
  191.  
  192. ;; CONGEN -- contour generator, patterned after gated analog env gen
  193. ;;
  194. (defun congen (gate rise fall) (multichan-expand #'snd-congen gate rise fall))
  195.  
  196.  
  197. ;; S-EXP -- exponentiate a sound
  198. ;;
  199. (defun s-exp (s) (multichan-expand #'nyq:exp s))
  200.  
  201.  
  202. ;; NYQ:EXP -- exponentiate number or sound
  203. ;;
  204. (defun nyq:exp (s) (if (soundp s) (snd-exp s) (exp s)))
  205.  
  206. ;; S-ABS -- absolute value of a sound
  207. ;;
  208. (defun s-abs (s) (multichan-expand #'nyq:abs s))
  209.  
  210. ;; NYQ:ABS -- absolute value of number or sound
  211. ;;
  212. (defun nyq:abs (s) (if (soundp s) (snd-abs s) (abs s)))
  213.  
  214. ;; S-SQRT -- square root of a sound
  215. ;;
  216. (defun s-sqrt (s) (multichan-expand #'nyq:sqrt s))
  217.  
  218. ;; NYQ:SQRT -- square root of a number or sound
  219. ;;
  220. (defun nyq:sqrt (s) (if (soundp s) (snd-sqrt s) (sqrt s)))
  221.  
  222.  
  223. ;; INTEGRATE -- integration
  224. ;;
  225. (defun integrate (s) (multichan-expand #'snd-integrate s))
  226.  
  227.  
  228. ;; S-LOG -- natural log of a sound
  229. ;;
  230. (defun s-log (s) (multichan-expand #'nyq:log s))
  231.  
  232.  
  233. ;; NYQ:LOG -- log of a number or sound
  234. ;;
  235. (defun nyq:log (s) (if (soundp s) (snd-log s) (log s)))
  236.  
  237.  
  238. ;; NOISE -- white noise
  239. ;;
  240. (defun noise (&optional (dur 1.0))
  241.   (let ((d (get-duration dur)))
  242.     (snd-white *rslt* *SOUND-SRATE* d)))
  243.  
  244.  
  245. (defun noise-gate (snd &optional (lookahead 0.5) (risetime 0.02) (falltime 0.5)
  246.                                                  (floor 0.01) (threshold 0.01))
  247.   (let ((rms (lp (mult snd snd) (/ *control-srate* 10.0))))
  248.     (setf threshold (* threshold threshold))
  249.     (mult snd (gate rms lookahead risetime falltime floor threshold))))
  250.  
  251.  
  252. ;; QUANTIZE -- quantize a sound
  253. ;;
  254. (defun quantize (s f) (multichan-expand #'snd-quantize s f))
  255.  
  256.  
  257. ;; RECIP -- reciprocal of a sound
  258. ;;
  259. (defun recip (s) (multichan-expand #'nyq:recip s))
  260.  
  261.  
  262. ;; NYQ:RECIP -- reciprocal of a number or sound
  263. ;;
  264. (defun nyq:recip (s) (if (soundp s) (snd-recip s) (/ (float s))))
  265.  
  266. ;; RMS -- compute the RMS of a sound
  267. ;;
  268. (defun rms (s &optional (rate 100.0) window-size)
  269.   (let (rslt step-size)
  270.     (cond ((not (eq (type-of s) 'SOUND))
  271.        (break "in RMS, first parameter must be a monophonic SOUND")))
  272.     (setf step-size (round (/ (snd-srate s) rate)))
  273.     (cond ((null window-size)
  274.                (setf window-size step-size)))
  275.     (setf s (prod s s))
  276.     (setf result (snd-avg s window-size step-size OP-AVERAGE))
  277.         ;; compute square root of average
  278.         (s-exp (scale 0.5 (s-log result)))))
  279.  
  280.  
  281. ;; RESON - bandpass filter
  282. ;; 
  283. (defun reson (s c b &optional (n 0))
  284.   (multichan-expand #'nyq:reson s c b n))
  285.  
  286. (setf reson-implementations
  287.       (vector #'snd-reson #'snd-resonvc #'snd-resoncv #'snd-resonvv))
  288.  
  289. ;; NYQ:RESON - bandpass filter, single channel
  290. ;;
  291. (defun nyq:reson (signal center bandwidth normalize)
  292.   (select-implementation-1-2 reson-implementations 
  293.    signal center bandwidth normalize))
  294.  
  295.  
  296. ;; SHAPE -- waveshaper
  297. ;;
  298. (defun shape (snd shape origin)
  299.   (multichan-expand #'snd-shape snd shape origin))
  300.  
  301.  
  302. ;; SLOPE -- calculate the first derivative of a signal
  303. ;;
  304. (defun slope (s) (multichan-expand #'nyq:slope s))
  305.  
  306.  
  307. ;; NYQ:SLOPE -- first derivative of single channel
  308. ;;
  309. (defun nyq:slope (s)
  310.   (let* ((sr (snd-srate s))
  311.          (sr-inverse (/ sr)))
  312.     (snd-xform (snd-slope s) sr 0 sr-inverse MAX-STOP-TIME 1.0)))
  313.  
  314.  
  315. ;; lp - lowpass filter
  316. ;; 
  317. (defun lp (s c)
  318.   (multichan-expand #'nyq:lp s c))
  319.  
  320. (setf lp-implementations
  321.       (vector #'snd-tone #'snd-tonev))
  322.  
  323. ;; NYQ:lp - lowpass filter, single channel
  324. ;;
  325. (defun nyq:lp (s c)
  326.   (select-implementation-1-1 lp-implementations s c))
  327.  
  328.  
  329.  
  330. ;;; fixed-parameter filters based on snd-biquad
  331. ;;; note: snd-biquad is implemented in biquadfilt.[ch],
  332. ;;; while BiQuad.{cpp,h} is part of STK
  333.  
  334. (setf Pi 3.14159265358979)
  335.  
  336. (defun square (x) (* x x))
  337. (defun sinh (x) (* 0.5 (- (exp x) (exp (- x)))))
  338.  
  339.  
  340. ; remember that snd-biquad uses the opposite sign convention for a_i's 
  341. ; than Matlab does.
  342.  
  343. ; convenient biquad: normalize a0, and use zero initial conditions.
  344. (defun nyq:biquad (x b0 b1 b2 a0 a1 a2)
  345.   (let ((a0r (/ 1.0 a0)))
  346.     (snd-biquad x (* a0r b0) (* a0r b1) (* a0r b2) 
  347.                              (* a0r a1) (* a0r a2) 0 0)))
  348.  
  349.  
  350. (defun biquad (x b0 b1 b2 a0 a1 a2)
  351.   (multichan-expand #'nyq:biquad x b0 b1 b2 a0 a1 a2))
  352.  
  353.  
  354. ; biquad with Matlab sign conventions for a_i's.
  355. (defun biquad-m (x b0 b1 b2 a0 a1 a2)
  356.   (multichan-expand #'nyq:biquad-m x b0 b1 b2 a0 a1 a2))
  357.  
  358. (defun nyq:biquad-m (x b0 b1 b2 a0 a1 a2)
  359.   (nyq:biquad x b0 b1 b2 a0 (- a1) (- a2)))
  360.  
  361. ; two-pole lowpass
  362. (defun lowpass2 (x hz &optional (q 0.7071))
  363.   (multichan-expand #'nyq:lowpass2 x hz q))
  364.  
  365. ;; NYQ:LOWPASS2 -- operates on single channel
  366. (defun nyq:lowpass2 (x hz q)
  367.   (let* ((w (* 2.0 Pi (/ hz (snd-srate x))))
  368.          (cw (cos w))
  369.          (sw (sin w))
  370.          (alpha (* sw (sinh (/ 0.5 q))))
  371.          (a0 (+ 1.0 alpha))
  372.          (a1 (* -2.0 cw))
  373.          (a2 (- 1.0 alpha))
  374.          (b1 (- 1.0 cw))
  375.          (b0 (* 0.5 b1))
  376.          (b2 b0))
  377.     (nyq:biquad-m x b0 b1 b2 a0 a1 a2)))
  378.  
  379. ; two-pole highpass
  380. (defun highpass2 (x hz &optional (q 0.7071))
  381.   (multichan-expand #'nyq:highpass2 x hz q))
  382.  
  383. (defun nyq:highpass2 (x hz q)
  384.   (let* ((w (* 2.0 Pi (/ hz (snd-srate x))))
  385.          (cw (cos w))
  386.          (sw (sin w))
  387.          (alpha (* sw (sinh (/ 0.5 q))))
  388.          (a0 (+ 1.0 alpha))
  389.          (a1 (* -2.0 cw))
  390.          (a2 (- 1.0 alpha))
  391.          (b1 (- -1.0 cw))
  392.          (b0 (* -0.5 b1))
  393.          (b2 b0))
  394.     (nyq:biquad-m x b0 b1 b2 a0 a1 a2)))
  395.  
  396. ; two-pole bandpass.  max gain is unity.
  397. (defun bandpass2 (x hz q)
  398.   (multichan-expand #'nyq:bandpass2 x hz q))
  399.  
  400. (defun nyq:bandpass2 (x hz q)
  401.   (let* ((w (* 2.0 Pi (/ hz (snd-srate x))))
  402.          (cw (cos w))
  403.          (sw (sin w))
  404.          (alpha (* sw (sinh (/ 0.5 q))))
  405.          (a0 (+ 1.0 alpha))
  406.          (a1 (* -2.0 cw))
  407.          (a2 (- 1.0 alpha))
  408.          (b0 alpha)
  409.          (b1 0.0)
  410.          (b2 (- alpha)))
  411.     (nyq:biquad-m x b0 b1 b2 a0 a1 a2)))
  412.  
  413. ; two-pole notch.
  414. (defun notch2 (x hz q)
  415.   (multichan-expand #'nyq:notch2 x hz q))
  416.  
  417. (defun nyq:notch2 (x hz q)
  418.   (let* ((w (* 2.0 Pi (/ hz (snd-srate x))))
  419.          (cw (cos w))
  420.          (sw (sin w))
  421.          (alpha (* sw (sinh (/ 0.5 q))))
  422.          (a0 (+ 1.0 alpha))
  423.          (a1 (* -2.0 cw))
  424.          (a2 (- 1.0 alpha))
  425.          (b0 1.0)
  426.          (b1 (* -2.0 cw))
  427.          (b2 1.0))
  428.     (nyq:biquad-m x b0 b1 b2 a0 a1 a2)))
  429.  
  430.  
  431. ; two-pole allpass.
  432. (defun allpass2 (x hz q)
  433.   (multichan-expand #'nyq:allpass x hz q))
  434.  
  435. (defun nyq:allpass (x hz q)
  436.   (let* ((w (* 2.0 Pi (/ hz (snd-srate x))))
  437.          (cw (cos w))
  438.          (sw (sin w))
  439.          (k (exp (* -0.5 w (/ 1.0 q))))
  440.          (a0 1.0)
  441.          (a1 (* -2.0 cw k))
  442.          (a2 (* k k))
  443.          (b0 a2)
  444.          (b1 a1)
  445.          (b2 1.0))
  446.     (nyq:biquad-m x b0 b1 b2 a0 a1 a2)))
  447.  
  448.  
  449. ; bass shelving EQ.  gain in dB; Fc is halfway point.
  450. ; response becomes peaky at slope > 1.
  451. (defun eq-lowshelf (x hz gain &optional (slope 1.0))
  452.   (multichan-expand #'nyq:eq-lowshelf x hz gain slope))
  453.  
  454. (defun nyq:eq-lowshelf (x hz gain slope)
  455.   (let* ((w (* 2.0 Pi (/ hz (snd-srate x))))
  456.          (sw (sin w))
  457.          (cw (cos w))
  458.          (A (expt 10.0 (/ gain (* 2.0 20.0))))
  459.          (b (sqrt (- (/ (+ 1.0 (square A)) slope) (square (- A 1.0)))))
  460.          (apc (* cw (+ A 1.0)))
  461.          (amc (* cw (- A 1.0)))
  462.          (bs (* b sw))
  463.  
  464.          (b0 (*      A (+ A  1.0 (- amc)    bs  )))
  465.          (b1 (*  2.0 A (+ A -1.0 (- apc)        )))
  466.          (b2 (*      A (+ A  1.0 (- amc) (- bs) )))
  467.          (a0           (+ A  1.0    amc     bs  ))
  468.          (a1 (* -2.0   (+ A -1.0    apc         )))
  469.          (a2           (+ A  1.0    amc  (- bs) )))
  470.     (nyq:biquad-m x b0 b1 b2 a0 a1 a2)))
  471.  
  472.  
  473. ; treble shelving EQ.  gain in dB; Fc is halfway point.
  474. ; response becomes peaky at slope > 1.
  475. (defun eq-highshelf (x hz gain &optional (slope 1.0))
  476.   (multichan-expand #'nyq:eq-highshelf x hz gain slope))
  477.  
  478. (defun nyq:eq-highshelf (x hz gain slope)
  479.   (let* ((w (* 2.0 Pi (/ hz (snd-srate x))))
  480.          (sw (sin w))
  481.          (cw (cos w))
  482.          (A (expt 10.0 (/ gain (* 2.0 20.0))))
  483.          (b (sqrt (- (/ (+ 1.0 (square A)) slope) (square (- A 1.0)))))
  484.          (apc (* cw (+ A 1.0)))
  485.          (amc (* cw (- A 1.0)))
  486.          (bs (* b sw))
  487.  
  488.          (b0 (*      A (+ A  1.0    amc     bs  )))
  489.          (b1 (* -2.0 A (+ A -1.0    apc         )))
  490.          (b2 (*      A (+ A  1.0    amc  (- bs) )))
  491.          (a0           (+ A  1.0 (- amc)    bs  ))
  492.          (a1 (*  2.0   (+ A -1.0 (- apc)        )))
  493.          (a2           (+ A  1.0 (- amc) (- bs) )))
  494.     (nyq:biquad-m x b0 b1 b2 a0 a1 a2)))
  495.     
  496. (defun nyq:eq-band (x hz gain width)
  497.   (cond ((and (numberp hz) (numberp gain) (numberp width))
  498.          (eq-band-ccc x hz gain width))
  499.         ((and (soundp hz) (soundp gain) (soundp width))
  500.          (snd-eqbandvvv x hz (db-to-linear gain) width))
  501.         (t
  502.          (error "eq-band hz, gain, and width must be all numbers or all sounds"))))
  503.  
  504. ; midrange EQ.  gain in dB, width in octaves (half-gain width).
  505. (defun eq-band (x hz gain width)
  506.   (multichan-expand #'nyq:eq-band x hz gain width))
  507.   
  508.   
  509. (defun eq-band-ccc (x hz gain width)
  510.   (let* ((w (* 2.0 Pi (/ hz (snd-srate x))))
  511.          (sw (sin w))
  512.          (cw (cos w))
  513.          (J (sqrt (expt 10.0 (/ gain 20.0))))
  514.          ;(dummy (display "eq-band-ccc" gain J))
  515.          (g (* sw (sinh (* 0.5 (log 2.0) width (/ w sw)))))
  516.          ;(dummy2 (display "eq-band-ccc" width w sw g))
  517.          (b0 (+ 1.0 (* g J)))
  518.          (b1 (* -2.0 cw))
  519.          (b2 (- 1.0 (* g J)))
  520.          (a0 (+ 1.0 (/ g J)))
  521.          (a1 (- b1))
  522.          (a2 (- (/ g J) 1.0)))
  523.     (biquad x b0 b1 b2 a0 a1 a2)))
  524.  
  525. ; see failed attempt in eub-reject.lsp to do these with higher-order fns:
  526.  
  527. ; four-pole Butterworth lowpass
  528. (defun lowpass4 (x hz)
  529.   (lowpass2 (lowpass2 x hz 0.60492333) hz 1.33722126))
  530.  
  531. ; six-pole Butterworth lowpass
  532. (defun lowpass6 (x hz)
  533.   (lowpass2 (lowpass2 (lowpass2 x hz 0.58338080) 
  534.                                   hz 0.75932572) 
  535.                                   hz 1.95302407))
  536.  
  537. ; eight-pole Butterworth lowpass
  538. (defun lowpass8 (x hz)
  539.   (lowpass2 (lowpass2 (lowpass2 (lowpass2 x hz 0.57622191)
  540.                                             hz 0.66045510) 
  541.                                             hz 0.94276399)
  542.                                             hz 2.57900101))
  543.  
  544. ; four-pole Butterworth highpass
  545. (defun highpass4 (x hz)
  546.   (highpass2 (highpass2 x hz 0.60492333) hz 1.33722126))
  547.  
  548. ; six-pole Butterworth highpass
  549. (defun highpass6 (x hz)
  550.   (highpass2 (highpass2 (highpass2 x hz 0.58338080) 
  551.                                      hz 0.75932572) 
  552.                                      hz 1.95302407))
  553.  
  554. ; eight-pole Butterworth highpass
  555. (defun highpass8 (x hz)
  556.   (highpass2 (highpass2 (highpass2 (highpass2 x hz 0.57622191)
  557.                                                 hz 0.66045510) 
  558.                                                 hz 0.94276399)
  559.                                                 hz 2.57900101))
  560.  
  561. ; YIN
  562. ; maybe this should handle multiple channels, etc.
  563. (setfn yin snd-yin)
  564.  
  565.  
  566. ; FOLLOW
  567. (defun follow (sound floor risetime falltime lookahead)
  568.   ;; use 10000s as "infinite" -- that's about 2^30 samples at 96K
  569.   (setf lookahead (round (* lookahead (snd-srate sound))))
  570.   (extract (/ lookahead (snd-srate sound)) 10000
  571.            (snd-follow sound floor risetime falltime lookahead)))
  572.  
  573. (defun gate (sound floor risetime falltime lookahead threshold)
  574.   (setf lookahead (round (* lookahead (snd-srate sound))))
  575.   (setf lookahead (/ lookahead (snd-srate sound)))
  576.   (extract lookahead 10000
  577.            (snd-gate sound lookahead risetime falltime floor threshold)))
  578.