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