-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
731 lines (661 loc) · 20.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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
require("includes/track_play_count")
require("includes/note_triggers")
require("includes/cutoff_points")
require("includes/fill")
-- Some basic vars for reuse:
local app = renoise.app()
local tool = renoise.tool()
local song = nil
local doc = renoise.Document
local benchmark = true -- Output benchmarking information to the console, for dev purposes
-- View Builder for preferences and set scale
local vbp = renoise.ViewBuilder()
local vbc = renoise.ViewBuilder
local vbwp = vbp.views
-- Variables used:
local currLine = 0
local prevLine = -1
local currPattern = doc.ObservableNumber(0)
local nextPattern = doc.ObservableNumber(1)
local patternPlayCount = 0
local patternSetCount = 1 -- How many patterns in a "set"
local trackLengths = {} -- Remember the individual lengths of tracks
local userInitiatedFill = false
local buttonSize = 96
reset = function()
currLine = 0
prevLine = -1
currPattern.value = 0
nextPattern.value = 1
patternPlayCount = 0
patternSetCount = 1
trackLengths = {}
userInitiatedFill = false
end
-- Pattern indicator
local patternIndicatorView = vbp:text {
text = "-",
align = "center",
font = "big",
style = "strong",
width = buttonSize,
height = buttonSize
}
updatePatternIndicator = function()
if currPattern.value ~= nextPattern.value then
patternIndicatorView.text = string.format(
"%s → %s (%s/%s)",
currPattern.value,
nextPattern.value,
(patternPlayCount % patternSetCount) + 1,
patternSetCount
)
else
patternIndicatorView.text = string.format(
"%s (%s/%s)%s",
currPattern.value,
(patternPlayCount % patternSetCount) + 1,
patternSetCount,
userInitiatedFill and " (F)" or ""
)
end
end
nextPattern:add_notifier(updatePatternIndicator)
-- Queue the next pattern
local queue_next_pattern = function()
if nextPattern.value < song.transport.song_length.sequence - 1 then
nextPattern.value = nextPattern.value + 1
updatePattern()
end
end
-- Queue the previous pattern
local queue_previous_pattern = function()
if nextPattern.value > 1 then
nextPattern.value = nextPattern.value - 1
updatePattern()
end
end
-- Trigger a fill
local trigger_fill = function()
userInitiatedFill = true
vbp.views.fill_button.color = {255, 255, 255}
updatePatternIndicator()
updatePattern()
end
-- Keep state of the tracks (mute status, etc.)
local trackState = {}
setTrackButtonColor = function(trackIndex)
local button = vbp.views["track_button_" .. trackIndex]
if trackState[trackIndex].trigged.value == true then
if trackState[trackIndex].muted.value == true then
button.color = {255, 0, 0}
else
button.color = {128, 200, 0}
end
elseif trackState[trackIndex].muted.value == true then
button.color = {200, 0, 0}
else
button.color = trackState[trackIndex].trackColor
end
end
toggleMute = function(trackIndex)
local track = song.tracks[trackIndex]
if track == nil or track.type ~= 1 then
return
end
if track.mute_state == renoise.Track.MUTE_STATE_ACTIVE then
track:mute()
trackState[trackIndex].muted.value = true
else
track:unmute()
trackState[trackIndex].unmuteCounter.value = 0
trackState[trackIndex].muted.value = false
trackState[trackIndex].mutedColumnCount.value = 0
-- Unmute all columns?
end
setTrackButtonColor(trackIndex)
end
createTrackButton = function(trackIndex)
local button = vbp:button {
id = "track_button_" .. trackIndex,
text = "-",
color = {0, 0, 0},
width = buttonSize,
height = buttonSize,
active = false,
pressed = function()
toggleMute(trackIndex)
end
}
trackState[trackIndex] = {
track = nil,
trackName = "-",
trackColor = {0, 0, 0},
muted = doc.ObservableBoolean(false),
unmuteCounter = doc.ObservableNumber(0),
trigged = doc.ObservableBoolean(false),
mutedColumnCount = doc.ObservableNumber(0)
}
local function setButtonText()
local trackName = trackState[trackIndex].trackName
if trackState[trackIndex].unmuteCounter.value > 0 then
button.text = string.format(
"%s\n%s\n(M:%s)",
trackIndex,
trackName,
trackState[trackIndex].unmuteCounter.value
)
elseif trackState[trackIndex].muted.value == true then
button.text = string.format("%s\n%s\n(M)", trackIndex, trackName)
elseif trackState[trackIndex].mutedColumnCount.value > 0 then
button.text = string.format(
"%s\n%s\n(MC:%s)",
trackIndex,
trackName,
trackState[trackIndex].mutedColumnCount.value
)
else
button.text = string.format("%s\n%s", trackIndex, trackName)
end
end
-- Observer for the mute button change color behavior
trackState[trackIndex].unmuteCounter:add_notifier(setButtonText)
trackState[trackIndex].muted:add_notifier(setButtonText)
trackState[trackIndex].mutedColumnCount:add_notifier(setButtonText)
-- Observer for the blinking Indicator
trackState[trackIndex].trigged:add_notifier(function()
setTrackButtonColor(trackIndex)
end)
return button
end
local updateTrackButton = function(trackIndex)
local track = song.tracks[trackIndex]
local button = vbp.views["track_button_" .. trackIndex]
if track == nil or track.type ~= 1 then
trackState[trackIndex].track = nil
button.text = "-"
button.color = {0, 0, 0}
button.active = false
else
local trackName = track.name
local trackColor = track.color
trackState[trackIndex].track = trackIndex
trackState[trackIndex].trackName = trackName
trackState[trackIndex].trackColor = trackColor
trackState[trackIndex].muted.value = track.mute_state ~= renoise.Track.MUTE_STATE_ACTIVE
trackState[trackIndex].unmuteCounter.value = 0
trackState[trackIndex].trigged.value = false
trackState[trackIndex].mutedColumnCount.value = 0
button.color = trackColor
button.active = true
button.text = string.format("%s\n%s", trackIndex, trackName)
end
end
local playButton = vbp:button {
id = "transport_button",
text = "Play",
width = buttonSize,
height = buttonSize,
color = {0, 128, 0},
pressed = function()
if song.transport.playing then
song.transport:stop()
reset()
setupPattern()
vbp.views.transport_button.text = "Play"
vbp.views.transport_button.color = {0, 128, 0}
else
-- Play pattern 0 in loop
currPattern.value = 1
nextPattern.value = 1
song.transport.loop_pattern = true
local song_pos = renoise.SongPos(1, 1)
song.transport:start_at(song_pos)
vbp.views.transport_button.text = "Stop"
vbp.views.transport_button.color = {128, 0, 0}
end
end
}
-- Dialog structure:
--
-- .---------. .---.
-- | 1 | | 2 |
-- | | | |
-- | .-----. | | |
-- | | 3 | | | |
-- | `-----` | | |
-- | etc | | |
-- `---------` `---`
-- .---------------.
-- | 4 |
-- `---------------`
--
createDialog = function()
local dialog = vbp:column {
id = "container",
margin = 0,
vbp:row {
id = "top_wrapper",
margin = 0,
vbp:column {
id = "track_buttons_container",
margin = 0,
vbp:horizontal_aligner {
id = "track_buttons_row1",
margin = 0,
mode = "justify",
createTrackButton(1),
createTrackButton(2),
createTrackButton(3),
createTrackButton(4)
},
vbp:horizontal_aligner {
id = "track_buttons_row2",
margin = 0,
mode = "justify",
createTrackButton(5),
createTrackButton(6),
createTrackButton(7),
createTrackButton(8)
},
vbp:horizontal_aligner {
id = "track_buttons_row3",
margin = 0,
mode = "justify",
createTrackButton(9),
createTrackButton(10),
createTrackButton(11),
createTrackButton(12)
},
vbp:horizontal_aligner {
id = "track_buttons_row4",
margin = 0,
mode = "justify",
createTrackButton(13),
createTrackButton(14),
createTrackButton(15),
createTrackButton(16)
},
},
vbp:column {
id = "fill_container",
margin = 0,
style = "plain",
vbp:button {
id = "fill_button",
text = "Fill",
width = buttonSize,
height = buttonSize,
pressed = trigger_fill,
color = {1, 1, 1}
},
vbp:button {
width = buttonSize,
height = buttonSize,
text = "-",
active = false,
color = {1, 1, 1}
},
vbp:button {
width = buttonSize,
height = buttonSize,
text = "-",
active = false,
color = {1, 1, 1}
},
vbp:button {
width = buttonSize,
height = buttonSize,
text = "-",
active = false,
color = {1, 1, 1}
},
}
},
vbp:row {
id = "transport_container",
margin = 0,
style = "plain",
vbp:horizontal_aligner {
margin = 0,
mode = "justify",
playButton,
vbp:button {
width = buttonSize,
height = buttonSize,
text = "-",
active = false,
color = {1, 1, 1}
},
vbp:button {
text = "Prev",
width = buttonSize,
height = buttonSize,
color = {1, 1, 1},
pressed = queue_previous_pattern
},
patternIndicatorView,
vbp:button {
text = "Next",
width = buttonSize,
height = buttonSize,
color = {1, 1, 1},
pressed = queue_next_pattern
}
}
}
}
return dialog
end
-- Setup pattern, this is called every time a new pattern begins
setupPattern = function()
local dst = song:pattern(1)
if nextPattern.value ~= currPattern.value and (patternPlayCount + 1) % patternSetCount == 0 then
-- Prepare a new pattern
local src = song:pattern(nextPattern.value + 1)
dst.number_of_lines = src.number_of_lines
dst:copy_from(src)
-- Reset some stuff:
patternPlayCount = 0
patternSetCount = 1
userInitiatedFill = false
vbp.views.fill_button.color = {1, 1, 1}
for t=1, #dst.tracks do
-- Only for note tracks
if song.tracks[t].type == 1 then
trackLengths[t] = getPatternTrackLength(dst:track(t))
end
end
currPattern.value = nextPattern.value
updatePattern()
updatePatternIndicator()
else
-- Update play count
patternPlayCount = patternPlayCount + 1
-- If we're back at the start, the user initiated fill needs to be reset:
if patternPlayCount % patternSetCount == 0 then
userInitiatedFill = false
vbp.views.fill_button.color = {1, 1, 1}
end
-- Update pattern
updatePattern()
if patternSetCount > 1 then
updatePatternIndicator()
end
end
end
-- Get the length of an individual track (based on it's cutoff point)
getPatternTrackLength = function(patternTrack)
local dst = song:pattern(1)
local number_of_lines = dst.number_of_lines
for l=1, number_of_lines do
local line = patternTrack:line(l)
local effect = line:effect_column(1)
if effect.number_string == "ZC" then
-- Cut!
return l - 1
end
end
return number_of_lines
end
-- Check for transition fills. These are triggered when a transition is going to happen from one pattern to the other
updatePattern = function()
-- Benchmark
local time
if benchmark == true then
time = os.clock()
end
-- TODO: refactor this whole function in multiple - testable functions
local dst = song:pattern(1)
local src = song:pattern(currPattern + 1)
dst:copy_from(src)
local trackPlayCount = get_track_play_count(patternPlayCount, dst.number_of_lines, trackLengths)
for t=1, #dst.tracks do
-- Only for note tracks
local track = song:track(t)
if track.type == 1 then
-- Do a separate iteration for the "ZC" effect.
if not process_cutoff_points(t, dst, src, song, trackLengths, patternPlayCount) then
-- Usual filtering:
for l=1, dst.number_of_lines do
-- Check for filter
local line = dst:track(t):line(l)
-- Check for track effect (these apply to the whole line):
local effect = line:effect_column(1)
-- Fill:
if effect.number_string == "ZF" then
if not is_fill(currPattern.value, nextPattern.value, patternPlayCount, patternSetCount, effect.amount_string, userInitiatedFill) then
line:clear()
end
-- Auto-queue next pattern:
elseif effect.number_string == "ZN" then
if nextPattern.value == currPattern.value then
nextPattern.value = tonumber(effect.amount_value)
end
-- Trigger:
elseif effect.number_string == "ZR" then
if not is_trig_active(effect.amount_string, patternPlayCount) then
line:clear()
end
-- Inversed Trigger:
elseif effect.number_string == "ZI" then
if is_trig_active(effect.amount_string, patternPlayCount) then
line:clear()
end
-- Start track muted, and provide functionality for auto-unmute:
elseif effect.number_string == "ZM" then
if patternPlayCount == 0 then
track:mute()
trackState[t].muted.value = true
end
if effect.amount_string ~= "00" then
local amount = tonumber(effect.amount_string)
if patternPlayCount == amount then
track:unmute()
trackState[t].muted.value = false
trackState[t].unmuteCounter.value = 0
elseif track.mute_state ~= renoise.Track.MUTE_STATE_ACTIVE then
-- Update unmute counter
trackState[t].unmuteCounter.value = amount - (patternPlayCount % amount)
end
end
elseif effect.number_string == "ZP" then
patternSetCount = tonumber(effect.amount_string)
end
-- Check for column effects (they apply to a single column):
local columns = line.note_columns
for c=1, #columns do
local column = line:note_column(c)
local effect_number = column.effect_number_string
local effect_amount = column.effect_amount_string
-- Fill:
if effect_number == "ZF" then
if not is_fill(currPattern.value, nextPattern.value, patternPlayCount, patternSetCount, effect_amount, userInitiatedFill) then
column:clear()
end
-- Trigger:
elseif effect_number == "ZR" then
if not is_trig_active(effect_amount, patternPlayCount) then
column:clear()
end
-- Inversed Trigger:
elseif effect_number == "ZI" then
if is_trig_active(effect_amount, patternPlayCount) then
column:clear()
end
-- Start column muted, and provide functionality for auto-unmute:
elseif effect_number == "ZM" then
if patternPlayCount == 0 then
track:set_column_is_muted(c, true)
trackState[t].mutedColumnCount.value = trackState[t].mutedColumnCount.value + 1
end
if effect_amount ~= "00" then
if patternPlayCount == tonumber(effect_amount) then
track:set_column_is_muted(c, false)
trackState[t].mutedColumnCount.value = trackState[t].mutedColumnCount.value - 1
end
end
end -- end if
end -- end for#columns
end -- end for#lines
end -- end normal filtering
end -- end if
end -- end for#tracks
-- Benchmark:
if benchmark == true then
-- For reference:
-- At 140 BPM, 1 step (1/16th note) is approximately 107.14 milliseconds.
-- So if this script performs well under that it's ok
print(string.format("updatePattern() - function elapsed time: %.4f\n", os.clock() - time))
end
end
local resetTriggerLights = false
local function hasNote(line)
for _, note_column in ipairs(line.note_columns) do
if note_column.note_value ~= renoise.PatternLine.EMPTY_NOTE then
return true
end
end
return false
end
-- Add notifier each time the loop ends:
local function stepNotifier()
-- Benchmark
local time
if benchmark == true then
time = os.clock()
end
-- Check for pattern change:
if currLine == song.patterns[1].number_of_lines - 1 then
if currPattern.value ~= nextPattern.value then
-- Add a "ZB00" the the last line of the master track, so the next pattern will start at 0
end
elseif currLine == song.patterns[1].number_of_lines then
-- Change patterns:
setupPattern()
end
-- Show trig indicator:
-- TODO: Performance check on RPI:
for key in pairs(trackState) do
local trackData = trackState[key]
if trackData.track ~= nil then
local line = song:pattern(1):track(trackData.track):line(currLine)
trackState[key].trigged.value = hasNote(line)
end
end
resetTriggerLights = true
if benchmark == true then
-- For reference:
-- At 140 BPM, 1 step (1/16th note) is approximately 107.14 milliseconds.
-- So if this script performs well under that it's ok
print(string.format("stepNotifier() - total elapsed time: %.4f\n", os.clock() - time))
end
end
-- Idle observer
local function idleObserver()
if song ~= nil then
currLine = song.transport.playback_pos.line
if song.transport.playing and currLine ~= prevLine then
stepNotifier()
prevLine = currLine
elseif resetTriggerLights == true then
for key in pairs(trackState) do
local trackData = trackState[key]
if trackData.track ~= nil then
local line = song:pattern(1):track(trackData.track):line(currLine)
trackState[key].trigged.value = false
end
end
resetTriggerLights = false
end
end
end
-- Function to handle key presses
local function key_handler(dialog, key)
if key.name == "left" then
queue_previous_pattern()
elseif key.name == "right" then
queue_next_pattern()
elseif key.name == "f" then
trigger_fill()
elseif key.name == "esc" then
dialog:close()
elseif key.name == "1" then
toggleMute(1)
elseif key.name == "2" then
toggleMute(2)
elseif key.name == "3" then
toggleMute(3)
elseif key.name == "4" then
toggleMute(4)
elseif key.name == "5" then
toggleMute(5)
elseif key.name == "6" then
toggleMute(6)
elseif key.name == "7" then
toggleMute(7)
elseif key.name == "8" then
toggleMute(8)
elseif key.name == "q" then
toggleMute(9)
elseif key.name == "w" then
toggleMute(10)
elseif key.name == "e" then
toggleMute(11)
elseif key.name == "r" then
toggleMute(12)
elseif key.name == "t" then
toggleMute(13)
elseif key.name == "y" then
toggleMute(14)
elseif key.name == "u" then
toggleMute(15)
elseif key.name == "i" then
toggleMute(16)
end
end
local dialog = nil
local dialog_content = nil
function showDialog()
if not dialog or not dialog.visible then
-- create, or re-create if hidden
if not dialog_content then
dialog_content = createDialog() -- run only once
end
for trackIndex = 1, 16 do
updateTrackButton(trackIndex)
end
dialog = app:show_custom_dialog("Live", dialog_content, key_handler)
else
-- bring existing/visible dialog to front
dialog:show()
end
end
-- Main window
showMainWindow = function()
if song == nil then
song = renoise.song()
end
-- Step notifier:
if renoise.tool().app_idle_observable:has_notifier(idleObserver) == false then
renoise.tool().app_idle_observable:add_notifier(idleObserver)
end
-- Load song comments (pattern remarks are in song comments)
updatePatternIndicator()
-- Reset properties:
reset()
-- Show dialog:
showDialog()
setupPattern()
end
-- Reset when a new project is loaded:
renoise.tool().app_release_document_observable:add_notifier(function()
song = nil
end)
-- Add menu entry:
tool:add_menu_entry {
name = "Main Menu:Tools:Live",
invoke = function()
showMainWindow()
end
}