-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
576 lines (521 loc) · 13.3 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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
require("util")
require("gui")
local name = "breakmapper"
Model = {
width = 1000,
unselected = {},
selected = {},
note = 48,
octave = 4,
vel_step = 1,
note_op = 1,
vel_op = 1,
vel_equal_mode = 1,
vel_fixed_mode = 1,
vel_to_vol = 3,
key_to_pitch = 3,
auto_apply = true,
dirty = false,
follow_selection = false,
}
function default_prefs()
return {
}
end
local prefs = renoise.Document.create("ScriptingToolPreferences")(default_prefs())
local tool = renoise.tool()
tool.preferences = prefs
local dialog = nil
local KEY_TO_PITCH_OPS = {
"Keep",
"On",
"Off",
}
local VEL_TO_VOL_OPS = {
"Keep",
"On",
"Off",
}
local NOTE_OPS = {
"Keep",
"Single",
"Octaves",
"Reset",
}
local VEL_OPS = {
"Keep",
"Equal",
"Fixed",
"Reset",
}
local VEL_EQUAL_MODE = {
"Distribute",
"Extend Highest",
"Extend Lowest",
-- "Shrink Lowest",
}
local VEL_FIXED_MODE = {
"From Lowest",
"From Highest",
}
function modify_keyzones(m)
if #m.selected == 0 then
return
end
local s = renoise.song()
local instrument = s.selected_instrument
local vel_step = 1
local vel_left_over = 128 % #m.selected
print(vel_left_over, vel_step)
local distributed = 0
for sel_i = 1, #m.selected do
local i = math.fmod(sel_i,128)
local note = math.floor((sel_i - 1 ) / 128) + (12*3)
if i > 0 and i <= 128 then
local sm = instrument.sample_mappings[1][m.selected[sel_i].index]
-- equal velocity stack
local range_start = 0
if i > 1 then
range_start = instrument.sample_mappings[1][m.selected[sel_i - 1].index].velocity_range[2] + 1
end
local range_end = range_start + vel_step - 1
local step_size = 1
range_end = range_start + step_size - 1
if i == #m.selected then
range_end = 128
end
sm.velocity_range = { clamp(range_start, 0, 128), clamp(range_end, 0, 128) }
-- singe note
sm.base_note = note
sm.note_range = { note, note}
sm.map_velocity_to_volume = false
sm.map_key_to_pitch = false
end
end
end
function on_instrument_change()
if dialog ~= nil then
open()
end
end
function on_sample_change()
if Model.follow_selection then
if dialog then
dialog.process(dialog.model, message("toggle sample", renoise.song().selected_sample_index), dialog)
end
end
end
renoise.tool().app_became_active_observable:add_notifier(function()
local hook_notifiers = function()
enable_notifier(renoise.song(), "selected_sample", on_sample_change)
enable_notifier(renoise.song(), "selected_instrument", on_instrument_change)
end
enable_notifier(renoise.tool(), "app_new_document", hook_notifiers)
hook_notifiers()
end)
renoise.tool().app_release_document_observable:add_notifier(function()
if dialog then
if dialog.window.visible then
dialog.window:close()
end
dialog = nil
end
end)
function init(m)
m.unselected = renoise.song().selected_instrument.samples
m.dirty = false
-- SILLY
return m
end
function update(m, msg)
if msg.type == "select sample" then
if not m.follow_selection then
renoise.song().selected_sample_index = msg.value
end
table.insert(m.selected, { index = msg.value, name = renoise.song().selected_sample.name })
m.vel_step = 1
elseif msg.type == "unselect sample" then
table.remove(m.selected, msg.value)
elseif msg.type == "toggle sample" then
local i = table:find_index(m.selected, function(a)
return a.index == msg.value
end)
if i > 0 then
table.remove(m.selected, i)
else
table.insert(m.selected, { index = msg.value, name = renoise.song().selected_sample.name })
end
elseif msg.type == "select note" then
m.note = m.octave * 12 + msg.value - 1
elseif msg.type == "select octave" then
m.octave = msg.value - 1
m.note = m.octave * 12 + (m.note % 12)
elseif msg.type == "set note op" then
m.note_op = msg.value
elseif msg.type == "set vel op" then
m.vel_op = msg.value
elseif msg.type == "set vel step" then
m.vel_step = msg.value
elseif msg.type == "set vel equal mode" then
m.vel_equal_mode = msg.value
elseif msg.type == "set vel fixed mode" then
m.vel_fixed_mode = msg.value
elseif msg.type == "set vel vol" then
m.vel_to_vol = msg.value
elseif msg.type == "set key pitch" then
m.key_to_pitch = msg.value
elseif msg.type == "set auto apply" then
m.auto_apply = msg.value
elseif msg.type == "apply" then
modify_keyzones(m)
elseif msg.type == "Invert Selection" then
local ls = {}
for i = 1, #m.unselected do
if not prop_with_value_exist_in_table("index", i, m.selected) then
table.insert(ls, { index = i, name = m.unselected[i].name })
end
end
m.selected = ls
m.vel_step = 1
elseif msg.type == "Reverse" then
m.selected = table:reverse(m.selected)
elseif msg.type == "Shuffle" then
m.selected = table:shuffle(m.selected)
elseif msg.type == "move up" then
if msg.value > 1 then
table:swap(m.selected, msg.value, msg.value - 1)
end
elseif msg.type == "move down" then
if msg.value < #m.selected then
table:swap(m.selected, msg.value, msg.value + 1)
end
elseif msg.type == "Clear" then
m.selected = {}
elseif msg.type == "Select All" then
m.selected = table:map(m.unselected, function(s, i)
return {
index = i,
name = s.name
}
end)
m.vel_step = 1
elseif msg.type == "quit" then
return 1
end
if m.auto_apply then
modify_keyzones(m)
else
m.dirty = msg.type ~= "apply"
end
return 0
end
function view(m, o)
local vb = renoise.ViewBuilder()
local process = function(name, value)
return function()
o.process(m, message(name, value), o)
end
end
local process_button = function(name, visible, value, text)
return vb:button {
text = text and text or name,
visible = ifelse(visible == nil, true, visible),
pressed = process(name, value),
}
end
local w = m.width - 15 * 2
local content = vb:column {
width = m.width,
margin = 15,
spacing = 5,
vb:row {
-- style = "group",
width = w,
vb:column {
width = w / 2,
vb:row {
spacing = 5,
-- style = "group",
process_button("Select All", #m.unselected > #m.selected),
process_button("Invert Selection", #m.unselected > #m.selected and #m.selected > 0),
},
empty_row(),
list_view(
m.unselected,
function(s, i)
-- local pre = renoise.song().selected_sample_index == i and " > " or ""
return vb:button {
text = s.name,
width = w / 2,
visible = not prop_with_value_exist_in_table("index", i, m.selected),
pressed = function()
o.process(m, message("select sample", i), o)
end,
}
end
)
},
vb:column {
width = w / 2,
vb:horizontal_aligner {
mode = "right",
-- spacing = 5,
-- style = "group",
process_button("Clear", #m.selected > 1),
process_button("Shuffle", #m.selected > 1),
process_button("Reverse", #m.selected > 1),
},
vb:row {
visible = #m.selected == 1,
vb:text {
}
},
empty_row(),
list_view(
m.selected,
-- renoise.song().selected_instrument.samples,
function(s, i)
local aw = 20
-- local pre = renoise.song().selected_sample_index == i and " > " or ""
return vb:row {
vb:button {
width = w / 2 - aw * 2,
text = s.name,
pressed = function()
o.process(m, message("unselect sample", i), o)
-- print(s.name)
-- select_sample(s.name, i)
end,
},
vb:button {
width = aw,
text = "↑",
pressed = process("move up", i)
},
vb:button {
width = aw,
text = "↓",
pressed = process("move down", i)
},
}
end
),
vb:row {
visible = #m.selected > 0,
vb:text {
width = w / 2 - 5,
align = "right",
style = "disabled",
text = #m.selected .. " selected"
}
},
},
},
vb:column {
width = w,
visible = #m.selected > 0,
empty_row(),
vb:row {
vb:text {
width = w,
align = "center",
text = "Velocity",
}
},
vb:row {
vb:switch {
width = w,
value = m.vel_op,
items = VEL_OPS,
notifier = function(v)
o.process(m, message("set vel op", v), o)
end
}
},
vb:row {
visible = m.vel_op == 2,
vb:switch {
width = w,
value = m.vel_equal_mode,
items = VEL_EQUAL_MODE,
notifier = function(v)
o.process(m, message("set vel equal mode", v), o)
end
}
},
vb:horizontal_aligner {
mode = "distribute",
visible = m.vel_op == 3,
vb:text {
-- width = w / 2,
align = "right",
text = "Step Size",
},
vb:valuebox {
value = m.vel_step,
min = 1,
max = 16,
notifier = function(v)
m.vel_step = v
if m.auto_apply then
modify_keyzones(m)
end
end
},
vb:switch {
width = w / 2,
value = m.vel_fixed_mode,
items = VEL_FIXED_MODE,
notifier = function(v)
o.process(m, message("set vel fixed mode", v), o)
end
},
},
empty_row(),
vb:row {
vb:text {
width = w,
align = "center",
text = "Note",
}
},
vb:row {
vb:switch {
width = w,
value = m.note_op,
items = NOTE_OPS,
notifier = function(v)
o.process(m, message("set note op", v), o)
end
}
},
vb:row {
visible = m.note_op == 2 or m.note_op == 3,
vb:column {
vb:row {
vb:switch {
width = w,
value = (m.note % 12) + 1,
items = NOTE_NAMES,
notifier = function(v)
o.process(m, message("select note", v), o)
end,
},
},
vb:row {
vb:switch {
width = w,
value = m.octave + 1,
items = OCTAVES,
notifier = function(v)
o.process(m, message("select octave", v), o)
end,
}
}
}
},
empty_row(),
vb:row {
vb:column {
vb:row {
vb:text {
width = w / 2 - 15,
align = "center",
text = "Key -> Pitch",
}
},
vb:row {
vb:switch {
width = w / 2 - 15,
value = m.key_to_pitch,
items = KEY_TO_PITCH_OPS,
notifier = function(v)
o.process(m, message("set key pitch", v), o)
end
}
},
},
vb:column {
width = 30,
},
vb:column {
vb:row {
vb:text {
width = w / 2 - 15,
align = "center",
text = "Velocity -> Volume",
}
},
vb:row {
vb:switch {
width = w / 2 - 15,
value = m.vel_to_vol,
items = VEL_TO_VOL_OPS,
notifier = function(v)
o.process(m, message("set vel vol", v), o)
end
}
},
},
},
},
vb:horizontal_aligner {
spacing = 5,
margin = 5,
vb:checkbox {
value = m.auto_apply,
notifier = function(b)
o.process(m, message("set auto apply", b), o)
end
},
vb:text {
text = "Auto Apply",
style = m.auto_apply and "normal" or "disabled"
},
},
vb:button {
width = w,
visible = not m.auto_apply and #m.selected > 0 and m.dirty,
text = "Apply Now",
pressed = process("apply")
},
}
return content
end
function open()
renoise.app().window.active_middle_frame = renoise.ApplicationWindow.MIDDLE_FRAME_INSTRUMENT_SAMPLE_KEYZONES
if dialog then
if dialog.window.visible then
dialog.window:close()
end
dialog = nil
end
local options = {
title = name,
model = Model,
init = init,
update = update,
view = view,
on_exit = function()
dialog = nil
end
}
dialog = model_view_dialog(options)
end
tool:add_menu_entry {
name = "Main Menu:Tools:" .. name,
invoke = function()
open()
end
}
tool:add_menu_entry {
name = "Sample Mappings:breakmapper - Stack...",
invoke = open
}
tool:add_keybinding {
name = "Global:Tools:Open zoner",
invoke = open
}
_AUTO_RELOAD_DEBUG = function()
end
print(name .. " loaded.")