-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.html
More file actions
1374 lines (1166 loc) · 38.9 KB
/
index.html
File metadata and controls
1374 lines (1166 loc) · 38.9 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Web chords, parse, play and loop jazzy chord progressions as you type</title>
<meta name="description" content="Free online jazz chord progression looper, player and parser. Fast easy extended triads foreign key in major minor seventh. Complex harmony." />
<meta property="og:title" content="Web Chords" />
<meta property="og:site_name" content="webchords" />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://flappix.github.io/webchords/" />
<link rel="stylesheet" href="jquery-ui.min.css" />
<script src="MIDI.js/inc/shim/Base64.js" type="text/javascript"></script>
<script src="MIDI.js/inc/shim/Base64binary.js" type="text/javascript"></script>
<script src="MIDI.js/inc/shim/WebAudioAPI.js" type="text/javascript"></script>
<!-- midi.js package -->
<script src="MIDI.js/js/midi/audioDetect.js" type="text/javascript"></script>
<script src="MIDI.js/js/midi/gm.js" type="text/javascript"></script>
<script src="MIDI.js/js/midi/loader.js" type="text/javascript"></script>
<script src="MIDI.js/js/midi/plugin.audiotag.js" type="text/javascript"></script>
<script src="MIDI.js/js/midi/plugin.webaudio.js" type="text/javascript"></script>
<script src="MIDI.js/js/midi/plugin.webmidi.js" type="text/javascript"></script>
<!-- utils -->
<script src="MIDI.js/js/util/dom_request_xhr.js" type="text/javascript"></script>
<script src="MIDI.js/js/util/dom_request_script.js" type="text/javascript"></script>
<script src="jquery-3.5.1.min.js"></script>
<script src="jquery-ui.js"></script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Boogaloo&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta2/css/all.min.css" integrity="sha512-YWzhKL2whUzgiheMoBFwW8CKV4qpHQAEuvilg9FAn5VJUDwKZZxkJNuGM4XkWuk94WCrrwslk8yWNGmY1EduTA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<style>
:root {
--chord-bg: white;
--chord-bg-play: white;
--chord-border-dark: black;
--chord-border-light: white;
--button-fg: black;
--button-bg: white;
--button-border: black;
--button-disabled: #8C8C8C;
}
* {
margin: 0;
padding: 0;
}
html, body
{
font-family: 'Boogaloo', cursive;
padding: 0px;
margin: 0xp;
border-collapse: collapse;
background-image: url(background_light.jpg);
background-attachment: fixed;
}
label
{
display: block;
font-family: Sans-Serif;
font-weight: bold;
}
#content, #loading
{
margin: 0px;
background: rgba(0, 0, 0, 0.4);
overflow: hidden;
-webkit-backdrop-filter: blur(5px);
backdrop-filter: blur(5px);
}
.menuItem
{
font-size: 3em;
margin: 0px;
background: rgba(0, 0, 0, 0.2);
padding: 20px;
-webkit-backdrop-filter: blur(5px);
backdrop-filter: blur(5px);
}
.example, .loaditem
{
padding: 10px;
}
.example:hover, .loaditem:hover
{
background: rgba(0,0,0, 0.2);
cursor: pointer;
padding: 10px;
}
#chords, #chords:focus
{
font-size: 3em;
margin: 10px auto;
padding: 5px;
width: 90%;
border: none;
outline: none;
text-align: center;
background: rgba(255, 255, 255, 0.7);
}
#parsed
{
margin: 0px auto;
padding-top: 20px;
display: flex;
flex-direction: column;
font-size: 5em;
width: 70%;
padding: 20px;
}
.bar
{
min-width: 500px;
white-space: nowrap;
margin: 10px;
display: flex;
box-sizing: border-box;
}
.chord
{
margin: 10px;
display: inline-block;
background-color: var(--chord-bg);
border-radius: 10px;
padding: 0px;
height: 100px;
box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 12px;
text-align: center;
}
.play
{
background-color: var(--chord-bg-play);
box-shadow: rgba(0, 0, 0, 0.3) 0px 4px 12px;
}
input[type=number] {
padding: 0.5em;
font-weight: bold;
border: 4px solid var(--button-border);
}
input[type=range] {
-webkit-appearance: none;
-moz-appearance: none;
min-width: 400px;
height: 25px;
background: #d3d3d3;
outline: none;
opacity: 0.7;
-webkit-transition: .2s;
transition: opacity .2s;
}
input[type=range]:hover {
opacity: 1;
}
input[type=range]::-webkit-slider-thumb {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
width: 25px;
height: 25px;
background: var(--button-bg);
cursor: pointer;
}
input[type=slider]::-moz-range-thumb {
width: 25px;
height: 25px;
background: #04AA6D;
cursor: pointer;
}
button, select, option {
color: var(--button-fg);
background-color: var(--button-bg);
border: 4px solid var(--button-border);
padding: 8px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
font-weight: bold;
}
button[disabled] {
background-color: var(--button-disabled);
cursor: wait;
}
select, option
{
padding: 5px !important;
margin: 5px !important;
}
.range_value
{
display: inline-block;
width: 2em;
padding: 5px;
font-size: 2em;
color: white;
}
sup {
vertical-align: top;
}
h1
{
font-size: 2.5em;
color: white;
text-align: center;
margin: 0px auto;
}
#header
{
background-color: rgba(0,0,0,0.7);
display: flex;
padding: 0px;
}
.config
{
font-size: 2em; font-weight: bold;
color: white;
}
.loading {
background: url('loading.gif');
border: none;
background-repeat: no-repeat;
object-fit: cover;
}
select
{
}
.loaditem
{
text-overflow: ellipsis;
overflow: hidden;
}
.transpose
{
padding: 5px;
}
.transposeDisplay
{
color: black;
width: 50px;
font-size: 2em;
}
.ui-resizable-s {
height: 20px !important;
}
.optionGroup
{
background-color: rgba(0,0,0,0.4);
border-radius: 10px;
padding: 5px;
margin: 5px;
}
</style>
</head>
<body>
<script type="text/javascript">
let map = {
'c': 0,
'c#': 1, 'db': 1,
'd': 2,
'd#': 3, 'eb': 3,
'e': 4,
'e#': 5, 'f': 5,
'f#': 6, 'gb': 6,
'g': 7,
'g#': 8, 'ab': 8,
'a': 9,
'a#': 10, 'bb': 10,
'b': 11, 'cb': 11
};
let chord_patterns = [
// pattern with forceParenthese = true have to come first
{name: 'flat five', postfix: '<sup>♭5</sup>', pattern: /^[a-g][#b]?\(b5\)(\/[a-g][#b]?)?$/i, notes: [0, 4, 6], forceParenthese: true},
{name: 'power chord', postfix: '5', pattern: /^[a-g][#b]?5(\/[a-g][#b]?)?$/i, notes: [0, 7]},
{name: 'major', postfix: '', pattern: /^[a-g][#b]?(maj)?(\/[a-g][#b]?)?$/i, notes: [0, 4, 7]},
{name: 'major6', postfix: '<sup>6</sup>', pattern: /^[a-g][#b]?6(\/[a-g][#b]?)?$/i, notes: [0, 4, 7, 9]},
{name: 'major6 flat 5', postfix: '<sup>6♭5</sup>', pattern: /^[a-g][#b]?6b5(\/[a-g][#b]?)?$/i, notes: [0, 4, 6, 9]},
{name: 'major7', postfix: '<sup>7</sup>', pattern: /^[a-g][#b]?7(\/[a-g][#b]?)?$/i, notes: [0, 4, 7, 10]},
{name: 'major7 flat 5', postfix: '<sup>7♭5</sup>', pattern: /^[a-g][#b]?7b5(\/[a-g][#b]?)?$/i, notes: [0, 4, 6, 10]},
{name: 'major9', postfix: '<sup>9</sup>', pattern: /^[a-g][#b]?9(\/[a-g][#b]?)?$/i, notes: [0, 4, 7, 10, 14]},
{name: 'major9 flat 5', postfix: '<sup>9♭5</sup>', pattern: /^[a-g][#b]?9b5(\/[a-g][#b]?)?$/i, notes: [0, 4, 7, 10, 14]},
{name: 'major13', postfix: '<sup>13</sup>', pattern: /^[a-g][#b]?13(\/[a-g][#b]?)?$/i, notes: [0, 4, 7, 10, 22]},
{name: 'major13 flat five', postfix: '<sup>13b5</sup>', pattern: /^[a-g][#b]?13b5(\/[a-g][#b]?)?$/i, notes: [0, 4, 6, 10, 22]},
{name: 'minor', postfix: 'm', pattern: /^[a-g][#b]?(m|min)(\/[a-g][#b]?)?$/i, notes: [0, 3, 7]},
{name: 'minor flat 5', postfix: 'm<sup>b5</sup>', pattern: /^[a-g][#b]?(m|min)b5(\/[a-g][#b]?)?$/i, notes: [0, 3, 6]},
{name: 'minor7', postfix: 'm<sup>7</sup>', pattern: /^[a-g][#b]?(m|min)7(\/[a-g][#b]?)?$/i, notes: [0, 3, 7, 10]},
{name: 'minor7 flat five', postfix: 'm<sup>7b5</sup>', pattern: /^[a-g][#b]?(m|min)7b5(\/[a-g][#b]?)?$/i, notes: [0, 3, 6, 10]},
{name: 'minor6', postfix: 'm<sup>6</sup>', pattern: /^[a-g][#b]?(m|min)6(\/[a-g][#b]?)?$/i, notes: [0, 3, 7, 9]},
{name: 'maj7', postfix: 'maj7', pattern: /^[a-g][#b]?maj7(\/[a-g][#b]?)?$/i, notes: [0, 4, 7, 11]},
{name: 'maj7 flat five', postfix: 'maj7<sup>b5</sup>', pattern: /^[a-g][#b]?maj7b5(\/[a-g][#b]?)?$/i, notes: [0, 4, 6, 11]},
{name: 'maj9', postfix: 'maj9', pattern: /^[a-g][#b]?maj9(\/[a-g][#b]?)?$/i, notes: [0, 4, 7, 11, 14]},
{name: 'maj9 flat five', postfix: 'maj9<sup>b5</sup>', pattern: /^[a-g][#b]?maj9b5(\/[a-g][#b]?)?$/i, notes: [0, 4, 6, 11, 14]},
{name: 'maj13', postfix: 'maj13', pattern: /^[a-g][#b]?maj13(\/[a-g][#b]?)?$/i, notes: [0, 4, 7, 11, 22]},
{name: 'maj13 flat five', postfix: 'maj13<sup>b5</sup>', pattern: /^[a-g][#b]?maj13b5(\/[a-g][#b]?)?$/i, notes: [0, 4, 6, 11, 22]},
{name: 'dim', postfix: 'dim', pattern: /^[a-g][#b]?dim(\/[a-g][#b]?)?$/i, notes: [0, 3, 6]},
{name: 'dim7', postfix: 'dim<sup>7</sup>', pattern: /^[a-g][#b]?dim7(\/[a-g][#b]?)?$/i, notes: [0, 3, 6, 9]},
{name: 'minor add 9', postfix: 'm<sup>add9</sup>', pattern: /^[a-g][#b]?(m|min)add9(\/[a-g][#b]?)?$/i, notes: [0, 3, 7, 14]},
{name: 'minor add 9 flat five', postfix: 'm<sup>add9b5</sup>', pattern: /^[a-g][#b]?(m|min)add9b5(\/[a-g][#b]?)?$/i, notes: [0, 3, 6, 14]},
{name: 'minor9', postfix: 'm<sup>9</sup>', pattern: /^[a-g][#b]?(m|min)9(\/[a-g][#b]?)?$/i, notes: [0, 3, 7, 10, 14]},
{name: 'minor9 flat five', postfix: 'm<sup>9b5</sup>', pattern: /^[a-g][#b]?(m|min)9b5(\/[a-g][#b]?)?$/i, notes: [0, 3, 6, 10, 14]},
{name: 'major add 9', postfix: '<sup>add9</sup>', pattern: /^[a-g][#b]?add9(\/[a-g][#b]?)?$/i, notes: [0, 4, 7, 14]},
{name: 'major add 9 flat five', postfix: '<sup>add9♭5</sup>', pattern: /^[a-g][#b]?add9b5(\/[a-g][#b]?)?$/i, notes: [0, 4, 6, 14]},
{name: 'augumented', postfix: 'aug', pattern: /^[a-g][#b]?(aug|\+)(\/[a-g][#b]?)?$/i, notes: [0, 4, 8]},
{name: 'augumented 7', postfix: 'aug7', pattern: /^[a-g][#b]?(aug|\+)7(\/[a-g][#b]?)?$/i, notes: [0, 4, 8, 10]},
{name: 'augumented maj7', postfix: 'aug<sup>(maj7)</sup>', pattern: /^[a-g][#b]?(aug|\+)maj7(\/[a-g][#b]?)?$/i, notes: [0, 4, 8, 11]},
{name: 'sus2', postfix: 'sus2', pattern: /^[a-g][#b]?sus2(\/[a-g][#b]?)?$/i, notes: [0, 2, 7]},
{name: 'sus4', postfix: 'sus4', pattern: /^[a-g][#b]?sus4(\/[a-g][#b]?)?$/i, notes: [0, 5, 7]},
{name: 'sus2flat5', postfix: 'sus2', pattern: /^[a-g][#b]?sus2b5(\/[a-g][#b]?)?$/i, notes: [0, 2, 6]},
{name: 'sus4flat5', postfix: 'sus4', pattern: /^[a-g][#b]?sus4b5(\/[a-g][#b]?)?$/i, notes: [0, 5, 6]},
{name: 'seven flat five', postfix: '<sup>7♭5</sup>', pattern: /^[a-g][#b]?7b5(\/[a-g][#b]?)?$/i, notes: [0, 4, 6, 10]},
// template chords
{name: 'add n', postfixStart: '<sup>add', postfixEnd: '</sup>', pattern: /^[a-g][#b]?add([b#]?[1-9]{1,2})(\/[a-g][#b]?)?$/i, notes: [0, 4, 7, 'n'], template: true},
{name: 'minor add n', postfixStart: 'm<sup>add', postfixEnd: '</sup>', pattern: /^[a-g][#b]?(m|min)add([b#]?[1-9]{1,2})(\/[a-g][#b]?)?$/i, notes: [0, 4, 7, 'n'], template: true},
];
let scale_map = {
'1': 0,
'b2': 1,
'2': 2,
'#2': 3,
'b3': 3,
'3': 4,
'#3': 5,
'b4': 4,
'4': 5,
'#4': 6,
'b5': 6,
'5': 7,
'#5': 8,
'b6': 8,
'6': 9,
'#6': 10,
'b7': 9,
'7': 10,
'#7': 11,
'8': 12,
'b9': 13,
'9': 14,
'#9': 15,
'b10': 15,
'10': 16,
'#10': 17,
'b11': 16,
'11': 17,
'#11': 18,
'b12': 18,
'12': 19,
'#12': 20,
'b13': 21,
}
let loaded = false;
let play = false;
let play_metronome = true;
let sequence;
let velocity= 127;
let channel = {'chord': 0, 'drum': 1};
let loop_label_play = '<i class="fa fa-play"></i> Loop';
let loop_label_stop = '<i class="fa fa-stop"></i> Stop';
let drum_sound = 42;
let volume = {'chord': 127, 'drum': 10};
let last_chord;
let humanize = 10;
let staccato_legato = 0.25;
let start_at_bar = 0;
let start_at_chord = 0;
function bar_length()
{
return ( 60 / Number ( $('#bpm').val() ) ) * Number ( $('#beats_per_bar').val() ) ;
}
function linearTrans (x1, x2, a, b, c)
{
return x1 + ((x2 - x1)/(b - a)) * (c - a);
}
function loopSequence (seq)
{
let min_note = Infinity;
let max_note = -Infinity;
function playBeat() {
MIDI.setVolume (channel['drum'], volume['drum']);
MIDI.noteOn (channel['drum'], drum_sound, 127, 0);
let beats = Number ( $('#beats_per_bar').val() );
for (let b=1; b<beats; b++)
{
MIDI.noteOn (channel['drum'], drum_sound, 127, (bar_length() / beats) * b );
}
}
function playChord (bar_c, chord_c, beat=false)
{
if (play == true)
{
if (seq[bar_c][chord_c].notes != null)
{
let inversion = seq[bar_c][chord_c].notes;
if (last_chord)
{
if ( $('#voice_leading').is (':checked') )
{
inversion = voiceLeading (last_chord, inversion);
}
}
/*if (last_chord)
{
//MIDI.chordOff (channel['chord'], last_chord, 0);
for (let [n, d] of Object.entries (last_chord) )
{
if ( !inversion.includes (n))
{
MIDI.noteOff (channel['chord'], n, 0);
}
}
}*/
MIDI.setVolume (channel['chord'], volume['chord']);
played = [];
for (let n of inversion)
{
if ( last_chord == null || !Object.keys (last_chord).includes (n)/* || Date.now() - Object.values ( Object.entries (last_chord).find ( (ln, ld) => ln == n ) )[0] > max_note_length */ )
{
min_note = Math.min (min_note, n);
max_note = Math.max (max_note, n);
let vel_off = linearTrans (100, 127, 0, 127, humanize);
let vel = (127 - vel_off) + Math.floor ( Math.random() * vel_off );
let off = Math.random() * (humanize / 100);
MIDI.noteOn (channel['chord'], n, vel, off);
played.push ({n: Date.now()});
let timeout = seq[bar_c][chord_c].duration * 1000;
setTimeout (()=> {
MIDI.noteOff (channel['chord'], n, 0);
}, timeout - (timeout * staccato_legato) );
}
$([document.documentElement, document.body]).animate({
scrollTop: $(`#chord_${bar_c}_${chord_c}`).offset().top - ( $('#content').height() + 100 )
}, 100);
}
//MIDI.chordOn (channel['chord'], seq[bar_c][chord_c].notes, velocity, 0);
last_chord = played;
}
if (beat && play_metronome)
{
/*MIDI.setVolume (channel['drum'], volume['drum']);
MIDI.noteOn (channel['drum'], drum_sound, 127, 0);
let beats = Number ( $('#beats_per_bar').val() );
for (let b=1; b<beats; b++)
{
MIDI.noteOn (channel['drum'], drum_sound, 127, (bar_length() / beats) * b );
}*/
playBeat();
}
$(`#chord_${bar_c}_${chord_c}`).addClass ('play');
let next_call;
if (bar_c < seq.length && chord_c < seq[bar_c].length - 1)
{
next_call = function() { playChord (bar_c, chord_c + 1); }
}
else if (bar_c < seq.length - 1)
{
next_call = function () { playChord (bar_c + 1, 0, true); }
}
else
{
next_call = function () { playChord (0, 0, true); }
}
setTimeout ( function()
{
$(`#chord_${bar_c}_${chord_c}`).removeClass ('play');
next_call();
}, seq[bar_c][chord_c].duration * 1000 );
}
else if (play == 'stop')
{
play = false; // confirm stopping
}
}
let start_bar = start_at_bar;
let start_chord = start_at_chord;
let count_in = $('#count_in').is (':checked');
let pre_delay = count_in ? bar_length() * 1000 : 0;
if (count_in)
{
/*MIDI.setVolume (channel['drum'], volume['drum']);
MIDI.noteOn (channel['drum'], drum_sound, 127, 0);
let beats = Number ( $('#beats_per_bar').val() );
for (let b=1; b<beats; b++)
{
MIDI.noteOn (channel['drum'], drum_sound, 127, (bar_length() / beats) * b );
}*/
playBeat();
}
setTimeout ( () => {
playChord (start_at_bar, start_at_chord, seq[start_at_bar][start_at_chord].duration);
}, pre_delay);
}
function formatChord (chord)
{
return chord[0].toUpperCase() + ( chord.length > 1 ? ( chord[1] == '#' ? '#' : (chord[1] == 'b' ? '♭' : '') ) : '' )
}
function parseInput()
{
//if (loaded)
//{
let query = $('#chords').val().replace(/\s\s+/g, ' ').replace (/\s?[\|,;]\s?/g, '|').replace (/\([\|,;]\)/g, '').trim();
let repeat = true;
while (repeat) {
repeat = false;
query = query.replace (/:(.*?):([0-9])?/g, function (a,b,c) {
let n = Number (c ?? 1);
let arr = Array (n + 1).fill (b);
repeat = true;
return arr.join (',');
});
}
let query_bars = query.split (/[|,;]+/);
let sequence = [];
for (let b of query_bars)
{
let query_chords = b.split (' ');
let bar = [];
for (let c of query_chords)
{
if (c != '')
{
let octave = 4;
let notes = [];
let str = '';
if (c == '%') // repeat last chord
{
for (offset = 1; offset <= bar.length && bar[bar.length - offset] == null; offset++) {}
if (bar[bar.length - offset] != null)
{
bar[bar.length - offset].duration *= 2;
}
bar.push ({
'duration': 0,
'symbol': ''
});
}
else if (c == '_') // pause
{
bar.push ({
'duration': bar_length() / query_chords.length,
'symbol': ''
});
}
else
{
found = false;
for (let p of chord_patterns)
{
let m;
if ( Array.isArray (p.pattern) )
{
for (let pp of p.pattern)
{
m = ( pp.forceParenthese ? c : c.replace (/\(|\)/g, '') ).toLowerCase().match (pp);
if (m) break;
}
}
else
{
m = ( p.forceParenthese ? c : c.replace (/\(|\)/g, '') ).toLowerCase().match (p.pattern);
}
if (m)
{
let root = map[c[0].toLowerCase()] + octave*12;
if (c.length >= 2)
{
if (c[1] == '#') root++;
if (c[1] == 'b') root--;
}
let chord;
if (p.template == true)
{
let group = m.length - 1;
if (m[group] == null || m[group][0] == '/') group = m.length - 2;
chord = {
'notes': p.notes.map ( (x) => { return x == 'n' ? scale_map[m[group]] + root : x + root } ),
'duration': bar_length() / query_chords.length,
'symbol': c[0].toUpperCase() + ( c.length > 1 ? ( c[1] == '#' ? '#' : (c[1] == 'b' ? '♭' : '') ) : '' ) + p.postfixStart + m[group].replace (/b/g, '♭') + p.postfixEnd
}
}
else
{
chord = {
'notes': p.notes.map ( (x) => { return x + root } ),
'duration': bar_length() / query_chords.length,
'symbol': c[0].toUpperCase() + ( c.length > 1 ? ( c[1] == '#' ? '#' : (c[1] == 'b' ? '♭' : '') ) : '' ) + p.postfix
}
}
// add bass note
// slash chords
let slash_group = m.length - 1;
if (m.length >= 2 && m[slash_group] != null && m[slash_group][0] == '/')
{
chord.notes.unshift ( map[m[slash_group].slice(1)] + (octave -1 ) * 12 );
if (m[slash_group].length > 2 && m[slash_group][m[slash_group].length - 1] == 'b')
{
chord.symbol += m[slash_group].toUpperCase().substring (0, m[slash_group].length - 1) + '♭';
}
else
{
chord.symbol += m[slash_group].toUpperCase();
}
}
else // normal chord (no slash)
{
chord.notes.unshift (root - 12);
}
bar.push (chord);
found = true;
break;
}
}
if (!found)
{
// chord not recognized
bar.push ({
'notes': [],
'duration': 0,
'symbol': `<span style="color: red;">${c}</span>`
});
}
}
}
}
if (bar.length > 0)
{
sequence.push (bar);
}
}
if ( $('#chords').get(0).selectionStart >= $('#chords').val().length )
{
$([document.documentElement, document.body]).animate({
scrollTop: $(document).height()
}, 100);
}
return sequence;
//}
}
function changeVolume (c, v)
{
volume[c] = v;
MIDI.setVolume (channel['chord'], volume[c]);
}
function changeBPM()
{
let restart = play;
if (play == true)
{
$('#loop').attr ('disabled', 'disabled');
$('#loop').html ('... loading ...');
stop();
}
for (let bar of sequence)
{
for (let chord of bar)
{
chord.duration = bar_length() / bar.length;
}
}
function startMetronome()
{
if (play == false && play_metronome == false)
{
$('#loop').removeAttr ('disabled');
$('#loop').html (loop_label_stop);
start();
}
else
{
setTimeout (startMetronome, 500);
}
}
if (restart == true)
{
startMetronome();
}
}
function changeHumanize()
{
humanize = $('#humanize').val();
}
function loadLocalStorage()
{
let docs = localStorage.getItem ('docs') ? localStorage.getItem ('docs') : '[]';
try {
// new json format
docs = JSON.parse (docs);
} catch (e) {
docs = ( docs.split ('+++').filter ( d => d != '' ) || []).map ( (p) => ({'beats': 4, progression: p}) ); // stay compatible with old string format
};
return docs;
}
function save()
{
let docs = loadLocalStorage();
if ( docs.filter ( d => d.progression.trim() == $('#chords').val().trim() ).length == 0 )
{
docs.push ({progression: $('#chords').val().trim(), beats: $('#beats_per_bar').val()});
}
localStorage.setItem ('docs', JSON.stringify (docs) );
updateloadlist();
$('#save').animate({
opacity: '0.5'
},
{
complete: function ()
{
$('#save').animate({
opacity: '1'
});
}
});
}
function updateloadlist()
{
let docs = loadLocalStorage();
$('#load').empty();
let s = '';
for (let d of docs)
{
if (d && d.progression && d.progression.trim() != '')
{
s += `<div class="loaditem"><span onclick="loadProgression ('${d.progression}', ${parseInt (d.beats)})">${d.progression}</span> <i class="fa fa-trash" onclick="remove ('${d.progression}')"></i></div>`;
}
}
$('#load').html (s);
if ( s.trim() != '' )
{
$('#showLoad').removeAttr ('disabled');
}
else
{
$('#showLoad').attr ('disabled', 'disabled');
}
}
function remove (str)
{
let docs = loadLocalStorage().filter ( d => d.progression != 'str' );
localStorage.setItem ('docs', JSON.stringify (docs) );
if (docs != null )
{
docs.progression = docs.replace (str + '+++', '');
updateloadlist();
}
}
let curr_transpose = 0;
function transpose (offset)
{
let scale = ['A', ['A#', 'B♭'], 'B', 'C', ['C#', 'D♭'], 'D', ['D#', 'E♭'], 'E', 'F', ['F#', 'G♭'], 'G', ['G#', 'A♭']];
let new_roots = [];
for (let bar of sequence)
{
for (let chord of bar)
{
for (let n in chord.notes)
{
chord.notes[n] += offset;
}
let root = chord.symbol.substring (0, chord.symbol[1] == '#' || chord.symbol[1] == '♭' ? 2 : 1);
for ( const [i, s] of scale.entries() )
{
if ( ( !Array.isArray (s) && s == root ) || ( Array.isArray (s) && s.includes (root) ) )
{
new_roots.push (scale[(i + offset) % scale.length]);
}
}
}
}
let sharps = 0;
let flats = 0;
for (const [i, r] of new_roots.entries())
{
if ( Array.isArray (r) )
{
if ( new_roots.includes (r[0][0]) )
{
//new_roots[i] = r[1];
flats++;
}
else if ( new_roots.includes (r[1][0]) )
{
//new_roots[i] = r[0];
sharps++;
}
}
}
for (const [i, r] of new_roots.entries())
{
if ( Array.isArray (r) )
{
new_roots[i] = r[flats > sharps ? 1 : 0];
}
}
curr_transpose += offset;
$('#transposeUpDisplay').html ('');
$('#transposeDownDisplay').html ('');
if (curr_transpose != 0)
{
$('#transpose' + (curr_transpose > 0 ? 'Up' : 'Down') + 'Display').html ( (curr_transpose > 0 ? '+' : '') + curr_transpose);
}
}
function render()
{
$('#parsed').empty();
for ( const [b, bar] of sequence.entries() )
{
let bar_div = $('<div class="bar"></div>');
for ( const [c, chord] of bar.entries() )
{
if (chord != null)
{
let chord_element = $(`<div class="chord" id="chord_${b}_${c}" style="width: ${ (chord.duration / bar_length() ) * 1500 - 30}px;">${chord != null ? chord.symbol : '%'}</div>`);
chord_element.click ( function()
{
stop();
$('.chord').removeClass ('play');
$(this).addClass ('play');
start_at_bar = b;
start_at_chord = c;
});
bar_div.append (chord_element);
}
}
$('#parsed').append (bar_div);
}
}
function loopUI()
{
$('#loop').html ( $('#loop').html() == loop_label_play ? loop_label_stop : loop_label_play);
if (!play)
{
start();
$('#parsed').show();
$('#inputSection').show();
$('.menuItem').hide();
}
else
{
stop();
play_metronome = false;
//stopAllNotes();
}
}
function stopAllNotes()
{
function waitForConfirm()
{
$('#loop').attr ('disabled', 'disabled');
MIDI.setVolume (0, 10);
// wait for confirmation
if (play == false)
{
//$('#loop').removeAttr ('disabled');
//MIDI.setVolume (0, 127);
}
else
{
setTimeout (waitForConfirm, 500);
}
}