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