13
0

rewrite spectrogram using PBD::Ringbuffer

This commit is contained in:
Robin Gareus 2016-07-07 15:33:26 +02:00
parent 91ab5e3175
commit 29184a2b72

View File

@ -42,81 +42,67 @@ function dsp_params ()
} }
end end
-- a C memory area.
-- It needs to be in global scope.
-- When the variable is set to nil, the allocated memory
-- is free()ed
local cmem = nil
function dsp_init (rate) function dsp_init (rate)
-- global variables (DSP part only) -- global variables (DSP part only)
samplerate = rate
bufsiz = 2 * rate
dpy_hz = rate / 25 dpy_hz = rate / 25
dpy_wr = 0 dpy_wr = 0
end
function dsp_configure (ins, outs) -- create a ringbuffer to hold (float) audio-data
-- store configuration in global variable rb = PBD.RingBufferF (2 * rate)
audio_ins = ins:n_audio ()
-- allocate shared memory area, ringbuffer between DSP/GUI -- allocate memory, local mix buffer
self:shmem ():allocate (4 + bufsiz) cmem = ARDOUR.DSP.DspShm (8192)
self:shmem ():clear ()
self:shmem ():atomic_set_int (0, 0) -- create a table of objects to share with the GUI
local cfg = self:shmem ():to_int (1):array () local tbl = {}
cfg[1] = samplerate tbl['rb'] = rb;
cfg[2] = bufsiz tbl['samplerate'] = rate
-- "self" is a special DSP variable referring
-- to the plugin instance itself.
--
-- "table()" is-a http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR.LuaTableRef
-- which allows to store/retrieve lua-tables to share them other interpreters
self:table ():set (tbl);
end end
function dsp_runmap (bufs, in_map, out_map, n_samples, offset) function dsp_runmap (bufs, in_map, out_map, n_samples, offset)
local shmem = self:shmem () -- here we sum all audio input channels channels and then copy the data to a ringbuffer
local write_ptr = shmem:atomic_get_int (0) -- for the GUI to process later
-- sum channels, copy to ringbuffer local audio_ins = in_map:count (): n_audio () -- number of audio input buffers
local ccnt = 0 -- processed channel count
local mem = cmem:to_float(0) -- a "FloatArray", float* for direct C API usage from the previously allocated buffer
for c = 1,audio_ins do for c = 1,audio_ins do
-- Note: lua starts counting at 1, ardour's ChanMapping::get() at 0 -- Note: lua starts counting at 1, ardour's ChanMapping::get() at 0
local ib = in_map:get (ARDOUR.DataType ("audio"), c - 1) -- get id of mapped input buffer for given cannel local ib = in_map:get (ARDOUR.DataType ("audio"), c - 1) -- get id of mapped input buffer for given cannel
local ob = out_map:get (ARDOUR.DataType ("audio"), c - 1) -- get id of mapped output buffer for given cannel local ob = out_map:get (ARDOUR.DataType ("audio"), c - 1) -- get id of mapped output buffer for given cannel
if (ib ~= ARDOUR.ChanMapping.Invalid) then if (ib ~= ARDOUR.ChanMapping.Invalid) then
-- check ringbuffer wrap-around if c == 1 then
if (write_ptr + n_samples < bufsiz) then -- first channel, copy as-is
if c == 1 then ARDOUR.DSP.copy_vector (mem, bufs:get_audio (ib):data (offset), n_samples)
ARDOUR.DSP.copy_vector (shmem:to_float (4 + write_ptr), bufs:get_audio (ib):data (offset), n_samples)
else
ARDOUR.DSP.mix_buffers_no_gain (shmem:to_float (4 + write_ptr), bufs:get_audio (ib):data (offset), n_samples)
end
else else
local w0 = bufsiz - write_ptr -- all other channels, add to existing data.
if c == 1 then ARDOUR.DSP.mix_buffers_no_gain (mem, bufs:get_audio (ib):data (offset), n_samples)
ARDOUR.DSP.copy_vector (shmem:to_float (4 + write_ptr), bufs:get_audio (ib):data (offset), w0)
ARDOUR.DSP.copy_vector (shmem:to_float (4) , bufs:get_audio (ib):data (offset + w0), n_samples - w0)
else
ARDOUR.DSP.mix_buffers_no_gain (shmem:to_float (4 + write_ptr), bufs:get_audio (ib):data (offset), w0)
ARDOUR.DSP.mix_buffers_no_gain (shmem:to_float (4) , bufs:get_audio (ib):data (offset + w0), n_samples - w0)
end
end end
ccnt = ccnt + 1;
-- copy data to output (if not processing in-place) -- copy data to output (if not processing in-place)
if (ob ~= ARDOUR.ChanMapping.Invalid and ib ~= ob) then if (ob ~= ARDOUR.ChanMapping.Invalid and ib ~= ob) then
ARDOUR.DSP.copy_vector (bufs:get_audio (ob):data (offset), bufs:get_audio (ib):data (offset), n_samples) ARDOUR.DSP.copy_vector (bufs:get_audio (ob):data (offset), bufs:get_audio (ib):data (offset), n_samples)
end end
else
-- invalid (unconnnected) input
if (write_ptr + n_samples < bufsiz) then
ARDOUR.DSP.memset (shmem:to_float (4 + write_ptr), 0, n_samples)
else
local w0 = bufsiz - write_ptr
ARDOUR.DSP.memset (shmem:to_float (4 + write_ptr), 0, w0)
ARDOUR.DSP.memset (shmem:to_float (4) , 0, n_samples - w0)
end
end end
end end
-- normalize 1 / channel-count -- Clear unconnected output buffers.
if audio_ins > 1 then -- In case we're processing in-place some buffers may be identical,
if (write_ptr + n_samples < bufsiz) then -- so this must be done *after processing*.
ARDOUR.DSP.apply_gain_to_buffer (shmem:to_float (4 + write_ptr), n_samples, 1 / audio_ins)
else
local w0 = bufsiz - write_ptr
ARDOUR.DSP.apply_gain_to_buffer (shmem:to_float (4 + write_ptr), w0, 1 / audio_ins)
ARDOUR.DSP.apply_gain_to_buffer (shmem:to_float (4) , n_samples - w0, 1 / audio_ins)
end
end
-- clear unconnected inplace buffers
for c = 1,audio_ins do for c = 1,audio_ins do
local ib = in_map:get (ARDOUR.DataType ("audio"), c - 1) -- get id of mapped input buffer for given cannel local ib = in_map:get (ARDOUR.DataType ("audio"), c - 1) -- get id of mapped input buffer for given cannel
local ob = out_map:get (ARDOUR.DataType ("audio"), c - 1) -- get id of mapped output buffer for given cannel local ob = out_map:get (ARDOUR.DataType ("audio"), c - 1) -- get id of mapped output buffer for given cannel
@ -125,11 +111,21 @@ function dsp_runmap (bufs, in_map, out_map, n_samples, offset)
end end
end end
write_ptr = (write_ptr + n_samples) % bufsiz -- Normalize gain (1 / channel-count)
shmem:atomic_set_int (0, write_ptr) if ccnt > 1 then
ARDOUR.DSP.apply_gain_to_buffer (mem, n_samples, 1 / ccnt)
end
-- if no channels were processed, feed silence.
if ccnt == 0 then
ARDOUR.DSP.memset (mem, 0, n_samples)
end
-- write data to the ringbuffer
rb:write (mem, n_samples)
-- emit QueueDraw every FPS -- emit QueueDraw every FPS
-- TODO: call every window-size worth of samples, at most every FPS -- TODO: call every FFT window-size worth of samples, at most every FPS
dpy_wr = dpy_wr + n_samples dpy_wr = dpy_wr + n_samples
if (dpy_wr > dpy_hz) then if (dpy_wr > dpy_hz) then
dpy_wr = dpy_wr % dpy_hz dpy_wr = dpy_wr % dpy_hz
@ -149,13 +145,10 @@ local last_log = false
function render_inline (ctx, w, max_h) function render_inline (ctx, w, max_h)
local ctrl = CtrlPorts:array () -- get control port array (read/write) local ctrl = CtrlPorts:array () -- get control port array (read/write)
local shmem = self:shmem () -- get shared memory region local tbl = self:table ():get () -- get shared memory table
local cfg = shmem:to_int (1):array () -- "cast" into lua-table local rate = tbl['samplerate']
local rate = cfg[1] if not cmem then
local buf_size = cfg[2] cmem = ARDOUR.DSP.DspShm (0)
if buf_size == 0 then
return
end end
-- get settings -- get settings
@ -187,6 +180,7 @@ function render_inline (ctx, w, max_h)
if not fft then if not fft then
fft = ARDOUR.DSP.FFTSpectrum (fft_size, rate) fft = ARDOUR.DSP.FFTSpectrum (fft_size, rate)
cmem:allocate (fft_size)
end end
if last_log ~= logscale then if last_log ~= logscale then
@ -226,25 +220,15 @@ function render_inline (ctx, w, max_h)
local f_b = w / math.log (fft_size / 2) -- inverse log-scale base local f_b = w / math.log (fft_size / 2) -- inverse log-scale base
local f_l = math.log (fft_size / rate) * f_b -- inverse logscale lower-bound local f_l = math.log (fft_size / rate) * f_b -- inverse logscale lower-bound
-- available samples in ring-buffer local rb = tbl['rb'];
local write_ptr = shmem:atomic_get_int (0) local mem = cmem:to_float (0)
local avail = (write_ptr + buf_size - read_ptr) % buf_size
while (avail >= fft_size) do while (rb:read_space() >= fft_size) do
-- process one line / buffer -- process one line / buffer
if read_ptr + fft_size < buf_size then rb:read (mem, fft_size)
fft:set_data_hann (shmem:to_float (read_ptr + 4), fft_size, 0) fft:set_data_hann (mem, fft_size, 0)
else
local r0 = buf_size - read_ptr
fft:set_data_hann (shmem:to_float (read_ptr + 4), r0, 0)
fft:set_data_hann (shmem:to_float (4), fft_size - r0, r0)
end
fft:execute () fft:execute ()
read_ptr = (read_ptr + fft_size) % buf_size
avail = (write_ptr + buf_size - read_ptr ) % buf_size
-- draw spectrum -- draw spectrum
assert (bpx >= 1) assert (bpx >= 1)