1
0
mirror of https://github.com/cookiengineer/audacity synced 2026-01-12 15:45:54 +01:00

Fix for bug 849. Validates user selected frequency in Spectral Edit plugins to prevent Nyquist applying invalid frequency (greater than Nyquist frequency) parameters to filters.

This does not address the broader question of how Spectral Selection should handle multiple sample rates.
This commit is contained in:
stevethefiddle@gmail.com
2015-02-02 01:46:34 +00:00
parent abda1166de
commit 2e562600af
3 changed files with 79 additions and 46 deletions

View File

@@ -17,43 +17,52 @@
(setf control-gain (min 24 (max -24 control-gain))) ; excessive settings may crash
(defun wet (sig gain)
(let ((f0 (get '*selection* 'low-hz))
(f1 (get '*selection* 'high-hz))
(fc (get '*selection* 'center-hz))
(bw (get '*selection* 'bandwidth)))
(cond
((not (or f0 f1))
(throw 'debug-message (format nil "~aPlease select frequencies." p-err)))
((not f0)
(throw 'debug-message (format nil "~aLow frequency is undefined." p-err)))
((not f1)
(throw 'debug-message (format nil "~aHigh frequency is undefined." p-err)))
((= bw 0)
(throw 'debug-message (format nil "~aBandwidth is zero.\nSelect a frequency range." p-err)))
(t (eq-band sig fc gain (/ bw 2))))))
(defmacro validate (hz)
"Ensure frequency is below Nyquist"
`(setf hz (min (/ *sound-srate* 2.0),hz)))
(defun wet (sig gain f0 f1)
;; (re)calculate bw and fc to ensure we do not exceed Nyquist frequency
(let ((fc (sqrt (* f0 (validate f1))))
(bw (/ (log (/ (validate f1) f0))
(log 2.0))))
(eq-band sig fc gain (/ bw 2))))
(defun result (sig)
(let*
((tn (truncate len))
((f0 (get '*selection* 'low-hz))
(f1 (get '*selection* 'high-hz))
(bw (get '*selection* 'bandwidth))
(tn (truncate len))
(rate (snd-srate sig))
(transition (truncate (* 0.01 rate))) ; 10 ms
(t1 (min transition (/ tn 2))) ; fade in length (samples)
(t2 (max (- tn transition) (/ tn 2))) ; length before fade out (samples)
(breakpoints (list t1 1.0 t2 1.0 tn))
(env (snd-pwl 0.0 rate breakpoints)))
(sum (prod env (wet sig control-gain)) (prod (diff 1.0 env) sig))))
(cond
((not (or f0 f1))
(throw 'error-message (format nil "~aPlease select frequencies." p-err)))
((not f0)
(throw 'error-message (format nil "~aLow frequency is undefined." p-err)))
((not f1)
(throw 'error-message (format nil "~aHigh frequency is undefined." p-err)))
((= bw 0)
(throw 'error-message (format nil "~aBandwidth is zero.~%Select a frequency range." p-err)))
;; If seleted frequency band is above Nyquist, do nothing.
((< f0 (/ *sound-srate* 2.0))
(sum (prod env (wet sig control-gain f0 f1)) (prod (diff 1.0 env) sig))))))
(cond
((not (get '*TRACK* 'VIEW)) ; 'View is NIL during Preview
(setf p-err (format nil "This effect requires a frequency selection in the~%~
'Spectrogram' or 'Spectrogram (log f)' track view.~%~%"))
(catch 'debug-message
(catch 'error-message
(multichan-expand #'result *track*)))
((string-not-equal (get '*TRACK* 'VIEW) "spectrogram" :end1 4 :end2 4)
"Use this effect in the 'Spectrogram'\nor 'Spectrogram (log f)' view.")
(T (setf p-err "")
(if (= control-gain 0) ; Allow dry preview
"Gain is zero. Nothing to do."
(catch 'debug-message
(catch 'error-message
(multichan-expand #'result *track*)))))