Bug 1275 - No preview in Spectral edit effects

Introduces a new type 'spectral' t Nyquist plug-ins
This commit is contained in:
Steve Daulton 2015-12-26 19:26:39 +00:00 committed by James Crook
parent 0d4b58ba1c
commit 6663f406d3
5 changed files with 134 additions and 87 deletions

View File

@ -1,13 +1,13 @@
;nyquist plug-in
;version 4
;type process
;type process spectral
;name "Spectral edit multi tool"
;action "Filtering..."
;author "Paul Licameli"
;copyright "Released under terms of the GNU General Public License version 2"
;; SpectralEditMulti.ny by Paul Licameli, November 2014.
;; Updated to version 4 by Steve Daulton November 2014.
;; Updated by Steve Daulton 2014 / 2015.
;; Released under terms of the GNU General Public License version 2:
;; http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
@ -22,6 +22,7 @@
(let* ((f0 (get '*selection* 'low-hz))
(f1 (get '*selection* 'high-hz))
(fc (get '*selection* 'center-hz))
(bw (get '*selection* 'bandwidth))
(tn (truncate len))
(rate (snd-srate sig))
(transition (truncate (* 0.01 rate))) ; 10 ms
@ -30,14 +31,27 @@
(breakpoints (list t1 1.0 t2 1.0 tn))
(env (snd-pwl 0.0 rate breakpoints)))
(cond
((not (or f0 f1)) (throw 'error-message "Please select frequencies."))
((and f0 f1 (= f0 f1)) (throw 'error-message
"Bandwidth is zero.\nPlease select a frequency range."))
((not (or f0 f1)) ; This should never happen for a 'spectral' effect.
(throw 'error-message "Please select frequencies."))
((and f0 f1 (= f0 f1))
(throw 'error-message
(format nil "~aBandwidth is zero (the upper and lower~%~
frequencies are both ~a Hz).~%~
Please select a frequency range."
p-err f0)))
;; Biqud filter fails if centre frequency is very low and bandwidth very high.
;; 'Magic numbers' 10 Hz and 10 octaves are experimental.
((and f0 (< f0 10) (or (not bw)(> bw 10)))
(throw 'error-message
(format nil "~aNotch filter parameters cannot be applied.~%~
Try increasing the low frequency bound~%~
or reduce the filter 'Width'."
p-err)))
;; low pass frequency is above Nyquist so do nothing
((and (not f1) (>= f0 (/ *sound-srate* 2.0)))
nil)
;; notch frequency is above Nyquist so do nothing
((and (and f0 f1) (>= fc (/ *sound-srate* 2.0)))
((and f0 f1 (>= fc (/ *sound-srate* 2.0)))
nil)
;; high-pass above Nyquist so fade to silence
((and (not f0) (>= f1 (/ *sound-srate* 2.0)))
@ -46,4 +60,5 @@
(prod (diff 1.0 env) sig))))))
(catch 'error-message
(setf p-err "Error.\n")
(multichan-expand #'result *track*))

View File

@ -1,41 +1,28 @@
;nyquist plug-in
;version 4
;type process
;type process spectral
;preview linear
;name "Spectral edit parametric EQ..."
;action "Filtering..."
;author "Paul Licameli"
;copyright "Released under terms of the GNU General Public License version 2"
;; SpectralEditParametricEQ.ny by Paul Licameli, November 2014.
;; Updated to version 4 by Steve Daulton November 2014.
;; Updated by Steve Daulton 2014 / 2015.
;; Released under terms of the GNU General Public License version 2:
;; http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
;control control-gain "Gain (dB)" real "" 0 -24 24
(setf control-gain (min 24 (max -24 control-gain))) ; excessive settings may crash
(defmacro validate (hz)
"Ensure frequency is below Nyquist"
`(setf ,hz (max 0 (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
(validate f0)
(validate f1)
(let ((fc (sqrt (* f0 f1)))
(bw (/ (log (/ f1 f0))
(log 2.0))))
(when (= bw 0)
(throw 'error-message (format nil "~aBandwidth is zero.~%Select a frequency range." p-err)))
(eq-band sig fc gain (/ bw 2))))
(defun wet (sig gain fc bw)
(eq-band sig fc gain (/ bw 2)))
(defun result (sig)
(let*
((f0 (get '*selection* 'low-hz))
(f1 (get '*selection* 'high-hz))
(fc (get '*selection* 'center-hz))
(bw (get '*selection* 'bandwidth))
(tn (truncate len))
(rate (snd-srate sig))
(transition (truncate (* 0.01 rate))) ; 10 ms
@ -44,18 +31,34 @@
(breakpoints (list t1 1.0 t2 1.0 tn))
(env (snd-pwl 0.0 rate breakpoints)))
(cond
((not (or f0 f1))
(throw 'error-message (format nil "~aPlease select frequencies." p-err)))
((or (not f0) (= f0 0))
(throw 'error-message (format nil "~aLow frequency is undefined." p-err)))
((not (or f0 f1)) ; This should never happen for a 'spectral' effect.
(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)))
;; 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))))))
(throw 'error-message (format nil "~aHigh frequency is undefined." p-err)))
((and fc (= fc 0))
(throw 'error-message (format nil "~aCenter frequency must be above 0 Hz." p-err)))
((and f1 (> f1 (/ *sound-srate* 2)))
(throw 'error-message
(format nil "~aFrequency selection is too high for track sample rate.
For the current track, the high frequency setting cannot~%~
be greater than ~a Hz"
p-err (/ *sound-srate* 2))))
((and bw (= bw 0))
(throw 'error-message
(format nil "~aBandwidth is zero (the upper and lower~%~
frequencies are both ~a Hz).~%~
Please select a frequency range."
p-err f0)))
;; If centre frequency band is above Nyquist, do nothing.
((and fc (>= fc (/ *sound-srate* 2.0)))
nil)
(t (sum (prod env (wet sig control-gain fc bw))
(prod (diff 1.0 env) sig))))))
(catch 'error-message
(setf p-err "")
(if (= control-gain 0) ; Allow dry preview
"Gain is zero. Nothing to do."
(multichan-expand #'result *track*)))
(setf p-err "Error.\n")
(if (= control-gain 0)
nil ; Do nothing
(multichan-expand #'result *track*)))

View File

@ -1,6 +1,6 @@
;nyquist plug-in
;version 4
;type process
;type process spectral
;preview linear
;name "Spectral edit shelves..."
;action "Filtering..."
@ -9,35 +9,31 @@
;; SpectralEditShelves.ny by Paul Licameli, November 2014.
;; Updated to version 4 by Steve Daulton November 2014.
;; Updated by Steve Daulton 2014 / 2015.
;; Released under terms of the GNU General Public License version 2:
;; http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
;control control-gain "Gain (dB)" real "" 0 -24 24
(setf control-gain (min 24 (max -24 control-gain))) ; excessive settings may crash
(defmacro validate (hz)
"Ensure frequency is below Nyquist"
`(setf ,hz (max 0 (min (/ *sound-srate* 2.0) ,hz))))
"If frequency is above Nyquist, don't use it"
`(if (or (>= ,hz (/ *sound-srate* 2.0))
(<= ,hz 0))
(setf ,hz nil)))
(defun mid-shelf (sig lf hf gain)
"Combines high shelf and low shelf filters"
(let* ((invg (- gain)))
(let ((invg (- gain)))
(scale (db-to-linear gain)
(eq-highshelf (eq-lowshelf sig lf invg)
hf invg))))
(defun wet (sig gain f0 f1)
"Apply appropriate filter"
(cond
((not f0) (eq-lowshelf sig (validate f1) gain))
((not f1) (eq-highshelf sig (validate f0) gain))
(t
(validate f0)
(validate f1)
(when (= f0 f1)
(throw 'error-message "Please select a frequency range."))
(mid-shelf sig f0 f1 gain))))
((not f0) (eq-lowshelf sig f1 gain))
((not f1) (eq-highshelf sig f0 gain))
(t (mid-shelf sig f0 f1 gain))))
(defun result (sig)
(let*
@ -51,24 +47,26 @@
(breakpoints (list t1 1.0 t2 1.0 tn))
(env (snd-pwl 0.0 rate breakpoints)))
(cond
((not (or f0 f1))
((not (or f0 f1)) ; This should never happen for a 'spectral' effect.
(throw 'error-message (format nil "~aPlease select frequencies." p-err)))
; shelf is above Nyquist frequency so do nothing
((and f0 (>= f0 (/ *sound-srate* 2.0))) nil)
(T (sum (prod env (wet sig control-gain f0 f1))
(prod (diff 1.0 env) sig))))))
;; Frequency selection must be between 0 Hz and Nyquist.
(if (and (get '*selection* 'low-hz)
(<= (get '*selection* 'low-hz) 0))
(remprop '*selection* 'low-hz))
(if (and (get '*selection* 'high-hz)
(>= (get '*selection* 'high-hz)(/ *sound-srate* 2)))
(remprop '*selection* 'high-hz))
((and f0 (>= f0 (/ *sound-srate* 2.0)))
; Shelf is above Nyquist frequency so do nothing.
nil)
((and f0 f1 (= f0 f1))
(throw 'error-message
(format nil "~aBandwidth is zero (the upper and lower~%~
frequencies are both ~a Hz).~%~
Please select a frequency range."
p-err f0)))
(T (if f0 (validate f0))
(if f1 (validate f1))
(if (not (or f0 f1)) ; 'validate' may return nil
nil ; Do nothing
(sum (prod env (wet sig control-gain f0 f1))
(prod (diff 1.0 env) sig)))))))
(catch 'error-message
(setf p-err "")
(if (= control-gain 0) ; Allow dry preview
"Gain is zero. Nothing to do."
(multichan-expand #'result *track*)))
(setf p-err "Error.\n")
(if (= control-gain 0)
nil ; Do nothing
(multichan-expand #'result *track*)))

View File

@ -377,6 +377,34 @@ bool NyquistEffect::SetAutomationParameters(EffectAutomationParameters & parms)
bool NyquistEffect::Init()
{
// As of Audacity 2.1.2 rc1, 'spectral' effects are allowed only if
// the selected track(s) are in a spectrogram view, and there is at
// least one frequency bound and Spectral Selection is enabled for the
// selected track(s) - (but don't apply to Nyquist Prompt).
if (!mIsPrompt && mIsSpectral) {
AudacityProject *project = GetActiveProject();
bool bAllowSpectralEditing = true;
SelectedTrackListOfKindIterator sel(Track::Wave, project->GetTracks());
for (WaveTrack *t = (WaveTrack *) sel.First(); t; t = (WaveTrack *) sel.Next()) {
if (t->GetDisplay() != WaveTrack::Spectrum ||
!(t->GetSpectrogramSettings().SpectralSelectionEnabled())) {
bAllowSpectralEditing = false;
break;
}
}
if (!bAllowSpectralEditing || ((mF0 < 0.0) && (mF1 < 0.0))) {
wxMessageBox(_("To use 'Spectral effects', enable 'Spectral Selection'\n"
"in the track Spectrogram settings and select the\n"
"frequency range for the effect to act on."),
_("Error"), wxOK | wxICON_EXCLAMATION | wxCENTRE);
return false;
}
}
if (!mIsPrompt && !mExternal)
{
//TODO: If we want to auto-add parameters from spectral selection,
@ -621,27 +649,25 @@ bool NyquistEffect::Process()
wxString centerHz = wxT("nil");
wxString bandwidth = wxT("nil");
const WaveTrack::WaveTrackDisplay display = mCurTrack[0]->GetDisplay();
const bool bAllowSpectralEditing =
(display == WaveTrack::Spectrum) &&
mCurTrack[0]->GetSpectrogramSettings().SpectralSelectionEnabled();
if (bAllowSpectralEditing) {
#if defined(EXPERIMENTAL_SPECTRAL_EDITING)
if (mF0 >= 0.0) {
lowHz.Printf(wxT("(float %s)"), Internat::ToString(mF0).c_str());
}
if (mF0 >= 0.0) {
lowHz.Printf(wxT("(float %s)"), Internat::ToString(mF0).c_str());
}
if (mF1 >= 0.0) {
highHz.Printf(wxT("(float %s)"), Internat::ToString(mF1).c_str());
}
if (mF1 >= 0.0) {
highHz.Printf(wxT("(float %s)"), Internat::ToString(mF1).c_str());
}
if ((mF0 >= 0.0) && (mF1 >= 0.0)) {
centerHz.Printf(wxT("(float %s)"), Internat::ToString(sqrt(mF0 * mF1)).c_str());
}
if ((mF0 >= 0.0) && (mF1 >= 0.0)) {
centerHz.Printf(wxT("(float %s)"), Internat::ToString(sqrt(mF0 * mF1)).c_str());
}
if ((mF0 > 0.0) && (mF1 >= mF0)) {
bandwidth.Printf(wxT("(float %s)"), Internat::ToString(log(mF1 / mF0) / log(2.0)).c_str());
if ((mF0 > 0.0) && (mF1 >= mF0)) {
// with very small values, bandwidth calculation may be inf.
// (Observed on Linux)
double bw = log(mF1 / mF0) / log(2.0);
if (!isinf(bw)) {
bandwidth.Printf(wxT("(float %s)"), Internat::ToString(bw).c_str());
}
}
@ -1346,6 +1372,9 @@ void NyquistEffect::Parse(wxString line)
else if (tokens[1] == wxT("analyze")) {
mType = EffectTypeAnalyze;
}
if (len >= 3 && tokens[2] == wxT("spectral")) {;
mIsSpectral = true;
}
return;
}
@ -1582,6 +1611,7 @@ bool NyquistEffect::ParseProgram(wxInputStream & stream)
mIsSal = false;
mControls.Clear();
mCategories.Clear();
mIsSpectral = false;
mFoundType = false;
while (!stream.Eof() && stream.IsOk())

View File

@ -180,6 +180,7 @@ private:
bool mCompiler;
bool mIsSal;
bool mExternal;
bool mIsSpectral;
/** True if the code to execute is obtained interactively from the user via
* the "Nyquist Prompt", false for all other effects (lisp code read from
* files)