File: foo~.pd_lua

package info (click to toggle)
pd-lua 0.12.23%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,912 kB
  • sloc: ansic: 3,733; lisp: 66; makefile: 64
file content (42 lines) | stat: -rw-r--r-- 1,063 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
local foo = pd.Class:new():register("foo~")

function foo:initialize(sel, atoms)
   self.outlets = {SIGNAL}
   self.phase = 0
   self.cycle = 0
   self.freq  = 133
   return true
end

function foo:dsp(samplerate, blocksize)
   self.samplerate = samplerate
   self.blocksize = blocksize
end

function foo:random_walk(dmin, dmax)
   self.cycle = self.cycle + 1
   if self.cycle > 100 then
      self.freq = self.freq + math.random(-1, 1)*math.random(dmin, dmax)
      self.freq = math.min(1000, math.max(150, self.freq))
      self.cycle = 0
   end
end

function foo:perform()
   local dmin, dmax = 50, 100
   self:random_walk(dmin, dmax)
   local freq, a, b = self.freq, 0.3, 0.1
   local out = {} -- result table
   -- random phase offset
   local d = b * math.random()
   for i = 1, self.blocksize do
      -- phase in radians
      local x = 2 * math.pi * (self.phase + d)
      out[i] = a * math.sin(x)
      self.phase = self.phase + freq / self.samplerate
      if self.phase >= 1 then
         self.phase = self.phase - 1
      end
   end
   return out
end