implement fetch_valid_settings_file() to avoid the user having to see stub files
[ardour.git] / scripts / HiAndLowPass.lua
index 3694ec315b38247520dbb93de1e592a101c02880..75704d97016577d5d3ece380903f2db726bf7005 100644 (file)
@@ -53,6 +53,7 @@ local cur = {0, 0, 0, 0, 0, 0} -- current parameters
 local lpf = 0.03 -- parameter low-pass filter time-constant
 local chn = 0 -- channel/filter count
 local lpf_chunk = 0 -- chunk size for audio processing when interpolating parameters
+local max_freq = 20000
 
 local mem = nil -- memory x-fade buffer
 
@@ -60,21 +61,26 @@ function dsp_init (rate)
        -- allocate some mix-buffer
        mem = ARDOUR.DSP.DspShm (8192)
 
+       -- max allowed cut-off frequency
+       max_freq = .499 * rate
+
        -- create a table of objects to share with the GUI
        local tbl = {}
        tbl['samplerate'] = rate
+       tbl['max_freq'] = max_freq
        self:table ():set (tbl)
 
+
        -- Parameter smoothing: we want to filter out parameter changes that are
        -- faster than 15Hz, and interpolate between parameter values.
        -- For performance reasons, we want to ensure that two consecutive values
        -- of the interpolated "steepness" are less that 1 apart. By choosing the
        -- interpolation chunk size to be 64 in most cases, but 32 if the rate is
-       -- strictly less than 16kHz (there's only 8kHz in standard rates), we can
+       -- strictly less than 22kHz (there's only 8kHz in standard rates), we can
        -- ensure that steepness interpolation will never change the parameter by
-       -- more than ~0.82.
+       -- more than ~0.86.
        lpf_chunk = 64
-       if rate < 16000 then lpf_chunk = 32 end
+       if rate < 22000 then lpf_chunk = 32 end
        -- We apply a discrete version of the standard RC low-pass, with a cutoff
        -- frequency of 15Hz. For more information about the underlying math, see
        -- https://en.wikipedia.org/wiki/Low-pass_filter#Discrete-time_realization
@@ -113,6 +119,21 @@ function dsp_configure (ins, outs)
        end
 end
 
+function santize_params (ctrl)
+       -- don't allow manual cross-fades. enforce enums
+       ctrl[1] = math.floor(ctrl[1] + .5)
+       ctrl[4] = math.floor(ctrl[4] + .5)
+
+       -- high pass, clamp range
+       ctrl[2] = math.min (max_freq, math.max (5, ctrl[2]))
+       ctrl[3] = math.min (6, math.max (0.1, ctrl[3]))
+
+       -- low pass, clamp range
+       ctrl[5] = math.min (max_freq, math.max (20, ctrl[5]))
+       ctrl[6] = math.min (6, math.max (0.1, ctrl[6]))
+       return ctrl
+end
+
 -- helper functions for parameter interpolation
 function param_changed (ctrl)
        for p = 1,6 do
@@ -156,8 +177,9 @@ end
 
 -- the actual DSP callback
 function dsp_run (ins, outs, n_samples)
-       assert (n_samples < 8192)
+       assert (n_samples <= 8192)
        assert (#ins == chn)
+       local ctrl = santize_params (CtrlPorts:array ())
 
        local changed = false
        local siz = n_samples
@@ -166,13 +188,13 @@ function dsp_run (ins, outs, n_samples)
        -- if a parameter was changed, process at most lpf_chunk samples
        -- at a time and interpolate parameters until the current settings
        -- match the target values
-       if param_changed (CtrlPorts:array ()) then
+       if param_changed (ctrl) then
                changed = true
                siz = lpf_chunk
        end
 
        while n_samples > 0 do
-               if changed then apply_params (CtrlPorts:array ()) end
+               if changed then apply_params (ctrl) end
                if siz > n_samples then siz = n_samples end
 
                local ho = math.floor(cur[1])
@@ -198,11 +220,11 @@ function dsp_run (ins, outs, n_samples)
                        if xfade > 0 then
                                ARDOUR.DSP.apply_gain_to_buffer (outs[c]:offset (off), siz, 1 - xfade)
                                hp[c][ho+1]:run (mem:to_float (off), siz)
-                               ARDOUR.DSP.mix_buffers_with_gain (outs[c]:offset (off), mem:to_float (off), siz, 1 - xfade)
+                               ARDOUR.DSP.mix_buffers_with_gain (outs[c]:offset (off), mem:to_float (off), siz, xfade)
                                -- also run the next biquad because it needs to have the correct state
                                -- in case it start affecting the next chunck of output. Higher order
                                -- ones are guaranteed not to be needed for the next run because the
-                               -- interpolated order won't increase more than 0.8 in one step thanks
+                               -- interpolated order won't increase more than 0.86 in one step thanks
                                -- to the choice of the value of |lpf|.
                                if ho + 2 <= 4 then hp[c][ho+2]:run (mem:to_float (off), siz) end
                        elseif ho + 1 <= 4 then
@@ -227,7 +249,7 @@ function dsp_run (ins, outs, n_samples)
                        if xfade > 0 then
                                ARDOUR.DSP.apply_gain_to_buffer (outs[c]:offset (off), siz, 1 - xfade)
                                lp[c][lo+1]:run (mem:to_float (off), siz)
-                               ARDOUR.DSP.mix_buffers_with_gain (outs[c]:offset (off), mem:to_float (off), siz, 1 - xfade)
+                               ARDOUR.DSP.mix_buffers_with_gain (outs[c]:offset (off), mem:to_float (off), siz, xfade)
                                -- also run the next biquad in case it start affecting the next
                                -- chunck of output.
                                if lo + 2 <= 4 then lp[c][lo+2]:run (mem:to_float (off), siz) end
@@ -304,11 +326,12 @@ function render_inline (ctx, w, max_h)
                filt = {}
                filt['hp'] = ARDOUR.DSP.Biquad (tbl['samplerate'])
                filt['lp'] = ARDOUR.DSP.Biquad (tbl['samplerate'])
+               max_freq   = tbl['max_freq']
        end
 
+       local ctrl = santize_params (CtrlPorts:array ())
        -- set filter coefficients if they have changed
-       if param_changed (CtrlPorts:array ()) then
-               local ctrl = CtrlPorts:array ()
+       if param_changed (ctrl) then
                for k = 1,6 do cur[k] = ctrl[k] end
                filt['hp']:compute (ARDOUR.DSP.BiquadType.HighPass, cur[2], cur[3], 0)
                filt['lp']:compute (ARDOUR.DSP.BiquadType.LowPass,  cur[5], cur[6], 0)