-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
1886 lines (1548 loc) · 58.6 KB
/
script.js
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
let isTouchDevice = !!('ontouchstart' in window)
let svgRoot = $('svg')[0]
const xlinkns = "http://www.w3.org/1999/xlink";
const svgns = "http://www.w3.org/2000/svg";
let play = false
let bpm = 120
let loop = true
const straightSegmentLength = 5
let numberOfBeatsPerPhrase = 4
let beatDuration = 60 / bpm
let currentPattern = 0
//MUSICAL-COMPONENTS
let semitoneList = [ 'C', 'Cplus', 'D', 'Dplus', 'E', 'F', 'Fplus', 'G', 'Gplus', 'A', 'Aplus', 'B' ]
let scaleList = {
major : [ 2, 2, 1, 2, 2, 2 ],
minor : [ 2, 1, 2, 2, 1, 2 ],
dorian : [ 2, 1, 2, 2, 2, 1 ],
phrygian : [ 1, 2, 2, 2, 1, 2 ],
lydian : [ 2, 2, 2, 2, 1, 2 ],
mixolydian : [ 2, 2, 1, 2, 2, 1 ],
locrian : [ 1, 2, 2, 1, 2, 2 ],
arabian : [ 1, 3, 1, 2, 1, 3 ],
oriental : [ 1, 3, 1, 1, 3, 1 ],
enigmatic : [ 1, 3, 2, 2, 2, 1 ],
bebop : [ 2, 2, 1, 2, 2, 1, 1 ],
gypsy : [ 2, 1, 3, 1, 1, 2, 1],
pentatonic : [ 2, 2, 3, 2 ],
chromatic : [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]
}
//INTERFACE-ELEMENTS
let $_defs = $('defs')
let $_editingArea = $('#editing-area')
let $_anchorSettingsButton = $('#anchor-settings-button')
let $_anchorSettingsMenu = $('#anchor-settings-menu')
let $_anchorSheetX = $('#anchor-sheet-x-button')
let $_anchorSheetY = $('#anchor-sheet-y-button')
let $_anchorDrawingX = $('#anchor-drawing-x-button')
let $_anchorDrawingY = $('#anchor-drawing-y-button')
let $_newSheetButton = $('#new-sheet-button')
let $_changeViewCursor = $('#view-mode-cursor')
let $_changeBpmCursor = $('#change-bpm-cursor')
let $_changePartialsCursor = $('#change-partials-cursor')
let $_changeVolumeCursor = $('#change-volume-cursor')
let $_clearAllButton = $('#clear-all-button')
let $_playBar = $('#play-bar')
let $_levelsGroup = $('#levels-group')
let $_hideLevelButton = $('.hide-level-button')
let $_lockLevelButton = $('.lock-level-button')
let $_stopButton = $('#stop-button')
let $_scaleSettingsButton = $('#scale-settings-button')
let $_scaleSettingsMenu = $('#scale-settings-menu')
let $_scaleChoiceMenu = $('#scale-choice-menu')
let $_octaveCursorContent = $('#octave-cursor-content')
let $_ghostSheet = $('#ghost-sheet')
let $_soundDeleteCursor = $('#sound-delete-cursor')
//SHEET-ELEMENTS
let $_sheetFrame = $('#sheet-frame')
let $_sheetDeleteButton = $('#sheet-delete-button')
let $_sheetTransformButton = $('#sheet-transform-button')
let $_sheetDuplicateButton = $('#sheet-duplicate-button')
let $_codeEditorTransformButton = $('#code-editor-transform-button')
let $_soundFreeDrawButton = $('#sound-free-draw')
let $_soundStraightDrawButton = $('#sound-straight-draw')
let $_codeEditorButton = $('#code-editor-button')
let $_soundDeleteLineButton = $('#sound-delete-line')
let $_sheetSettingsButton = $('#sheet-settings-button')
let $_sheetSettingsMenu = $('#sheet-settings-menu')
let $_soundDrawGroup = $('#sound-draw-group')
let $_soundDrawMenu = $('#sound-draw-menu')
let $_sineButton = $('#sine-button')
let $_squareButton = $('#square-button')
let $_triangleButton = $('#triangle-button')
let $_sawtoothButton = $('#sawtooth-button')
//FUNCTIONS
uniqueEvent = function(e){
if(isTouchDevice) {
e.x = e.touches[0].pageX;
e.y = e.touches[0].pageY;
} else {
e.x = e.pageX;
e.y = e.pageY;
}
return e;
}
function findSVGCoords(e, matrix, point, offsetX, offsetY){
matrix = matrix || svgRoot.getScreenCTM();
point = point || svgRoot.createSVGPoint();
offsetY = offsetY || $(window).scrollTop();
offsetX = offsetX || $(window).scrollLeft();
point.x = e.x - offsetX;
point.y = e.y - offsetY;// - $_editingArea.attr('data-y');
point = point.matrixTransform(matrix.inverse());
return point;
}
function map(value, start1, stop1, start2, stop2) {
return start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1));
}
//DEFAULT CODE EDITOR CONST
const editingAreaWidth = 800
const editingAreaHeight = 665
const numberOfPhrases = 8
const editorHeight = editingAreaHeight
const semitoneHeight = 17.5
const seminoteConst = Math.pow(2,1/12)
//DEFAULT CODE EDITOR FUNCTIONS
let sineWave
let straightLine
let newSheet
let updateMusicData
let updateTime
//CODE EDITOR ACCESSIBLE DATA
let numberOfSheets = 0
let beatWidth = editingAreaWidth / numberOfPhrases / numberOfBeatsPerPhrase
let minFrequency = 123.47
let sheetWidth
let sheetHeight
//DOCUMENT-READY FUNCTIONS
jQuery(document).ready(function($){
let sheetList = []
function createSheet (x, y) {
numberOfSheets ++
if ( x > editingAreaWidth - 200) x = editingAreaWidth - 200
if ( y > editingAreaHeight - 120) y = editingAreaHeight - 120
let $_sheetGroup = $(document.createElementNS(svgns, 'g'))
$_sheetGroup.addClass('sheet-group current')
$_sheetGroup.attr('transform', `translate(${x},${y})`)
$_sheetGroup.attr('instrument', 'sine')
$_sheetGroup.attr('data-x', x)
$_sheetGroup.attr('data-y', y)
$_sheetGroup.attr('data-partials', 2)
$_sheetGroup.attr('data-volume', -20)
$_editingArea.append($_sheetGroup)
let sheet = document.createElementNS(svgns, 'rect')
sheet.setAttribute('class', 'sheet')
sheet.setAttribute('id', `sheet-${numberOfSheets}`)
sheet.setAttribute('width', '200px')
sheet.setAttribute('height', '120px')
$_sheetGroup.append(sheet)
let sheetGroupContent = document.createElementNS(svgns, 'g')
sheetGroupContent.setAttribute('class', 'sheet-group-content')
sheetGroupContent.setAttribute('id', `sheet-${numberOfSheets}-content`)
$_sheetGroup.append(sheetGroupContent)
let mask = document.createElementNS(svgns, 'clipPath')
mask.setAttribute('id', `sheet-${numberOfSheets}-mask`)
$_defs.append(mask)
let use = document.createElementNS(svgns, 'use')
use.setAttributeNS(xlinkns, 'href', `#sheet-${numberOfSheets}`)
mask.appendChild(use)
sheetGroupContent.setAttribute('clip-path', `url(#sheet-${numberOfSheets}-mask)`)
sheetList.push($(sheet).attr('id'))
return $_sheetGroup
}
function addLevel ($_sheetGroup) {
let id = $_sheetGroup.find('.sheet').attr('id')
let width = 128
let height = 24
$('.level').each(function(){
let $_group = $(this)
let y = parseFloat($_group.attr('data-y'))
y += height
$_group.attr('transform', `translate(0, ${y})`)
$_group.attr('data-y', y)
$_group.removeClass('current')
})
let $_level = $(document.createElementNS(svgns, 'g'))
$_level.attr('id', `level-${id}`)
$_level.attr('class', 'level')
$_level.attr('data-y', 0)
let $_rect = $(document.createElementNS(svgns, 'rect'))
$_rect.attr('x', '0')
$_rect.attr('y', '0')
$_rect.attr('width' ,width)
$_rect.attr('height' ,height)
let $_text = $(document.createElementNS(svgns, 'text'))
$_text.attr('id', `level-${id}-text`)
$_text.attr('x', 48)
$_text.attr('y', height/2+3)
let $_linesGroup = $(document.createElementNS(svgns, 'g'))
let $_linesGroupRect = $(document.createElementNS(svgns, 'rect'))
$_linesGroupRect.attr('x', width-24)
$_linesGroupRect.attr('y', 0)
$_linesGroupRect.attr('width', 24)
$_linesGroupRect.attr('height', height)
$_linesGroupRect.attr('fill-opacity', 0)
$_linesGroupRect.attr('class', 'move-level-area')
let $_line1 = $(document.createElementNS(svgns, 'line'))
$_line1.attr('x1', width-5)
$_line1.attr('y1', height/2-2.5)
$_line1.attr('x2', width-20)
$_line1.attr('y2', height/2-2.5)
$_line1.attr('stroke-width', 2)
$_line1.attr('stroke', 'white')
let $_line2 = $(document.createElementNS(svgns, 'line'))
$_line2.attr('x1', width-5)
$_line2.attr('y1', height/2+2.5)
$_line2.attr('x2', width-20)
$_line2.attr('y2', height/2+2.5)
$_line2.attr('stroke-width', 2)
$_line2.attr('stroke', 'white')
let $_hideButton = $_hideLevelButton.clone()
$_hideButton.attr('display', '')
let $_lockButton = $_lockLevelButton.clone()
$_lockButton.attr('display', '')
$_level.append($_rect)
$_level.append($_text)
$_linesGroup.append($_line1)
$_linesGroup.append($_line2)
$_linesGroup.append($_linesGroupRect)
$_level.append($_linesGroup)
$_level.append($_hideButton)
$_level.append($_lockButton)
$_level.addClass('current')
$_levelsGroup.append($_level)
document.querySelector(`#level-${id}-text`).textContent = id.replace('sheet', 's')
}
function removeLevel (sheetGroup) {
id = sheetGroup.find('.sheet').attr('id')
let levelToRemove = $(`#level-${id}`)
let levelToRemoveY = levelToRemove.attr('data-y')
levelToRemove.remove()
let index = sheetList.indexOf(id)
sheetList.splice(index, 1)
sheetList.forEach(function(element){
let id = element
let $_group = $(`#level-${id}`)
let y = parseFloat($_group.attr('data-y'))
if ( y > levelToRemoveY) {
y -= 24
$_group.attr('data-y',y)
$_group.attr('transform', `translate(0, ${y})`)
}
})
}
$(document).on('click', '.level', function(e){
let target = $(this)
$('.level').not(target).removeClass('current')
target.addClass('current')
let id = target.attr('id').replace('level-','')
$('.sheet').not($(`#${id}`)).parent().removeClass('current')
let sheetToCurrent = $(`#${id}`).parent()
sheetToCurrent.addClass('current')
showInterface(sheetToCurrent)
})
$(document).on('click', '.level .hide-level-button', function(e){
e.stopImmediatePropagation()
$(this).toggleClass('current')
let $_sheetGroup = $(`#${$(this).parent().attr('id').replace('level-', '')}`).parent()
$_sheetGroup.toggleClass('hidden')
if ($_sheetGroup.hasClass('current')){
hideInterface()
$_sheetGroup.removeClass('current on-straight-draw on-free-draw on-delete-line')
}
updateMusicData()
})
$(document).on('click', '.level .lock-level-button', function(e){
e.stopImmediatePropagation()
$(this).toggleClass('current')
let $_sheetGroup = $(`#${$(this).parent().attr('id').replace('level-', '')}`).parent()
$_sheetGroup.toggleClass('lock')
if ($_sheetGroup.hasClass('lock')){
hideInterface()
$_sheetGroup.removeClass('current on-straight-draw on-free-draw on-delete-line')
}
})
let $_level
let maxLevelY
let previousLevelY
$(document).on('click','.level .move-level-area',function(e){
e.stopImmediatePropagation()
})
$(document).on('mousedown', '.level .move-level-area', function(e){
let currentLevel = $(this).closest('.level')
$('.level').not(currentLevel).removeClass('dragging')
currentLevel.addClass('dragging')
maxLevelY = (numberOfSheets-1)*24
$(document).on('mousemove', moveLevel)
$(document).on('mouseup', endMoveLevel)
})
function moveLevel (e) {
$_level = $('.level.dragging')
let $_sheetGroup = $(`#${$_level.attr('id').replace('level-', '')}`).parent()
let mouse = findSVGCoords(uniqueEvent(e))
let y = Math.min(maxLevelY, Math.max(0, mouse.y))
y = Math.floor(y/24)*24
$_level.attr('transform', `translate(0, ${y})`)
$_level.attr('data-y', y)
$('.level').not('.dragging').each(function(){
let parsedLevelY = parseFloat($(this).attr('data-y'))
let $_parsedLevelGroup = $(`#${$(this).attr('id').replace('level-', '')}`).parent()
if ( parsedLevelY == y) {
if ( y > previousLevelY) {
//parsedLevelY = parsedLevelY > 0 ? parsedLevelY-24 : 0
parsedLevelY = parsedLevelY-24
$_parsedLevelGroup.insertAfter($_sheetGroup )
} else if (y < previousLevelY) {
//parsedLevelY = parsedLevelY < maxLevelY ? parsedLevelY+24 : parsedLevelY
parsedLevelY = parsedLevelY+24
$_parsedLevelGroup.insertBefore($_sheetGroup)
}
$(this).attr('transform', `translate(0, ${parsedLevelY})`)
$(this).attr('data-y', parsedLevelY)
}
})
previousLevelY = y
}
function endMoveLevel (event) {
$(document).off('mousemove', moveLevel)
$(document).off('mouseup', endMoveLevel)
}
function showInterface (target) {
let $_target = $(target)
let sheetWidth = parseFloat($_target.find('rect').attr('width'))
let sheetHeight = parseFloat($_target.find('rect').attr('height'))
$_target.append($_sheetFrame)
$_sheetFrame.attr('transform',`translate(-2, -24)`)
$_sheetFrame.find('#sheet-frame-bar').attr('width', `130`)
$_sheetFrame.find('#sheet-frame-bar').attr('height', `24`)
$_sheetFrame.attr('display','')
$('#sheet-frame-text').text($_target.find('.sheet').attr('id'))
$_target.append($_sheetTransformButton)
$_sheetTransformButton.attr('display','')
$_sheetTransformButton.attr('transform',`translate(${sheetWidth-6}, ${sheetHeight-6})`)
$_target.append($_soundDrawGroup)
$_soundDrawGroup.attr('display','')
$_soundDrawGroup.attr('transform',`translate(${sheetWidth}, -3)`)
$_soundDrawGroup.find('.current').removeClass('current')
let sheetInstrument = $('.sheet-group.current').attr('instrument')
let sheetPartials = $('.sheet-group.current').attr('data-partials')
let sheetVolume = Math.round(map(parseInt($('.sheet-group.current').attr('data-volume')), -50, 10, 0, 100))
$('#sheet-settings-menu').find(`#${sheetInstrument}-button`).addClass('current')
$('#change-partials-cursor').attr('transform', `translate(${map(sheetPartials, 0, 18, 6, 90)},14)`)
$('#change-volume-cursor').attr('transform', `translate(${map(sheetVolume, 0, 100, 6, 90)},14)`)
document.querySelector('#partials-value').textContent = sheetPartials
document.querySelector('#volume-value').textContent = sheetVolume
$_soundDrawMenu.attr('display','none')
}
function hideInterface () {
endMoveDeleteCursor()
$_sheetFrame.appendTo($_editingArea)
$_sheetTransformButton.appendTo($_editingArea)
$_soundDrawGroup.appendTo($_editingArea)
$_sheetFrame.attr('display','none')
$_sheetTransformButton.attr('display','none')
$_soundDrawGroup.attr('display','none')
$_soundDeleteCursor.toggle(false)
$_sheetSettingsMenu.attr('display','none')
}
let $_sheetGroup,
sheetOffsetX,
sheetOffsetY;
$_newSheetButton.on('click', function (e) {
$(this).toggleClass('current')
$('.sheet-group.current').removeClass('current')
hideInterface()
if ( !$(this).hasClass('current') ) {
setMode(defaultMode)
} else {
setMode('addSheetGroup')
$(document).on('mousemove', showGhost)
$(document).on('mouseup', endShowGhost)
}
})
let ghostX
let ghostY
function showGhost (e) {
let mouse = findSVGCoords(uniqueEvent(e))
if ( mouse.x <= editingAreaWidth && mouse.x >= 0 && mouse.y <= editingAreaHeight && mouse.y >= 0 ) {
ghostX = Math.min(editingAreaWidth-200, Math.max(0, mouse.x))
ghostY = Math.min(editingAreaHeight-120, Math.max(0, mouse.y))
ghostX = anchorX ? Math.round(ghostX/anchorXInterval)*anchorXInterval : ghostX
ghostY = anchorY ? Math.round(ghostY/anchorYInterval)*anchorYInterval : ghostY
$_ghostSheet.attr('display', '')
$_ghostSheet.attr('transform', `translate(${ghostX}, ${ghostY})`)
$_ghostSheet.attr('data-x', ghostX)
$_ghostSheet.attr('data-y', ghostY)
} else {
$_ghostSheet.attr('display', 'none')
}
}
function endShowGhost () {
$_ghostSheet.attr('display', 'none')
$(document).off('mousemove', showGhost)
$(document).off('mouseup', endShowGhost)
}
let mouseAbsPosition
let cursorXDifference
$_changeViewCursor.on('mousedown', (e) => {
let mouse = findSVGCoords(uniqueEvent(e))
mouseAbsPosition = mouse.x-parseInt($('#view-mode').attr('data-x'))
cursorXDifference = mouseAbsPosition-$_changeViewCursor.attr('data-x')
$(document).on('mousemove', changeView)
$(document).on('mouseup', endChangeView)
})
$('#view-mode-cursor-rect').on('mousedown', (e) => {
let mouse = findSVGCoords(uniqueEvent(e))
mouseAbsPosition = mouse.x-parseInt($('#view-mode').attr('data-x'))
cursorXDifference = mouseAbsPosition-$('#view-mode-cursor-rect').attr('data-x')
$(document).on('mousemove', changeView)
$(document).on('mouseup', endChangeView)
})
let effectiveCursorPosition = 0
let anchorXInterval = 100
let anchorYInterval = 17.5
function changeView (e) {
let mouse = findSVGCoords(uniqueEvent(e))
mouseAbsPosition = mouse.x-700
let $_rect = $('#view-mode-background')
let maxX = parseFloat($_rect.attr('width'))
let minX = 0
let cursorX = Math.min(maxX-maxX/5, Math.max(mouseAbsPosition-cursorXDifference, minX))
effectiveCursorPosition = (Math.floor(cursorX/20)*20)-2.5
switch (effectiveCursorPosition) {
case 17.5:
$('#background').attr('fill', 'url(#pattern-1)')
anchorXInterval = 50
break;
case 37.5:
$('#background').attr('fill', 'url(#pattern-2)')
anchorXInterval = 25
break;
case 57.5:
$('#background').attr('fill', 'url(#pattern-3)')
anchorXInterval = 12.5
break;
case 77.5:
$('#background').attr('fill', 'url(#pattern-4)')
anchorXInterval = 6.25
break;
default:
$('#background').attr('fill', 'url(#pattern-0)')
anchorXInterval = 100
break;
}
$_changeViewCursor.attr('transform',`translate(${effectiveCursorPosition}, -4)`)
$_changeViewCursor.attr('data-x', cursorX)
$('#view-mode-cursor-rect').attr('transform',`translate(${effectiveCursorPosition}, 0)`)
$('#view-mode-cursor-rect').attr('data-x', cursorX+5)
}
function endChangeView () {
$(document).off('mousemove', changeView)
$(document).off('mouseup', endChangeView)
}
function updateBpm (val) {
bpm = val
beatDuration = 60 / bpm
}
let wasPlaying = false
$_changeBpmCursor.on('mousedown', (e) => {
$(document).on('mousemove', changeBpm)
$(document).on('mouseup', endChangeBpm)
wasPlaying = play
})
function changeBpm (e) {
let mouse = findSVGCoords(uniqueEvent(e))
mouseAbsPosition = mouse.x - 404
let maxX = 82
let minX = 0
let cursorX = Math.round(Math.min(maxX, Math.max(mouseAbsPosition, minX)))
let changeBpm = Math.round(map(cursorX, 0, 82, 70, 185))
console.log('changing',$_playBar.attr('data-x'),xToTime($_playBar.attr('data-x')))
if(play) $_playPauseButton.trigger('click')
updateBpm(changeBpm)
document.querySelector(`#bpm-value`).textContent = changeBpm
$_changeBpmCursor.attr('transform',`translate(${cursorX}, 0)`)
$_changeBpmCursor.attr('data-x', cursorX)
}
function endChangeBpm () {
let barTime = xToTime($_playBar.attr('data-x'))
$_playBar.attr('data-time', barTime)
pauseTime = barTime
if(wasPlaying && !play) $_playPauseButton.trigger('click')
$(document).off('mousemove', changeBpm)
$(document).off('mouseup', endChangeBpm)
}
let changeOctaveX = parseInt($('#change-octave').attr('data-x'))
let octaveCursorContentPrevX
let octaveCursorContentMouseDelta
let mouseStartX
$_octaveCursorContent.on('mousedown', function(e) {
let mouse = findSVGCoords(uniqueEvent(e))
octaveCursorContentPrevX = parseInt($_octaveCursorContent.attr('data-x'))
octaveCursorContentMouseDelta = mouse.x - changeOctaveX
mouseStartX = mouse.x
$(document).on('mousemove', changeOctave)
$(document).on('mouseup', endChangeOctave)
})
let cursorX
function changeOctave (e) {
let mouse = findSVGCoords(uniqueEvent(e))
mouseAbsPosition = mouse.x-changeOctaveX
let mouseDelta = mouse.x - mouseStartX + octaveCursorContentPrevX
let maxX = 6
let minX = -114
cursorX = Math.round(Math.min(maxX, Math.max(mouseDelta, minX)))
cursorX = Math.round(cursorX/24)*24 + 6
$_octaveCursorContent.attr('transform',`translate(${cursorX}, 0)`)
$_octaveCursorContent.attr('data-x', cursorX)
}
function endChangeOctave () {
switch (cursorX) {
case 6:
//C1 -> B0
minFrequency = 30.87
break;
case -18:
//C2 -> B1
minFrequency = 61.74
break;
case -42:
//C3 -> B2
minFrequency = 123.47
break;
case -66:
//C4 -> B3
minFrequency = 246.94
break;
case -90:
//C5 -> B4
minFrequency = 493.88
break;
case -114:
//C6 -> B5
minFrequency = 987.77
break;
}
$(document).off('mousemove', changeOctave)
$(document).off('mouseup', endChangeOctave)
}
$_clearAllButton.on('click', function () {
hideInterface()
$('.sheet-group').remove()
$('.level').remove()
sheetList = []
numberOfSheets = 0
})
$_editingArea.on('click', function(e) {
switch(mode){
case 'addSheetGroup':
endShowGhost()
let newSheet = createSheet(ghostX,ghostY)
showInterface(newSheet)
setMode(defaultMode)
addLevel(newSheet)
$_newSheetButton.removeClass('current')
break;
}
})
$(document).on('mousedown', '.sheet-group', function(event) {
if($(this).hasClass('current')) return;
if(mode == 'addSheetGroup') return;
$('.sheet-group').removeClass('current on-free-draw on-straight-draw on-delete-line')
$_soundDeleteCursor.toggle(false)
$(this).addClass('current')
showInterface($(this))
setMode(defaultMode)
let id = $(this).find('.sheet').attr('id')
let $_level = $(`#level-${id}`)
$('.level').not($_level).removeClass('current')
$_level.addClass('current')
})
function setMode(newMode){
$(svgRoot)
.attr('class','')
.addClass(newMode)
mode = newMode
}
let anchorX = true
let anchorY = true
let currentChoiceX
let anchorSettingsMenuX = 600
$_anchorSettingsButton.on('click', function (e) {
$(this).toggleClass('current')
$_anchorSettingsMenu.toggle()
})
$('.switch.anchor-toggler').on('mousedown', function(e){
let $_currentChoice = $(this)
currentChoiceId = $_currentChoice.attr('id')
currentChoiceX = $_currentChoice.attr('data-x')
let mouse = findSVGCoords(uniqueEvent(e))
cursorXDifference = mouseAbsPosition-$(`#${currentChoiceId}`).find('circle').attr('data-x')
$(document).on('mousemove', toggleAnchor)
$(document).on('mouseup', endToggleAnchor)
})
function toggleAnchor (e) {
let mouse = findSVGCoords(uniqueEvent(e))
let $_circle = $(`#${currentChoiceId} circle`)
let $_path = $(`#${currentChoiceId} path`)
mouseAbsPosition = mouse.x-currentChoiceX-anchorSettingsMenuX
let maxX = 10
let minX = 0
let cursorX = Math.min(maxX, Math.max(mouseAbsPosition, minX))
effectiveCursorPosition = (Math.round(cursorX/10)*10)
$_circle.attr('transform', `translate(${effectiveCursorPosition}, 0)`)
if (effectiveCursorPosition ) {
$_circle.attr('fill', '#1D1D1D')
$_path.attr('fill', 'white')
} else {
$_circle.attr('fill', 'white')
$_path.attr('fill', '#1D1D1D')
}
}
function endToggleAnchor (e) {
switch (currentChoiceId) {
case 'anchor-sheet-x-switch':
anchorX = effectiveCursorPosition
break;
case 'anchor-sheet-y-switch':
anchorY = effectiveCursorPosition
break;
case 'anchor-drawing-x-switch':
anchorDrawX = effectiveCursorPosition
break;
case 'anchor-drawing-y-switch':
anchorDrawY = effectiveCursorPosition
break;
}
$(document).off('mousemove', toggleAnchor)
$(document).off('mouseup', endToggleAnchor)
}
let currentScale = 'chromatic'
let scaleStartTone = 'C'
let visibleTones = semitoneList
$_scaleSettingsButton.on('click', function(e) {
$(this).toggleClass('current')
$_scaleSettingsMenu.toggle('fast')
})
$('#chosen-scale').on('click', function(e){
$_scaleChoiceMenu.toggle('fast')
})
$('.scale-option').on('click', function(e) {
target = $(this)
$('.scale-option').not(target).removeClass('current')
target.addClass('current')
id = target.attr('id')
setScale(id)
})
function setScale(scale) {
let intervals = scaleList[scale]
currentScale = scale
let visibleToneText = scaleStartTone.includes('plus') ? scaleStartTone.replace('plus', '+') : scaleStartTone
document.querySelector(`#chosen-scale-text`).textContent = `${visibleToneText} - ${scale}`
let startIndex = semitoneList.indexOf(scaleStartTone)
let indexes = [startIndex]
intervals.forEach(function(interval){
startIndex += interval
startIndex = startIndex <= semitoneList.length-1 ? startIndex : startIndex-semitoneList.length
indexes.push(startIndex)
})
visibleTones = []
indexes.forEach( function (index){
visibleTones.push(semitoneList[index])
})
//nota di partenza
$('.scale-key').removeClass('current')
$('.scale-led').removeClass('current')
$('.horizontal-line').removeClass('current main-line')
$(`.horizontal-line[data-id="${scaleStartTone}"]`).addClass('main-line')
$(`#${scaleStartTone}-key`).addClass('current')
visibleTones.forEach( function (tone) {
//led
$(`#${tone}-led`).addClass('current')
//linee
$(`.horizontal-line[data-id="${tone}"]`).addClass('current')
})
}
$('.scale-key').on('click', function (e) {
scaleStartTone = $(this).attr('id').replace('-key', '')
setScale(currentScale)
})
$_sheetTransformButton.on('mousedown', (event) => {
event.stopImmediatePropagation()
let $_sheetGroup = $_sheetTransformButton.parent()
sheetOffsetX = parseFloat($_sheetGroup.attr('data-x'))
sheetOffsetY = parseFloat($_sheetGroup.attr('data-y'))
$(document).on('mousemove', transformSheet)
$(document).on('mouseup', endTransformSheet)
})
function transformSheet (event) {
let mouse = findSVGCoords(uniqueEvent(event))
let $_sheetGroup = $_sheetTransformButton.parent()
let buttonX = Math.min(editingAreaWidth-sheetOffsetX, Math.max(155, mouse.x-sheetOffsetX))
let buttonY = Math.min(editingAreaHeight-sheetOffsetY, Math.max(55,mouse.y-sheetOffsetY))
buttonX = anchorX ? Math.round((sheetOffsetX+buttonX)/anchorXInterval)*anchorXInterval-sheetOffsetX : buttonX
buttonY = anchorY ? Math.round((sheetOffsetY+buttonY)/anchorYInterval)*anchorYInterval-sheetOffsetY : buttonY
$_sheetTransformButton.attr('transform',`translate(${buttonX-5}, ${buttonY-5})`)
$_soundDrawGroup.attr('transform', `translate(${buttonX}, 0)`)
let $_rect = $_sheetGroup.find('.sheet')
$_rect.attr('width', buttonX)
$_rect.attr('height', buttonY)
$_editingArea.attr('cursor', 'se-resize')
$_sheetGroup.attr('cursor', 'nw-resize')
}
function endTransformSheet () {
$(document).off('mousemove', transformSheet)
$(document).off('mouseup', endTransformSheet)
$_sheetTransformButton.parent().attr('cursor', 'default')
$_editingArea.attr('cursor', 'default')
}
$(document).on('mousedown', '.current #sheet-frame', function(event) {
event.stopImmediatePropagation()
$_sheetGroup = $('.sheet-group.current')
$('.sheet-group.current').removeClass('on-free-draw on-straight-draw on-delete-line')
$_soundDrawMenu.css('display','none')
$_soundFreeDrawButton.removeClass('current')
$_soundStraightDrawButton.removeClass('current')
$_soundDeleteLineButton.removeClass('current')
$_soundDeleteCursor.toggle(false)
mode = defaultMode
let mouse = findSVGCoords(uniqueEvent(event))
sheetOffsetX = mouse.x-parseFloat($_sheetGroup.attr('data-x'))
sheetOffsetY = mouse.y-parseFloat($_sheetGroup.attr('data-y'))
$(document).on('mousemove', moveSheet)
$(document).on('mouseup', endMoveSheet)
$_editingArea.attr('cursor', 'grabbing')
})
function moveSheet (event) {
event.stopImmediatePropagation()
let mouse = findSVGCoords(uniqueEvent(event))
let sheetWidth = parseFloat($_sheetGroup.find('rect').attr('width'))
let sheetHeight = parseFloat($_sheetGroup.find('rect').attr('height'))
let sheetX = Math.min(editingAreaWidth-sheetWidth, Math.max(0, mouse.x-sheetOffsetX))
let sheetY = Math.min(editingAreaHeight-sheetHeight, Math.max(0, mouse.y-sheetOffsetY))
sheetX = anchorX ? Math.round(sheetX/anchorXInterval)*anchorXInterval : sheetX
sheetY = anchorY ? Math.round(sheetY/anchorYInterval)*anchorYInterval : sheetY
$_sheetGroup.attr('transform',`translate(${sheetX}, ${sheetY})`)
$_sheetGroup.attr('data-x', sheetX)
$_sheetGroup.attr('data-y', sheetY)
}
function endMoveSheet () {
$(document).off('mousemove', moveSheet)
$(document).off('mouseup', endMoveSheet)
$_editingArea.attr('cursor', 'auto')
}
$(document).on('click', '#sheet-delete-button', function(event){
hideInterface()
removeLevel($('.sheet-group.current'))
$('.sheet-group.current').remove()
})
let mode = 'moveSheetGroups'
let defaultMode = mode
setMode(defaultMode)
$('#sheet-instrument-settings').on('click', function (e) {
let id = $(e.target).parent().attr('id')
let $_sheetGroup = $('.sheet-group.current')
$('.sheet-group.current .button').not($(e.target).closest('.button')).removeClass('current')
switch (id) {
case 'sawtooth-button':
$_sheetGroup.attr('instrument','sawtooth')
$(e.target).closest('.button').addClass('current')
break;
case 'square-button':
$_sheetGroup.attr('instrument','square')
$(e.target).closest('.button').addClass('current')
break;
case 'triangle-button':
$_sheetGroup.attr('instrument','triangle')
$(e.target).closest('.button').addClass('current')
break;
default:
$_sheetGroup.attr('instrument','sine')
$(e.target).closest('.button').addClass('current')
break;
}
})
function verifyInstrument (sheetGroup) {
let instrument = sheetGroup.attr('instrument')
switch (instrument){
case 'square':
return 'square'
break;
case 'triangle':
return 'triangle'
break;
case 'sawtooth':
return 'sawtooth'
break;
default:
return 'sine'
break;
}
}
$_sheetSettingsButton.on('click', function (e) {
$_sheetSettingsMenu.toggle('fast')
$('.sheet-group.current').removeClass('on-delete-line on-free-draw on-straight-draw')
$_soundFreeDrawButton.removeClass('current')
$_soundDeleteLineButton.removeClass('current')
$_soundStraightDrawButton.removeClass('current')
$_soundDeleteCursor.toggle(false)
})
$_changePartialsCursor.on('mousedown', function (e) {
$(document).on('mousemove', changePartials)
$(document).on('mouseup', endChangePartials)
})
let changePartialsCount
function changePartials (e) {
let mouse = findSVGCoords(uniqueEvent(e))
mouseAbsPosition = mouse.x - parseFloat($('.sheet-group.current').attr('data-x')) - parseFloat($('.sheet-group.current .sheet').attr('width')) + 11
let maxX = 90
let minX = 6
let cursorX = Math.round(Math.min(maxX, Math.max(mouseAbsPosition, minX)))
changePartialsCount = Math.round(map(cursorX, 6, 90, 0, 18))
document.querySelector('#partials-value').textContent = changePartialsCount
$_changePartialsCursor.attr('transform',`translate(${cursorX}, 14)`)
$_changePartialsCursor.attr('data-x', cursorX)
}
function endChangePartials () {
$('.sheet-group.current').attr('data-partials', changePartialsCount)
$(document).off('mousemove', changePartials)
$(document).off('mouseup', endChangePartials)
}
$_changeVolumeCursor.on('mousedown', function (e) {
$(document).on('mousemove', changeVolume)
$(document).on('mouseup', endChangeVolume)
})
let changeVolumeVal
let newVolume
function changeVolume(e) {
let mouse = findSVGCoords(uniqueEvent(e))
mouseAbsPosition = mouse.x - parseFloat($('.sheet-group.current').attr('data-x')) - parseFloat($('.sheet-group.current .sheet').attr('width')) + 11
let maxX = 90
let minX = 6
let cursorX = Math.round(Math.min(maxX, Math.max(mouseAbsPosition, minX)))
newVolume = Math.round(map(cursorX, 6, 90, -50, 10))
let changeVolumeVal = Math.round(map(newVolume, -50, 10, 0, 100))
document.querySelector('#volume-value').textContent = changeVolumeVal
$_changeVolumeCursor.attr('transform',`translate(${cursorX}, 14)`)
$_changeVolumeCursor.attr('data-x', cursorX)
}
function endChangeVolume () {
$('.sheet-group.current').attr('data-volume', newVolume)
$(document).off('mousemove', changeVolume)
$(document).off('mouseup', endChangeVolume)
}
let $_sheetGroupDuplicate