-
Notifications
You must be signed in to change notification settings - Fork 0
/
First.lua
95 lines (83 loc) · 1.87 KB
/
First.lua
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
--- unique melody box
-- each crow sings it's own song
length = {}
rhythm = {}
notes = {}
step = {}
bits = {}
scale = { {0,2,4,7,9}, {0,2,4,5,7,9,11} }
decay = 0.4
attack = 0.04
function set_d(t) decay = (t-1)/8 + 0.05 end
function set_a(t) attack = (t-1)/64 + 0.003 end
function play(out,ix)
-- play rhythm
if rhythm[ix][ step[ix] ] & 8 == 8 then
if ix == 1 then set_d(t[ix]) else set_a(t[ix]) end
t[ix] = 0
output[out+1]()
end
-- set note
if rhythm[ix+2][ step[ix+2] ] & 8 == 8 then
n1 = notes[ix][ step[ix+2] ]
n2 = notes[ix][ step[ix] ]
abs = math.abs(input[2].volts)/5
note = n1 + abs*(n2-n1)
note = math.floor(note * (abs*3 + 0.1))
s = scale[input[2].volts > -0.04 and 1 or 2]
nn = s[ note%(#s) + 1 ]
oct = math.floor(note/12)
output[out].volts = nn/12 + oct
end
step[ix] = (step[ix] % length[ix]) + 1
step[ix+2] = (step[ix+2] % length[ix+2]) + 1
end
input[1].change = function(s)
play(1,1)
play(3,2)
end
sd = 0
function lcg(seed)
local s = seed or sd
sd = (1103515245*s + 12345) % (1<<31)
return sd
end
function get_d() return decay end
function get_a() return attack end
t = {0,0}
function env(count)
for i=1,2 do t[i] = t[i] + 1 end
end
function init()
-- generate unique tables
lcg(unique_id())
lcg()
lcg()
lcg()
for i=1,4 do
length[i] = lcg()%19 + 6
rhythm[i] = {}
for n=1,32 do
rhythm[i][n] = lcg()
end
step[i] = 1
end
-- notes
for i=1,4 do
notes[i] = {0}
for n=1,31 do
notes[i][n+1] = notes[i][n] + (lcg() % 7) -3
end
end
-- out params
output[1].slew = 0
output[1].volts = 0
output[2].action = ar(get_a,get_d)
output[3].slew = 0.01
output[3].volts = 0
output[4].action = ar(get_a,get_d)
-- start sequence!
input[1]{ mode = 'change', direction = 'rising' }
dec = metro.init{ event = env, time = 0.1 }
dec:start()
end