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"},
46 -- It needs to be in global scope.
47 -- When the variable is set to nil, the allocated memory is free()ed.
48 -- the memory can be interpeted as float* for use in DSP, or read/write
49 -- to a C++ Ringbuffer instance.
50 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP:DspShm
53 function dsp_init (rate)
54 -- global variables (DSP part only)
58 -- create a ringbuffer to hold (float) audio-data
59 -- http://manual.ardour.org/lua-scripting/class_reference/#PBD:RingBufferF
60 rb = PBD.RingBufferF (2 * rate)
62 -- allocate memory, local mix buffer
63 cmem = ARDOUR.DSP.DspShm (8192)
65 -- create a table of objects to share with the GUI
68 tbl['samplerate'] = rate
70 -- "self" is a special DSP variable referring
71 -- to the plugin instance itself.
73 -- "table()" is-a http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR.LuaTableRef
74 -- which allows to store/retrieve lua-tables to share them other interpreters
75 self:table ():set (tbl);
78 -- "dsp_runmap" uses Ardour's internal processor API, eqivalent to
79 -- 'connect_and_run()". There is no overhead (mapping, translating buffers).
80 -- The lua implementation is responsible to map all the buffers directly.
81 function dsp_runmap (bufs, in_map, out_map, n_samples, offset)
82 -- here we sum all audio input channels channels and then copy the data to a ringbuffer
83 -- for the GUI to process later
85 local audio_ins = in_map:count (): n_audio () -- number of audio input buffers
86 local ccnt = 0 -- processed channel count
87 local mem = cmem:to_float(0) -- a "FloatArray", float* for direct C API usage from the previously allocated buffer
88 for c = 1,audio_ins do
89 -- see http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:ChanMapping
90 -- Note: lua starts counting at 1, ardour's ChanMapping::get() at 0
91 local ib = in_map:get (ARDOUR.DataType ("audio"), c - 1) -- get index of mapped input buffer
92 local ob = out_map:get (ARDOUR.DataType ("audio"), c - 1) -- get index of mapped output buffer
94 -- check if the input is connected to a buffer
95 if (ib ~= ARDOUR.ChanMapping.Invalid) then
97 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:AudioBuffer
98 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP
100 -- first channel, copy as-is
101 ARDOUR.DSP.copy_vector (mem, bufs:get_audio (ib):data (offset), n_samples)
103 -- all other channels, add to existing data.
104 ARDOUR.DSP.mix_buffers_no_gain (mem, bufs:get_audio (ib):data (offset), n_samples)
108 -- copy data to output (if not processing in-place)
109 if (ob ~= ARDOUR.ChanMapping.Invalid and ib ~= ob) then
110 ARDOUR.DSP.copy_vector (bufs:get_audio (ob):data (offset), bufs:get_audio (ib):data (offset), n_samples)
115 -- Clear unconnected output buffers.
116 -- In case we're processing in-place some buffers may be identical,
117 -- so this must be done *after processing*.
118 for c = 1,audio_ins do
119 local ib = in_map:get (ARDOUR.DataType ("audio"), c - 1)
120 local ob = out_map:get (ARDOUR.DataType ("audio"), c - 1)
121 if (ib == ARDOUR.ChanMapping.Invalid and ob ~= ARDOUR.ChanMapping.Invalid) then
122 bufs:get_audio (ob):silence (n_samples, offset)
126 -- Normalize gain (1 / channel-count)
128 ARDOUR.DSP.apply_gain_to_buffer (mem, n_samples, 1 / ccnt)
131 -- if no channels were processed, feed silence.
133 ARDOUR.DSP.memset (mem, 0, n_samples)
136 -- write data to the ringbuffer
137 -- http://manual.ardour.org/lua-scripting/class_reference/#PBD:RingBufferF
138 rb:write (mem, n_samples)
140 -- emit QueueDraw every FPS
141 -- TODO: call every FFT window-size worth of samples, at most every FPS
142 dpy_wr = dpy_wr + n_samples
143 if (dpy_wr > dpy_hz) then
144 dpy_wr = dpy_wr % dpy_hz
149 ----------------------------------------------------------------
157 local last_log = false
159 function render_inline (ctx, w, max_h)
160 local ctrl = CtrlPorts:array () -- get control port array (read/write)
161 local tbl = self:table ():get () -- get shared memory table
162 local rate = tbl['samplerate']
164 cmem = ARDOUR.DSP.DspShm (0)
168 local logscale = ctrl[1] or 0; logscale = logscale > 0 -- x-axis logscale
169 local pink = ctrl[2] or 0; pink = pink > 0 -- 1/f scale
170 local fftsizeenum = ctrl[3] or 3 -- fft-size enum
171 local hmode = ctrl[4] or 1 -- height mode enum
172 local dbrange = ctrl[5] or 60
173 local gaindb = ctrl[6] or 0
176 if fftsizeenum == 0 then fftsize = 512
177 elseif fftsizeenum == 1 then fftsize = 1024
178 elseif fftsizeenum == 2 then fftsize = 2048
179 elseif fftsizeenum == 4 then fftsize = 8192
183 if fftsize ~= fft_size then
188 if dbrange < 20 then dbrange = 20; end
189 if dbrange > 160 then dbrange = 160; end
190 if gaindb < -40 then dbrange = -40; end
191 if gaindb > 40 then dbrange = 40; end
195 fft = ARDOUR.DSP.FFTSpectrum (fft_size, rate)
196 cmem:allocate (fft_size)
199 if last_log ~= logscale then
207 h = math.ceil (w * 10 / 16)
211 elseif (hmode == 2) then
213 elseif (hmode == 3) then
216 h = math.ceil (w * 10 / 16)
222 -- re-create image surface
223 if not img or img:get_width() ~= w or img:get_height () ~= h then
224 img = Cairo.ImageSurface (Cairo.Format.ARGB32, w, h)
227 local ictx = img:context ()
229 local bins = fft_size / 2 - 1 -- fft bin count
230 local bpx = bins / w -- bins per x-pixel (linear)
231 local fpb = rate / fft_size -- freq-step per bin
232 local f_e = rate / 2 / fpb -- log-scale exponent
233 local f_b = w / math.log (fft_size / 2) -- inverse log-scale base
234 local f_l = math.log (fft_size / rate) * f_b -- inverse logscale lower-bound
236 local rb = tbl['rb'];
237 local mem = cmem:to_float (0)
239 while (rb:read_space() >= fft_size) do
240 -- process one line / buffer
241 rb:read (mem, fft_size)
242 fft:set_data_hann (mem, fft_size, 0)
249 if line == 0 then line = h - 1; else line = line - 1; end
252 ictx:set_source_rgba (0, 0, 0, 1)
253 ictx:rectangle (0, line, w, 1)
261 b0 = math.floor (f_e ^ (x / w))
262 b1 = math.floor (f_e ^ ((x + 1) / w))
264 b0 = math.floor (x * bpx)
265 b1 = math.floor ((x + 1) * bpx)
268 if b1 >= b0 and b1 <= bins and b0 >= 0 then
270 local level = gaindb + fft:power_at_bin (i, pink and i or 1) -- pink ? i : 1
271 if level > -dbrange then
272 local p = (dbrange + level) / dbrange
273 if p > pk then pk = p; end
278 if pk > 1.0 then pk = 1.0; end
279 ictx:set_source_rgba (ARDOUR.LuaAPI.hsla_to_rgba (.70 - .72 * pk, .9, .3 + pk * .4));
280 ictx:rectangle (x, line, 1, 1)
286 -- copy image surface
288 img:set_as_source (ctx, 0, 0)
289 ctx:rectangle (0, 0, w, h)
292 local yp = h - line - 1;
293 img:set_as_source (ctx, 0, yp)
294 ctx:rectangle (0, yp, w, line)
297 img:set_as_source (ctx, 0, -line)
298 ctx:rectangle (0, 0, w, yp)
304 function x_at_freq (f)
306 return f_l + f_b * math.log (f)
308 return 2 * w * f / rate;
312 function grid_freq (f)
313 -- draw vertical grid line
314 local x = .5 + math.floor (x_at_freq (f))
321 local dash3 = C.DoubleVector ()
323 ctx:set_line_width (1.0)
324 ctx:set_dash (dash3, 2) -- dotted line
325 ctx:set_source_rgba (.5, .5, .5, .8)