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"},
44 -- It needs to be in global scope.
45 -- When the variable is set to nil, the allocated memory is free()ed.
46 -- the memory can be interpeted as float* for use in DSP, or read/write
47 -- to a C++ Ringbuffer instance.
48 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP:DspShm
51 function dsp_init (rate)
52 -- global variables (DSP part only)
56 -- create a ringbuffer to hold (float) audio-data
57 -- http://manual.ardour.org/lua-scripting/class_reference/#PBD:RingBufferF
58 rb = PBD.RingBufferF (2 * rate)
60 -- allocate memory, local mix buffer
61 cmem = ARDOUR.DSP.DspShm (8192)
63 -- create a table of objects to share with the GUI
66 tbl['samplerate'] = rate
68 -- "self" is a special DSP variable referring
69 -- to the plugin instance itself.
71 -- "table()" is-a http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR.LuaTableRef
72 -- which allows to store/retrieve lua-tables to share them other interpreters
73 self:table ():set (tbl);
76 -- "dsp_runmap" uses Ardour's internal processor API, eqivalent to
77 -- 'connect_and_run()". There is no overhead (mapping, translating buffers).
78 -- The lua implementation is responsible to map all the buffers directly.
79 function dsp_runmap (bufs, in_map, out_map, n_samples, offset)
80 -- here we sum all audio input channels channels and then copy the data to a ringbuffer
81 -- for the GUI to process later
83 local audio_ins = in_map:count (): n_audio () -- number of audio input buffers
84 local ccnt = 0 -- processed channel count
85 local mem = cmem:to_float(0) -- a "FloatArray", float* for direct C API usage from the previously allocated buffer
86 for c = 1,audio_ins do
87 -- see http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:ChanMapping
88 -- Note: lua starts counting at 1, ardour's ChanMapping::get() at 0
89 local ib = in_map:get (ARDOUR.DataType ("audio"), c - 1) -- get index of mapped input buffer
90 local ob = out_map:get (ARDOUR.DataType ("audio"), c - 1) -- get index of mapped output buffer
92 -- check if the input is connected to a buffer
93 if (ib ~= ARDOUR.ChanMapping.Invalid) then
95 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:AudioBuffer
96 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP
98 -- first channel, copy as-is
99 ARDOUR.DSP.copy_vector (mem, bufs:get_audio (ib):data (offset), n_samples)
101 -- all other channels, add to existing data.
102 ARDOUR.DSP.mix_buffers_no_gain (mem, bufs:get_audio (ib):data (offset), n_samples)
106 -- copy data to output (if not processing in-place)
107 if (ob ~= ARDOUR.ChanMapping.Invalid and ib ~= ob) then
108 ARDOUR.DSP.copy_vector (bufs:get_audio (ob):data (offset), bufs:get_audio (ib):data (offset), n_samples)
113 -- Clear unconnected output buffers.
114 -- In case we're processing in-place some buffers may be identical,
115 -- so this must be done *after processing*.
116 for c = 1,audio_ins do
117 local ib = in_map:get (ARDOUR.DataType ("audio"), c - 1)
118 local ob = out_map:get (ARDOUR.DataType ("audio"), c - 1)
119 if (ib == ARDOUR.ChanMapping.Invalid and ob ~= ARDOUR.ChanMapping.Invalid) then
120 bufs:get_audio (ob):silence (n_samples, offset)
124 -- Normalize gain (1 / channel-count)
126 ARDOUR.DSP.apply_gain_to_buffer (mem, n_samples, 1 / ccnt)
129 -- if no channels were processed, feed silence.
131 ARDOUR.DSP.memset (mem, 0, n_samples)
134 -- write data to the ringbuffer
135 -- http://manual.ardour.org/lua-scripting/class_reference/#PBD:RingBufferF
136 rb:write (mem, n_samples)
138 -- emit QueueDraw every FPS
139 -- TODO: call every FFT window-size worth of samples, at most every FPS
140 dpy_wr = dpy_wr + n_samples
141 if (dpy_wr > dpy_hz) then
142 dpy_wr = dpy_wr % dpy_hz
147 ----------------------------------------------------------------
155 local last_log = false
157 function render_inline (ctx, w, max_h)
158 local ctrl = CtrlPorts:array () -- get control port array (read/write)
159 local tbl = self:table ():get () -- get shared memory table
160 local rate = tbl['samplerate']
162 cmem = ARDOUR.DSP.DspShm (0)
166 local logscale = ctrl[1] or 0; logscale = logscale > 0 -- x-axis logscale
167 local pink = ctrl[2] or 0; pink = pink > 0 -- 1/f scale
168 local fftsizeenum = ctrl[3] or 3 -- fft-size enum
169 local hmode = ctrl[4] or 1 -- height mode enum
170 local dbrange = ctrl[5] or 60
171 local gaindb = ctrl[6] or 0
174 if fftsizeenum == 0 then fftsize = 512
175 elseif fftsizeenum == 1 then fftsize = 1024
176 elseif fftsizeenum == 2 then fftsize = 2048
177 elseif fftsizeenum == 4 then fftsize = 8192
181 if fftsize ~= fft_size then
186 if dbrange < 20 then dbrange = 20; end
187 if dbrange > 160 then dbrange = 160; end
188 if gaindb < -40 then dbrange = -40; end
189 if gaindb > 40 then dbrange = 40; end
193 fft = ARDOUR.DSP.FFTSpectrum (fft_size, rate)
194 cmem:allocate (fft_size)
197 if last_log ~= logscale then
205 h = math.ceil (w * 10 / 16)
209 elseif (hmode == 2) then
211 elseif (hmode == 3) then
214 h = math.ceil (w * 10 / 16)
220 -- re-create image surface
221 if not img or img:get_width() ~= w or img:get_height () ~= h then
222 img = Cairo.ImageSurface (Cairo.Format.ARGB32, w, h)
225 local ictx = img:context ()
227 local bins = fft_size / 2 - 1 -- fft bin count
228 local bpx = bins / w -- bins per x-pixel (linear)
229 local fpb = rate / fft_size -- freq-step per bin
230 local f_e = rate / 2 / fpb -- log-scale exponent
231 local f_b = w / math.log (fft_size / 2) -- inverse log-scale base
232 local f_l = math.log (fft_size / rate) * f_b -- inverse logscale lower-bound
234 local rb = tbl['rb'];
235 local mem = cmem:to_float (0)
237 while (rb:read_space() >= fft_size) do
238 -- process one line / buffer
239 rb:read (mem, fft_size)
240 fft:set_data_hann (mem, fft_size, 0)
247 if line == 0 then line = h - 1; else line = line - 1; end
250 ictx:set_source_rgba (0, 0, 0, 1)
251 ictx:rectangle (0, line, w, 1)
259 b0 = math.floor (f_e ^ (x / w))
260 b1 = math.floor (f_e ^ ((x + 1) / w))
262 b0 = math.floor (x * bpx)
263 b1 = math.floor ((x + 1) * bpx)
266 if b1 >= b0 and b1 <= bins and b0 >= 0 then
268 local level = gaindb + fft:power_at_bin (i, pink and i or 1) -- pink ? i : 1
269 if level > -dbrange then
270 local p = (dbrange + level) / dbrange
271 if p > pk then pk = p; end
276 if pk > 1.0 then pk = 1.0; end
277 ictx:set_source_rgba (ARDOUR.LuaAPI.hsla_to_rgba (.70 - .72 * pk, .9, .3 + pk * .4));
278 ictx:rectangle (x, line, 1, 1)
284 -- copy image surface
286 img:set_as_source (ctx, 0, 0)
287 ctx:rectangle (0, 0, w, h)
290 local yp = h - line - 1;
291 img:set_as_source (ctx, 0, yp)
292 ctx:rectangle (0, yp, w, line)
295 img:set_as_source (ctx, 0, -line)
296 ctx:rectangle (0, 0, w, yp)
302 function x_at_freq (f)
304 return f_l + f_b * math.log (f)
306 return 2 * w * f / rate;
310 function grid_freq (f)
311 -- draw vertical grid line
312 local x = .5 + math.floor (x_at_freq (f))
319 local dash3 = C.DoubleVector ()
321 ctx:set_line_width (1.0)
322 ctx:set_dash (dash3, 2) -- dotted line
323 ctx:set_source_rgba (.5, .5, .5, .8)