fix lua filters for multi-channels processing
authorRobin Gareus <robin@gareus.org>
Fri, 1 Jul 2016 14:01:19 +0000 (16:01 +0200)
committerRobin Gareus <robin@gareus.org>
Fri, 1 Jul 2016 14:02:03 +0000 (16:02 +0200)
scripts/filt.lua
scripts/hplp.lua

index 5f22a48953b8d1a98642208b7d8e3546cb267efc..5c1b6df73d429f351e7e1e7791d0aebcaaacc281 100644 (file)
@@ -53,9 +53,11 @@ function map_type (t)
 end
 
 -- these globals are *not* shared between DSP and UI
-local filt -- the biquad filter instance
+local filters = {}  -- the biquad filter instances (DSP)
+local filt -- the biquad filter instance (GUI, response)
 local cur = {0, 0, 0, 0} -- current parameters
 local lpf = 0.03 -- parameter low-pass filter time-constant
+local chn = 0 -- channel/filter count
 
 function dsp_init (rate)
        self:shmem ():allocate (1) -- shared mem to tell the GUI the samplerate
@@ -66,6 +68,16 @@ function dsp_init (rate)
        lpf = 13000 / rate -- interpolation time constant
 end
 
+function dsp_configure (ins, outs)
+       assert (ins:n_audio () == outs:n_audio ())
+       local cfg = self:shmem ():to_int (0):array ()
+       local rate = cfg[1]
+       chn = ins:n_audio ()
+       for c = 1, chn do
+               filters[c] = ARDOUR.DSP.Biquad (rate) -- initialize filters
+       end
+end
+
 -- helper functions for parameter interpolation
 function param_changed (ctrl)
        if ctrl[1] == cur[1] and ctrl[2] == cur[2] and ctrl[3] == cur[3] and ctrl[4] == cur[4] then
@@ -99,7 +111,9 @@ function apply_params (ctrl)
                cur[4] = low_pass_filter_param (cur[4], ctrl[4], 0.01) -- quality
        end
 
-       filt:compute (map_type (cur[1]), cur[3], cur[4], cur[2])
+       for c = 1, chn do
+               filters[c]:compute (map_type (cur[1]), cur[3], cur[4], cur[2])
+       end
 end
 
 
@@ -126,11 +140,11 @@ function dsp_run (ins, outs, n_samples)
                        -- check if output and input buffers for this channel are identical
                        -- http://manual.ardour.org/lua-scripting/class_reference/#C:FloatArray
                        if ins[c]:sameinstance (outs[c]) then
-                               filt:run (ins[c]:offset (off), siz) -- in-place processing
+                               filters[c]:run (ins[c]:offset (off), siz) -- in-place processing
                        else
                                -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP
                                ARDOUR.DSP.copy_vector (outs[c]:offset (off), ins[c]:offset (off), siz)
-                               filt:run (outs[c]:offset (off), siz)
+                               filters[c]:run (outs[c]:offset (off), siz)
                        end
                end
 
index 3b6a34c0fd0ebb8b16950cb2d8c7c5184131efd0..f894732e7db90323e861c5a8c23bbf3dd01dd94a 100644 (file)
@@ -55,18 +55,29 @@ local filters = {}  -- the biquad filter instances (DSP)
 local filt -- the biquad filter instance (GUI, response)
 local cur = {0, 0, 0, 0} -- current parameters
 local lpf = 0.03 -- parameter low-pass filter time-constant
+local chn = 0 -- channel/filter count
 
 function dsp_init (rate)
        self:shmem ():allocate (1) -- shared mem to tell the GUI the samplerate
        local cfg = self:shmem ():to_int (0):array ()
        cfg[1] = rate
-       -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP:Biquad
-       for k = 1,4 do
-               filters[k] = ARDOUR.DSP.Biquad (rate) -- initialize filters
-       end
        lpf = 13000 / rate -- interpolation time constant
 end
 
+function dsp_configure (ins, outs)
+       assert (ins:n_audio () == outs:n_audio ())
+       local cfg = self:shmem ():to_int (0):array ()
+       local rate = cfg[1]
+       chn = ins:n_audio ()
+       for c = 1, chn do
+               filters[c] = {}
+               -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP:Biquad
+               for k = 1,4 do
+                       filters[c][k] = ARDOUR.DSP.Biquad (rate) -- initialize filters
+               end
+       end
+end
+
 -- helper functions for parameter interpolation
 function param_changed (ctrl)
        if ctrl[1] == cur[1] and ctrl[2] == cur[2] and ctrl[3] == cur[3] and ctrl[4] == cur[4] then
@@ -91,8 +102,10 @@ function apply_params (ctrl)
 
        if cur[1] ~= ctrl[1] or cur[2] ~= ctrl[2] then
                -- reset filter state when type or order changes
-               for k = 1,4 do
-                       filters[k]:reset ()
+               for c = 1, chn do
+                       for k = 1,4 do
+                               filters[c][k]:reset ()
+                       end
                end
                for k = 1,4 do cur[k] = ctrl[k] end
        else
@@ -104,8 +117,10 @@ function apply_params (ctrl)
        if cur[2] < 1 then cur[2] = 1 end
        if cur[2] > 4 then cur[2] = 4 end
 
-       for k = 1,4 do
-               filters[k]:compute (map_type (cur[1]), cur[3], cur[4], 0)
+       for c = 1, chn do
+               for k = 1,4 do
+                       filters[c][k]:compute (map_type (cur[1]), cur[3], cur[4], 0)
+               end
        end
 end
 
@@ -134,15 +149,15 @@ function dsp_run (ins, outs, n_samples)
                for c = 1,#ins do
                        -- check if output and input buffers for this channel are identical
                        -- http://manual.ardour.org/lua-scripting/class_reference/#C:FloatArray
-                       if ins[c]:sameinstance (outs[c]) then
+                       if false then --- ins[c]:sameinstance (outs[c]) then
                                for k = 1,o do
-                                       filters[k]:run (ins[c]:offset (off), siz) -- in-place processing
+                                       filters[c][k]:run (ins[c]:offset (off), siz) -- in-place processing
                                end
                        else
                                -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP
                                ARDOUR.DSP.copy_vector (outs[c]:offset (off), ins[c]:offset (off), siz)
                                for k = 1,o do
-                                       filters[o]:run (outs[c]:offset (off), siz)
+                                       filters[c][k]:run (outs[c]:offset (off), siz)
                                end
                        end
                end