2016-07-03 18:03:46 -04:00
|
|
|
ardour {
|
|
|
|
["type"] = "dsp",
|
|
|
|
name = "Amplifier",
|
|
|
|
category = "Amplifier",
|
|
|
|
license = "MIT",
|
|
|
|
author = "Robin Gareus",
|
|
|
|
email = "robin@gareus.org",
|
|
|
|
site = "http://gareus.org",
|
2016-07-03 19:47:52 -04:00
|
|
|
description = [[Versatile +/- 20dB multichannel amplifier]]
|
2016-07-03 18:03:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function dsp_ioconfig ()
|
|
|
|
return
|
|
|
|
{
|
2016-07-03 19:47:52 -04:00
|
|
|
-- -1, -1 = any number of channels as long as input and output count matches
|
2016-07-03 18:03:46 -04:00
|
|
|
{ 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
|
2016-07-03 19:47:52 -04:00
|
|
|
local cur_gain = 0 -- current smoothed gain (dB)
|
2016-07-03 18:03:46 -04:00
|
|
|
|
|
|
|
-- 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
|
|
|
|
|
2016-07-03 19:47:52 -04:00
|
|
|
-- the DSP callback function
|
|
|
|
-- "ins" and "outs" are http://manual.ardour.org/lua-scripting/class_reference/#C:FloatArray
|
2016-07-03 18:03:46 -04:00
|
|
|
function dsp_run (ins, outs, n_samples)
|
|
|
|
local ctrl = CtrlPorts:array() -- get control port array (read/write)
|
2016-07-03 19:47:52 -04:00
|
|
|
local siz = n_samples -- samples remaining to process
|
|
|
|
local off = 0 -- already processed samples
|
2016-07-03 18:03:46 -04:00
|
|
|
local changed = false
|
|
|
|
|
2016-07-03 19:47:52 -04:00
|
|
|
-- if the gain parameter was changed, process at most 32 samples at a time
|
2016-07-03 18:03:46 -04:00
|
|
|
-- 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
|
2016-07-03 19:47:52 -04:00
|
|
|
if siz > n_samples then siz = n_samples end -- process at most "remaining samples"
|
2016-07-03 18:03:46 -04:00
|
|
|
if changed then
|
2016-07-03 19:47:52 -04:00
|
|
|
-- smooth gain changes above 0.02 dB difference
|
|
|
|
cur_gain = low_pass_filter_param (cur_gain, ctrl[1], 0.02)
|
2016-07-03 18:03:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
local gain = ARDOUR.DSP.dB_to_coefficient (cur_gain) -- 10 ^ (0.05 * cur_gain)
|
2016-07-03 19:47:52 -04:00
|
|
|
|
2016-07-03 18:03:46 -04:00
|
|
|
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
|
2016-07-03 19:47:52 -04:00
|
|
|
ARDOUR.DSP.apply_gain_to_buffer (outs[c]:offset (off), siz, gain); -- apply-gain, process in-place
|
2016-07-03 18:03:46 -04:00
|
|
|
end
|
|
|
|
n_samples = n_samples - siz
|
|
|
|
off = off + siz
|
|
|
|
end
|
|
|
|
|
|
|
|
if changed then
|
|
|
|
self:queue_draw () -- notify display
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
--- inline display + text example
|
|
|
|
|
2016-07-03 19:47:52 -04:00
|
|
|
local txt = nil -- cache pango context globally
|
|
|
|
|
2016-07-03 18:03:46 -04:00
|
|
|
function render_inline (ctx, w, max_h)
|
2016-07-03 19:47:52 -04:00
|
|
|
local ctrl = CtrlPorts:array () -- get control ports
|
2016-07-03 18:03:46 -04:00
|
|
|
|
|
|
|
if not txt then
|
2016-07-03 19:47:52 -04:00
|
|
|
-- allocate PangoLayout and set font
|
|
|
|
--http://manual.ardour.org/lua-scripting/class_reference/#Cairo:PangoLayout
|
2016-07-03 18:03:46 -04:00
|
|
|
txt = Cairo.PangoLayout (ctx, "Mono 8px")
|
|
|
|
end
|
|
|
|
|
|
|
|
txt:set_text (string.format ("%+.2f dB", ctrl[1]));
|
|
|
|
tw, th = txt:get_pixel_size ()
|
|
|
|
|
2016-07-03 19:47:52 -04:00
|
|
|
local h = th + 4 -- use text-height with 4px padding
|
2016-07-03 18:03:46 -04:00
|
|
|
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)
|
2016-07-03 19:47:52 -04:00
|
|
|
ctx:move_to (.5 * (w - tw), .5 * (h - th))
|
2016-07-03 18:03:46 -04:00
|
|
|
txt:show_in_cairo_context (ctx)
|
|
|
|
|
|
|
|
return {w, h}
|
|
|
|
end
|