Optimize automation-event process splitting
[ardour.git] / scripts / spectrogram.lua
index 94ae043914cf1c43114a1a6c7df82ad56556ee4b..86419682b42b5858a1913a70d40f1bff7944c37c 100644 (file)
@@ -1,12 +1,10 @@
 ardour {
        ["type"]    = "dsp",
-       name        = "Inline Spectrogram",
+       name        = "a-Inline Spectrogram",
        category    = "Visualization",
-       license     = "GPLv2",
-       author      = "Robin Gareus",
-       email       = "robin@gareus.org",
-       site        = "http://gareus.org",
-       description = [[An Example DSP Plugin to display a spectrom on the mixer strip]]
+       license     = "MIT",
+       author      = "Ardour Team",
+       description = [[Mixer strip inline spectrum display]]
 }
 
 -- return possible i/o configurations
@@ -42,94 +40,112 @@ function dsp_params ()
        }
 end
 
+-- symbolic names for shmem offsets
+local SHMEM_RATE = 0
+local SHMEM_WRITEPTR = 1
+local SHMEM_AUDIO = 2
+
+-- a C memory area.
+-- It needs to be in global scope.
+-- When the variable is set to nil, the allocated memory is free()ed.
+-- the memory can be interpeted as float* for use in DSP, or read/write
+-- to a C++ Ringbuffer instance.
+-- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP:DspShm
+local cmem = nil
+
 function dsp_init (rate)
        -- global variables (DSP part only)
-       samplerate = rate
-       bufsiz = 2 * rate
        dpy_hz = rate / 25
        dpy_wr = 0
-end
 
-function dsp_configure (ins, outs)
-       -- store configuration in global variable
-       audio_ins = ins:n_audio ()
-       -- allocate shared memory area, ringbuffer between DSP/GUI
-       self:shmem ():allocate (4 + bufsiz)
-       self:shmem ():clear ()
-       self:shmem ():atomic_set_int (0, 0)
-       local cfg = self:shmem ():to_int (1):array ()
-       cfg[1] = samplerate
-       cfg[2] = bufsiz
+       -- create a shared memory area to hold the sample rate, the write_pointer,
+       -- and (float) audio-data. Make it big enough to store 2s of audio which
+       -- should be enough. If not, the DSP will overwrite the oldest data anyway.
+       self:shmem ():allocate(2 + 2 * rate)
+       self:shmem ():clear()
+       self:shmem ():atomic_set_int (SHMEM_RATE, rate)
+       self:shmem ():atomic_set_int (SHMEM_WRITEPTR, 0)
+
+       -- allocate memory, local mix buffer
+       cmem = ARDOUR.DSP.DspShm (8192)
 end
 
+-- "dsp_runmap" uses Ardour's internal processor API, eqivalent to
+-- 'connect_and_run()". There is no overhead (mapping, translating buffers).
+-- The lua implementation is responsible to map all the buffers directly.
 function dsp_runmap (bufs, in_map, out_map, n_samples, offset)
-       local shmem = self:shmem ()
-       local write_ptr = shmem:atomic_get_int (0)
+       -- here we sum all audio input channels and then copy the data to a
+       -- custom-made circular table for the GUIs to process later
+
+       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
+       local rate = self:shmem ():atomic_get_int (SHMEM_RATE)
+       local write_ptr  = self:shmem ():atomic_get_int (SHMEM_WRITEPTR)
+
+       local ringsize = 2 * rate
+       local ptr_wrap = math.floor(2^50 / ringsize) * ringsize
 
-       -- sum channels, copy to ringbuffer
        for c = 1,audio_ins do
+               -- see http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:ChanMapping
                -- 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 ob = out_map:get (ARDOUR.DataType ("audio"), c - 1) -- get id of mapped output buffer for given cannel
+               local ib = in_map:get (ARDOUR.DataType ("audio"), c - 1) -- get index of mapped input buffer
+               local ob = out_map:get (ARDOUR.DataType ("audio"), c - 1) -- get index of mapped output buffer
+
+               -- check if the input is connected to a buffer
                if (ib ~= ARDOUR.ChanMapping.Invalid) then
-                       -- check ringbuffer wrap-around
-                       if (write_ptr + n_samples < bufsiz) then
-                               if c == 1 then
-                                       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
+
+                       -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:AudioBuffer
+                       -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP
+                       if c == 1 then
+                               -- first channel, copy as-is
+                               ARDOUR.DSP.copy_vector (mem, bufs:get_audio (ib):data (offset), n_samples)
                        else
-                               local w0 = bufsiz - write_ptr
-                               if c == 1 then
-                                       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
+                               -- all other channels, add to existing data.
+                               ARDOUR.DSP.mix_buffers_no_gain (mem, bufs:get_audio (ib):data (offset), n_samples)
                        end
+                       ccnt = ccnt + 1;
+
                        -- copy data to output (if not processing in-place)
                        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)
                        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
-
-       -- normalize  1 / channel-count
-       if audio_ins > 1 then
-               if (write_ptr + n_samples < bufsiz) then
-                       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
+       -- Clear unconnected output buffers.
+       -- In case we're processing in-place some buffers may be identical,
+       -- so this must be done  *after processing*.
        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 ob = out_map:get (ARDOUR.DataType ("audio"), c - 1) -- get id of mapped output buffer for given cannel
+               local ib = in_map:get (ARDOUR.DataType ("audio"), c - 1)
+               local ob = out_map:get (ARDOUR.DataType ("audio"), c - 1)
                if (ib == ARDOUR.ChanMapping.Invalid and ob ~= ARDOUR.ChanMapping.Invalid) then
                        bufs:get_audio (ob):silence (n_samples, offset)
                end
        end
 
-       write_ptr = (write_ptr + n_samples) % bufsiz
-       shmem:atomic_set_int (0, write_ptr)
+       -- Normalize gain (1 / channel-count)
+       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 circular table
+       if (write_ptr % ringsize + n_samples < ringsize) then
+               ARDOUR.DSP.copy_vector (self:shmem ():to_float (SHMEM_AUDIO + write_ptr % ringsize), mem, n_samples)
+       else
+               local chunk = ringsize - write_ptr % ringsize
+               ARDOUR.DSP.copy_vector (self:shmem ():to_float (SHMEM_AUDIO + write_ptr % ringsize), mem, chunk)
+               ARDOUR.DSP.copy_vector (self:shmem ():to_float (SHMEM_AUDIO), cmem:to_float (chunk), n_samples - chunk)
+       end
+       self:shmem ():atomic_set_int (SHMEM_WRITEPTR, (write_ptr + n_samples) % ptr_wrap)
 
        -- 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
        if (dpy_wr > dpy_hz) then
                dpy_wr = dpy_wr % dpy_hz
@@ -145,16 +161,14 @@ local read_ptr = 0
 local line = 0
 local img = nil
 local fft_size = 0
+local last_log = false
+
 
 function render_inline (ctx, w, max_h)
        local ctrl = CtrlPorts:array () -- get control port array (read/write)
-       local shmem = self:shmem () -- get shared memory region
-       local cfg = shmem:to_int (1):array () -- "cast" into lua-table
-       local rate = cfg[1]
-       local buf_size = cfg[2]
-
-       if buf_size == 0 then
-               return
+       local rate = self:shmem ():atomic_get_int (SHMEM_RATE)
+       if not cmem then
+               cmem = ARDOUR.DSP.DspShm (0)
        end
 
        -- get settings
@@ -186,6 +200,13 @@ function render_inline (ctx, w, max_h)
 
        if not fft then
                fft = ARDOUR.DSP.FFTSpectrum (fft_size, rate)
+               cmem:allocate (fft_size)
+       end
+
+       if last_log ~= logscale then
+               last_log = logscale
+               img = nil
+               line = 0
        end
 
        -- calc height
@@ -208,36 +229,53 @@ function render_inline (ctx, w, max_h)
        -- re-create image surface
        if not img or img:get_width() ~= w or img:get_height () ~= h then
                img = Cairo.ImageSurface (Cairo.Format.ARGB32, w, h)
+               line = 0
        end
-
-       -- read ring-buffer, analyze
-       local write_ptr = shmem:atomic_get_int (0)
-       local avail = (write_ptr + buf_size - read_ptr) % buf_size
-
        local ictx = img:context ()
 
-       while (avail >= fft_size) do
-               -- process one line / buffer
-               if read_ptr + fft_size < buf_size then
-                       fft:set_data_hann (shmem:to_float (read_ptr + 4), fft_size, 0)
+       local bins = fft_size / 2 - 1 -- fft bin count
+       local bpx = bins / w  -- bins per x-pixel (linear)
+       local fpb = rate / fft_size -- freq-step per bin
+       local f_e = rate / 2 / fpb -- log-scale exponent
+       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 mem = cmem:to_float (0)
+
+       local ringsize = 2 * rate
+       local ptr_wrap = math.floor(2^50 / ringsize) * ringsize
+
+       local write_ptr
+       function read_space()
+               write_ptr   = self:shmem ():atomic_get_int (SHMEM_WRITEPTR)
+               local space = (write_ptr - read_ptr + ptr_wrap) % ptr_wrap
+               if space > ringsize then
+                       -- the GUI lagged too much and unread data was overwritten
+                       -- jump to the oldest audio still present in the ringtable
+                       read_ptr = write_ptr - ringsize
+                       space = ringsize
+               end
+               return space
+       end
+
+       while (read_space() >= fft_size) do
+               -- read one window from the circular table
+               if (read_ptr % ringsize + fft_size < ringsize) then
+                       ARDOUR.DSP.copy_vector (mem, self:shmem ():to_float (SHMEM_AUDIO + read_ptr % ringsize), fft_size)
                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)
+                       local chunk = ringsize - read_ptr % ringsize
+                       ARDOUR.DSP.copy_vector (mem, self:shmem ():to_float (SHMEM_AUDIO + read_ptr % ringsize), chunk)
+                       ARDOUR.DSP.copy_vector (cmem:to_float(chunk), self:shmem ():to_float (SHMEM_AUDIO), fft_size - chunk)
                end
+               read_ptr = (read_ptr + fft_size) % ptr_wrap
 
+               -- process one line
+               fft:set_data_hann (mem, fft_size, 0)
                fft:execute ()
 
-               read_ptr = (read_ptr + fft_size) % buf_size
-               avail = (write_ptr + buf_size - read_ptr ) % buf_size
-
                -- draw spectrum
-               local bins = fft_size / 2 - 1
-               local bpx = bins / w
-               local fpb = rate / fft_size
                assert (bpx >= 1)
 
-               local f_e = rate / 2 / fpb
                -- scroll
                if line == 0 then line = h - 1; else line = line - 1; end
 
@@ -292,5 +330,34 @@ function render_inline (ctx, w, max_h)
                ctx:fill ()
        end
 
+
+       -- draw grid on top
+       function x_at_freq (f)
+               if logscale then
+                       return f_l + f_b * math.log (f)
+               else
+                       return 2 * w * f / rate;
+               end
+       end
+
+       function grid_freq (f)
+               -- draw vertical grid line
+               local x = .5 + math.floor (x_at_freq (f))
+               ctx:move_to (x, 0)
+               ctx:line_to (x, h)
+               ctx:stroke ()
+       end
+
+       -- draw grid on top
+       local dash3 = C.DoubleVector ()
+       dash3:add ({1, 3})
+       ctx:set_line_width (1.0)
+       ctx:set_dash (dash3, 2) -- dotted line
+       ctx:set_source_rgba (.5, .5, .5, .8)
+       grid_freq (100)
+       grid_freq (1000)
+       grid_freq (10000)
+       ctx:unset_dash ()
+
        return {w, h}
 end