3 name = "Inline Spectrogram",
4 category = "Visualization",
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]]
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}, }
18 function dsp_params ()
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 =
32 { ["type"] = "input", name = "Height (Aspect)", min = 0, max = 3, default = 1, enum = true, scalepoints =
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"},
45 function dsp_init (rate)
46 -- global variables (DSP part only)
53 function dsp_configure (ins, outs)
54 -- store configuration in global variable
55 audio_ins = ins:n_audio ()
56 -- allocate shared memory area, ringbuffer between DSP/GUI
57 self:shmem ():allocate (4 + bufsiz)
58 self:shmem ():clear ()
59 self:shmem ():atomic_set_int (0, 0)
60 local cfg = self:shmem ():to_int (1):array ()
65 function dsp_runmap (bufs, in_map, out_map, n_samples, offset)
66 local shmem = self:shmem ()
67 local write_ptr = shmem:atomic_get_int (0)
69 -- sum channels, copy to ringbuffer
70 for c = 1,audio_ins do
71 -- Note: lua starts counting at 1, ardour's ChanMapping::get() at 0
72 local ib = in_map:get (ARDOUR.DataType ("audio"), c - 1) -- get id of mapped input buffer for given cannel
73 local ob = out_map:get (ARDOUR.DataType ("audio"), c - 1) -- get id of mapped output buffer for given cannel
74 if (ib ~= ARDOUR.ChanMapping.Invalid) then
75 -- check ringbuffer wrap-around
76 if (write_ptr + n_samples < bufsiz) then
78 ARDOUR.DSP.copy_vector (shmem:to_float (4 + write_ptr), bufs:get_audio (ib):data (offset), n_samples)
80 ARDOUR.DSP.mix_buffers_no_gain (shmem:to_float (4 + write_ptr), bufs:get_audio (ib):data (offset), n_samples)
83 local w0 = bufsiz - write_ptr
85 ARDOUR.DSP.copy_vector (shmem:to_float (4 + write_ptr), bufs:get_audio (ib):data (offset), w0)
86 ARDOUR.DSP.copy_vector (shmem:to_float (4) , bufs:get_audio (ib):data (offset + w0), n_samples - w0)
88 ARDOUR.DSP.mix_buffers_no_gain (shmem:to_float (4 + write_ptr), bufs:get_audio (ib):data (offset), w0)
89 ARDOUR.DSP.mix_buffers_no_gain (shmem:to_float (4) , bufs:get_audio (ib):data (offset + w0), n_samples - w0)
92 -- copy data to output (if not processing in-place)
93 if (ob ~= ARDOUR.ChanMapping.Invalid and ib ~= ob) then
94 ARDOUR.DSP.copy_vector (bufs:get_audio (ob):data (offset), bufs:get_audio (ib):data (offset), n_samples)
97 -- invalid (unconnnected) input
98 if (write_ptr + n_samples < bufsiz) then
99 ARDOUR.DSP.memset (shmem:to_float (4 + write_ptr), 0, n_samples)
101 local w0 = bufsiz - write_ptr
102 ARDOUR.DSP.memset (shmem:to_float (4 + write_ptr), 0, w0)
103 ARDOUR.DSP.memset (shmem:to_float (4) , 0, n_samples - w0)
108 -- normalize 1 / channel-count
109 if audio_ins > 1 then
110 if (write_ptr + n_samples < bufsiz) then
111 ARDOUR.DSP.apply_gain_to_buffer (shmem:to_float (4 + write_ptr), n_samples, 1 / audio_ins)
113 local w0 = bufsiz - write_ptr
114 ARDOUR.DSP.apply_gain_to_buffer (shmem:to_float (4 + write_ptr), w0, 1 / audio_ins)
115 ARDOUR.DSP.apply_gain_to_buffer (shmem:to_float (4) , n_samples - w0, 1 / audio_ins)
119 -- clear unconnected inplace buffers
120 for c = 1,audio_ins do
121 local ib = in_map:get (ARDOUR.DataType ("audio"), c - 1) -- get id of mapped input buffer for given cannel
122 local ob = out_map:get (ARDOUR.DataType ("audio"), c - 1) -- get id of mapped output buffer for given cannel
123 if (ib == ARDOUR.ChanMapping.Invalid and ob ~= ARDOUR.ChanMapping.Invalid) then
124 bufs:get_audio (ob):silence (n_samples, offset)
128 write_ptr = (write_ptr + n_samples) % bufsiz
129 shmem:atomic_set_int (0, write_ptr)
131 -- emit QueueDraw every FPS
132 -- TODO: call every window-size worth of samples, at most every FPS
133 dpy_wr = dpy_wr + n_samples
134 if (dpy_wr > dpy_hz) then
135 dpy_wr = dpy_wr % dpy_hz
140 ----------------------------------------------------------------
148 local last_log = false
150 function render_inline (ctx, w, max_h)
151 local ctrl = CtrlPorts:array () -- get control port array (read/write)
152 local shmem = self:shmem () -- get shared memory region
153 local cfg = shmem:to_int (1):array () -- "cast" into lua-table
155 local buf_size = cfg[2]
157 if buf_size == 0 then
162 local logscale = ctrl[1] or 0; logscale = logscale > 0 -- x-axis logscale
163 local pink = ctrl[2] or 0; pink = pink > 0 -- 1/f scale
164 local fftsizeenum = ctrl[3] or 3 -- fft-size enum
165 local hmode = ctrl[4] or 1 -- height mode enum
166 local dbrange = ctrl[5] or 60
167 local gaindb = ctrl[6] or 0
170 if fftsizeenum == 0 then fftsize = 512
171 elseif fftsizeenum == 1 then fftsize = 1024
172 elseif fftsizeenum == 2 then fftsize = 2048
173 elseif fftsizeenum == 4 then fftsize = 8192
177 if fftsize ~= fft_size then
182 if dbrange < 20 then dbrange = 20; end
183 if dbrange > 160 then dbrange = 160; end
184 if gaindb < -40 then dbrange = -40; end
185 if gaindb > 40 then dbrange = 40; end
189 fft = ARDOUR.DSP.FFTSpectrum (fft_size, rate)
192 if last_log ~= logscale then
200 h = math.ceil (w * 10 / 16)
204 elseif (hmode == 2) then
206 elseif (hmode == 3) then
209 h = math.ceil (w * 10 / 16)
215 -- re-create image surface
216 if not img or img:get_width() ~= w or img:get_height () ~= h then
217 img = Cairo.ImageSurface (Cairo.Format.ARGB32, w, h)
219 local ictx = img:context ()
221 local bins = fft_size / 2 - 1 -- fft bin count
222 local bpx = bins / w -- bins per x-pixel (linear)
223 local fpb = rate / fft_size -- freq-step per bin
224 local f_e = rate / 2 / fpb -- log-scale exponent
225 local f_b = w / math.log (fft_size / 2) -- inverse log-scale base
226 local f_l = math.log (fft_size / rate) * f_b -- inverse logscale lower-bound
228 -- available samples in ring-buffer
229 local write_ptr = shmem:atomic_get_int (0)
230 local avail = (write_ptr + buf_size - read_ptr) % buf_size
232 while (avail >= fft_size) do
233 -- process one line / buffer
234 if read_ptr + fft_size < buf_size then
235 fft:set_data_hann (shmem:to_float (read_ptr + 4), fft_size, 0)
237 local r0 = buf_size - read_ptr
238 fft:set_data_hann (shmem:to_float (read_ptr + 4), r0, 0)
239 fft:set_data_hann (shmem:to_float (4), fft_size - r0, r0)
244 read_ptr = (read_ptr + fft_size) % buf_size
245 avail = (write_ptr + buf_size - read_ptr ) % buf_size
251 if line == 0 then line = h - 1; else line = line - 1; end
254 ictx:set_source_rgba (0, 0, 0, 1)
255 ictx:rectangle (0, line, w, 1)
263 b0 = math.floor (f_e ^ (x / w))
264 b1 = math.floor (f_e ^ ((x + 1) / w))
266 b0 = math.floor (x * bpx)
267 b1 = math.floor ((x + 1) * bpx)
270 if b1 >= b0 and b1 <= bins and b0 >= 0 then
272 local level = gaindb + fft:power_at_bin (i, pink and i or 1) -- pink ? i : 1
273 if level > -dbrange then
274 local p = (dbrange + level) / dbrange
275 if p > pk then pk = p; end
280 if pk > 1.0 then pk = 1.0; end
281 ictx:set_source_rgba (ARDOUR.LuaAPI.hsla_to_rgba (.70 - .72 * pk, .9, .3 + pk * .4));
282 ictx:rectangle (x, line, 1, 1)
288 -- copy image surface
290 img:set_as_source (ctx, 0, 0)
291 ctx:rectangle (0, 0, w, h)
294 local yp = h - line - 1;
295 img:set_as_source (ctx, 0, yp)
296 ctx:rectangle (0, yp, w, line)
299 img:set_as_source (ctx, 0, -line)
300 ctx:rectangle (0, 0, w, yp)
306 function x_at_freq (f)
308 return f_l + f_b * math.log (f)
310 return 2 * w * f / rate;
314 function grid_freq (f)
315 -- draw vertical grid line
316 local x = .5 + math.floor (x_at_freq (f))
323 local dash3 = C.DoubleVector ()
325 ctx:set_line_width (1.0)
326 ctx:set_dash (dash3, 2) -- dotted line
327 ctx:set_source_rgba (.5, .5, .5, .8)