/
main.lua
212 lines (182 loc) · 4.69 KB
/
main.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
osc = {}
function osc.rect(f)
return function(t)
local t = (t * f) % 1
if t < .5 then return -1 end
return 1
end
end
function osc.saw(f)
return function(t)
local t = (t * f) % 1
return 2 * t - 1
end
end
function osc.tri(f)
return function(t)
local t = (t * f) % 1
if t < .5 then return 4 * t - 1 end
return 3 - 4 * t
end
end
function osc.sin(f)
return function(t)
return math.sin(2 * math.pi * t * f)
end
end
function osc.wn()
return function()
return math.random() * 2 - 1
end
end
function osc.pn()
local val = 0
return function()
val = math.min(1, math.max(-1, math.random() * 2 - 1 + val))
return val
end
end
function makesample(osc, len, rate)
local len, rate = len or 1, rate or 44100
local samples = math.floor(len * rate)
local sd = love.sound.newSoundData(samples, rate, 16, 1)
for i = 0,samples do
sd:setSample(i, osc(i / rate))
end
return sd
end
env = {}
function env.const(v)
return function() return v end
end
function env.fall(len)
return function(t)
return math.max(0, 1 - t / len)
end
end
function env.rise(len)
return function(t)
return math.min(1, t / len)
end
end
function env.risefall(rise,decay, fall)
return function(t)
if t > rise + decay then
return math.max(0, 1 - (t - decay - rise) / fall)
end
return math.min(1, t / rise)
end
end
function envelope(f, ...)
local envelopes = {...}
return function(t)
local v = f(t)
for _, g in ipairs(envelopes) do
v = v * g(t)
end
return v
end
end
pitch = {}
function pitch.step(k)
return math.pow(math.pow(2, 1/12), k)
end
pitch.fractions = {
["c"] = pitch.step(-9),
["c#"] = pitch.step(-8),
["d"] = pitch.step(-7),
["d#"] = pitch.step(-6),
["e"] = pitch.step(-5),
["f"] = pitch.step(-4),
["f#"] = pitch.step(-3),
["g"] = pitch.step(-2),
["g#"] = pitch.step(-1),
["a"] = 1,
['a#'] = pitch.step(1),
['b'] = pitch.step(2),
}
function pitch.base(n)
return 440 * math.pow(2, n - 4)
end
function pitch.get(p, octave)
local base = pitch.base(octave or 4)
return pitch.fractions[p] * base
end
grid = {}
function love.load()
love.graphics.setBackgroundColor(100,70,20)
love.graphics.setFont(60)
for i = 1,16 do
grid[i] = {}
end
end
local time, line, pause = 0, 1, true
function love.draw()
for i = 1,16 do
for k = 1,12 do
if not grid[i][k] then
love.graphics.setColor(40,20,0)
elseif grid[i][k].osc == osc.sin then
love.graphics.setColor(40,80,190)
elseif grid[i][k].osc == osc.tri then
love.graphics.setColor(40,190,80)
elseif grid[i][k].osc == osc.saw then
love.graphics.setColor(190,190,50)
elseif grid[i][k].osc == osc.rect then
love.graphics.setColor(190,40,40)
end
love.graphics.rectangle('fill', (i-1)*50 + 1, (k-1)*50+1, 48, 48)
end
end
love.graphics.setColor(255,255,255,40)
love.graphics.rectangle('fill', (line-1)*50, 0, 50, love.graphics.getHeight()-1)
if pause then
love.graphics.setColor(255,255,255,100)
love.graphics.print("LOVE ON HALT", 10, 50)
end
end
function love.update(dt)
if pause then
return
end
time = time + dt
if time >= .25 then
time = time - .25
line = line + 1
if line > 16 then line = 1 end
for k = 1,12 do
if grid[line][k] then
love.audio.play(grid[line][k].source)
end
end
end
end
function love.keyreleased(key)
if key == 'p' then
pause = not pause
end
end
local info = {
[osc.sin] = {next = osc.tri, amp = .7, oct = 5},
[osc.tri] = {next = osc.saw, amp = .6, oct = 4},
[osc.saw] = {next = osc.rect, amp = .3, oct = 3},
[osc.rect] = {next = osc.sin, amp = .2, oct = 2},
}
local pitchtable = { 'c', 'c#', 'd', 'd#', 'e', 'f', 'f#', 'g', 'g#', 'a', 'a#', 'b' }
function love.mousereleased(x,y, btn)
local i,k = math.floor(x/50) + 1, math.floor(y/50)+1
if btn == 'r' then
grid[i][k] = nil
return
end
if not grid[i][k] then
grid[i][k] = { osc = osc.sin }
else
grid[i][k] = { osc = info[ grid[i][k].osc ].next }
end
local theosc = grid[i][k].osc
grid[i][k].source = love.audio.newSource(makesample(envelope(
theosc(pitch.get( pitchtable[13 - k], info[ theosc ].oct)),
env.const(info[ theosc ].amp),
env.risefall(.1,.22,.2)), .25))
end