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