-
Notifications
You must be signed in to change notification settings - Fork 0
/
lemniscate.lua
351 lines (304 loc) · 8.04 KB
/
lemniscate.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
-- lemniscate
-- enc3 -> loop length
-- k1 + k2 -> toggle record
-- k2 -> toggle play
-- k1 + k3 -> stop all
-- k3 -> program select
-- k1 + k2 + k3 -> clear buffer
local util = require('util')
local Parameters = include('lib/parameters')
engine.name = 'Lemnisc8'
local ASSET_PATH = '/home/we/dust/code/lemniscate/assets/bg_frames/'
local MAX_PROGRAM_LENGTH = 87
local MIN_PROGRAM_LENGTH = 1
local MAX_TAPE_VISUALIVATION_WIDTH = 24
local MIN_TAPE_VISUALIZATION_WIDTH = MAX_TAPE_VISUALIVATION_WIDTH / (MAX_PROGRAM_LENGTH * 4)
local MAX_BG_FRAMES = 6
local PROGRAM_CELL_DIMENSIONS = {3, 14}
local PROGRAM_CELLS = {
{74, 26},
{78, 26},
{82, 26},
{86, 26}
}
local alt = false
local bg_frame = 1
local clear_time = 0.75
local frame_clock = nil
local parameters = nil
local playing = 0
local position = 1
local program = 1
local program_length = 10
local rec_amp = 1.0
local recording = 0
local shift = false
local tape_length = 40
local tape_visualization_width = 8
local throttle_keys = false
local function _bin_to_bool(v)
return v == 1
end
local function _animate_background()
if _bin_to_bool(playing) or _bin_to_bool(recording) then
bg_frame = util.wrap(bg_frame + 1, 1, MAX_BG_FRAMES)
end
end
local function _refresh_position(i, pos)
position = math.floor(pos)
end
local function _set_head_position()
for i = 1, 2 do
softcut.position(i, position)
end
softcut.voice_sync(2, 1, 0)
end
local function _set_tape_width()
tape_visualization_width = math.floor(tape_length * MIN_TAPE_VISUALIZATION_WIDTH) + 6
end
local function init_animation()
frame_clock = metro.init(_animate_background, 1/12)
frame_clock:start()
end
local function init_audio()
audio.rev_off()
audio.comp_off()
audio.level_adc_cut(1)
audio.level_eng_cut(0)
audio.level_tape_cut(0)
end
local function init_params()
local set_param = {
filter_cutoff = function(val)
for i = 1, 2 do
softcut.pre_filter_fc(i, val)
end
end,
play_amp = function(val)
for i = 1, 2 do
softcut.level(i, val)
end
end,
preserve_amp = function(val)
for i = 1, 2 do
softcut.pre_level(i, val)
end
end,
source_amp = function(val)
for i = 1, 2 do
softcut.level_input_cut(i, 1, val)
end
end
}
Parameters.init(set_param)
end
local function init_softcut()
softcut.buffer_clear()
softcut.event_position(_refresh_position)
for i = 1, 2 do
softcut.enable(i, 1)
softcut.buffer(i, i)
softcut.pre_filter_fc(i, params:get('filter_cutoff'))
softcut.pre_filter_dry(i, 0.5)
softcut.pre_filter_lp(i, 1)
softcut.level(i, params:get('play_amp'))
softcut.level_slew_time(i, 0)
softcut.loop(i, 1)
softcut.loop_start(i, 1)
softcut.loop_end(i, tape_length)
softcut.pre_level(i, params:get('preserve_amp'))
softcut.rec_level(i, rec_amp)
softcut.position(i, position)
softcut.level_input_cut(i, 1, params:get('source_amp'))
end
end
local function get_program_offset()
return (program - 1) * program_length
end
local function toggle_play()
playing = util.wrap(playing + 1, 0, 1)
if _bin_to_bool(playing) then
engine.play('eject', params:get('lem_skeuo_amp'))
else
engine.play('insert', params:get('lem_skeuo_amp'))
end
for i = 1, 2 do
softcut.play(i, playing)
end
softcut.voice_sync(2, 1, 0)
end
local function toggle_record()
recording = util.wrap(recording + 1, 0, 1)
engine.play('record', params:get('lem_skeuo_amp'))
for i = 1, 2 do
softcut.rec(i, recording)
end
softcut.voice_sync(2, 1, 0)
end
local function clear_all()
throttle_keys = true
clock.run(function()
clock.sleep(clear_time)
throttle_keys = false
end)
alt = false
shift = false
softcut.buffer_clear()
end
local function stop_all()
for i = 1, 2 do
softcut.play(i, 0)
softcut.rec(i, 0)
end
engine.play('eject', params:get('lem_skeuo_amp'))
position = 1
program = 1
playing = 0
recording = 0
_set_head_position()
end
local function program_select(n)
local segment_position = position - get_program_offset()
engine.play('program_select', params:get('lem_skeuo_amp'))
if n then
program = n
else
program = util.wrap(program + 1, 1, 4)
end
position = segment_position + get_program_offset()
_set_head_position()
end
local function refresh_background()
screen.display_png(ASSET_PATH..bg_frame..'.png', 0, 0)
end
local function format_time(s)
local minutes = math.floor(s / 60)
local seconds = s % 60
return ''..((minutes ~= 0 and minutes..'\'') or '')..seconds..'"'
end
local function refresh_foreground()
-- Variable Program Elements
local x, y = PROGRAM_CELLS[program][1], PROGRAM_CELLS[program][2]
local width, height = PROGRAM_CELL_DIMENSIONS[1], PROGRAM_CELL_DIMENSIONS[2]
local program_center = x + math.floor(width/2)
local program_length_string = format_time(program_length)
local program_length_string_center = math.floor(screen.text_extents(program_length_string) / 2)
screen.rect(x, y, width, height)
-- Above progrm viz info
screen.font_face(25)
screen.font_size(6)
screen.move(program_center, y - 4)
screen.line(program_center, y - 7)
screen.line(90, y - 7)
screen.line(100, y - 7)
screen.move_rel(3, 2)
screen.text(program)
screen.move_rel(0, -8)
screen.text_right('program')
screen.move_rel(2, -2)
screen.line_rel(program_length_string_center - 1, 0)
screen.move_rel(1, 0)
screen.line_rel(0, 14)
screen.move(108, 34)
screen.text(program_length_string)
-- Below program viz info
screen.font_face(21)
screen.font_size(18)
screen.move(program_center, y + height + 4)
screen.line(program_center, y + height + 7)
screen.line(90, y + height + 8)
screen.line(100, y + height + 8)
screen.move_rel(0, 6)
if _bin_to_bool(recording) then
screen.level(((position % 7) + 1) * 2)
screen.font_face(21)
screen.font_size(18)
screen.text('•')
screen.level(16)
end
if _bin_to_bool(playing) then
screen.move_rel(_bin_to_bool(recording) and 0 or 4, -4)
screen.font_face(1)
screen.font_size(8)
screen.text('▶')
end
screen.move(104, y + height + 19)
screen.font_face(25)
screen.font_size(6)
screen.text(format_time(position))
-- Tape length (fixed pos)
screen.move(0, 32)
screen.arc(0, 32, tape_visualization_width, 4, 6)
screen.move(23, 34)
screen.text(format_time(tape_length))
screen.move(23, 36)
screen.line_rel(tape_visualization_width, 0)
end
local function refresh_program()
if _bin_to_bool(playing) or _bin_to_bool(recording) then
softcut.query_position(1)
program = math.ceil(position / program_length)
end
end
local function set_tape_length(d)
program_length = util.clamp(program_length + d, MIN_PROGRAM_LENGTH, MAX_PROGRAM_LENGTH)
tape_length = 4 * program_length
for i = 1, 2 do
-- Loop should be inclusive of final second
softcut.loop_end(i, tape_length + 1)
end
position = util.clamp(position, 1, program_length)
_set_tape_width()
_set_head_position()
end
function init()
init_params()
init_animation()
init_audio()
init_softcut()
redraw()
end
function enc(e, d)
if e == 3 then
set_tape_length(d)
end
end
function key(k, z)
if not throttle_keys then
if k == 1 and z == 1 then
shift = true
elseif k == 1 and z == 0 then
alt = false
shift = false
elseif k == 2 and z == 1 and shift then
alt = true
elseif k == 2 and z == 0 and not shift then
if _bin_to_bool(playing) and _bin_to_bool(recording) then
toggle_record()
end
toggle_play()
elseif k== 2 and z == 0 and shift then
if not _bin_to_bool(playing) and not _bin_to_bool(recording) then
toggle_play()
end
toggle_record()
elseif k == 3 and z == 0 and not shift then
program_select()
elseif k == 3 and z == 0 and shift and not alt then
stop_all()
elseif k == 3 and z == 1 and shift and alt then
clear_all()
end
end
end
function redraw()
screen.clear()
refresh_program()
refresh_background()
refresh_foreground()
screen.stroke()
screen.update()
end
function refresh()
redraw()
end