prefix blessed scripted DSP plugins with a-*
[ardour.git] / scripts / scope.lua
1 ardour {
2         ["type"]    = "dsp",
3         name        = "a-Inline Scope",
4         category    = "Visualization",
5         license     = "MIT",
6         author      = "Ardour Team",
7         description = [[Mixer strip inline waveform display]]
8 }
9
10 -- return possible i/o configurations
11 function dsp_ioconfig ()
12         -- -1, -1 = any number of channels as long as input and output count matches
13         return { [1] = { audio_in = -1, audio_out = -1}, }
14 end
15
16 function dsp_params ()
17         return
18         {
19                 { ["type"] = "input", name = "Timescale", min = .1, max = 5, default = 2, unit="sec", logarithmic = true },
20                 { ["type"] = "input", name = "Logscale", min = 0, max = 1, default = 0, toggled = true },
21                 { ["type"] = "input", name = "Height (Aspect)", min = 0, max = 3, default = 1, enum = true, scalepoints =
22                         {
23                                 ["Min"] = 0,
24                                 ["16:10"] = 1,
25                                 ["1:1"] = 2,
26                                 ["Max"] = 3
27                         }
28                 },
29         }
30 end
31
32
33 function dsp_init (rate)
34         -- global variables (DSP part only)
35         samplerate = rate
36         bufsiz = 6 * rate
37         dpy_hz = rate / 25
38         dpy_wr = 0
39 end
40
41 function dsp_configure (ins, outs)
42         -- store configuration in global variable
43         audio_ins = ins:n_audio ()
44         -- allocate shared memory area
45         -- this is used to speed up DSP computaton (using a C array)
46         -- and to share data with the GUI
47         self:shmem ():allocate (4 + bufsiz * audio_ins)
48         self:shmem ():clear ()
49         self:shmem ():atomic_set_int (0, 0)
50         local cfg = self:shmem ():to_int (1):array ()
51         cfg[1] = samplerate
52         cfg[2] = bufsiz
53         cfg[3] = audio_ins
54 end
55
56 function dsp_runmap (bufs, in_map, out_map, n_samples, offset)
57         local shmem = self:shmem ()
58         local write_ptr = shmem:atomic_get_int (0)
59
60         for c = 1,audio_ins do
61                 -- Note: lua starts counting at 1, ardour's ChanMapping::get() at 0
62                 local ib = in_map:get (ARDOUR.DataType ("audio"), c - 1); -- get id of mapped input buffer for given cannel
63                 local ob = out_map:get (ARDOUR.DataType ("audio"), c - 1); -- get id of mapped output buffer for given cannel
64                 local chn_off = 4 + bufsiz * (c - 1)
65                 if (ib ~= ARDOUR.ChanMapping.Invalid) then
66                         if (write_ptr + n_samples < bufsiz) then
67                                 ARDOUR.DSP.copy_vector (shmem:to_float (write_ptr + chn_off), bufs:get_audio (ib):data (offset), n_samples)
68                         else
69                                 local w0 = bufsiz - write_ptr;
70                                 ARDOUR.DSP.copy_vector (shmem:to_float (write_ptr + chn_off), bufs:get_audio (ib):data (offset), w0)
71                                 ARDOUR.DSP.copy_vector (shmem:to_float (chn_off)            , bufs:get_audio (ib):data (offset + w0), n_samples - w0)
72                         end
73                         if (ob ~= ARDOUR.ChanMapping.Invalid and ib ~= ob) then
74                                 ARDOUR.DSP.copy_vector (bufs:get_audio (ob):data (offset), bufs:get_audio (ib):data (offset), n_samples)
75                         end
76                 else
77                         if (write_ptr + n_samples < bufsiz) then
78                                 ARDOUR.DSP.memset (shmem:to_float (write_ptr + chn_off), 0, n_samples)
79                         else
80                                 local w0 = bufsiz - write_ptr;
81                                 ARDOUR.DSP.memset (shmem:to_float (write_ptr + chn_off), 0, w0)
82                                 ARDOUR.DSP.memset (shmem:to_float (chn_off)            , 0, n_samples - w0)
83                         end
84                 end
85         end
86         -- clear unconnected inplace buffers
87         for c = 1,audio_ins do
88                 local ib = in_map:get (ARDOUR.DataType ("audio"), c - 1); -- get id of mapped input buffer for given cannel
89                 local ob = out_map:get (ARDOUR.DataType ("audio"), c - 1); -- get id of mapped output buffer for given cannel
90                 if (ib == ARDOUR.ChanMapping.Invalid and ob ~= ARDOUR.ChanMapping.Invalid) then
91                         bufs:get_audio (ob):silence (n_samples, offset)
92                 end
93         end
94
95         write_ptr = (write_ptr + n_samples) % bufsiz
96         shmem:atomic_set_int (0, write_ptr)
97
98         -- emit QueueDraw every FPS
99         dpy_wr = dpy_wr + n_samples
100         if (dpy_wr > dpy_hz) then
101                 dpy_wr = dpy_wr % dpy_hz;
102                 self:queue_draw ()
103         end
104 end
105
106
107 -- helper function for drawing symmetric grid
108 function gridline (ctx, x, xr, h, val)
109         ctx:move_to (math.floor (.5 + x + val * xr) -.5, 1)
110         ctx:line_to (math.floor (.5 + x + val * xr) -.5, h - 1)
111         ctx:stroke ()
112         ctx:move_to (math.floor (.5 + x - val * xr) -.5, 1)
113         ctx:line_to (math.floor (.5 + x - val * xr) -.5, h - 1)
114         ctx:stroke ()
115 end
116
117 function render_inline (ctx, w, max_h)
118         local ctrl = CtrlPorts:array () -- get control port array (read/write)
119         local shmem = self:shmem () -- get shared memory region
120         local cfg = shmem:to_int (1):array () -- "cast" into lua-table
121         local rate = cfg[1]
122         local buf_size = cfg[2]
123         local n_chn = cfg[3]
124
125         -- get settings
126         local timescale = ctrl[1] or 1.0 -- display size in seconds
127         local logscale = ctrl[2] or 0; logscale = logscale > 0 -- logscale
128         local hmode = ctrl[3] or 1 -- height mode
129
130         -- calc height
131         if hmode == 0 then
132                 h = math.ceil (w * 10 / 16)
133                 if (h > 44) then
134                         h = 44
135                 end
136         elseif (hmode == 2) then
137                 h = w
138         elseif (hmode == 3) then
139                 h = max_h
140         else
141                 h = math.ceil (w * 10 / 16)
142         end
143
144         if (h > max_h) then
145                 h = max_h
146         end
147
148         -- display settings
149         local spp = math.floor (timescale * rate / (h - 2)) -- samples per pixel
150         local spl = spp * (h - 1) -- total number of audio samples to read
151         local read_ptr = (shmem:atomic_get_int (0) + buf_size - spl - 1) % buf_size -- read pointer
152         local xr = math.ceil ((w - 2) * (0.47 / n_chn)) -- x-axis range (per channel)
153
154         -- clear background
155         ctx:rectangle (0, 0, w, h)
156         ctx:set_source_rgba (.2, .2, .2, 1.0)
157         ctx:fill ()
158
159         -- prepare drawing
160         ctx:set_line_width (1.0)
161         local dash3 = C.DoubleVector ()
162         dash3:add ({1, 3})
163         local dash4 = C.DoubleVector ()
164         dash4:add ({1, 4})
165
166         -- plot every channel
167         for c = 1,n_chn do
168                 local x = math.floor ((w - 2) * (c - .5) / n_chn) + 1.5  -- x-axis center for given channel
169
170                 -- draw grid --
171                 ctx:set_source_rgba (.5, .5, .5, 1.0)
172                 ctx:move_to (x, 1) ctx:line_to (x, h - 1) ctx:stroke ()
173
174                 ctx:set_dash (dash4, 2)
175                 ctx:set_source_rgba (.4, .4, .4, 1.0)
176                 if (logscale) then
177                         gridline (ctx, x, xr, h, ARDOUR.DSP.log_meter(-18))
178                         gridline (ctx, x, xr, h, ARDOUR.DSP.log_meter(-6))
179                         ctx:set_dash (dash3, 2)
180                         ctx:set_source_rgba (.5, .1, .1, 1.0)
181                         gridline (ctx, x, xr, h, ARDOUR.DSP.log_meter(-3))
182                 else
183                         gridline (ctx, x, xr, h, .1258)
184                         gridline (ctx, x, xr, h, .5)
185                         ctx:set_dash (dash3, 2)
186                         ctx:set_source_rgba (.5, .1, .1, 1.0)
187                         gridline (ctx, x, xr, h, .7079)
188                 end
189                 ctx:unset_dash ()
190                 ctx:set_source_rgba (.5, .1, .1, 0.7)
191                 gridline (ctx, x, xr, h, 1)
192
193
194                 -- prepare waveform display drawing
195                 ctx:set_source_rgba (.8, .8, .8, .7)
196                 ctx:save ()
197                 ctx:rectangle (math.floor (x - xr), 0, math.ceil (2 * xr), h)
198                 ctx:clip ()
199
200                 local chn_off = 4 + buf_size * (c - 1)
201                 local buf_off = read_ptr;
202
203                 -- iterate over every y-axis pixel
204                 for y = 1, h - 1 do
205                         local s_min = 0
206                         local s_max = 0
207                         -- calc min/max values for given range
208                         if (buf_off + spp < buf_size) then
209                                 _, s_min, s_max = table.unpack (ARDOUR.DSP.peaks (shmem:to_float (chn_off + buf_off), s_min, s_max, spp))
210                         else
211                                 local r0 = buf_size - buf_off;
212                                 _, s_min, s_max = table.unpack (ARDOUR.DSP.peaks (shmem:to_float (chn_off + buf_off), s_min, s_max, r0))
213                                 _, s_min, s_max = table.unpack (ARDOUR.DSP.peaks (shmem:to_float (chn_off)          , s_min, s_max, spp - r0))
214                         end
215                         buf_off = (buf_off + spp) % buf_size;
216
217                         if (logscale) then
218                                 s_max = ARDOUR.DSP.log_meter_coeff (s_max)
219                                 s_min = - ARDOUR.DSP.log_meter_coeff (-s_min)
220                         end
221
222                         ctx:move_to (x + s_min * xr, h - y + .5)
223                         ctx:line_to (x + s_max * xr, h - y + .5)
224                 end
225                 ctx:stroke ()
226                 ctx:restore ()
227         end
228         return {w, h}
229 end