From 3267b4892c1f971fbc3f613fa32a5476bf70dc62 Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Mon, 4 Jul 2016 00:03:46 +0200 Subject: update lua scripts * add proper amplifier (smooth gain change) + text-example * remove commented no-inplace from High/Low pass * amend 913609be inline spectrogam re-init --- scripts/amp4.lua | 118 ++++++++++++++++++++++++++++++++++++++++++++++++ scripts/hplp.lua | 12 ++--- scripts/spectrogram.lua | 1 + 3 files changed, 123 insertions(+), 8 deletions(-) create mode 100644 scripts/amp4.lua diff --git a/scripts/amp4.lua b/scripts/amp4.lua new file mode 100644 index 0000000000..a348d166f4 --- /dev/null +++ b/scripts/amp4.lua @@ -0,0 +1,118 @@ +ardour { + ["type"] = "dsp", + name = "Amplifier", + category = "Amplifier", + license = "MIT", + author = "Robin Gareus", + email = "robin@gareus.org", + site = "http://gareus.org", + description = [[ Versatile +/- 20dB multichannel amplifier]] +} + +function dsp_ioconfig () + return + { + { 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 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 + +-- use ardour's vectorized functions +-- +-- This is as efficient as Ardour doing it itself in C++ +-- Lua function overhead is negligible +-- +-- this also exemplifies the /simpler/ way of delegating the +-- channel-mapping to ardour. + +function dsp_run (ins, outs, n_samples) + assert (#ins == #outs) -- ensure that we can run in-place (channel count matches) + local ctrl = CtrlPorts:array() -- get control port array (read/write) + local siz = n_samples + local off = 0 + local changed = false + + -- if the gain parameter was changed, process at most 64 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 + if changed then + cur_gain = low_pass_filter_param (cur_gain, ctrl[1], 0.05) + 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 not ins[c]:sameinstance (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); -- 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 globally +function render_inline (ctx, w, max_h) + local ctrl = CtrlPorts:array () + + if not txt then + txt = Cairo.PangoLayout (ctx, "Mono 8px") + end + + txt:set_text (string.format ("%+.2f dB", ctrl[1])); + tw, th = txt:get_pixel_size () + + local h = math.ceil (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/scripts/hplp.lua b/scripts/hplp.lua index 3a1b7b981d..40cd22254e 100644 --- a/scripts/hplp.lua +++ b/scripts/hplp.lua @@ -150,16 +150,12 @@ 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 false then --- ins[c]:sameinstance (outs[c]) then - for k = 1,o do - filters[c][k]:run (ins[c]:offset (off), siz) -- in-place processing - end - else + if not ins[c]:sameinstance (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) - for k = 1,o do - filters[c][k]:run (outs[c]:offset (off), siz) - end + end + for k = 1,o do + filters[c][k]:run (outs[c]:offset (off), siz) -- in-place processing end end diff --git a/scripts/spectrogram.lua b/scripts/spectrogram.lua index f82d4fe4bb..980a308f95 100644 --- a/scripts/spectrogram.lua +++ b/scripts/spectrogram.lua @@ -215,6 +215,7 @@ function render_inline (ctx, w, max_h) -- re-create image surface if not img or img:get_width() ~= w or img:get_height () ~= h then img = Cairo.ImageSurface (Cairo.Format.ARGB32, w, h) + line = 0 end local ictx = img:context () -- cgit v1.2.3