-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.lua
1076 lines (946 loc) · 38.3 KB
/
app.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
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local App = {}
local MIDI = require "luamidi"
local UI = require "ui/ui"
local Phywire = require "phywire"
local Json = require "json"
local e_mode = { play = 1, move_piece = 2, move_kit = 3 }
local e_axis = { x = 1, y = 2, z = 3 }
local e_drum_kit_piece_type = {
snare = 1,
kick = 2,
hihat = 3,
crash_left = 4,
crash_right = 5,
ride = 6,
tom1 = 7,
tom2 = 8,
tom3 = 9,
tom4 = 10
}
local available_pieces = {
{ "Snare 14 X 5", 0.3556, 0.127, 1 },
{ "Snare 14 X 6", 0.3556, 0.1524, 1 },
{ "Snare 14 X 7", 0.3556, 0.1778, 1 },
{ "Kick 20 X 15", 0.508, 0.381, 2 },
{ "Kick 22 X 16", 0.5588, 0.4064, 2 },
{ "Kick 22 X 17", 0.5588, 0.4318, 2 },
{ "Tom 10 X 8", 0.254, 0.2032, 7 },
{ "Tom 12 X 9", 0.3048, 0.2286, 8 },
{ "Tom 14 X 10", 0.3556, 0.254, 9 },
{ "Tom 16 X 14", 0.4064, 0.3556, 10 },
{ "Hihat 13", 0.3302, 0, 3 },
{ "Hihat 14", 0.3556, 0, 3 },
{ "Hihat 15", 0.381, 0, 3 },
{ "Crash_L 18", 0.4572, 0, 4 },
{ "Crash_L 19", 0.4826, 0, 4 },
{ "Crash_L 21", 0.5334, 0, 4 },
{ "Crash_R 18", 0.4572, 0, 5 },
{ "Crash_R 19", 0.4826, 0, 5 },
{ "Crash_R 21", 0.5334, 0, 5 },
{ "Ride 18", 0.4572, 0, 6 },
{ "Ride 19", 0.4826, 0, 6 },
{ "Ride 21", 0.5334, 0, 6 },
}
local samples_list = {
{ inner = {}, outer = {} },
{ inner = {}, outer = {} },
{ inner = { open = {}, closed = {} }, outer = { open = {}, closed = {} }, pedal = {} },
{ inner = {}, outer = {} },
{ inner = {}, outer = {} },
{ inner = {}, outer = {} },
{ inner = {}, outer = {} },
{ inner = {}, outer = {} },
{ inner = {}, outer = {} },
{ inner = {}, outer = {} },
}
local output_mode = "samples"
local output_mode_idx = 1
local hihat_open_value = 127
local drum_kit_name = ""
local hihat_closed = false
local hihat_keybind = nil
local scheduled_off_notes = {}
local enable_haptics = true
local enable_hit_highlight = true
local keybind_window_open = false
local add_piece_window_open = false
local rename_kit_window_open = false
local key_pressed = nil
local drag_table = {}
local dragged_piece = nil
local drag_offset = lovr.math.newMat4()
local MIDI_ports = {}
local cur_MIDI_port = 0
local mode = e_mode.play
local setup_window_pose = lovr.math.newMat4( vec3( -1, 1, -0.6 ), quat( math.pi / 4, 0, 1, 0 ) )
local show_colliders = false
local skybox_tex = lovr.graphics.newTexture( "res/skybox.hdr", { mipmaps = false } )
local vs = lovr.filesystem.read( "light.vs" )
local fs = lovr.filesystem.read( "light.fs" )
local shader = lovr.graphics.newShader( vs, fs, { flags = { glow = true, normalMap = true, vertexTangents = false, tonemap = false } } )
local world = lovr.physics.newWorld( { tags = { "drums", "stickL", "stickR", "drums_inner" }, staticTags = { "drums", "stickL", "stickR", "drums_inner" } } )
local mdl_stick, mdl_cymbal, mdl_drum, mdl_drum_highlight, mdl_cymbal_highlight
local cur_piece_index = 1
local cur_drum_kit_index = 1
local event_info = { note = 0, velocity = 0 }
local drum_kits = {}
local sticks = {
left_collider = nil,
right_collider = nil,
left_tip = lovr.math.newVec3( 0, 0, 0 ),
right_tip = lovr.math.newVec3( 0, 0, 0 ),
left_vel = 0,
right_vel = 0,
left_colliding_drum = nil,
right_colliding_drum = nil,
left_colliding_drum_prev = nil,
right_colliding_drum_prev = nil,
length = 0.4318,
rotation = -0.35,
pivot_offset = 0.12,
left = lovr.math.newMat4(),
right = lovr.math.newMat4(),
left_tip_prev = lovr.math.newVec3( 0, 0, 0 ),
right_tip_prev = lovr.math.newVec3( 0, 0, 0 )
}
local function ReadFileToSTring( filename )
local f = assert( io.open( filename, "rb" ) )
local str = f:read( "*all" )
f:close()
return str
end
local function ShaderOn( pass )
pass:skybox( skybox_tex )
pass:setColor( 1, 1, 1 )
pass:setShader( shader )
local lightPos = vec3( 0, 2.5, -1.3 )
pass:send( 'ambience', { 0.05, 0.05, 0.05, 1.0 } )
pass:send( 'lightColor', { 1.0, 1.0, 1.0, 1.0 } )
pass:send( 'lightPos', lightPos )
pass:send( 'specularStrength', 0.5 )
pass:send( 'metallic', 32.0 )
end
local function ShaderOff( pass )
pass:setShader()
end
local function SetEnvironment( pass )
lovr.graphics.setBackgroundColor( 1, 1, 1 )
ShaderOff( pass )
pass:setColor( 1, 1, 1 )
end
local function MapRange( from_min, from_max, to_min, to_max, v )
return (v - from_min) * (to_max - to_min) / (from_max - from_min) + to_min
end
local function UpdateSticksColliders()
local pos = vec3( lovr.headset.getPosition( "hand/left" ) )
local ori = quat( lovr.headset.getOrientation( "hand/left" ) )
local pose = mat4( pos, quat() ):rotate( ori:mul( quat( sticks.rotation, 1, 0, 0 ) ) ):scale( vec3( 0.43, 0.43, sticks.length ) ):translate( 0, 0, -0.5 + (sticks.pivot_offset / sticks.length) )
sticks.left_collider:setPose( vec3( pose ), quat( pose ) )
local pos = vec3( lovr.headset.getPosition( "hand/right" ) )
local ori = quat( lovr.headset.getOrientation( "hand/right" ) )
local pose = mat4( pos, quat() ):rotate( ori:mul( quat( sticks.rotation, 1, 0, 0 ) ) ):scale( vec3( 0.43, 0.43, sticks.length ) ):translate( 0, 0, -0.5 + (sticks.pivot_offset / sticks.length) )
sticks.right_collider:setPose( vec3( pose ), quat( pose ) )
end
local function UpdateSticksVelocity()
local pos = vec3( lovr.headset.getPosition( "hand/left" ) )
local ori = quat( lovr.headset.getOrientation( "hand/left" ) )
local m = mat4( pos, ori ):rotate( sticks.rotation, 1, 0, 0 ):translate( 0, 0, -sticks.pivot_offset / sticks.length ):scale( 0.05 )
sticks.left_tip = lovr.math.newVec3( m )
sticks.left_vel = MapRange( 0, 0.14, 0, 127, sticks.left_tip:distance( sticks.left_tip_prev ) )
if sticks.left_tip.y > sticks.left_tip_prev.y then sticks.left_vel = 0 end
sticks.left_vel = math.floor( sticks.left_vel )
if sticks.left_vel < 0 then sticks.left_vel = 0 end
if sticks.left_vel > 127 then sticks.left_vel = 127 end
sticks.left_tip_prev = sticks.left_tip
local pos = vec3( lovr.headset.getPosition( "hand/right" ) )
local ori = quat( lovr.headset.getOrientation( "hand/right" ) )
local m = mat4( pos, ori ):rotate( sticks.rotation, 1, 0, 0 ):translate( 0, 0, -sticks.pivot_offset / sticks.length ):scale( 0.05 )
sticks.right_tip = lovr.math.newVec3( m )
sticks.right_vel = MapRange( 0, 0.14, 0, 127, sticks.right_tip:distance( sticks.right_tip_prev ) )
if sticks.right_tip.y > sticks.right_tip_prev.y then sticks.right_vel = 0 end
sticks.right_vel = math.floor( sticks.right_vel )
if sticks.right_vel < 0 then sticks.right_vel = 0 end
if sticks.right_vel > 127 then sticks.right_vel = 127 end
sticks.right_tip_prev = sticks.right_tip
end
local function SetupDrumColliders()
for i, v in ipairs( drum_kits ) do
for j, k in ipairs( drum_kits[ i ] ) do
if drum_kits[ i ][ j ].collider then
if not drum_kits[ i ][ j ].collider:isDestroyed() then drum_kits[ i ][ j ].collider:destroy() end
drum_kits[ i ][ j ].collider = nil
if not drum_kits[ i ][ j ].collider_inner:isDestroyed() then drum_kits[ i ][ j ].collider_inner:destroy() end
drum_kits[ i ][ j ].collider_inner = nil
end
end
end
for i, v in ipairs( drum_kits[ cur_drum_kit_index ] ) do
local x, y, z, sx, sy, sz = v.pose:unpack()
if v.type == e_drum_kit_piece_type.crash_left or v.type == e_drum_kit_piece_type.ride or v.type == e_drum_kit_piece_type.crash_right or v.type == e_drum_kit_piece_type.hihat then
v.collider = world:newCylinderCollider( 0, 0, 0, sx / 2, 0.08 )
v.collider_inner = world:newCylinderCollider( 0, 0, 0, sx / 8, 0.08 )
else
v.collider = world:newCylinderCollider( 0, 0, 0, sx / 2, sy )
v.collider_inner = world:newCylinderCollider( 0, 0, 0, sx / 2.6, sy )
end
local x, y, z, sx, sy, sz, angle, ax, ay, az = drum_kits[ cur_drum_kit_index ][ i ].pose:unpack()
drum_kits[ cur_drum_kit_index ][ i ].collider:setPose( vec3( drum_kits[ cur_drum_kit_index ][ i ].pose ), quat( drum_kits[ cur_drum_kit_index ][ i ].pose ):mul( quat( math.pi / 2, 1, 0, 0 ) ) )
drum_kits[ cur_drum_kit_index ][ i ].collider_inner:setPose( vec3( drum_kits[ cur_drum_kit_index ][ i ].pose ),
quat( drum_kits[ cur_drum_kit_index ][ i ].pose ):mul( quat( math.pi / 2, 1, 0, 0 ) ) )
v.collider:setTag( "drums" )
v.collider:setUserData( i )
v.collider:setKinematic( true )
v.collider_inner:setTag( "drums_inner" )
v.collider_inner:setUserData( i )
v.collider_inner:setKinematic( true )
end
end
local function DrawDrumKit( pass )
local cur_kit = drum_kits[ cur_drum_kit_index ]
for i, v in ipairs( cur_kit ) do
if v.type == e_drum_kit_piece_type.crash_left or v.type == e_drum_kit_piece_type.ride or v.type == e_drum_kit_piece_type.crash_right then
pass:draw( mdl_cymbal, v.pose )
if enable_hit_highlight then
if sticks.left_colliding_drum == i or sticks.right_colliding_drum == i then
local m = mat4( v.pose ):translate( 0, 0.001, 0 )
pass:setColor( 1, 0.9, 0.9 )
pass:draw( mdl_cymbal_highlight, m )
pass:setColor( 1, 1, 1 )
end
end
elseif v.type == e_drum_kit_piece_type.hihat then
local hihat_top_pose = mat4( v.pose )
local dist = MapRange( 0, 127, 0, 0.03, hihat_open_value )
hihat_top_pose:translate( 0, dist, 0 )
pass:draw( mdl_cymbal, hihat_top_pose )
local hihat_bottom_pose = mat4( v.pose ):rotate( math.pi, 1, 0, 0 )
pass:draw( mdl_cymbal, hihat_bottom_pose )
if enable_hit_highlight then
if sticks.left_colliding_drum == i or sticks.right_colliding_drum == i then
local m = mat4( hihat_top_pose ):translate( 0, 0.001, 0 )
pass:setColor( 1, 0.9, 0.9 )
pass:draw( mdl_cymbal_highlight, m )
pass:setColor( 1, 1, 1 )
end
end
else
pass:draw( mdl_drum, v.pose )
if enable_hit_highlight then
if sticks.left_colliding_drum == i or sticks.right_colliding_drum == i then
local m = mat4( v.pose ):translate( 0, 0.001, 0 )
pass:setColor( 1, 0.9, 0.9 )
pass:draw( mdl_drum_highlight, m )
pass:setColor( 1, 1, 1 )
end
end
end
end
pass:setColor( 1, 1, 1 )
end
local function DrawSticks( pass )
local pos = vec3( lovr.headset.getPosition( "hand/left" ) )
local ori = quat( lovr.headset.getOrientation( "hand/left" ) )
local pose = mat4( pos, quat() ):rotate( ori:mul( quat( sticks.rotation, 1, 0, 0 ) ) ):scale( vec3( 0.43, 0.43, sticks.length ) ):translate( 0, 0, sticks.pivot_offset / sticks.length )
pass:draw( mdl_stick, pose )
local pos = vec3( lovr.headset.getPosition( "hand/right" ) )
local ori = quat( lovr.headset.getOrientation( "hand/right" ) )
local pose = mat4( pos, quat() ):rotate( ori:mul( quat( sticks.rotation, 1, 0, 0 ) ) ):scale( vec3( 0.43, 0.43, sticks.length ) ):translate( 0, 0, sticks.pivot_offset / sticks.length )
pass:draw( mdl_stick, pose )
end
local function LoadSamples()
local options = { decode = true, pitchable = false, spatial = true, effects = { "spatialization", "attenuation" } }
local type_id = {
[ "sr" ] = 1,
[ "kk" ] = 2,
[ "cl" ] = 4,
[ "cr" ] = 5,
[ "rd" ] = 6,
[ "t1" ] = 7,
[ "t2" ] = 8,
[ "t3" ] = 9,
[ "t4" ] = 10,
}
local items = lovr.filesystem.getDirectoryItems( "samples" )
for i, filename in ipairs( items ) do
local type = string.sub( filename, 1, 2 )
local zone_prefix = string.sub( filename, 4, 4 )
local zone = zone_prefix == "i" and "inner" or "outer"
local sample_num = string.sub( filename, 6, 6 )
local source = lovr.audio.newSource( "samples/" .. items[ i ], options )
-- source:setEffectEnabled( "spatialization", true )
if type == "ho" then
table.insert( samples_list[ 3 ][ zone ].open, sample_num, source )
elseif type == "hc" then
table.insert( samples_list[ 3 ][ zone ].closed, sample_num, source )
elseif type == "hh" then
table.insert( samples_list[ 3 ][ "pedal" ], 1, source )
else
table.insert( samples_list[ type_id[ type ] ][ zone ], sample_num, source )
end
end
end
local function LoadKits()
local str = ReadFileToSTring( "kits.json" )
local decoded = Json.decode( str )
local kits = Json.decode( str )
local num_kits = #kits
for i = 1, num_kits do
local cur_kit = {}
cur_kit.name = kits[ i ][ "kitname" ]
local num_pieces = #kits[ i ][ "kitpieces" ]
for j = 1, num_pieces do
local cur_piece = {}
cur_piece.name = kits[ i ][ "kitpieces" ][ j ][ "name" ]
cur_piece.note = { kits[ i ][ "kitpieces" ][ j ][ "note" ][ 1 ], kits[ i ][ "kitpieces" ][ j ][ "note" ][ 2 ] }
cur_piece.type = kits[ i ][ "kitpieces" ][ j ][ "type" ]
cur_piece.channel = kits[ i ][ "kitpieces" ][ j ][ "channel" ]
if cur_piece.type == e_drum_kit_piece_type.hihat then
cur_piece.note[ 3 ] = kits[ i ][ "kitpieces" ][ j ][ "note" ][ 3 ]
end
cur_piece.keybind = kits[ i ][ "kitpieces" ][ j ][ "keybind" ]
local f = kits[ i ][ "kitpieces" ][ j ][ "pose" ]
cur_piece.pose = lovr.math.newMat4( f[ 1 ], f[ 2 ], f[ 3 ], f[ 4 ], f[ 5 ], f[ 6 ], f[ 7 ], f[ 8 ], f[ 9 ], f[ 10 ], f[ 11 ], f[ 12 ], f[ 13 ], f[ 14 ], f[ 15 ], f[ 16 ] )
table.insert( cur_kit, cur_piece )
end
table.insert( drum_kits, cur_kit )
end
end
local function SaveKits()
local f = io.open( "kits.json", "wb" )
local kits = {}
local num_kits = #drum_kits
for i = 1, num_kits do
local cur_kit = {}
cur_kit[ "kitname" ] = drum_kits[ i ].name
cur_kit[ "kitpieces" ] = {}
local num_pieces = #drum_kits[ i ]
for j = 1, num_pieces do
local cur_piece = {}
cur_piece[ "name" ] = drum_kits[ i ][ j ].name
cur_piece[ "note" ] = { drum_kits[ i ][ j ].note[ 1 ], drum_kits[ i ][ j ].note[ 2 ] }
cur_piece[ "type" ] = drum_kits[ i ][ j ].type
cur_piece[ "channel" ] = drum_kits[ i ][ j ].channel
if cur_piece.type == e_drum_kit_piece_type.hihat then
cur_piece[ "note" ][ 3 ] = drum_kits[ i ][ j ].note[ 3 ]
end
cur_piece[ "keybind" ] = drum_kits[ i ][ j ].keybind
local m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, m15, m16 = drum_kits[ i ][ j ].pose:unpack( true )
local pose = { m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, m15, m16 }
cur_piece.pose = pose
table.insert( cur_kit[ "kitpieces" ], cur_piece )
end
table.insert( kits, cur_kit )
end
local out = Json.encode( kits )
f:write( out )
io.close( f )
end
local function AddKit()
local new_kit = { name = "Custom kit" }
for i, v in ipairs( drum_kits[ cur_drum_kit_index ] ) do
local piece = {}
piece.name = v.name
piece.type = v.type
piece.channel = v.channel
piece.note = v.note
piece.keybind = v.keybind
piece.collider = v.collider
piece.collider_inner = v.collider_inner
local m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, m15, m16 = v.pose:unpack( true )
piece.pose = lovr.math.newMat4( m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, m15, m16 )
table.insert( new_kit, piece )
end
table.insert( drum_kits, new_kit )
end
local function DrawUI( pass )
UI.NewFrame( pass )
UI.Begin( "FirstWindow", setup_window_pose )
UI.Label( "Event: Note - " .. event_info.note .. ", Velocity - " .. event_info.velocity )
UI.Label( "MIDI ports", true )
local _, mp = UI.ListBox( "MIDI_ports", 5, 27, MIDI_ports, 1 )
cur_MIDI_port = mp - 1
UI.Label( "Drum Kits", true )
local dkits = {}
for i, v in ipairs( drum_kits ) do
table.insert( dkits, v.name )
end
local changed, idx = UI.ListBox( "kits", 9, 27, dkits, 1 )
if changed then
cur_drum_kit_index = idx
cur_piece_index = 1
SetupDrumColliders()
end
UI.SameLine()
if UI.Button( "Add kit", 300 ) then
AddKit()
end
UI.SameColumn()
if UI.Button( "Delete kit", 300 ) then
if #drum_kits > 1 then
for i = #drum_kits[ cur_drum_kit_index ], 1, -1 do
if not drum_kits[ cur_drum_kit_index ][ i ].collider:isDestroyed() then drum_kits[ cur_drum_kit_index ][ i ].collider:destroy() end
if not drum_kits[ cur_drum_kit_index ][ i ].collider_inner:isDestroyed() then drum_kits[ cur_drum_kit_index ][ i ].collider_inner:destroy() end
table.remove( drum_kits[ cur_drum_kit_index ], i )
end
drum_kits[ cur_drum_kit_index ].name = nil
table.remove( drum_kits, cur_drum_kit_index )
cur_drum_kit_index = 1
SetupDrumColliders()
return
end
end
UI.SameColumn()
if UI.Button( "Rename kit", 300 ) then
rename_kit_window_open = true
end
UI.SameColumn()
if UI.Button( "Save changes", 300 ) then
SaveKits()
end
UI.Label( "Pieces", true )
local pieces = {}
for i, v in ipairs( drum_kits[ cur_drum_kit_index ] ) do
table.insert( pieces, v.name )
end
local changed, cur_piece_index = UI.ListBox( "pieces", 12, 27, pieces, 1 )
UI.SameLine()
if UI.Button( "Add piece", 300 ) then
add_piece_window_open = true
end
UI.SameColumn()
if UI.Button( "Delete piece", 300 ) then
if #drum_kits[ cur_drum_kit_index ] > 1 then -- prevent deleting last piece
drum_kits[ cur_drum_kit_index ][ cur_piece_index ].collider:destroy()
drum_kits[ cur_drum_kit_index ][ cur_piece_index ].collider_inner:destroy()
table.remove( drum_kits[ cur_drum_kit_index ], cur_piece_index )
SetupDrumColliders()
return
end
end
if UI.Button( "-" ) then
if drum_kits[ cur_drum_kit_index ][ cur_piece_index ].note[ 1 ] > 0 then
drum_kits[ cur_drum_kit_index ][ cur_piece_index ].note[ 1 ] = drum_kits[ cur_drum_kit_index ][ cur_piece_index ].note[ 1 ] - 1
end
end
UI.SameLine()
if UI.Button( "+" ) then
if drum_kits[ cur_drum_kit_index ][ cur_piece_index ].note[ 1 ] < 127 then
drum_kits[ cur_drum_kit_index ][ cur_piece_index ].note[ 1 ] = drum_kits[ cur_drum_kit_index ][ cur_piece_index ].note[ 1 ] + 1
end
end
UI.SameLine()
local changed
changed, drum_kits[ cur_drum_kit_index ][ cur_piece_index ].note[ 1 ] = UI.SliderInt( "Note inner", drum_kits[ cur_drum_kit_index ][ cur_piece_index ].note[ 1 ], 0, 127 )
if UI.Button( "-" ) then
if drum_kits[ cur_drum_kit_index ][ cur_piece_index ].note[ 2 ] > 0 then
drum_kits[ cur_drum_kit_index ][ cur_piece_index ].note[ 2 ] = drum_kits[ cur_drum_kit_index ][ cur_piece_index ].note[ 2 ] - 1
end
end
UI.SameLine()
if UI.Button( "+" ) then
if drum_kits[ cur_drum_kit_index ][ cur_piece_index ].note[ 2 ] < 127 then
drum_kits[ cur_drum_kit_index ][ cur_piece_index ].note[ 2 ] = drum_kits[ cur_drum_kit_index ][ cur_piece_index ].note[ 2 ] + 1
end
end
UI.SameLine()
local changed
changed, drum_kits[ cur_drum_kit_index ][ cur_piece_index ].note[ 2 ] = UI.SliderInt( "Note outer", drum_kits[ cur_drum_kit_index ][ cur_piece_index ].note[ 2 ], 0, 127 )
-- hihat has 1 additional note (pedal down)
local selected_piece_type = drum_kits[ cur_drum_kit_index ][ cur_piece_index ].type
if selected_piece_type == e_drum_kit_piece_type.hihat then
if UI.Button( "-" ) then
if drum_kits[ cur_drum_kit_index ][ cur_piece_index ].note[ 3 ] > 0 then
drum_kits[ cur_drum_kit_index ][ cur_piece_index ].note[ 3 ] = drum_kits[ cur_drum_kit_index ][ cur_piece_index ].note[ 3 ] - 1
end
end
UI.SameLine()
if UI.Button( "+" ) then
if drum_kits[ cur_drum_kit_index ][ cur_piece_index ].note[ 3 ] < 127 then
drum_kits[ cur_drum_kit_index ][ cur_piece_index ].note[ 3 ] = drum_kits[ cur_drum_kit_index ][ cur_piece_index ].note[ 3 ] + 1
end
end
UI.SameLine()
local changed
changed, drum_kits[ cur_drum_kit_index ][ cur_piece_index ].note[ 3 ] = UI.SliderInt( "Note pedal down", drum_kits[ cur_drum_kit_index ][ cur_piece_index ].note[ 3 ], 0, 127 )
end
local cur_piece_channel = drum_kits[ cur_drum_kit_index ][ cur_piece_index ].channel
if UI.Button( "-" ) and cur_piece_channel > 0 then
drum_kits[ cur_drum_kit_index ][ cur_piece_index ].channel = drum_kits[ cur_drum_kit_index ][ cur_piece_index ].channel - 1
end
UI.SameLine()
if UI.Button( "+" ) and cur_piece_channel < 15 then
drum_kits[ cur_drum_kit_index ][ cur_piece_index ].channel = drum_kits[ cur_drum_kit_index ][ cur_piece_index ].channel + 1
end
UI.SameLine()
local changed
changed, drum_kits[ cur_drum_kit_index ][ cur_piece_index ].channel = UI.SliderInt( "MIDI Channel", drum_kits[ cur_drum_kit_index ][ cur_piece_index ].channel, 0, 15 )
local cur_bind = "-- none --"
if drum_kits[ cur_drum_kit_index ][ cur_piece_index ].keybind ~= "" then
cur_bind = drum_kits[ cur_drum_kit_index ][ cur_piece_index ].keybind
end
if UI.Button( "Assign key..." ) then
keybind_window_open = true
end
UI.SameLine()
UI.Label( "Current Keybind: " .. cur_bind )
UI.Dummy( 0, 20 )
UI.Separator()
UI.Dummy( 0, 20 )
if UI.RadioButton( "Output mode: Samples", output_mode_idx == 1 ) then
output_mode_idx = 1
output_mode = "samples"
end
if UI.RadioButton( "Output mode: MIDI", output_mode_idx == 2 ) then
output_mode_idx = 2
output_mode = "midi"
end
if UI.CheckBox( "Show colliders", show_colliders ) then show_colliders = not show_colliders end
if UI.CheckBox( "Enable haptics", enable_haptics ) then enable_haptics = not enable_haptics end
if UI.CheckBox( "Enable hit highlight", enable_hit_highlight ) then enable_hit_highlight = not enable_hit_highlight end
local released
released, sticks.pivot_offset = UI.SliderFloat( "Stick pivot", sticks.pivot_offset, 0, 0.3 )
released, sticks.length = UI.SliderFloat( "Stick length", sticks.length, 0.381, 0.4445 )
if released then
sticks.left_collider:destroy()
sticks.right_collider:destroy()
sticks.left_collider = world:newCylinderCollider( 0, 0, 0, 0.01, sticks.length )
sticks.left_collider:setTag( "stickL" )
sticks.left_collider:setKinematic( true )
sticks.right_collider = world:newCylinderCollider( 0, 0, 0, 0.01, sticks.length )
sticks.right_collider:setTag( "stickR" )
sticks.right_collider:setKinematic( true )
end
released, sticks.rotation = UI.SliderFloat( "Stick rotation", sticks.rotation, -1, 0.3 )
UI.End( pass )
-- add piece window
if add_piece_window_open then
local m = mat4( setup_window_pose )
m:translate( 0, 0, 0.01 )
UI.Begin( "add_piece_window", m, true )
UI.Label( "Select piece" )
local pcs = {}
for i, v in ipairs( available_pieces ) do
local str = available_pieces[ i ][ 1 ]
table.insert( pcs, str )
end
local _, pc_idx = UI.ListBox( "availablepieceslst", 25, 27, pcs, 1 )
if UI.Button( "OK" ) then
local new_piece = {}
new_piece.name = available_pieces[ pc_idx ][ 1 ]
new_piece.type = available_pieces[ pc_idx ][ 4 ]
new_piece.note = { 0, 0 }
new_piece.channel = 0
if new_piece.type == e_drum_kit_piece_type.hihat then
new_piece.note[ 3 ] = 0
end
new_piece.keybind = ""
local sy = available_pieces[ pc_idx ][ 3 ]
if new_piece.type == e_drum_kit_piece_type.crash_left or new_piece.type == e_drum_kit_piece_type.ride or new_piece.type == e_drum_kit_piece_type.crash_right or new_piece.type == e_drum_kit_piece_type.hihat then
sy = 0.5
end
new_piece.pose = lovr.math.newMat4( vec3( 0, 0.7, -0.6 ), vec3( available_pieces[ pc_idx ][ 2 ], sy, available_pieces[ pc_idx ][ 2 ] ), quat() )
table.insert( drum_kits[ cur_drum_kit_index ], new_piece )
SetupDrumColliders()
add_piece_window_open = false
UI.EndModalWindow()
end
UI.SameLine()
if UI.Button( "Cancel" ) then
add_piece_window_open = false
UI.EndModalWindow()
end
UI.End( pass )
end
-- keybind window
if keybind_window_open then
local m = mat4( setup_window_pose )
m:translate( 0, 0, 0.01 )
UI.Begin( "keybind_window", m, true )
UI.Label( "Press key to assign..." )
local detected = ""
if key_pressed then
detected = key_pressed
end
UI.Label( "Key:" .. detected )
if UI.Button( "OK" ) then
if key_pressed then
drum_kits[ cur_drum_kit_index ][ cur_piece_index ].keybind = key_pressed
end
keybind_window_open = false
key_pressed = nil
UI.EndModalWindow()
end
UI.SameLine()
if UI.Button( "Cancel" ) then
keybind_window_open = false
key_pressed = nil
UI.EndModalWindow()
end
UI.End( pass )
end
-- rename kit window
if rename_kit_window_open then
local m = mat4( setup_window_pose )
m:translate( 0, 0, 0.01 )
UI.Begin( "rename_kit_window", m, true )
UI.Label( "Enter a new name for this kit" )
local old_name = drum_kits[ cur_drum_kit_index ].name
local got_focus, buffer_changed, id
got_focus, buffer_changed, id, drum_kit_name = UI.TextBox( "kit name", 27, "" )
if got_focus then
UI.SetTextBoxText( id, old_name )
end
if UI.Button( "OK" ) then
if drum_kit_name ~= "" then
drum_kits[ cur_drum_kit_index ].name = drum_kit_name
end
rename_kit_window_open = false
UI.EndModalWindow()
end
UI.SameLine()
if UI.Button( "Cancel" ) then
rename_kit_window_open = false
UI.EndModalWindow()
end
UI.End( pass )
else
old_name = drum_kits[ cur_drum_kit_index ].name
end
ui_passes = UI.RenderFrame( pass )
end
local function PlaySample( type, zone, variation, velocity, position )
local sample = samples_list[ type ][ zone ][ variation ]
if type == e_drum_kit_piece_type.hihat then
if zone == "pedal" then
sample = samples_list[ type ][ zone ][ 1 ]
else
if hihat_closed then
sample = samples_list[ type ][ zone ].closed[ variation ]
else
sample = samples_list[ type ][ zone ].open[ variation ]
end
end
end
local clone = sample:clone()
clone:setPosition( position )
clone:setVolume( velocity / 127 )
clone:play()
end
local function UpdateNoteOffEvents()
-- scheduled_off_notes fields: 1 = note, 2 = frame count, 3 = channel
for i, v in ipairs( scheduled_off_notes ) do
v[ 2 ] = v[ 2 ] + 1
end
for i = #scheduled_off_notes, 1, -1 do
if scheduled_off_notes[ i ][ 2 ] > 15 then
MIDI.noteOn( cur_MIDI_port, scheduled_off_notes[ i ][ 1 ], 0, scheduled_off_notes[ i ][ 3 ] )
table.remove( scheduled_off_notes, i )
end
end
end
function lovr.keypressed( key, scancode, repeating )
if keybind_window_open then
if key then
key_pressed = key
end
else
local pieces = drum_kits[ cur_drum_kit_index ]
for i, v in ipairs( pieces ) do
if key == pieces[ i ].keybind then
if output_mode == "midi" then
if drum_kits[ cur_drum_kit_index ][ i ].type ~= e_drum_kit_piece_type.hihat then
MIDI.noteOn( cur_MIDI_port, drum_kits[ cur_drum_kit_index ][ i ].note[ 1 ], 127, drum_kits[ cur_drum_kit_index ][ i ].channel )
table.insert( scheduled_off_notes, { drum_kits[ cur_drum_kit_index ][ i ].note[ 1 ], 0 } )
break
elseif not hihat_closed and drum_kits[ cur_drum_kit_index ][ i ].type == e_drum_kit_piece_type.hihat then
hihat_open_value = 0
MIDI.noteOn( cur_MIDI_port, drum_kits[ cur_drum_kit_index ][ i ].note[ 3 ], 127, drum_kits[ cur_drum_kit_index ][ i ].channel )
MIDI.sendMessage( cur_MIDI_port, 176, 4, 127 )
table.insert( scheduled_off_notes, { drum_kits[ cur_drum_kit_index ][ i ].note[ 3 ], 0 } )
break
end
else
local type = drum_kits[ cur_drum_kit_index ][ i ].type
local zone = "inner"
local variation = 4
local velocity = 127
local position = vec3( drum_kits[ cur_drum_kit_index ][ i ].pose )
if type == e_drum_kit_piece_type.hihat then
zone = "pedal"
variation = 1
hihat_open_value = 0
PlaySample( type, zone, variation, velocity, position )
else
PlaySample( type, zone, variation, velocity, position )
end
end
end
end
end
end
function lovr.keyreleased( key, scancode )
if not keybind_window_open then
local pieces = drum_kits[ cur_drum_kit_index ]
for i, v in ipairs( pieces ) do
if key == pieces[ i ].keybind then
if drum_kits[ cur_drum_kit_index ][ i ].type == e_drum_kit_piece_type.hihat then
hihat_open_value = 127
MIDI.sendMessage( cur_MIDI_port, 176, 4, 0 )
end
end
end
end
end
function App.Init()
lovr.filesystem.mount( lovr.filesystem.getExecutablePath():gsub( '[^/]+$', '/' ) )
UI.Init()
-- Setup MIDI ports
local num_ports = MIDI.getoutportcount()
for i = 0, num_ports do
if MIDI.getOutPortName( i ) == "" then
table.insert( MIDI_ports, "--no name--" )
else
table.insert( MIDI_ports, MIDI.getOutPortName( i ) )
end
end
-- Load models
mdl_stick = lovr.graphics.newModel( "res/stick.glb" )
mdl_cymbal = lovr.graphics.newModel( "res/cymbal.glb" )
mdl_drum = lovr.graphics.newModel( "res/drum.glb" )
mdl_room = lovr.graphics.newModel( "res/room.glb" )
mdl_drum_highlight = lovr.graphics.newModel( "res/drum_highlight.glb" )
mdl_cymbal_highlight = lovr.graphics.newModel( "res/cymbal_highlight.glb" )
mdl_glass = lovr.graphics.newModel( "res/glass.glb" )
mdl_sofa = lovr.graphics.newModel( "res/sofa.glb" )
mdl_window = lovr.graphics.newModel( "res/window.glb" )
mdl_plant = lovr.graphics.newModel( "res/plant.glb" )
mdl_poster = lovr.graphics.newModel( "res/poster.glb" )
mdl_light = lovr.graphics.newModel( "res/light.glb" )
mdl_bookself = lovr.graphics.newModel( "res/bookself.glb" )
mdl_table = lovr.graphics.newModel( "res/table.glb" )
mdl_misc = lovr.graphics.newModel( "res/misc.glb" )
mdl_books = lovr.graphics.newModel( "res/books.glb" )
mdl_carpet = lovr.graphics.newModel( "res/carpet.glb" )
mdl_handle = lovr.graphics.newModel( "res/handle.glb" )
LoadSamples()
LoadKits()
SetupDrumColliders()
sticks.left_collider = world:newCylinderCollider( 0, 0, 0, 0.01, sticks.length )
sticks.left_collider:setTag( "stickL" )
sticks.left_collider:setKinematic( true )
sticks.right_collider = world:newCylinderCollider( 0, 0, 0, 0.01, sticks.length )
sticks.right_collider:setTag( "stickR" )
sticks.right_collider:setKinematic( true )
world:disableCollisionBetween( "drums_inner", "drums_inner" )
world:disableCollisionBetween( "drums", "drums" )
world:disableCollisionBetween( "stickL", "stickR" )
world:enableCollisionBetween( "stickL", "drums" )
world:enableCollisionBetween( "stickL", "drums_inner" )
world:enableCollisionBetween( "stickR", "drums" )
world:enableCollisionBetween( "stickR", "drums_inner" )
end
function App.Update( dt )
lovr.audio.setPose( lovr.headset.getPose() )
-- MIDI in test
-- local a, b, c = MIDI.getMessage( 1 )
-- if a and b == 44 then
-- hihat_open_value = MapRange( 127, 0, 0, 127, c )
-- if hihat_open_value < 0 then hihat_open_value = 0 end
-- if hihat_open_value > 127 then hihat_open_value = 127 end
-- MIDI.sendMessage( cur_MIDI_port, 208, 36, c )
-- end
for i, v in ipairs( drum_kits[ cur_drum_kit_index ] ) do
if drum_kits[ cur_drum_kit_index ][ i ].type == e_drum_kit_piece_type.hihat then
hihat_keybind = drum_kits[ cur_drum_kit_index ][ i ].keybind
end
end
if hihat_keybind and hihat_keybind ~= "" and lovr.system.isKeyDown( hihat_keybind ) then
hihat_closed = true
else
hihat_closed = false
end
if lovr.headset.wasPressed( "hand/left", "y" ) then
MIDI.sendMessage( cur_MIDI_port, 193, 1, 0 )
end
if lovr.headset.wasPressed( "hand/left", "x" ) then
MIDI.sendMessage( cur_MIDI_port, 193, 90, 0 )
end
UpdateNoteOffEvents()
UpdateSticksColliders()
UpdateSticksVelocity()
world:update( dt )
if lovr.headset.isDown( "hand/right", "a" ) then
mode = e_mode.move_piece
end
if lovr.headset.wasReleased( "hand/right", "a" ) then
dragged_piece = nil
mode = e_mode.play
end
if lovr.headset.isDown( "hand/right", "b" ) then
mode = e_mode.move_kit
end
if lovr.headset.wasReleased( "hand/right", "b" ) then
dragged_piece = nil
mode = e_mode.play
end
local lx, ly, lz, langle, lax, lay, laz = sticks.left_collider:getPose()
local rx, ry, rz, rangle, rax, ray, raz = sticks.right_collider:getPose()
local outer_left = world:overlapShape( sticks.left_collider:getShape(), vec3( lx, ly, lz ), quat( langle, lax, lay, laz ), "drums" )
local inner_left = world:overlapShape( sticks.left_collider:getShape(), vec3( lx, ly, lz ), quat( langle, lax, lay, laz ), "drums_inner" )
local outer_right = world:overlapShape( sticks.right_collider:getShape(), vec3( rx, ry, rz ), quat( rangle, rax, ray, raz ), "drums" )
local inner_right = world:overlapShape( sticks.right_collider:getShape(), vec3( rx, ry, rz ), quat( rangle, rax, ray, raz ), "drums_inner" )
if mode == e_mode.move_piece then
if dragged_piece == nil then
if inner_right or outer_right and lovr.headset.wasPressed( "hand/right", "a" ) then
dragged_piece = inner_right and inner_right:getUserData() or outer_right:getUserData()
drag_offset:set( mat4( lovr.headset.getPose( "hand/right" ) ):invert() * drum_kits[ cur_drum_kit_index ][ dragged_piece ].pose )
end
end
if dragged_piece ~= nil then
drum_kits[ cur_drum_kit_index ][ dragged_piece ].pose:set( mat4( lovr.headset.getPose( "hand/right" ) ) * drag_offset )
drum_kits[ cur_drum_kit_index ][ dragged_piece ].collider:setPose( vec3( drum_kits[ cur_drum_kit_index ][ dragged_piece ].pose ),
quat( drum_kits[ cur_drum_kit_index ][ dragged_piece ].pose ):mul( quat( math.pi / 2, 1, 0, 0 ) ) )
drum_kits[ cur_drum_kit_index ][ dragged_piece ].collider_inner:setPose( vec3( drum_kits[ cur_drum_kit_index ][ dragged_piece ].pose ),
quat( drum_kits[ cur_drum_kit_index ][ dragged_piece ].pose ):mul( quat( math.pi / 2, 1, 0, 0 ) ) )
end
end
if mode == e_mode.move_kit then
if dragged_piece == nil then
if inner_right or outer_right and lovr.headset.wasPressed( "hand/right", "b" ) then
dragged_piece = inner_right and inner_right:getUserData() or outer_right:getUserData()
drag_table = {}
for i, v in ipairs( drum_kits[ cur_drum_kit_index ] ) do
local m = lovr.math.newMat4()
m:set( mat4( lovr.headset.getPose( "hand/right" ) ):invert() * drum_kits[ cur_drum_kit_index ][ i ].pose )
table.insert( drag_table, m )
end
end
end
if dragged_piece ~= nil then
for i, v in ipairs( drum_kits[ cur_drum_kit_index ] ) do
drum_kits[ cur_drum_kit_index ][ i ].pose:set( mat4( lovr.headset.getPose( "hand/right" ) ) * drag_table[ i ] )
drum_kits[ cur_drum_kit_index ][ i ].collider:setPose( vec3( drum_kits[ cur_drum_kit_index ][ i ].pose ),
quat( drum_kits[ cur_drum_kit_index ][ i ].pose ):mul( quat( math.pi / 2, 1, 0, 0 ) ) )
drum_kits[ cur_drum_kit_index ][ i ].collider_inner:setPose( vec3( drum_kits[ cur_drum_kit_index ][ i ].pose ),
quat( drum_kits[ cur_drum_kit_index ][ i ].pose ):mul( quat( math.pi / 2, 1, 0, 0 ) ) )
end
end
end
if mode == e_mode.play then
local L_col_this_frame = false
local R_col_this_frame = false
local L_inner_col_this_frame = false
local R_inner_col_this_frame = false
if outer_left then
L_col_this_frame = true
if sticks.left_colliding_drum == nil then
sticks.left_colliding_drum = outer_left:getUserData()
end
end
if inner_left then
L_inner_col_this_frame = true
if sticks.left_colliding_drum == nil then
sticks.left_colliding_drum = inner_left:getUserData()
end
end
if outer_right then
R_col_this_frame = true
if sticks.right_colliding_drum == nil then
sticks.right_colliding_drum = outer_right:getUserData()
end
end
if inner_right then
R_inner_col_this_frame = true
if sticks.right_colliding_drum == nil then
sticks.right_colliding_drum = inner_right:getUserData()
end
end
if sticks.left_colliding_drum ~= nil then
if sticks.left_colliding_drum_prev == nil or sticks.left_colliding_drum_prev ~= sticks.left_colliding_drum then
if output_mode == "midi" then
local triggered_note = drum_kits[ cur_drum_kit_index ][ sticks.left_colliding_drum ].note[ 2 ]
if L_inner_col_this_frame then triggered_note = drum_kits[ cur_drum_kit_index ][ sticks.left_colliding_drum ].note[ 1 ] end
MIDI.noteOn( cur_MIDI_port, triggered_note, sticks.left_vel, drum_kits[ cur_drum_kit_index ][ sticks.left_colliding_drum ].channel )
table.insert( scheduled_off_notes, { triggered_note, 0, drum_kits[ cur_drum_kit_index ][ sticks.left_colliding_drum ].channel } )
event_info.note = triggered_note
if sticks.left_vel > 0 then event_info.velocity = sticks.left_vel end
else
-- play sample from left stick
local type = drum_kits[ cur_drum_kit_index ][ sticks.left_colliding_drum ].type
local zone = L_inner_col_this_frame and "inner" or "outer"
local variation = sticks.left_vel > 0 and math.floor( sticks.left_vel / 32 ) + 1 or 1
local sample = samples_list[ type ][ zone ][ variation ]
local velocity = sticks.left_vel
local position = vec3( drum_kits[ cur_drum_kit_index ][ sticks.left_colliding_drum ].pose )
PlaySample( type, zone, variation, velocity, position )
end
if enable_haptics then
local strength = MapRange( 0, 127, 0, 1, sticks.left_vel )
lovr.headset.vibrate( "hand/left", strength, 0.1 )
end