librelist archives

« back to archive

distortion ugen

distortion ugen

From:
Tom Lieber
Date:
2010-01-10 @ 12:27
The source is in a crazy state with a bunch of stuff not checked in,
otherwise this ugen would probably be in by now. It soft-clips, then
caps the first derivative for some interesting distortion. It's a port
of some ChucK I wrote a while back; it's great to be able to write
UGens like this. :)

Distort = {
  new = function(class, pregain)
    return UGen.initialize_io({
      last_value = 0.0,
      pregain = pregain or 100.0,
      max_slew = 0.5,
      tick = function(self)
        if not(now() == self.last_tick) then
          local i = UGen.sum_inputs(self) * self.pregain;
          local out = 0.0;

          -- make roundy
          if i >= 1 then
            out = 2.0 / 3.0
          elseif i <= -1 then
            out = -2.0 / 3.0
          else
            out = i - (i * i * i) / 3.0
          end

          -- max slew
          if math.abs(out - self.last_value * 2.0 / 3.0) > self.max_slew then
            if out < self.last_value * 2.0 / 3.0 then
              out = self.last_value * 2.0 / 3.0 - self.max_slew
            else
              out = self.last_value * 2.0 / 3.0 + self.max_slew
            end
          end

          self.last_value = out * 3.0 / 2.0;
          self.last_tick = now();
        end
        return self.last_value
      end
    })
  end
}

-- 
Tom Lieber
http://AllTom.com/
http://ckvlang.org/