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
51 function dsp_init (rate)
52 -- global variables (DSP part only)
56 -- create a ringbuffer to hold (float) audio-data
57 rb = PBD.RingBufferF (2 * rate)
59 -- allocate memory, local mix buffer
60 cmem = ARDOUR.DSP.DspShm (8192)
62 -- create a table of objects to share with the GUI
65 tbl['samplerate'] = rate
67 -- "self" is a special DSP variable referring
68 -- to the plugin instance itself.
70 -- "table()" is-a http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR.LuaTableRef
71 -- which allows to store/retrieve lua-tables to share them other interpreters
72 self:table ():set (tbl);
75 function dsp_runmap (bufs, in_map, out_map, n_samples, offset)
76 -- here we sum all audio input channels channels and then copy the data to a ringbuffer
77 -- for the GUI to process later
79 local audio_ins = in_map:count (): n_audio () -- number of audio input buffers
80 local ccnt = 0 -- processed channel count
81 local mem = cmem:to_float(0) -- a "FloatArray", float* for direct C API usage from the previously allocated buffer
82 for c = 1,audio_ins do
83 -- Note: lua starts counting at 1, ardour's ChanMapping::get() at 0
84 local ib = in_map:get (ARDOUR.DataType ("audio"), c - 1) -- get id of mapped input buffer for given cannel
85 local ob = out_map:get (ARDOUR.DataType ("audio"), c - 1) -- get id of mapped output buffer for given cannel
86 if (ib ~= ARDOUR.ChanMapping.Invalid) then
88 -- first channel, copy as-is
89 ARDOUR.DSP.copy_vector (mem, bufs:get_audio (ib):data (offset), n_samples)
91 -- all other channels, add to existing data.
92 ARDOUR.DSP.mix_buffers_no_gain (mem, bufs:get_audio (ib):data (offset), n_samples)
96 -- copy data to output (if not processing in-place)
97 if (ob ~= ARDOUR.ChanMapping.Invalid and ib ~= ob) then
98 ARDOUR.DSP.copy_vector (bufs:get_audio (ob):data (offset), bufs:get_audio (ib):data (offset), n_samples)
103 -- Clear unconnected output buffers.
104 -- In case we're processing in-place some buffers may be identical,
105 -- so this must be done *after processing*.
106 for c = 1,audio_ins do
107 local ib = in_map:get (ARDOUR.DataType ("audio"), c - 1) -- get id of mapped input buffer for given cannel
108 local ob = out_map:get (ARDOUR.DataType ("audio"), c - 1) -- get id of mapped output buffer for given cannel
109 if (ib == ARDOUR.ChanMapping.Invalid and ob ~= ARDOUR.ChanMapping.Invalid) then
110 bufs:get_audio (ob):silence (n_samples, offset)
114 -- Normalize gain (1 / channel-count)
116 ARDOUR.DSP.apply_gain_to_buffer (mem, n_samples, 1 / ccnt)
119 -- if no channels were processed, feed silence.
121 ARDOUR.DSP.memset (mem, 0, n_samples)
124 -- write data to the ringbuffer
125 rb:write (mem, n_samples)
127 -- emit QueueDraw every FPS
128 -- TODO: call every FFT window-size worth of samples, at most every FPS
129 dpy_wr = dpy_wr + n_samples
130 if (dpy_wr > dpy_hz) then
131 dpy_wr = dpy_wr % dpy_hz
136 ----------------------------------------------------------------
144 local last_log = false
146 function render_inline (ctx, w, max_h)
147 local ctrl = CtrlPorts:array () -- get control port array (read/write)
148 local tbl = self:table ():get () -- get shared memory table
149 local rate = tbl['samplerate']
151 cmem = ARDOUR.DSP.DspShm (0)
155 local logscale = ctrl[1] or 0; logscale = logscale > 0 -- x-axis logscale
156 local pink = ctrl[2] or 0; pink = pink > 0 -- 1/f scale
157 local fftsizeenum = ctrl[3] or 3 -- fft-size enum
158 local hmode = ctrl[4] or 1 -- height mode enum
159 local dbrange = ctrl[5] or 60
160 local gaindb = ctrl[6] or 0
163 if fftsizeenum == 0 then fftsize = 512
164 elseif fftsizeenum == 1 then fftsize = 1024
165 elseif fftsizeenum == 2 then fftsize = 2048
166 elseif fftsizeenum == 4 then fftsize = 8192
170 if fftsize ~= fft_size then
175 if dbrange < 20 then dbrange = 20; end
176 if dbrange > 160 then dbrange = 160; end
177 if gaindb < -40 then dbrange = -40; end
178 if gaindb > 40 then dbrange = 40; end
182 fft = ARDOUR.DSP.FFTSpectrum (fft_size, rate)
183 cmem:allocate (fft_size)
186 if last_log ~= logscale then
194 h = math.ceil (w * 10 / 16)
198 elseif (hmode == 2) then
200 elseif (hmode == 3) then
203 h = math.ceil (w * 10 / 16)
209 -- re-create image surface
210 if not img or img:get_width() ~= w or img:get_height () ~= h then
211 img = Cairo.ImageSurface (Cairo.Format.ARGB32, w, h)
214 local ictx = img:context ()
216 local bins = fft_size / 2 - 1 -- fft bin count
217 local bpx = bins / w -- bins per x-pixel (linear)
218 local fpb = rate / fft_size -- freq-step per bin
219 local f_e = rate / 2 / fpb -- log-scale exponent
220 local f_b = w / math.log (fft_size / 2) -- inverse log-scale base
221 local f_l = math.log (fft_size / rate) * f_b -- inverse logscale lower-bound
223 local rb = tbl['rb'];
224 local mem = cmem:to_float (0)
226 while (rb:read_space() >= fft_size) do
227 -- process one line / buffer
228 rb:read (mem, fft_size)
229 fft:set_data_hann (mem, fft_size, 0)
236 if line == 0 then line = h - 1; else line = line - 1; end
239 ictx:set_source_rgba (0, 0, 0, 1)
240 ictx:rectangle (0, line, w, 1)
248 b0 = math.floor (f_e ^ (x / w))
249 b1 = math.floor (f_e ^ ((x + 1) / w))
251 b0 = math.floor (x * bpx)
252 b1 = math.floor ((x + 1) * bpx)
255 if b1 >= b0 and b1 <= bins and b0 >= 0 then
257 local level = gaindb + fft:power_at_bin (i, pink and i or 1) -- pink ? i : 1
258 if level > -dbrange then
259 local p = (dbrange + level) / dbrange
260 if p > pk then pk = p; end
265 if pk > 1.0 then pk = 1.0; end
266 ictx:set_source_rgba (ARDOUR.LuaAPI.hsla_to_rgba (.70 - .72 * pk, .9, .3 + pk * .4));
267 ictx:rectangle (x, line, 1, 1)
273 -- copy image surface
275 img:set_as_source (ctx, 0, 0)
276 ctx:rectangle (0, 0, w, h)
279 local yp = h - line - 1;
280 img:set_as_source (ctx, 0, yp)
281 ctx:rectangle (0, yp, w, line)
284 img:set_as_source (ctx, 0, -line)
285 ctx:rectangle (0, 0, w, yp)
291 function x_at_freq (f)
293 return f_l + f_b * math.log (f)
295 return 2 * w * f / rate;
299 function grid_freq (f)
300 -- draw vertical grid line
301 local x = .5 + math.floor (x_at_freq (f))
308 local dash3 = C.DoubleVector ()
310 ctx:set_line_width (1.0)
311 ctx:set_dash (dash3, 2) -- dotted line
312 ctx:set_source_rgba (.5, .5, .5, .8)