ckv blog: Delay ugen

Posted January 4, 2010

I created a Delay unit generator in Lua to see how much we can expect where large arrays of samples are concerned. At 44100 sample rate, this doesn't start dropping samples for me until the buffer's at least a couple hundred milliseconds long. A second-long delay line clicks every second, but it's definitely not as bad as I was expecting.

Delay = {
  new = function(class, delay)
    return UGen.initialize_io({
      delay = delay or second,
      last_value = 0.0,
      last_values = {},
      tick = function(self)
        if not(now() == self.last_tick) then
          local vals = self.last_values;
          local offset = math.ceil(self.delay); -- no sub-samples for you!

          vals[#vals + 1] = UGen.sum_inputs(self);

          self.last_value = vals[#vals - offset] or 0.0;

          if #vals > offset * 2 then
            for i = 1, offset, 1 do
              vals[i] = vals[#vals - offset + i]
            end

            for i = offset + 1, #vals, 1 do
              vals[i] = nil
            end
          end

          self.last_tick = now();
        end
        return self.last_value
      end
    })
  end
}

You can e-mail comments to tom@alltom.com


Home