aboutsummaryrefslogtreecommitdiff
path: root/examples/luametro.pd_lua
blob: 8041e6e98819b53f4ce1d263f0f4ad7bef9b75b9 (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
-- reimplementation of [metro] with a twist
-- fbar 2007

-- we need an urn:
local urn = {}

function urn.new(size) 
    assert(size > 0, "Error: size of urn must be greater than 0")
    local t = {size=size, last=size}
    for i=1,size do
        t[i] = i
    end
    return t
end

function urn.get(u)
    if u.last > 0 then
        local i = math.random(u.last)
        local res = u[i]
        u[i] = u[u.last]
        u.last = u.last - 1
        return res
    else
        return nil
    end
end

function urn.reset(u)
    u.last = u.size
    for i=1,u.size do
        u[i] = i
    end
end

local modes = {"alea", "series", "sequence", "rota"}

local function contains(t,x)
    for k,v in pairs(t) do
        if v == x then return true end
    end
    return false
end

-- pd:

local M = pd.Class:new():register("luametro")

function M:initialize(name, atoms)
    self.periods = {}
    self.i = 1
    self.mode = "sequence"
    self.direction = 1
    self.u = urn.new(math.max(1, #atoms))
    if not self:setperiods(atoms) then return false end
    self.inlets = 2
    self.outlets = 2 
    return true
end

function M:postinitialize()
    self.clock = pd.Clock:new():register(self, "tick")
end

function M:finalize()
    self.clock:destruct()
end

function M:setperiods(periods)
    self.periods = {}
    for i,j in ipairs(periods) do
        if type(j) == "number" then
            self.periods[i] = math.max(0.01, j)
        else
            self:error("non-number in period list!")
            return false
        end
    end
    -- start over at front:
    self.i = 1
    return true
end

function M:in_2(sel, atoms)
    if sel == "list" or sel == "float" then
        self:setperiods(atoms)
    end
end

function M:in_1_float(f)
    if f ~= 0 then
        self:tick()
    else
        self.clock:unset()
    end
end

function M:in_1_bang()
    self.i = 1
    self:tick()
end

function M:in_1_start()
    self.i = 1
    self:tick()
end 

function M:in_1_stop()
    self.clock:unset()
end    

function M:in_1_mode(atoms)
    if type(atoms[1]) == "number" then
        self.mode = modes[atoms[1]]
        if self.mode == "series" then
            self.u = urn.new(#self.periods)
        end
        self.i = 1
    elseif type(atoms[1]) == "string" then
        if contains(modes, atoms[1]) then
            self.mode = atoms[1]
        else 
            self:error("no such mode: " .. atoms[1])
        end
    end
    pd.post("Mode set to: " .. self.mode)
end    


function M:tick()
    self:outlet(1, "bang", {})
    -- pd.post("selection: " .. tostring(self.i))
    if type(self.periods[self.i]) == "number" then
        self.clock:delay(self.periods[self.i])
    end
    if self.mode == "sequence" then
        self.i = self.i + 1 
        if self.i > #self.periods then 
            self.i = 1 
            self:outlet(2, "bang", {})
        end
    elseif self.mode == "alea" then
        self.i = math.random(#self.periods)
    elseif self.mode == "series" then
        local f = urn.get(self.u)
        if not f then
            urn.reset(self.u)
            f = urn.get(self.u)
            self:outlet(2, "bang", {})
        end
        self.i = f
    elseif self.mode == "rota" then
        self.i = self.i + self.direction
        if self.i > #self.periods or self.i < 1 then 
            self.direction = -self.direction
            self.i = self.i + self.direction
            self:outlet(2, "bang", {})
        end
    else
        self:error("Unknown mode")
    end
end