add a convenient lua forward mapped buffers method
[ardour.git] / scripts / amp4.lua
1 ardour {
2         ["type"]    = "dsp",
3         name        = "Amplifier",
4         category    = "Amplifier",
5         license     = "MIT",
6         author      = "Robin Gareus",
7         email       = "robin@gareus.org",
8         site        = "http://gareus.org",
9         description = [[Versatile +/- 20dB multichannel amplifier]]
10 }
11
12 function dsp_ioconfig ()
13         return
14         {
15                 -- -1, -1 = any number of channels as long as input and output count matches
16                 { audio_in = -1, audio_out = -1},
17         }
18 end
19
20
21 function dsp_params ()
22         return
23         {
24                 { ["type"] = "input", name = "Gain", min = -20, max = 20, default = 0, unit="dB"},
25         }
26 end
27
28 local lpf = 0.02 -- parameter low-pass filter time-constant
29 local cur_gain = 0 -- current smoothed gain (dB)
30
31 -- called once when plugin is instantiated
32 function dsp_init (rate)
33         lpf = 780 / rate -- interpolation time constant
34 end
35
36 function low_pass_filter_param (old, new, limit)
37         if math.abs (old - new) < limit  then
38                 return new
39         else
40                 return old + lpf * (new - old)
41         end
42 end
43
44 -- the DSP callback function
45 -- "ins" and "outs" are http://manual.ardour.org/lua-scripting/class_reference/#C:FloatArray
46 function dsp_run (ins, outs, n_samples)
47         local ctrl = CtrlPorts:array() -- get control port array (read/write)
48         local siz = n_samples -- samples remaining to process
49         local off = 0 -- already processed samples
50         local changed = false
51
52         -- if the gain parameter was changed, process at most 32 samples at a time
53         -- and interpolate gain until the current settings match the target values
54         if cur_gain ~= ctrl[1] then
55                 changed = true
56                 siz = 32
57         end
58
59         while n_samples > 0 do
60                 if siz > n_samples then siz = n_samples end -- process at most "remaining samples"
61                 if changed then
62                         -- smooth gain changes above 0.02 dB difference
63                         cur_gain = low_pass_filter_param (cur_gain, ctrl[1], 0.02)
64                 end
65
66                 local gain = ARDOUR.DSP.dB_to_coefficient (cur_gain) -- 10 ^ (0.05 * cur_gain)
67
68                 for c = 1,#ins do -- process all channels
69                         -- check if output and input buffers for this channel are identical
70                         -- http://manual.ardour.org/lua-scripting/class_reference/#C:FloatArray
71                         if not ins[c]:sameinstance (outs[c]) then
72                                 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP
73                                 ARDOUR.DSP.copy_vector (outs[c]:offset (off), ins[c]:offset (off), siz)
74                         end
75                         ARDOUR.DSP.apply_gain_to_buffer (outs[c]:offset (off), siz, gain); -- apply-gain, process in-place
76                 end
77                 n_samples = n_samples - siz
78                 off = off + siz
79         end
80
81         if changed then
82                 self:queue_draw () -- notify display
83         end
84 end
85
86 -------------------------------------------------------------------------------
87 --- inline display + text example
88
89 local txt = nil -- cache pango context globally
90
91 function render_inline (ctx, w, max_h)
92         local ctrl = CtrlPorts:array () -- get control ports
93
94         if not txt then
95                 -- allocate PangoLayout and set font
96                 --http://manual.ardour.org/lua-scripting/class_reference/#Cairo:PangoLayout
97                 txt = Cairo.PangoLayout (ctx, "Mono 8px")
98         end
99
100         txt:set_text (string.format ("%+.2f dB", ctrl[1]));
101         tw, th = txt:get_pixel_size ()
102
103         local h = th + 4 -- use text-height with 4px padding
104         if (h > max_h) then h = max_h end -- but at most max-height
105
106         -- clear background
107         ctx:rectangle (0, 0, w, h)
108         ctx:set_source_rgba (.2, .2, .2, 1.0)
109         ctx:fill ()
110
111         -- center text
112         ctx:set_source_rgba (.8, .8, .8, 1.0)
113         ctx:move_to (.5 * (w - tw), .5 * (h - th))
114         txt:show_in_cairo_context (ctx)
115
116         return {w, h}
117 end