Make ArdourDisplay a subclass of ArdourDropdown
[ardour.git] / scripts / spectrogram.lua
1 ardour {
2         ["type"]    = "dsp",
3         name        = "Inline Spectrogram",
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 a spectrom 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 = "Logscale", min = 0, max = 1, default = 0, toggled = true },
22                 { ["type"] = "input", name = "1/f scale", min = 0, max = 1, default = 1, toggled = true },
23                 { ["type"] = "input", name = "FFT Size", min = 0, max = 4, default = 3, enum = true, scalepoints =
24                         {
25                                 ["512"]  = 0,
26                                 ["1024"] = 1,
27                                 ["2048"] = 2,
28                                 ["4096"] = 3,
29                                 ["8192"] = 4,
30                         }
31                 },
32                 { ["type"] = "input", name = "Height (Aspect)", min = 0, max = 3, default = 1, enum = true, scalepoints =
33                         {
34                                 ["Min"] = 0,
35                                 ["16:10"] = 1,
36                                 ["1:1"] = 2,
37                                 ["Max"] = 3
38                         }
39                 },
40                 { ["type"] = "input", name = "Range", min = 20, max = 160, default = 60, unit="dB"},
41                 { ["type"] = "input", name = "Offset", min = -40, max = 40, default = 0, unit="dB"},
42         }
43 end
44
45 -- a C memory area.
46 -- It needs to be in global scope.
47 -- When the variable is set to nil, the allocated memory is free()ed.
48 -- the memory can be interpeted as float* for use in DSP, or read/write
49 -- to a C++ Ringbuffer instance.
50 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP:DspShm
51 local cmem = nil
52
53 function dsp_init (rate)
54         -- global variables (DSP part only)
55         dpy_hz = rate / 25
56         dpy_wr = 0
57
58         -- create a ringbuffer to hold (float) audio-data
59         -- http://manual.ardour.org/lua-scripting/class_reference/#PBD:RingBufferF
60         rb = PBD.RingBufferF (2 * rate)
61
62         -- allocate memory, local mix buffer
63         cmem = ARDOUR.DSP.DspShm (8192)
64
65         -- create a table of objects to share with the GUI
66         local tbl = {}
67         tbl['rb'] = rb;
68         tbl['samplerate'] = rate
69
70         -- "self" is a special DSP variable referring
71         -- to the plugin instance itself.
72         --
73         -- "table()" is-a http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR.LuaTableRef
74         -- which allows to store/retrieve lua-tables to share them other interpreters
75         self:table ():set (tbl);
76 end
77
78 -- "dsp_runmap" uses Ardour's internal processor API, eqivalent to
79 -- 'connect_and_run()". There is no overhead (mapping, translating buffers).
80 -- The lua implementation is responsible to map all the buffers directly.
81 function dsp_runmap (bufs, in_map, out_map, n_samples, offset)
82         -- here we sum all audio input channels channels and then copy the data to a ringbuffer
83         -- for the GUI to process later
84
85         local audio_ins = in_map:count (): n_audio () -- number of audio input buffers
86         local ccnt = 0 -- processed channel count
87         local mem = cmem:to_float(0) -- a "FloatArray", float* for direct C API usage from the previously allocated buffer
88         for c = 1,audio_ins do
89                 -- see http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:ChanMapping
90                 -- Note: lua starts counting at 1, ardour's ChanMapping::get() at 0
91                 local ib = in_map:get (ARDOUR.DataType ("audio"), c - 1) -- get index of mapped input buffer
92                 local ob = out_map:get (ARDOUR.DataType ("audio"), c - 1) -- get index of mapped output buffer
93
94                 -- check if the input is connected to a buffer
95                 if (ib ~= ARDOUR.ChanMapping.Invalid) then
96
97                         -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:AudioBuffer
98                         -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP
99                         if c == 1 then
100                                 -- first channel, copy as-is
101                                 ARDOUR.DSP.copy_vector (mem, bufs:get_audio (ib):data (offset), n_samples)
102                         else
103                                 -- all other channels, add to existing data.
104                                 ARDOUR.DSP.mix_buffers_no_gain (mem, bufs:get_audio (ib):data (offset), n_samples)
105                         end
106                         ccnt = ccnt + 1;
107
108                         -- copy data to output (if not processing in-place)
109                         if (ob ~= ARDOUR.ChanMapping.Invalid and ib ~= ob) then
110                                 ARDOUR.DSP.copy_vector (bufs:get_audio (ob):data (offset), bufs:get_audio (ib):data (offset), n_samples)
111                         end
112                 end
113         end
114
115         -- Clear unconnected output buffers.
116         -- In case we're processing in-place some buffers may be identical,
117         -- so this must be done  *after processing*.
118         for c = 1,audio_ins do
119                 local ib = in_map:get (ARDOUR.DataType ("audio"), c - 1)
120                 local ob = out_map:get (ARDOUR.DataType ("audio"), c - 1)
121                 if (ib == ARDOUR.ChanMapping.Invalid and ob ~= ARDOUR.ChanMapping.Invalid) then
122                         bufs:get_audio (ob):silence (n_samples, offset)
123                 end
124         end
125
126         -- Normalize gain (1 / channel-count)
127         if ccnt > 1 then
128                 ARDOUR.DSP.apply_gain_to_buffer (mem, n_samples, 1 / ccnt)
129         end
130
131         -- if no channels were processed, feed silence.
132         if ccnt == 0 then
133                 ARDOUR.DSP.memset (mem, 0, n_samples)
134         end
135
136         -- write data to the ringbuffer
137         -- http://manual.ardour.org/lua-scripting/class_reference/#PBD:RingBufferF
138         rb:write (mem, n_samples)
139
140         -- emit QueueDraw every FPS
141         -- TODO: call every FFT window-size worth of samples, at most every FPS
142         dpy_wr = dpy_wr + n_samples
143         if (dpy_wr > dpy_hz) then
144                 dpy_wr = dpy_wr % dpy_hz
145                 self:queue_draw ()
146         end
147 end
148
149 ----------------------------------------------------------------
150 -- GUI
151
152 local fft = nil
153 local read_ptr = 0
154 local line = 0
155 local img = nil
156 local fft_size = 0
157 local last_log = false
158
159 function render_inline (ctx, w, max_h)
160         local ctrl = CtrlPorts:array () -- get control port array (read/write)
161         local tbl = self:table ():get () -- get shared memory table
162         local rate = tbl['samplerate']
163         if not cmem then
164                 cmem = ARDOUR.DSP.DspShm (0)
165         end
166
167         -- get settings
168         local logscale = ctrl[1] or 0; logscale = logscale > 0 -- x-axis logscale
169         local pink = ctrl[2] or 0; pink = pink > 0 -- 1/f scale
170         local fftsizeenum = ctrl[3] or 3 -- fft-size enum
171         local hmode = ctrl[4] or 1 -- height mode enum
172         local dbrange = ctrl[5] or 60
173         local gaindb = ctrl[6] or 0
174
175         local fftsize
176         if fftsizeenum == 0 then fftsize = 512
177         elseif fftsizeenum == 1 then fftsize = 1024
178         elseif fftsizeenum == 2 then fftsize = 2048
179         elseif fftsizeenum == 4 then fftsize = 8192
180         else fftsize = 4096
181         end
182
183         if fftsize ~= fft_size then
184                 fft_size = fftsize
185                 fft = nil
186         end
187
188         if dbrange < 20 then dbrange = 20; end
189         if dbrange > 160 then dbrange = 160; end
190         if gaindb < -40 then dbrange = -40; end
191         if gaindb >  40 then dbrange =  40; end
192
193
194         if not fft then
195                 fft = ARDOUR.DSP.FFTSpectrum (fft_size, rate)
196                 cmem:allocate (fft_size)
197         end
198
199         if last_log ~= logscale then
200                 last_log = logscale
201                 img = nil
202                 line = 0
203         end
204
205         -- calc height
206         if hmode == 0 then
207                 h = math.ceil (w * 10 / 16)
208                 if (h > 44) then
209                         h = 44
210                 end
211         elseif (hmode == 2) then
212                 h = w
213         elseif (hmode == 3) then
214                 h = max_h
215         else
216                 h = math.ceil (w * 10 / 16)
217         end
218         if (h > max_h) then
219                 h = max_h
220         end
221
222         -- re-create image surface
223         if not img or img:get_width() ~= w or img:get_height () ~= h then
224                 img = Cairo.ImageSurface (Cairo.Format.ARGB32, w, h)
225                 line = 0
226         end
227         local ictx = img:context ()
228
229         local bins = fft_size / 2 - 1 -- fft bin count
230         local bpx = bins / w  -- bins per x-pixel (linear)
231         local fpb = rate / fft_size -- freq-step per bin
232         local f_e = rate / 2 / fpb -- log-scale exponent
233         local f_b = w / math.log (fft_size / 2) -- inverse log-scale base
234         local f_l = math.log (fft_size / rate) * f_b -- inverse logscale lower-bound
235
236         local rb = tbl['rb'];
237         local mem = cmem:to_float (0)
238
239         while (rb:read_space() >= fft_size) do
240                 -- process one line / buffer
241                 rb:read (mem, fft_size)
242                 fft:set_data_hann (mem, fft_size, 0)
243                 fft:execute ()
244
245                 -- draw spectrum
246                 assert (bpx >= 1)
247
248                 -- scroll
249                 if line == 0 then line = h - 1; else line = line - 1; end
250
251                 -- clear this line
252                 ictx:set_source_rgba (0, 0, 0, 1)
253                 ictx:rectangle (0, line, w, 1)
254                 ictx:fill ()
255
256                 for x = 0, w - 1 do
257                         local pk = 0
258                         local b0, b1
259                         if logscale then
260                                 -- 20 .. 20k
261                                 b0 = math.floor (f_e ^ (x / w))
262                                 b1 = math.floor (f_e ^ ((x + 1) / w))
263                         else
264                                 b0 = math.floor (x * bpx)
265                                 b1 = math.floor ((x + 1) * bpx)
266                         end
267
268                         if b1 >= b0 and b1 <= bins and b0 >= 0 then
269                                 for i = b0, b1 do
270                                         local level = gaindb + fft:power_at_bin (i, pink and i or 1) -- pink ? i : 1
271                                         if level > -dbrange then
272                                                 local p = (dbrange + level) / dbrange
273                                                 if p > pk then pk = p; end
274                                         end
275                                 end
276                         end
277                         if pk > 0.0 then
278                                 if pk > 1.0 then pk = 1.0; end
279                                 ictx:set_source_rgba (ARDOUR.LuaAPI.hsla_to_rgba (.70 - .72 * pk, .9, .3 + pk * .4));
280                                 ictx:rectangle (x, line, 1, 1)
281                                 ictx:fill ()
282                         end
283                 end
284         end
285
286         -- copy image surface
287         if line == 0 then
288                 img:set_as_source (ctx, 0, 0)
289                 ctx:rectangle (0, 0, w, h)
290                 ctx:fill ()
291         else
292                 local yp = h - line - 1;
293                 img:set_as_source (ctx, 0, yp)
294                 ctx:rectangle (0, yp, w, line)
295                 ctx:fill ()
296
297                 img:set_as_source (ctx, 0, -line)
298                 ctx:rectangle (0, 0, w, yp)
299                 ctx:fill ()
300         end
301
302
303         -- draw grid on top
304         function x_at_freq (f)
305                 if logscale then
306                         return f_l + f_b * math.log (f)
307                 else
308                         return 2 * w * f / rate;
309                 end
310         end
311
312         function grid_freq (f)
313                 -- draw vertical grid line
314                 local x = .5 + math.floor (x_at_freq (f))
315                 ctx:move_to (x, 0)
316                 ctx:line_to (x, h)
317                 ctx:stroke ()
318         end
319
320         -- draw grid on top
321         local dash3 = C.DoubleVector ()
322         dash3:add ({1, 3})
323         ctx:set_line_width (1.0)
324         ctx:set_dash (dash3, 2) -- dotted line
325         ctx:set_source_rgba (.5, .5, .5, .8)
326         grid_freq (100)
327         grid_freq (1000)
328         grid_freq (10000)
329         ctx:unset_dash ()
330
331         return {w, h}
332 end