-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy paththree-eyes.lua
executable file
·314 lines (261 loc) · 8.03 KB
/
three-eyes.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
-- three eyes (nc01-drone)
-- that world in their eyes
-- @2994898
--
-- E1 volume
-- E2 brightness
-- - playback rate modifier
-- E3 density
-- - length of each voices
-- K2 evolve
-- - add voice or replace the
-- oldest voice (long press
-- *1 sec.* to remove an
-- oldest voice)
-- K3 change worlds
-- - rebalance volume of
-- each voices
local sc = softcut -- hail to Tehn
local file = _path.code .. "nc01-drone/lib/eb.wav"
local length = 0
local voices = {}
local voiceLoopPoints = {}
local voiceIndex = 0
local perspectives = {
{1, 0.8, 0.7, 0.5, 0.3, 0.1},
{0.7, 0.8, 0.7, 0.5, 0.3, 0.2},
{0.2, 0.4, 0.7, 0.1, 0.7, 0.4}
}
local perspectiveIndex = 1
local distances = { 15, 7, 10 }
local volume = 80
local brightness = 50
local density = 80
function freeCounter(id)
if id ~= 0 and metro.assigned[id] then
metro.free(id)
end
end
function setVoiceProps(index, transitionTime, shouldRateTransition)
presence = perspectives[perspectiveIndex][index]
local rateFactor = util.linlin(1, 100, -1.5, 0.5, brightness)
local panValue = util.linlin(0, 1, -1, 0, presence)
if math.floor(panValue * 10) % 2 == 1 then panValue = -1 * panValue end
sc.fade_time(index, presence)
sc.level_slew_time(index, transitionTime)
sc.pan_slew_time(index, transitionTime)
if not shouldRateTransition or false then
sc.rate_slew_time(index, 0.1)
else
sc.rate_slew_time(index, transitionTime * 2)
end
sc.loop_end(voiceIndex, util.linlin(1, 100, voiceLoopPoints[index][1] + 1, voiceLoopPoints[index][2], density))
sc.rate(index, util.clamp(presence + rateFactor, -2, 1))
sc.pan(index, panValue)
sc.post_filter_fc(index, 10000 * presence)
sc.post_filter_rq(index, 2 * presence)
sc.post_filter_bp(index, 1 - presence)
sc.level(index, util.linexp(1, 100, 0.001, presence, volume))
end
function addVoice()
voiceIndex = voiceIndex + 1
if voiceIndex == 7 then voiceIndex = 1 end
voiceLoopPoints[voiceIndex] = {}
voiceLoopPoints[voiceIndex][1] = math.random(math.floor(length) - 2)
voiceLoopPoints[voiceIndex][2] = voiceLoopPoints[voiceIndex][1] + util.clamp(math.random(math.floor(length) - voiceLoopPoints[voiceIndex][1]), 2, 25)
sc.enable(voiceIndex, 1)
sc.buffer(voiceIndex, 1)
sc.level(voiceIndex, 0)
sc.loop(voiceIndex, 1)
sc.loop_start(voiceIndex, voiceLoopPoints[voiceIndex][1])
sc.loop_end(voiceIndex, voiceLoopPoints[voiceIndex][2])
sc.position(voiceIndex, voiceLoopPoints[voiceIndex][1])
sc.play(voiceIndex, 1)
setVoiceProps(voiceIndex, 7)
table.insert(voices, voiceIndex)
end
function removeVoice(transitionTime)
if #voices == 0 then return end
sc.level_slew_time(voices[1], transitionTime or 3)
sc.level(voices[1], 0)
local counterId = 0
local counter = metro.init(function()
if #voices == 0 then
-- better to do with throttle removeVoice tho, this is cheap enough to call even if it's redundant
return
end
sc.play(voices[1], 0)
table.remove(voices, 1)
freeCounter(counterId)
end, transitionTime or 3, 1)
counterId = counter.props.id
counter:start()
end
-- k2_counter: to detect key press / long press
local k2_isLongPress = false
function handle_k2_counter()
removeVoice(5)
k2_isLongPress = true
end
local k2_counter = metro.init(handle_k2_counter, 1, 1)
function fadePerspective()
perspectiveIndex = perspectiveIndex + 1
if perspectiveIndex == 4 then perspectiveIndex = 1 end
if #voices ~= 0 then
local counterId = 0
local counter = metro.init(function(c)
setVoiceProps(c, distances[perspectiveIndex], true)
if c == #voices then freeCounter(counterId) end
end, distances[perspectiveIndex] / 3, #voices)
counterId = counter.props.id
counter:start()
end
redraw()
end
function evolve()
local counterId = 0
local counter = metro.init(function()
addVoice()
freeCounter(counterId)
end, 0.1, 1)
if #voices == 6 then
removeVoice()
counter.time = 3
end
counterId = counter.props.id
counter:start()
end
function key(n, z)
if n == 3 and z == 1 then
fadePerspective()
elseif n == 2 then
if z == 0 then
k2_counter:stop()
if not k2_isLongPress then evolve() end
else
k2_isLongPress = false;
k2_counter:start()
end
end
end
function enc(n, d)
if n == 1 then
volume = util.clamp(volume + d, 1, 100)
for i = 1, #voices do
setVoiceProps(voices[i], 0.01)
end
redraw()
return
elseif n == 2 then
brightness = util.clamp(brightness + d, 1, 100)
redraw()
elseif n == 3 then
density = util.clamp(density + d, 1, 100)
end
setVoiceProps(voiceIndex, 0.01)
end
function init()
sc.buffer_read_mono(file, 0, 0, -1, 1, 1)
_, length, sr = audio.file_info(file)
length = length / sr
addVoice()
end
-- visual stuff
-- coordinates { x, y } and direction
local eyeBases = {
{
ltr = true,
edge = { 40, 40 },
size = { 72, 31 }
},
{
ltr = false,
edge = { 84, 32 },
size = { 82, 34 }
},
{
ltr = true,
edge = { 40, 24 },
size = { 62, 32 }
}
}
local irisX
local irisY
local irisSize = 14
local blinkState = 1
function handle_animate_counter(count)
local currentEye = eyeBases[perspectiveIndex]
local x = currentEye.edge[1] + util.round(((currentEye.ltr and 1 or -1) * currentEye.size[1]) / 2 + (irisSize / 1.5))
irisX = math.random(currentEye.ltr and x - 3 or x - 6, currentEye.ltr and x + 3 or x + 6)
if blinkState == 3 then
blinkState = 2
elseif count % 2 == 0 then
blinkState = util.clamp(math.random(blinkState - 1, blinkState + 1), 0, 3)
end
redraw()
end
local animate_counter = metro.init(handle_animate_counter, 5, -1)
animate_counter:start()
function drawEye()
local eye = eyeBases[perspectiveIndex]
irisX = eye.edge[1] + util.round(((eye.ltr and 1 or -1) * eye.size[1]) / 2 + (irisSize / 1.5))
irisY = eye.edge[2] - util.round(eye.size[2] * 0.6)
-- NOTE: pls disregard about these magics..
local magic_four = util.linlin(0, 3, 0, 16, blinkState)
local magic_six = util.linlin(0, 3, 0, 8, blinkState)
screen.move(eye.edge[1], eye.edge[2])
screen.level(util.round(util.linlin(1, 100, 0, 13, volume)))
screen.curve(
util.round(eye.edge[1] + ((eye.ltr and 0.3 or -0.3) * eye.size[1])),
util.round(eye.edge[2] - (eye.size[2] * 0.75) + magic_four),
util.round(eye.edge[1] + ((eye.ltr and 0.75 or -0.75) * eye.size[1])),
util.round(eye.edge[2] - (eye.size[2] * 0.65) + magic_four),
eye.edge[1] + ((eye.ltr and 1 or -1) * eye.size[1]),
eye.edge[2]
)
screen.curve(
util.round(eye.edge[1] + ((eye.ltr and 1 or -1) * eye.size[1]) + ((eye.ltr and -0.65 or 0.65) * eye.size[1])),
util.round(eye.edge[2] + (eye.size[2] * 0.5625) - magic_six),
util.round(eye.edge[1] + ((eye.ltr and 1 or -1) * eye.size[1]) + ((eye.ltr and -0.875 or 0.875) * eye.size[1])),
util.round(eye.edge[2] - (eye.size[2] * 0.125) + blinkState - magic_six),
eye.edge[1],
eye.edge[2]
)
screen.stroke()
screen.level(util.round(util.linlin(1, 100, 0, util.linlin(0, 3, 8, 4, blinkState), volume)))
screen.arc(
util.round(irisX - (irisSize * 0.4)) + blinkState,
util.round(irisY + (irisSize * 0.9)) + blinkState,
irisSize,
-1 * math.pi * util.linexp(0, 3, 0.22, 0.01, blinkState),
math.pi * util.linexp(0, 3, 0.4, 0.15, blinkState)
)
screen.move_rel(-1 * util.linlin(0, 3, 0.01, 20, blinkState), 0)
screen.arc(
util.round(irisX - (irisSize * 0.4)) + blinkState,
util.round(irisY + (irisSize * 0.9)) + blinkState,
irisSize,
math.pi * util.linexp(0, 3, 0.65, 0.9, blinkState),
math.pi * util.linexp(0, 3, 1.22, 1.00, blinkState)
)
screen.stroke()
if blinkState ~= 3 then
screen.circle(
irisX - util.round(irisSize * 0.4) + blinkState,
irisY + util.round(irisSize * 0.85) + blinkState,
util.linexp(0, 3, util.linlin(1, 100, 6, 3, brightness), 3, blinkState)
)
screen.fill()
end
end
function redraw()
screen.clear()
screen.aa(1)
drawEye()
screen.move(120, 62)
screen.level(14)
screen.font_face(10)
screen.font_size(14)
screen.text(perspectiveIndex)
screen.update()
end