-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathkeys.lua
More file actions
433 lines (350 loc) · 10.8 KB
/
Copy pathkeys.lua
File metadata and controls
433 lines (350 loc) · 10.8 KB
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
-- Timber Keys
-- 1.0.0 Beta 7 @markeats
-- llllllll.co/t/timber
--
-- Map samples across a
-- MIDI keyboard.
--
-- E1 : Page
-- K1+E1 : Sample slot
-- K1 (Hold) : Shift / Fine
--
-- K2 : Focus
-- K3 : Action
-- E2/3 : Params
--
local Timber = include("timber/lib/timber_engine")
local MusicUtil = require "musicutil"
local UI = require "ui"
local Formatters = require "formatters"
engine.name = "Timber"
local SCREEN_FRAMERATE = 15
local screen_dirty = true
local midi_in_device
local pages
local global_view
local sample_setup_view
local waveform_view
local filter_amp_view
local amp_env_view
local mod_env_view
local lfos_view
local mod_matrix_view
local key_matrix_view
local NUM_SAMPLES = 16
local current_sample_id = 0
local shift_mode = false
local function set_sample_id(id)
current_sample_id = id
while current_sample_id >= NUM_SAMPLES do current_sample_id = current_sample_id - NUM_SAMPLES end
while current_sample_id < 0 do current_sample_id = current_sample_id + NUM_SAMPLES end
sample_setup_view:set_sample_id(current_sample_id)
waveform_view:set_sample_id(current_sample_id)
filter_amp_view:set_sample_id(current_sample_id)
amp_env_view:set_sample_id(current_sample_id)
mod_env_view:set_sample_id(current_sample_id)
lfos_view:set_sample_id(current_sample_id)
mod_matrix_view:set_sample_id(current_sample_id)
key_matrix_view:set_sample_id(current_sample_id)
end
local function note_on(voice_id, freq, vel, sample_id)
engine.noteOn(voice_id, freq, vel, sample_id)
if params:get("follow") == 2 then
set_sample_id(sample_id)
end
screen_dirty = true
end
local function note_off(voice_id, sample_id)
engine.noteOff(voice_id)
screen_dirty = true
end
local function note_off_all()
engine.noteOffAll()
screen_dirty = true
end
local function note_kill_all()
engine.noteKillAll()
screen_dirty = true
end
local function set_pressure_voice(voice_id, pressure)
engine.pressureVoice(voice_id, pressure)
end
local function set_pressure_sample(sample_id, pressure)
engine.pressureSample(sample_id, pressure)
end
local function set_pressure_all(pressure)
engine.pressureAll(pressure)
end
local function set_pitch_bend_voice(voice_id, bend_st)
engine.pitchBendVoice(voice_id, MusicUtil.interval_to_ratio(bend_st))
end
local function set_pitch_bend_sample(sample_id, bend_st)
engine.pitchBendSample(sample_id, MusicUtil.interval_to_ratio(bend_st))
end
local function set_pitch_bend_all(bend_st)
engine.pitchBendAll(MusicUtil.interval_to_ratio(bend_st))
end
-- Encoder input
function enc(n, delta)
-- Global
if n == 1 then
if shift_mode then
if pages.index > 1 then
set_sample_id(current_sample_id + delta)
end
else
pages:set_index_delta(delta, false)
end
else
if pages.index == 2 then
sample_setup_view:enc(n, delta)
elseif pages.index == 3 then
waveform_view:enc(n, delta)
elseif pages.index == 4 then
filter_amp_view:enc(n, delta)
elseif pages.index == 5 then
amp_env_view:enc(n, delta)
elseif pages.index == 6 then
mod_env_view:enc(n, delta)
elseif pages.index == 7 then
lfos_view:enc(n, delta)
elseif pages.index == 8 then
mod_matrix_view:enc(n, delta)
elseif pages.index == 9 then
key_matrix_view:enc(n, delta)
end
end
screen_dirty = true
end
-- Key input
function key(n, z)
if n == 1 then
-- Shift
if z == 1 then
shift_mode = true
Timber.shift_mode = shift_mode
else
shift_mode = false
Timber.shift_mode = shift_mode
end
else
if pages.index == 2 then
sample_setup_view:key(n, z)
elseif pages.index == 3 then
waveform_view:key(n, z)
elseif pages.index == 4 then
filter_amp_view:key(n, z)
elseif pages.index == 5 then
amp_env_view:key(n, z)
elseif pages.index == 6 then
mod_env_view:key(n, z)
elseif pages.index == 7 then
lfos_view:key(n, z)
elseif pages.index == 8 then
mod_matrix_view:key(n, z)
elseif pages.index == 9 then
key_matrix_view:key(n, z)
end
end
screen_dirty = true
end
-- MIDI input
local function midi_event(data)
local msg = midi.to_msg(data)
local sample_id
if msg.ch then
sample_id = msg.ch - 1
end
local voice_id
if msg.note then
voice_id = (msg.ch - 1) * 128 + msg.note
end
-- Note off
if msg.type == "note_off" then
note_off(voice_id, sample_id)
-- print("note off", msg.note, voice_id, sample_id)
-- Note on
elseif msg.type == "note_on" then
if pages.index == 2 then
sample_setup_view:sample_key(sample_id)
end
note_on(voice_id, MusicUtil.note_num_to_freq(msg.note), msg.vel / 127, sample_id)
-- print("note on", msg.note, msg.vel, voice_id, sample_id)
-- Key pressure
elseif msg.type == "key_pressure" then
set_pressure_voice(voice_id, msg.val / 127)
-- Channel pressure
elseif msg.type == "channel_pressure" then
set_pressure_sample(sample_id, msg.val / 127)
-- Pitch bend
elseif msg.type == "pitchbend" then
local bend_st = (util.round(msg.val / 2)) / 8192 * 2 -1 -- Convert to -1 to 1
local bend_range = params:get("bend_range")
set_pitch_bend_sample(sample_id, bend_st * bend_range)
end
end
local function update()
waveform_view:update()
lfos_view:update()
end
-- Views
local GlobalView = {}
GlobalView.__index = GlobalView
function GlobalView.new(sample_id)
local global = {
sample_id = sample_id or 1,
particle_properties = {}
}
-- Generate random properties for particles
for i = 0, 127 do
global.particle_properties[i] = {}
global.particle_properties[i].level = math.random(1, 5)
global.particle_properties[i].length = math.random(4, 9)
if math.random() > 0.5 then
global.particle_properties[i].y = math.random(3, 20)
else
global.particle_properties[i].y = math.random(43, 60)
end
end
setmetatable(GlobalView, {__index = GlobalView})
setmetatable(global, GlobalView)
return global
end
function GlobalView:redraw()
local text_x, text_y = 4, 29
local PARTICLES_X, PARTICLES_W = 4, 120
for i = 0, NUM_SAMPLES - 1 do
-- Draw numbers
screen.move(text_x, text_y)
if Timber.samples_meta[i] and Timber.samples_meta[i].num_frames > 0 then
if Timber.samples_meta[i].playing then screen.level(15) else screen.level(3) end
screen.text(i)
else
screen.level(1)
screen.text("/")
end
if (i + 1) % 8 == 0 then
text_x = 4
text_y = text_y + 11
else
text_x = text_x + 14
end
-- Draw particles
if Timber.samples_meta[i].playing then
for k, v in pairs(Timber.samples_meta[i].positions) do
local note = k % 128
local position_x = PARTICLES_X + util.round(v * (PARTICLES_W - 1 + self.particle_properties[note].length))
screen.move(util.clamp(position_x, PARTICLES_X, PARTICLES_X + PARTICLES_W - 1), self.particle_properties[note].y + 0.5)
screen.line(position_x - self.particle_properties[note].length, self.particle_properties[note].y + 0.5)
screen.level(self.particle_properties[note].level)
screen.stroke()
end
end
end
screen.fill()
end
-- Drawing functions
local function draw_background_rects()
-- 4px edge margins. 8px gutter.
screen.level(1)
screen.rect(4, 22, 56, 38)
screen.rect(68, 22, 56, 38)
screen.fill()
end
function redraw()
screen.clear()
if Timber.file_select_active then
Timber.FileSelect.redraw()
return
end
-- draw_background_rects()
pages:redraw()
if pages.index == 1 then
global_view:redraw()
elseif pages.index == 2 then
sample_setup_view:redraw()
elseif pages.index == 3 then
waveform_view:redraw()
elseif pages.index == 4 then
filter_amp_view:redraw()
elseif pages.index == 5 then
amp_env_view:redraw()
elseif pages.index == 6 then
mod_env_view:redraw()
elseif pages.index == 7 then
lfos_view:redraw()
elseif pages.index == 8 then
mod_matrix_view:redraw()
elseif pages.index == 9 then
key_matrix_view:redraw()
end
screen.update()
end
local function callback_set_screen_dirty(id)
if id == nil or id == current_sample_id or pages.index == 1 then
screen_dirty = true
end
end
local function callback_set_waveform_dirty(id)
if ((id == nil or id == current_sample_id) and pages.index == 3) or pages.index == 1 then
screen_dirty = true
end
end
function init()
midi_in_device = midi.connect(1)
midi_in_device.event = midi_event
pages = UI.Pages.new(1, 9)
-- Callbacks
Timber.sample_changed_callback = function(id)
if Timber.samples_meta[id].manual_load then
-- Set our own loop point defaults
params:set("loop_start_frame_" .. id, util.round(Timber.samples_meta[id].num_frames * 0.2))
params:set("loop_end_frame_" .. id, util.round(Timber.samples_meta[id].num_frames * 0.4))
-- Set env defaults
params:set("amp_env_attack_" .. id, 0.01)
params:set("amp_env_sustain_" .. id, 0.8)
params:set("amp_env_release_" .. id, 0.4)
end
callback_set_screen_dirty(id)
end
Timber.meta_changed_callback = callback_set_screen_dirty
Timber.waveform_changed_callback = callback_set_waveform_dirty
Timber.play_positions_changed_callback = callback_set_waveform_dirty
Timber.views_changed_callback = callback_set_screen_dirty
-- Add params
params:add_separator("Keys")
params:add{type = "number", id = "midi_device", name = "MIDI Device", min = 1, max = 4, default = 1, action = function(value)
midi_in_device.event = nil
midi_in_device = midi.connect(value)
midi_in_device.event = midi_event
end}
params:add{type = "number", id = "bend_range", name = "Pitch Bend Range", min = 1, max = 48, default = 2}
params:add{type = "option", id = "follow", name = "Follow", options = {"Off", "On"}, default = 2}
Timber.add_params()
params:add_separator()
for i = 0, NUM_SAMPLES - 1 do
Timber.add_sample_params(i)
end
-- Default sample
Timber.load_sample(0, _path.code .. "/timber/audio/piano-c.wav")
-- UI
global_view = GlobalView.new()
sample_setup_view = Timber.UI.SampleSetup.new(current_sample_id)
waveform_view = Timber.UI.Waveform.new(current_sample_id)
filter_amp_view = Timber.UI.FilterAmp.new(current_sample_id)
amp_env_view = Timber.UI.AmpEnv.new(current_sample_id)
mod_env_view = Timber.UI.ModEnv.new(current_sample_id)
lfos_view = Timber.UI.Lfos.new(current_sample_id)
mod_matrix_view = Timber.UI.ModMatrix.new(current_sample_id)
key_matrix_view = Timber.UI.KeyMatrix.new(current_sample_id)
screen.aa(1)
local screen_refresh_metro = metro.init()
screen_refresh_metro.event = function()
update()
if screen_dirty then
redraw()
screen_dirty = false
end
end
screen_refresh_metro:start(1 / SCREEN_FRAMERATE)
end