-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
803 lines (572 loc) · 22.5 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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
--Curve Tests--
local app = renoise.app()
local tool = renoise.tool()
local vb = renoise.ViewBuilder()
local window_obj = nil
local window_content = nil
local gridsize = {x = 48, y = 48}
local curvegrid = {}
local buffer1 = {}
local buffer2 = {}
local points = { {0,1,1}, {1,1,1}, {1,0,1} } --x,y,weight
local selectedpoint = 1
local padreleased = true
local rainbow_mode = true
local use_x_rainbow = false
local idle_processing = false
local samplesize = 34
local draw_points_mode = false
local sampled_points = {}
local point_x
local point_y
local pascals_triangle = {}
--BINOMIAL COEFFECIENT---------------------------------
local function binom(n,k)
if k == 0 or k == n then return 1 end
if k < 0 or k > n then return 0 end
if not pascals_triangle[n] then pascals_triangle[n] = {} end
if not pascals_triangle[n][k] then
pascals_triangle[n][k] = binom(n-1,k-1) + binom(n-1,k)
end
return pascals_triangle[n][k]
end
--BERNSTEIN BASIS POLYNOMIAL---------------------------
local function bern(val,v,n)
return binom(n,v) * (val^v) * (1 - val)^(n-v)
end
--GET CURVE--------------------------------------
local function get_curve(t,points)
local coords = {}
local numerators,denominators = {0,0},{0,0} --{x,y numerators}, {x,y denominators}
local n = #points
for j = 1, 2 do --run j loop once for x coords, once for y coords
for i,point in ipairs(points)do --sum all of the points up with bernstein blending
numerators[j] = numerators[j] + ( bern(t,i-1,n-1) * point[j] * point[3] )
denominators[j] = denominators[j] + ( bern(t,i-1,n-1) * point[3] )
end
coords[j] = numerators[j]/denominators[j]
end
return coords
end
--INIT BUFFERS----------------------------
local function init_buffers()
for x = 1, gridsize.x do
if not buffer1[x] then buffer1[x] = {} end
if not buffer2[x] then buffer2[x] = {} end
for y = 1, gridsize.y do
buffer1[x][y] = 0
buffer2[x][y] = 0
end
end
end
--UPDATE CURVE GRID-------------------------------
local function update_curve_grid()
--draw our line
for x,column in ripairs(curvegrid) do
for y,pixel in ipairs(column) do
if buffer1[x][y] ~= buffer2[x][y] then
pixel.bitmap = ("Bitmaps/%s.bmp"):format(tostring(buffer1[x][y]))
end
end
end
end
--CALCULATE CURVE---------------------------------
local function calculate_curve()
--store our buffer from last frame
buffer2 = table.rcopy(buffer1)
--clear buffer1 to all 0's
for x = 1, gridsize.x do
for y = 1, gridsize.y do
buffer1[x][y] = 0
end
end
table.clear(sampled_points)
local samplesize = samplesize
--find the x,y coords for each samplesize'd-increment of t along our curve
for x = 1, samplesize do
--get our t value
local t = (x-1) / (samplesize-1)
local coords = get_curve(t,points)
--rprint(coords)
sampled_points[x] = {coords[1],coords[2]}
end
end
--REMAP RANGE-------------------------------------------------------
local function remap_range(val,lo1,hi1,lo2,hi2)
return lo2 + (hi2 - lo2) * ((val - lo1) / (hi1 - lo1))
end
--SIGN------------------------------------
local function sign(number)
return number > 0 and 1 or (number == 0 and 0 or -1)
end
--RASTERIZE CURVE------------------------------------
local function rasterize_curve()
if draw_points_mode then
for i = 1, #sampled_points - 1 do
local coords = {sampled_points[i][1],sampled_points[i][2]}
--convert from float in 0-1 range to integer in 1-gridsize range
coords[1] = math.floor(coords[1] * (gridsize.x-1) + 1.5)
--convert from float in 0-1 range to integer in 1-gridsize range
coords[2] = math.floor(coords[2] * (gridsize.y-1) + 1.5)
if not (coords[1] < coords[1] - 1 and coords[2] < coords[2] - 1) then --nan check
--add this pixel into our buffer
if rainbow_mode then
if use_x_rainbow then
--draw our line rainbow according to x coordinates
buffer1[coords[1]][coords[2]] = ("rainbow/" .. math.floor(remap_range(coords[1],1,gridsize.x,0,23)))
else
--draw our line rainbow according to t value
buffer1[coords[1]][coords[2]] = ("rainbow/" .. math.floor(remap_range(i,1,#sampled_points,0,23)))
end
else
--draw our line white
buffer1[coords[1]][coords[2]] = 1
end
end
end
else
for i = 1, #sampled_points - 1 do
--print(i)
local point_a, point_b, pixel_a, pixel_b =
{ sampled_points[i][1], sampled_points[i][2] },
{ sampled_points[i+1][1], sampled_points[i+1][2] },
{},
{}
--convert point_a from float in 0-1 range to float in 1-gridsize range
point_a[1] = remap_range(point_a[1],0,1,1,gridsize.x)
point_a[2] = remap_range(point_a[2],0,1,1,gridsize.y)
--convert point_b from float in 0-1 range to float in 1-gridsize range
point_b[1] = remap_range(point_b[1],0,1,1,gridsize.x)
point_b[2] = remap_range(point_b[2],0,1,1,gridsize.y)
--local floatslope = (point_b[2] - point_a[2]) / (point_b[1] - point_a[1]) --y/x
--convert point_a from float to integer (pixel)
pixel_a[1] = math.floor(point_a[1] + 0.5)
pixel_a[2] = math.floor(point_a[2] + 0.5)
--convert point_b from float to integer (pixel)
pixel_b[1] = math.floor(point_b[1] + 0.5)
pixel_b[2] = math.floor(point_b[2] + 0.5)
local color
if rainbow_mode and not use_x_rainbow then
--draw our line rainbow according to t value
color = ("rainbow/" .. math.floor(remap_range(i,1,#sampled_points,0,23)))
else
--draw our line white
color = 1
end
--calculate the difference in our x and y coords from point b to point a
local diff = { pixel_b[1]-pixel_a[1] , pixel_b[2]-pixel_a[2] }
--find out which plane we will traverse by 1 pixel each loop iteration
local plane
if math.abs(diff[1]) >= math.abs(diff[2]) then
--we want to traverse the x-plane
plane = 1
else
--we want to traverse the y-plane
plane = 2
end
--determine if we will be moving in positive or negative direction along plane
local step = sign(diff[plane])
--calculate our slope
local slope = step * ((plane == 1 and diff[2]/diff[1]) or diff[1]/diff[2]) --(our slope is dependent on which plane we're on)
print("!!!!!!!!!!!!!!!!!")
local current_coords = {pixel_a[1],pixel_a[2]}
local slope_acc = point_a[plane%2 + 1] - pixel_a[plane%2 + 1]
while(true) do
print("slope_acc: " .. slope_acc)
if rainbow_mode and use_x_rainbow then
--draw our line rainbow according to x coordinates
buffer1[current_coords[1]][current_coords[2]] =
("rainbow/" .. math.floor(remap_range(current_coords[1],1,gridsize.x,0,23)))
else
buffer1[current_coords[1]][current_coords[2]] = color
end
if current_coords[plane] == pixel_b[plane] then break end --if we are at the end pixel, we break
current_coords[plane] = current_coords[plane] + step
slope_acc = slope_acc + slope
current_coords[plane%2 + 1] = math.floor(pixel_a[plane%2 + 1] + slope_acc + 0.5)
end
end
end
end
--SHOW POINTS-----------------------------
local function show_points()
for i,p in ipairs(points) do
local point = {}
--convert from float in 0-1 range to integer in 1-gridsize range
point[1] = math.floor(p[1] * (gridsize.x-1) + 1.5)
--convert from float in 0-1 range to integer in 1-gridsize range
point[2] = math.floor(p[2] * (gridsize.y-1) + 1.5)
--add this pixel into our buffer
if rainbow_mode then
--get the t value where this point exerts its influence on the line
local t = (i-1) / (#points-1)
--get the coordinates of where the line sits for that t value
local coords = get_curve(t,points)
--convert from float in 0-1 range to integer in 1-gridsize range
coords[1] = math.floor(coords[1] * (gridsize.x-1) + 1.5)
--convert from float in 0-1 range to integer in 1-gridsize range
coords[2] = math.floor(coords[2] * (gridsize.y-1) + 1.5)
if use_x_rainbow then
--draw our point rainbow according to x coordinates
buffer1[point[1]][point[2]] = ("rainbow/" .. math.floor((coords[1] * 23) / gridsize.x))
else
--draw our point rainbow according to t value
buffer1[point[1]][point[2]] = ("rainbow/" .. math.floor(t * 23))
end
else
--draw our point white
buffer1[point[1]][point[2]] = 1
end
--draw a grey cross around our currently selected point
if i == selectedpoint then
if point[1] - 1 > 0 then buffer1[point[1] - 1][point[2]] = 0.25 end
if point[1] + 1 < gridsize.x then buffer1[point[1] + 1][point[2]] = 0.25 end
if point[2] - 1 > 0 then buffer1[point[1]][point[2] - 1] = 0.25 end
if point[2] + 1 < gridsize.y then buffer1[point[1]][point[2] + 1] = 0.25 end
end
end
end
--SHOW GUIDES-----------------------------
local function show_guides()
buffer1[math.floor((gridsize.x/2) + 0.5)][1] = 0.25
buffer1[math.floor((gridsize.x/2) + 0.5)][gridsize.y] = 0.25
buffer1[1][math.floor((gridsize.y/2) + 0.5)] = 0.25
buffer1[gridsize.x][math.floor((gridsize.y/2) + 0.5)] = 0.25
buffer1[math.floor((gridsize.x/2) + 0.5)][math.floor((gridsize.y/2) + 0.5)] = 0.25
end
--UPDATE TEXTS-----------------------------
local function update_texts()
vb.views.sel_text.value = selectedpoint
vb.views.x_text.value = points[selectedpoint][1]
vb.views.y_text.value = points[selectedpoint][2]
end
--PROCESSING---------------------------------
local function processing()
show_guides() --displaying some grid markers
calculate_curve() --samples points on the curve and stores them
rasterize_curve() --interpolates sampled points, adding them to the pixel buffer
show_points() --showing the control points
update_curve_grid() --pushes the pixel buffer to the display
update_texts()
end
--APPLY UPDATE NOTIFIER----------------------------------
local function apply_update_notifier()
processing()
tool.app_idle_observable:remove_notifier(apply_update_notifier)
end
--QUEUE PROCESSING--------------------------------------
local function queue_processing()
if not idle_processing then
processing()
else
if not tool.app_idle_observable:has_notifier(apply_update_notifier) then
tool.app_idle_observable:add_notifier(apply_update_notifier)
end
end
end
--FIND NEAREST POINT------------------------------------
local function find_nearest_point(x,y)
--print("x: " .. x .. " y: " .. y)
local nearest_point = {5,0} --distance,index
for k,point in ipairs(points) do
local distance = math.abs(x - point[1]) + math.abs(y - point[2])
--print("point: " .. k .. " distance: " .. distance)
if distance < nearest_point[1] then
nearest_point[2] = k
nearest_point[1] = distance
end
end
return nearest_point[2]
end
--CREATE DIALOG----------------------------------------------------
local function create_dialog()
if not window_content then
window_content = vb:column {}
local curvegridcontent = vb:row{
spacing = -gridsize.x * 4,
vb:xypad {
width = gridsize.x * 4,
height = gridsize.y * 4,
value = {x = 0.5, y = 0.5},
snapback = {x = 0.5, y = 0.5},
notifier = function(value)
if padreleased then
if value.x ~= 0.5 then point_x = value.x end
if value.y ~= 0.5 then point_y = value.y end
if value.x ~= 0.5 and value.y ~= 0.5 then
selectedpoint = find_nearest_point(point_x,point_y)
padreleased = false
--print("find_nearest_point")
vb.views.slider.value = points[selectedpoint][3] - 1
end
end
if value.x == 0.5 or value.y == 0.5 then
padreleased = true
--print("padreleased!")
else
--print("processing")
--set point to our xyPad coordinates
points[selectedpoint][1] = value.x
points[selectedpoint][2] = value.y
queue_processing()
end
end
}
}
local gridcolumn = vb:column{}
local gridrow = vb:row {}
--create our curve grid
for x = 1, gridsize.x do
curvegrid[x] = {}
-- create a column
local column = vb:column {}
for y = 1, gridsize.y do
--fill the column with 16 pixels
curvegrid[x][gridsize.y+1 - y] = vb:bitmap {
bitmap = "Bitmaps/0.bmp",
active = false
}
-- add the pixel by "hand" into the row
column:add_child(curvegrid[x][gridsize.y+1 - y])
end
gridrow:add_child(column)
end
gridcolumn:add_child(gridrow)
curvegridcontent:add_child(gridcolumn)
window_content:add_child(curvegridcontent)
local controlcolumn = vb:column {
vb:row {
vb:valuefield {
id = "sel_text",
width = 16,
tooltip = "Currently selected point",
min = 1,
max = 99,
value = selectedpoint,
--tonumber converts any typed-in user input to a number value
--(called only if value was typed)
tonumber = function(str)
local val = str:gsub("[^0-9.-]", "") --filter string to get numbers and decimals
val = tonumber(val) --this tonumber() is Lua's basic string-to-number converter
if val and 1 <= val then --if val is a number, and within min/max
selectedpoint = val
vb.views.slider.value = points[selectedpoint][3] - 1
queue_processing()
end
return val
end,
--tostring is called when field is clicked,
--after tonumber is called,
--and after the notifier is called
--it converts the value to a formatted string to be displayed
tostring = function(value)
return ("%i"):format(value)
end,
--notifier is called whenever the value is changed
notifier = function(value)
end
},
vb:minislider {
id = "slider",
tooltip = "Set the weight of the selected point",
width = gridsize.x,
height = 16,
min = -1,
max = 8,
value = 0,
notifier = function(value)
points[selectedpoint][3] = value + 1
queue_processing()
end
},
vb:button {
text = "+",
tooltip = "Add a new control point after the currently selected control point",
pressed = function()
table.insert(points, math.floor(selectedpoint + 1), {0.5,0.5,1})
selectedpoint = selectedpoint + 1
queue_processing()
end
},
vb:button {
text = "-",
tooltip = "Remove the currently selected control point",
pressed = function()
if #points > 2 then
table.remove(points, selectedpoint)
queue_processing()
end
end
},
vb:valuefield {
id = "x_text",
width = 32,
tooltip = "X coordinate of the currently selected point",
min = 0,
max = 1,
value = points[selectedpoint][1],
--tonumber converts any typed-in user input to a number value
--(called only if value was typed)
tonumber = function(str)
local val = str:gsub("[^0-9.-]", "") --filter string to get numbers and decimals
val = tonumber(val) --this tonumber() is Lua's basic string-to-number converter
if val and 0 <= val and val <= 1 then --if val is a number, and within min/max
--set point to our xyPad coordinates
points[selectedpoint][1] = val
queue_processing()
end
return val
end,
--tostring is called when field is clicked,
--after tonumber is called,
--and after the notifier is called
--it converts the value to a formatted string to be displayed
tostring = function(value)
return ("%.3f"):format(value)
end,
--notifier is called whenever the value is changed
notifier = function(value)
end
},
vb:valuefield {
id = "y_text",
width = 32,
tooltip = "Y coordinate of the currently selected point",
min = 0,
max = 1,
value = points[selectedpoint][2],
--tonumber converts any typed-in user input to a number value
--(called only if value was typed)
tonumber = function(str)
local val = str:gsub("[^0-9.-]", "") --filter string to get numbers and decimals
val = tonumber(val) --this tonumber() is Lua's basic string-to-number converter
if val and 0 <= val and val <= 1 then --if val is a number, and within min/max
--set point to our xyPad coordinates
points[selectedpoint][2] = val
queue_processing()
end
return val
end,
--tostring is called when field is clicked,
--after tonumber is called,
--and after the notifier is called
--it converts the value to a formatted string to be displayed
tostring = function(value)
return ("%.3f"):format(value)
end,
--notifier is called whenever the value is changed
notifier = function(value)
end
}
},
vb:row {
vb:checkbox {
tooltip = "Rainbow Mode",
value = rainbow_mode,
notifier = function(val)
rainbow_mode = val
queue_processing()
end
},
vb:checkbox {
tooltip = "True - Distribute rainbow across range of entire window based on X coordinate\nFalse - Distribute rainbow across range of the line segment fully",
value = use_x_rainbow,
notifier = function(val)
use_x_rainbow = val
queue_processing()
end
},
vb:checkbox {
tooltip = "True - Offload processing to idle notifier\nFalse - Process changes immediately",
value = idle_processing,
notifier = function(val)
idle_processing = val
queue_processing()
end
},
vb:checkbox {
tooltip = "Point Mode",
value = draw_points_mode,
notifier = function(val)
draw_points_mode = val
queue_processing()
end
},
vb:minislider {
tooltip = "t sample size",
width = gridsize.x,
height = 16,
min = 0,
max = 524,
value = 0,
notifier = function(value)
samplesize = math.floor(value + 1)
print("samplesize: " .. samplesize)
queue_processing()
end
}
}
}
window_content:add_child(controlcolumn)
end
--key handler function
local function key_handler(dialog,key)
if key.state == "pressed" then
if not key.repeated then
if key.modifiers == "" then
elseif key.modifiers == "shift" then
elseif key.modifiers == "alt" then
elseif key.modifiers == "control" then
elseif key.modifiers == "shift + alt" then
elseif key.modifiers == "shift + control" then
elseif key.modifiers == "alt + control" then
elseif key.modifiers == "shift + alt + control" then
end
elseif key.repeated then
if key.modifiers == "" then
elseif key.modifiers == "shift" then
elseif key.modifiers == "alt" then
elseif key.modifiers == "control" then
elseif key.modifiers == "shift + alt" then
elseif key.modifiers == "shift + control" then
elseif key.modifiers == "alt + control" then
elseif key.modifiers == "shift + alt + control" then
end
end --end if key.repeated
elseif key.state == "released" then
if key.modifiers == "" then
elseif key.modifiers == "shift" then
elseif key.modifiers == "alt" then
elseif key.modifiers == "control" then
elseif key.modifiers == "shift + alt" then
elseif key.modifiers == "shift + control" then
elseif key.modifiers == "alt + control" then
elseif key.modifiers == "shift + alt + control" then
end
end --end if key.state == "pressed"/"released"
end --end key_handler()
--key handler options
local key_handler_options = {
send_key_repeat = true,
send_key_release = true
}
--create the dialog if it show the dialog window
if not window_obj or not window_obj.visible then
window_obj = app:show_custom_dialog("Curves", window_content, key_handler, key_handler_options)
else window_obj:show() end
end
--INIT TOOL--------------------------
local function init_tool()
create_dialog()
init_buffers()
queue_processing()
end
--MENU ENTRIES----------------------------------------------------
renoise.tool():add_menu_entry {
name = "Main Menu:Tools:Curve Tests...",
invoke = function() init_tool() end
}
renoise.tool():add_keybinding {
name = "Global:Tools:Curve Tests...",
invoke = function() init_tool() end
}