-
Notifications
You must be signed in to change notification settings - Fork 0
/
segments.lua
351 lines (282 loc) · 7 KB
/
segments.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
-- ~~ segments ~~
--
-- slicing audio looper
--
-- KEY2 hold to record audio
-- assigns slices to midi keyboard
--
-- KEY3 hold to record slice playback
-- loops on release
-- tap again to clear
--
-- ENC1 slice volume
-- ENC2 slice octave
-- ENC3 slice direction
--
-- inspired by c&g's segmenti
-- patch for organelle
-----------------------------
-- INCLUDES, ETC.
-----------------------------
local pattern_time = require "pattern_time"
-----------------------------
-- STATE
-----------------------------
local recording_audio = false
local recording_pattern = false
local playing_pattern = false
local rec_end = 0
local rec_start = 0
local last_note = 0
local MAX_RECORD_TIME = 24
local loop_vol = 1
local loop_direction = 1
local loop_octave = 1
local AMP_POLL_TIME = 0.2
local amp_samples_l = {}
-----------------------------
-- INIT
-----------------------------
function init()
setup_softcut()
setup_midi()
setup_pattern()
setup_polls()
end
function setup_polls()
amp_in_left_poll = poll.set('amp_in_l')
amp_in_left_poll.callback = function(x)
recording_time = util.time() - rec_start
if recording_audio and recording_time < MAX_RECORD_TIME then
table.insert(amp_samples_l, x)
redraw()
end
end
amp_in_left_poll.time = AMP_POLL_TIME
amp_in_left_poll:start()
end
function setup_softcut()
softcut.buffer_clear()
-- Setup voice.
softcut.enable(1, 1)
softcut.buffer(1, 1)
softcut.level(1, loop_vol)
softcut.position(1, 0)
softcut.play(1, 1)
softcut.rate(1, 1)
softcut.loop(1, 1)
softcut.loop_start(1, 0)
softcut.loop_end(1, 16)
softcut.level_slew_time(1, 0.5)
softcut.fade_time(1, 0.01)
-- Setup inputs.
audio.level_adc_cut(1)
softcut.level_input_cut(1,1,1.0)
softcut.level_input_cut(2,1,1.0)
softcut.rec_level(1, 1)
softcut.pre_level(1, 1)
end
function setup_midi()
m = midi.connect()
m.event = function(data)
local d = midi.to_msg(data)
-- Skip MIDI input if recording audio/pattern.
if recording_audio or playing_pattern then
return
end
-- Play MIDI note, and add to pattern if recording.
if d.type == "note_on" then
note = d.note % 12
midi_note_on(note)
last_note = note
if recording_pattern then
ev = {}
ev.midi_event = "note_on"
ev.midi_note = note
pattern:watch(ev)
end
elseif d.type == "note_off" then
note = d.note % 12
-- Allows "legato" playing.
if note ~= last_note then
return
end
midi_note_off()
if recording_pattern then
ev = {}
ev.midi_event = "note_off"
ev.midi_note = note
pattern:watch(ev)
end
end
end
end
function setup_pattern()
pattern = pattern_time.new()
pattern.process = process_pattern_event
end
-----------------------------
-- MIDI/PATTERN HANDLING
-----------------------------
function process_pattern_event(ev)
if ev.midi_event == "note_on" then
midi_note_on(ev.midi_note)
elseif ev.midi_event == "note_off" then
midi_note_off()
end
end
function midi_note_on(note)
notes_in_octave = 12
rec_time = rec_end - rec_start
time_per_key = rec_time / notes_in_octave
softcut.loop_start(1, note * time_per_key)
softcut.loop_end(1, (note + 1) * time_per_key)
softcut.play(1,1)
softcut.level(1, loop_vol)
redraw()
end
function midi_note_off()
softcut.level(1, 0)
redraw()
end
-----------------------------
-- INPUT HANDLING
-----------------------------
function key(n,z)
if n == 2 then
-- Audio recorder.
if z == 1 then
pattern:clear()
pattern:stop()
recording_pattern = false
playing_pattern = false
softcut.buffer_clear()
softcut.rate(1, 1)
softcut.loop_start(1, 0)
softcut.loop_end(1, MAX_RECORD_TIME)
softcut.loop(1, 0)
softcut.position(1, 0)
softcut.rec_offset(1, 0)
softcut.rec_level(1, 1)
softcut.rec(1, 1)
softcut.play(1, 0)
rec_start = util.time()
amp_samples_l = {}
recording_audio = true
elseif z == 0 then
softcut.rate(1, loop_direction * loop_octave)
rec_end = util.time()
softcut.loop_end(1, util.time() - rec_start)
softcut.loop(1, 1)
softcut.position(1, 0)
softcut.rec_level(1, 0)
softcut.rec(1, 0)
softcut.play(1, 0)
recording_audio = false
print("Recorded " .. (rec_end - rec_start) .. " seconds of audio...")
end
end
if n == 3 then
-- Pattern recorder.
if z == 1 then
if playing_pattern then
softcut.play(1, 0)
end
pattern:clear()
pattern:stop()
pattern:rec_start()
recording_pattern = true
playing_pattern = false
elseif z == 0 then
midi_note_off()
pattern:rec_stop()
pattern:start()
recording_pattern = false
playing_pattern = pattern.count > 0
end
end
redraw()
end
function enc(n, d)
if n == 1 then
-- Set loop volume.
loop_vol = util.clamp(loop_vol + (d * 0.01), 0, 1)
softcut.level(1, loop_vol)
elseif n == 2 then
-- Set loop octave.
if d > 0 then
loop_octave = 1
else
loop_octave = 0.5
end
softcut.rate(1, loop_direction * loop_octave)
elseif n == 3 then
-- Set loop direction.
if d < 0 then
loop_direction = -1
else
loop_direction = 1
end
softcut.rate(1, loop_direction * loop_octave)
end
redraw()
end
-----------------------------
-- DRAWING
-----------------------------
function redraw()
screen.clear()
draw_audio_info()
draw_pattern_info()
screen.update()
end
function draw_audio_info()
screen.move(1, 10)
screen.level(15)
screen.text("audio >")
screen.move(80, 10)
screen.level(15)
screen.text("o: ")
screen.level(5)
screen.text(loop_octave == 1 and "+" or "-")
screen.move(99, 10)
screen.level(15)
screen.text("d: ")
screen.level(5)
screen.text(loop_direction == 1 and "fwd" or "rev")
if rec_start == 0 and rec_end == 0 and not recording_audio then
screen.move(1, 20)
screen.level(1)
screen.text(".. empty, hold KEY2 to record")
else
screen.level(5)
if #amp_samples_l > 0 then
max_amp_l = math.max(table.unpack(amp_samples_l))
for i=1,#amp_samples_l do
amp_l = (amp_samples_l[i] / max_amp_l) * 10
screen.move(i, 20)
screen.rect(i, 25, 1, -amp_l)
screen.fill()
screen.rect(i, 25, 1, amp_l)
screen.fill()
end
end
end
end
function draw_pattern_info()
screen.move(1, 50)
screen.level(15)
screen.text("pattern >")
screen.move(1, 60)
screen.level(5)
if recording_pattern then
screen.level(5)
screen.text(".. recording!")
elseif playing_pattern and (rec_start ~= 0 and rec_end ~=0) then
screen.level(5)
screen.text(".. playing! tap KEY3 to cancel")
else
screen.level(1)
screen.text(".. empty, hold KEY3 to record")
end
end