3 name = "a-Inline Spectrogram",
4 category = "Visualization",
6 author = "Ardour Team",
7 description = [[Mixer strip inline spectrum display]]
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}, }
16 function dsp_params ()
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 =
30 { ["type"] = "input", name = "Height (Aspect)", min = 0, max = 3, default = 1, enum = true, scalepoints =
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"},
43 -- symbolic names for shmem offsets
45 local SHMEM_WRITEPTR = 1
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
56 function dsp_init (rate)
57 -- global variables (DSP part only)
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)
66 self:shmem ():atomic_set_int (SHMEM_RATE, rate)
67 self:shmem ():atomic_set_int (SHMEM_WRITEPTR, 0)
69 -- allocate memory, local mix buffer
70 cmem = ARDOUR.DSP.DspShm (8192)
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
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)
86 local ringsize = 2 * rate
87 local ptr_wrap = math.floor(2^50 / ringsize) * ringsize
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
95 -- check if the input is connected to a buffer
96 if (ib ~= ARDOUR.ChanMapping.Invalid) then
98 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:AudioBuffer
99 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP
101 -- first channel, copy as-is
102 ARDOUR.DSP.copy_vector (mem, bufs:get_audio (ib):data (offset), n_samples)
104 -- all other channels, add to existing data.
105 ARDOUR.DSP.mix_buffers_no_gain (mem, bufs:get_audio (ib):data (offset), n_samples)
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)
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)
127 -- Normalize gain (1 / channel-count)
129 ARDOUR.DSP.apply_gain_to_buffer (mem, n_samples, 1 / ccnt)
132 -- if no channels were processed, feed silence.
134 ARDOUR.DSP.memset (mem, 0, n_samples)
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)
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)
145 self:shmem ():atomic_set_int (SHMEM_WRITEPTR, (write_ptr + n_samples) % ptr_wrap)
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
156 ----------------------------------------------------------------
164 local last_log = false
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)
171 cmem = ARDOUR.DSP.DspShm (0)
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
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
190 if fftsize ~= fft_size then
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
202 fft = ARDOUR.DSP.FFTSpectrum (fft_size, rate)
203 cmem:allocate (fft_size)
206 if last_log ~= logscale then
214 h = math.ceil (w * 10 / 16)
218 elseif (hmode == 2) then
220 elseif (hmode == 3) then
223 h = math.ceil (w * 10 / 16)
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)
234 local ictx = img:context ()
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
243 local mem = cmem:to_float (0)
245 local ringsize = 2 * rate
246 local ptr_wrap = math.floor(2^50 / ringsize) * ringsize
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
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)
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)
270 read_ptr = (read_ptr + fft_size) % ptr_wrap
273 fft:set_data_hann (mem, fft_size, 0)
280 if line == 0 then line = h - 1; else line = line - 1; end
283 ictx:set_source_rgba (0, 0, 0, 1)
284 ictx:rectangle (0, line, w, 1)
292 b0 = math.floor (f_e ^ (x / w))
293 b1 = math.floor (f_e ^ ((x + 1) / w))
295 b0 = math.floor (x * bpx)
296 b1 = math.floor ((x + 1) * bpx)
299 if b1 >= b0 and b1 <= bins and b0 >= 0 then
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
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)
317 -- copy image surface
319 img:set_as_source (ctx, 0, 0)
320 ctx:rectangle (0, 0, w, h)
323 local yp = h - line - 1;
324 img:set_as_source (ctx, 0, yp)
325 ctx:rectangle (0, yp, w, line)
328 img:set_as_source (ctx, 0, -line)
329 ctx:rectangle (0, 0, w, yp)
335 function x_at_freq (f)
337 return f_l + f_b * math.log (f)
339 return 2 * w * f / rate;
343 function grid_freq (f)
344 -- draw vertical grid line
345 local x = .5 + math.floor (x_at_freq (f))
352 local dash3 = C.DoubleVector ()
354 ctx:set_line_width (1.0)
355 ctx:set_dash (dash3, 2) -- dotted line
356 ctx:set_source_rgba (.5, .5, .5, .8)