From 8edc94edea0ec31600a691327ff4ef196414ad77 Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Sun, 19 Apr 2020 00:49:43 +0200 Subject: Update a-Amplifier, use Ardour::Amp This fixes an issue with a-Amp interpolating the parameter in dB, resulting in a double-exponential fade when the parameter changes. Now fade is linear in dB, also using Ardour' Amp processor is more efficient, since interpolation happens in C++. --- share/scripts/_amp4.lua | 79 ++++++++++++++++++++++++++++++++ share/scripts/amp4.lua | 119 ------------------------------------------------ share/scripts/amp5.lua | 95 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 174 insertions(+), 119 deletions(-) create mode 100644 share/scripts/_amp4.lua delete mode 100644 share/scripts/amp4.lua create mode 100644 share/scripts/amp5.lua diff --git a/share/scripts/_amp4.lua b/share/scripts/_amp4.lua new file mode 100644 index 0000000000..ff4826315d --- /dev/null +++ b/share/scripts/_amp4.lua @@ -0,0 +1,79 @@ +ardour { + ["type"] = "dsp", + name = "Simple Amp IV", + category = "Amplifier", + license = "MIT", + author = "Ardour Team", + description = [[Versatile +/- 20dB multichannel amplifier]] +} + +function dsp_ioconfig () + return + { + -- -1, -1 = any number of channels as long as input and output count matches + { audio_in = -1, audio_out = -1}, + } +end + + +function dsp_params () + return + { + { ["type"] = "input", name = "Gain", min = -20, max = 20, default = 0, unit="dB"}, + } +end + +local lpf = 0.02 -- parameter low-pass filter time-constant +local cur_gain = 0 -- current smoothed gain (dB) + +-- called once when plugin is instantiated +function dsp_init (rate) + lpf = 780 / rate -- interpolation time constant +end + +function low_pass_filter_param (old, new, limit) + if math.abs (old - new) < limit then + return new + else + return old + lpf * (new - old) + end +end + +-- the DSP callback function +-- "ins" and "outs" are http://manual.ardour.org/lua-scripting/class_reference/#C:FloatArray +function dsp_run (ins, outs, n_samples) + local ctrl = CtrlPorts:array() -- get control port array (read/write) + local siz = n_samples -- samples remaining to process + local off = 0 -- already processed samples + local changed = false + + -- if the gain parameter was changed, process at most 32 samples at a time + -- and interpolate gain until the current settings match the target values + if cur_gain ~= ctrl[1] then + changed = true + siz = 32 + end + + while n_samples > 0 do + if siz > n_samples then siz = n_samples end -- process at most "remaining samples" + if changed then + -- smooth gain changes above 0.02 dB difference + -- XXX this interpolates dB, resulting in a double-exponential fade XXX + cur_gain = low_pass_filter_param (cur_gain, ctrl[1], 0.02) + end + + local gain = ARDOUR.DSP.dB_to_coefficient (cur_gain) -- 10 ^ (0.05 * cur_gain) + + for c = 1,#ins do -- process all channels + -- check if output and input buffers for this channel are identical + -- http://manual.ardour.org/lua-scripting/class_reference/#C:FloatArray + if ins[c] ~= outs[c] then + -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP + ARDOUR.DSP.copy_vector (outs[c]:offset (off), ins[c]:offset (off), siz) + end + ARDOUR.DSP.apply_gain_to_buffer (outs[c]:offset (off), siz, gain); -- apply-gain, process in-place + end + n_samples = n_samples - siz + off = off + siz + end +end diff --git a/share/scripts/amp4.lua b/share/scripts/amp4.lua deleted file mode 100644 index 276b4a0af6..0000000000 --- a/share/scripts/amp4.lua +++ /dev/null @@ -1,119 +0,0 @@ -ardour { - ["type"] = "dsp", - name = "a-Amplifier", - category = "Amplifier", - license = "MIT", - author = "Ardour Team", - description = [[Versatile +/- 20dB multichannel amplifier]] -} - -function dsp_ioconfig () - return - { - -- -1, -1 = any number of channels as long as input and output count matches - { audio_in = -1, audio_out = -1}, - } -end - - -function dsp_params () - return - { - { ["type"] = "input", name = "Gain", min = -20, max = 20, default = 0, unit="dB"}, - } -end - -local lpf = 0.02 -- parameter low-pass filter time-constant -local cur_gain = 0 -- current smoothed gain (dB) - --- called once when plugin is instantiated -function dsp_init (rate) - lpf = 780 / rate -- interpolation time constant -end - -function low_pass_filter_param (old, new, limit) - if math.abs (old - new) < limit then - return new - else - return old + lpf * (new - old) - end -end - --- the DSP callback function --- "ins" and "outs" are http://manual.ardour.org/lua-scripting/class_reference/#C:FloatArray -function dsp_run (ins, outs, n_samples) - local ctrl = CtrlPorts:array() -- get control port array (read/write) - local siz = n_samples -- samples remaining to process - local off = 0 -- already processed samples - local changed = false - - -- if the gain parameter was changed, process at most 32 samples at a time - -- and interpolate gain until the current settings match the target values - if cur_gain ~= ctrl[1] then - changed = true - siz = 32 - end - - while n_samples > 0 do - if siz > n_samples then siz = n_samples end -- process at most "remaining samples" - if changed then - -- smooth gain changes above 0.02 dB difference - cur_gain = low_pass_filter_param (cur_gain, ctrl[1], 0.02) - end - - local gain = ARDOUR.DSP.dB_to_coefficient (cur_gain) -- 10 ^ (0.05 * cur_gain) - - for c = 1,#ins do -- process all channels - -- check if output and input buffers for this channel are identical - -- http://manual.ardour.org/lua-scripting/class_reference/#C:FloatArray - if ins[c] ~= outs[c] then - -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP - ARDOUR.DSP.copy_vector (outs[c]:offset (off), ins[c]:offset (off), siz) - end - ARDOUR.DSP.apply_gain_to_buffer (outs[c]:offset (off), siz, gain); -- apply-gain, process in-place - end - n_samples = n_samples - siz - off = off + siz - end - ---[[ - if changed then - self:queue_draw () -- notify display - end ---]] -end - -------------------------------------------------------------------------------- ---- inline display + text example - ---[[ -local txt = nil -- cache pango context globally - -function render_inline (ctx, w, max_h) - local ctrl = CtrlPorts:array () -- get control ports - - if not txt then - -- allocate PangoLayout and set font - --http://manual.ardour.org/lua-scripting/class_reference/#Cairo:PangoLayout - txt = Cairo.PangoLayout (ctx, "Mono 8px") - end - - txt:set_text (string.format ("%+.2f dB", ctrl[1])); - tw, th = txt:get_pixel_size () - - local h = th + 4 -- use text-height with 4px padding - if (h > max_h) then h = max_h end -- but at most max-height - - -- clear background - ctx:rectangle (0, 0, w, h) - ctx:set_source_rgba (.2, .2, .2, 1.0) - ctx:fill () - - -- center text - ctx:set_source_rgba (.8, .8, .8, 1.0) - ctx:move_to (.5 * (w - tw), .5 * (h - th)) - txt:show_in_cairo_context (ctx) - - return {w, h} -end ---]] diff --git a/share/scripts/amp5.lua b/share/scripts/amp5.lua new file mode 100644 index 0000000000..f992d0d029 --- /dev/null +++ b/share/scripts/amp5.lua @@ -0,0 +1,95 @@ +ardour { + ["type"] = "dsp", + name = "a-Amplifier", + category = "Amplifier", + license = "MIT", + author = "Ardour Team", + description = [[Versatile +/- 20dB multichannel amplifier]] +} + +function dsp_ioconfig () + return + { + -- -1, -1 = any number of channels as long as input and output count matches + { audio_in = -1, audio_out = -1}, + } +end + + +function dsp_params () + return + { + { ["type"] = "input", name = "Gain", min = -20, max = 20, default = 0, unit="dB"}, + } +end + +local sr = 48000 +local cur_gain = 1 + +function dsp_init (rate) + sr = rate +end + +function dsp_configure (ins, outs) + n_out = outs + n_audio = outs:n_audio () + assert (outs:n_midi () == 0) +end + +-- the DSP callback function +function dsp_runmap (bufs, in_map, out_map, n_samples, offset) + -- apply I/O map + ARDOUR.DSP.process_map (bufs, n_out, in_map, out_map, n_samples, offset) + + local ctrl = CtrlPorts:array() -- get control port array + local target_gain = ARDOUR.DSP.dB_to_coefficient (ctrl[1]) -- 10 ^ (0.05 * ctrl[1]) + local current_gain = cur_gain -- start with the same for all channels + + for c = 1, n_audio do + local ob = out_map:get (ARDOUR.DataType ("audio"), c - 1); -- get id of mapped output buffer for given cannel + if (ob ~= ARDOUR.ChanMapping.Invalid) then + cur_gain = ARDOUR.Amp.apply_gain (bufs:get_audio(ob), sr, n_samples, current_gain, target_gain, offset) + end + end + +--[[ + if current_gain ~= cur_gain then + self:queue_draw () -- notify display + end +--]] +end + +------------------------------------------------------------------------------- +--- inline display + text example + +--[[ +local txt = nil -- cache pango context globally + +function render_inline (ctx, w, max_h) + local ctrl = CtrlPorts:array () -- get control ports + + if not txt then + -- allocate PangoLayout and set font + --http://manual.ardour.org/lua-scripting/class_reference/#Cairo:PangoLayout + txt = Cairo.PangoLayout (ctx, "Mono 8px") + end + + txt:set_text (string.format ("%+.2f dB", ctrl[1])); + tw, th = txt:get_pixel_size () + + local h = th + 4 -- use text-height with 4px padding + if (h > max_h) then h = max_h end -- but at most max-height + + -- clear background + ctx:rectangle (0, 0, w, h) + ctx:set_source_rgba (.2, .2, .2, 1.0) + ctx:fill () + + -- center text + ctx:set_source_rgba (.8, .8, .8, 1.0) + ctx:move_to (.5 * (w - tw), .5 * (h - th)) + txt:show_in_cairo_context (ctx) + + return {w, h} +end +--]] -- cgit v1.2.3