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 ----------------------------------------------------------------
149 function render_inline (ctx, w, max_h)
150 local ctrl = CtrlPorts:array () -- get control port array (read/write)
151 local shmem = self:shmem () -- get shared memory region
152 local cfg = shmem:to_int (1):array () -- "cast" into lua-table
154 local buf_size = cfg[2]
156 if buf_size == 0 then
161 local logscale = ctrl[1] or 0; logscale = logscale > 0 -- x-axis logscale
162 local pink = ctrl[2] or 0; pink = pink > 0 -- 1/f scale
163 local fftsizeenum = ctrl[3] or 3 -- fft-size enum
164 local hmode = ctrl[4] or 1 -- height mode enum
165 local dbrange = ctrl[5] or 60
166 local gaindb = ctrl[6] or 0
169 if fftsizeenum == 0 then fftsize = 512
170 elseif fftsizeenum == 1 then fftsize = 1024
171 elseif fftsizeenum == 2 then fftsize = 2048
172 elseif fftsizeenum == 4 then fftsize = 8192
176 if fftsize ~= fft_size then
181 if dbrange < 20 then dbrange = 20; end
182 if dbrange > 160 then dbrange = 160; end
183 if gaindb < -40 then dbrange = -40; end
184 if gaindb > 40 then dbrange = 40; end
188 fft = ARDOUR.DSP.FFTSpectrum (fft_size, rate)
193 h = math.ceil (w * 10 / 16)
197 elseif (hmode == 2) then
199 elseif (hmode == 3) then
202 h = math.ceil (w * 10 / 16)
208 -- re-create image surface
209 if not img or img:get_width() ~= w or img:get_height () ~= h then
210 img = Cairo.ImageSurface (Cairo.Format.ARGB32, w, h)
213 -- read ring-buffer, analyze
214 local write_ptr = shmem:atomic_get_int (0)
215 local avail = (write_ptr + buf_size - read_ptr) % buf_size
217 local ictx = img:context ()
219 while (avail >= fft_size) do
220 -- process one line / buffer
221 if read_ptr + fft_size < buf_size then
222 fft:set_data_hann (shmem:to_float (read_ptr + 4), fft_size, 0)
224 local r0 = buf_size - read_ptr
225 fft:set_data_hann (shmem:to_float (read_ptr + 4), r0, 0)
226 fft:set_data_hann (shmem:to_float (4), fft_size - r0, r0)
231 read_ptr = (read_ptr + fft_size) % buf_size
232 avail = (write_ptr + buf_size - read_ptr ) % buf_size
235 local bins = fft_size / 2 - 1
237 local fpb = rate / fft_size
240 local f_e = rate / 2 / fpb
242 if line == 0 then line = h - 1; else line = line - 1; end
245 ictx:set_source_rgba (0, 0, 0, 1)
246 ictx:rectangle (0, line, w, 1)
254 b0 = math.floor (f_e ^ (x / w))
255 b1 = math.floor (f_e ^ ((x + 1) / w))
257 b0 = math.floor (x * bpx)
258 b1 = math.floor ((x + 1) * bpx)
261 if b1 >= b0 and b1 <= bins and b0 >= 0 then
263 local level = gaindb + fft:power_at_bin (i, pink and i or 1) -- pink ? i : 1
264 if level > -dbrange then
265 local p = (dbrange + level) / dbrange
266 if p > pk then pk = p; end
271 if pk > 1.0 then pk = 1.0; end
272 ictx:set_source_rgba (ARDOUR.LuaAPI.hsla_to_rgba (.70 - .72 * pk, .9, .3 + pk * .4));
273 ictx:rectangle (x, line, 1, 1)
279 -- copy image surface
281 img:set_as_source (ctx, 0, 0)
282 ctx:rectangle (0, 0, w, h)
285 local yp = h - line - 1;
286 img:set_as_source (ctx, 0, yp)
287 ctx:rectangle (0, yp, w, line)
290 img:set_as_source (ctx, 0, -line)
291 ctx:rectangle (0, 0, w, yp)