1$nyquist plug-in
2$version 4
3$type process spectral
4$preview linear
5$name (_ "Spectral edit parametric EQ")
6$manpage "Spectral_edit_parametric_EQ"
7$action (_ "Filtering...")
8$author (_ "Paul Licameli")
9$release 2.3.0
10$copyright (_ "Released under terms of the GNU General Public License version 2")
11
12;; SpectralEditParametricEQ.ny by Paul Licameli, November 2014.
13;; Updated by Steve Daulton 2014 / 2015.
14
15;; Released under terms of the GNU General Public License version 2:
16;; http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
17;;
18;; For information about writing and modifying Nyquist plug-ins:
19;; https://wiki.audacityteam.org/wiki/Nyquist_Plug-ins_Reference
20
21$control control-gain (_ "Gain (dB)") real "" 0 -24 24
22
23(defun wet (sig gain fc bw)
24  (eq-band sig fc gain (/ bw 2)))
25
26(defun result (sig)
27  (let*
28      ((f0 (get '*selection* 'low-hz))
29       (f1 (get '*selection* 'high-hz))
30       (fc (get '*selection* 'center-hz))
31       (bw (get '*selection* 'bandwidth))
32       (tn (truncate len))
33       (rate (snd-srate sig))
34       (transition (truncate (* 0.01 rate)))  ; 10 ms
35       (t1 (min transition (/ tn 2)))         ; fade in length (samples)
36       (t2 (max (- tn transition) (/ tn 2)))  ; length before fade out (samples)
37       (breakpoints (list t1 1.0 t2 1.0 tn))
38       (env (snd-pwl 0.0 rate breakpoints)))
39    (cond
40      ((not (or f0 f1)) ; This should never happen for a 'spectral' effect.
41          (throw 'error-message (format nil (_ "~aPlease select frequencies.") p-err)))
42      ((not f0)
43          (throw 'error-message (format nil (_ "~aLow frequency is undefined.") p-err)))
44      ((not f1)
45          (throw 'error-message (format nil (_ "~aHigh frequency is undefined.") p-err)))
46      ((and fc (= fc 0))
47          (throw 'error-message (format nil (_ "~aCenter frequency must be above 0 Hz.") p-err)))
48      ((and f1 (> f1 (/ *sound-srate* 2)))
49          (throw 'error-message
50            (format nil (_ "~aFrequency selection is too high for track sample rate.~%~
51                        For the current track, the high frequency setting cannot~%~
52                        be greater than ~a Hz")
53                    p-err (/ *sound-srate* 2))))
54      ((and bw (= bw 0))
55          (throw 'error-message
56            (format nil (_ "~aBandwidth is zero (the upper and lower~%~
57                         frequencies are both ~a Hz).~%~
58                         Please select a frequency range.")
59                    p-err f0)))
60      ;; If centre frequency band is above Nyquist, do nothing.
61      ((and fc (>= fc (/ *sound-srate* 2.0)))
62          nil)
63      (t  (sum (prod env (wet sig control-gain fc bw))
64               (prod (diff 1.0 env) sig))))))
65
66(catch 'error-message
67  (setf p-err (format nil (_ "Error.~%")))
68  (if (= control-gain 0)
69      nil ; Do nothing
70      (multichan-expand #'result *track*)))
71