File: lua_sig.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 (48 lines) | stat: -rw-r--r-- 1,392 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
43
44
45
46
47
48
local lua_sig = pd.Class:new():register("lua_sig")

function lua_sig:initialize(sel, atoms)
    self.inlets = {SIGNAL}
    self.outlets = {SIGNAL}
    self.phase = 0
    self.freq = 220
    self.amp = 0.5
    return true
end

-- dsp method is called when processing is about to start
function lua_sig:dsp(samplerate, blocksize)
    self.samplerate = samplerate
end

-- message to set frequency...
function lua_sig:in_1_freq(atoms)
    self.freq = atoms[1]
end

-- ... and amplitude.
function lua_sig:in_1_amp(atoms)
    self.amp = atoms[1]
end

-- perform method gets called with a table for each signal inlet
-- you must return a table for each signal outlet
function lua_sig:perform(in1)
    local frequency = self.freq   -- frequency of the sine wave in Hz
    local amplitude = self.amp    -- amplitude of the sine wave (0 to 1)

    -- Calculate the angular frequency (in radians per sample)
    local angular_freq = 2 * math.pi * frequency / self.samplerate

    -- Loop through each sample index
    for i = 1, #in1 do
       -- NOTE: We ignore the input signal in this example. Try multiplying by
       -- in1[i] for an amplitude/ring modulation effect.
        in1[i] = amplitude * math.sin(self.phase)
        self.phase = self.phase + angular_freq
        if self.phase >= 2 * math.pi then
            self.phase = self.phase - 2 * math.pi
        end
    end

    return in1
end