-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
798 lines (657 loc) · 18.7 KB
/
index.html
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
<html>
<head>
<title>Musical Dice Game</title>
<script src="https://plugin.humdrum.org/scripts/humdrum-notation-plugin-worker.js"></script>
<link href="https://fonts.googleapis.com/css2?family=UnifrakturMaguntia&display=swap" rel="stylesheet">
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-175309821-1"></script>
</head>
<body>
<h1> Musikalisches Würfelspiel </h1>
<p>
Below is a musical dice game attributed to Mozart
(K<sup>6</sup> 516f). Click on measure numbers in the following
grid to construct a score. To play the game, roll two dice at a
time to select a specific row in each column of the grid based on
the sum of the two dice. Do this sixteen times to generate a
randomized score. If you are lazy, type the letter "<span
class="command">r</span>" to generate a realization, or "<span
class="command">?</span>" to list other commands.
</p>
<div id="command-list">
<ul>
<li> <span class="command">?</span> — Toggle display of this list. </li>
<li> <span class="command">r</span> — Generate a random composition.</li>
<li> <span class="command">c</span> — Clear a random composition. </li>
<li> <span class="command">h</span> — Toggle display of Humdrum digital score. </li>
<li> <span class="command">v</span> — Toggle display of VHV link. </li>
<li> <span class="command">+</span> — Make notation larger. </li>
<li> <span class="command">-</span> — Make notation smaller. </li>
<li> <span class="command">shift-↑</span> — Select next smaller dice roll in last clicked measure.</li>
<li> <span class="command">shift-↓</span> — Select next larger dice roll in last clicked measure.</li>
<li> <span class="command">shift-←</span> — Go to measure before last clicked measure.</li>
<li> <span class="command">shift-→</span> — Go to measure after last clicked measure.</li>
</ul>
<div id="source">
Scans of two early editions of this game are available on <a target="imslp" href="https://imslp.org/wiki/Musikalisches_W%C3%BCrfelspiel,_K.516f_(Mozart,_Wolfgang_Amadeus)">IMSLP</a>. <a target="humdrum" href="https://raw.githubusercontent.com/craigsapp/Musikalisches-Wuerfelspiel/main/mozart-k516f.krn">Humdrum digital score</a> used on this webpage.
</div>
</div>
<div id="measure-grid"></div>
<div class="link">
<!--
<a id="vhv-link" target="_blank" href="https://verovio.humdrum.org/?file=dice/mozart/k516f.krn&k=e">
-->
<a id="vhv-link" target="_blank" href="https://raw.githubusercontent.com/craigsapp/Musikalisches-Wuerfelspiel/main/mozart-kanhc30-01.krn&k=e">
Listen to or print the score in VHV</a>
<br>
(click on play button in upper right corner,<br>or menu: <i>File→Save PDF</i>).
</div>
<script>
//////////////////////////////
//
// displayHumdrumFilteredData -- Copy filtered humdrum data output from
// verovio and display at bottom of page.
//
function displayHumdrumFilteredData(humdrumData, hid, hnpdatabase) {
var target = document.querySelector("pre#humdrum-score");
if (!target) {
console.log("NO TARGET ELEMENT FOR DISPLAYING HUMDRUM SCORE");
return;
}
var lines = humdrumData.split(/\r?\n/);
if (lines.length > 1000) {
// When the entire input score is displayed, hide the
// output Humdrum score (since it is not the output score,
// but rather the input score.
target.style.display = "none";
return;
}
var entry = hnpdatabase.entries[hid];
if (!entry) {
console.log("NO ENTRY IN", hnpdatabase);
return;
}
var output = "";
for (var i=0; i<lines.length; i++) {
if (lines[i].match(/^!!/)) {
continue;
}
if (lines[i].match(/^\s*$/)) {
continue;
}
output += lines[i] + "\n";
}
target.innerHTML = output;
if (HumdrumDisplay) {
target.style.display = "block";
}
}
</script>
<script>
var HnpOptions = {
// uri: "humdrum://dice/mozart/k516f.krn",
url: "https://raw.githubusercontent.com/craigsapp/Musikalisches-Wuerfelspiel/main/mozart-kanhc30-01.krn",
source: "dice",
autoResize: true,
scale: 50,
pageMarginTop: 150,
spacingSystem: 20,
postFunctionHumdrum: displayHumdrumFilteredData
}
displayHumdrum(HnpOptions);
</script>
<script type="text/x-humdrum" id="dice"></script>
<pre id="humdrum-score"></pre>
<script>
///////////////////////////////////////////////////////////////////////////
//
// Javascript code to handle interaction with the dice grid of measures
//
//////////////////////////////
//
// DOMContentLoaded event listener -- What to do when the page has
// finished loading.
//
document.addEventListener("DOMContentLoaded", function() {
prepareMeasureGrid(MeasureGrid);
});
//////////////////////////////
//
// keydown event listener -- for processing keyboard shortcuts.
//
document.addEventListener("keydown", function(event) {
console.log("EVENT", event);
if (event.metaKey) {
return;
}
if (event.key === "?") {
toggleHelpListDisplay();
event.preventDefault();
} else if (event.key === "c") {
clearGrid();
event.preventDefault();
} else if (event.key === "r") {
randomizeGrid();
event.preventDefault();
} else if (event.key === "v") {
toggleVhvLinkDisplay();
event.preventDefault();
} else if (event.key === "h") {
toggleHumdrumDisplay();
event.preventDefault();
} else if (event.key === "+" || event.key === "=") {
makeMusicNotationLarger();
event.preventDefault();
} else if (event.key === "-" || event.key === "_") {
makeMusicNotationSmaller();
event.preventDefault();
} else if (event.shiftKey && (event.key === "ArrowDown")) {
selectNextRollInMeasure();
event.preventDefault();
} else if (event.shiftKey && (event.key === "ArrowUp")) {
selectPreviousRollInMeasure();
event.preventDefault();
} else if (event.shiftKey && (event.key === "ArrowRight")) {
goToNextMeasure();
event.preventDefault();
} else if (event.shiftKey && (event.key === "ArrowLeft")) {
goToPreviousMeasure();
event.preventDefault();
}
});
//////////////////////////////
//
// click event listener delegate -- Check to see if a cell in the dice
// table was clicked on, and update the score based on that click.
//
document.addEventListener("click", function(event) {
target = event.target;
if (!target) {
return;
}
if (target.nodeName.toUpperCase() !== "TD") {
return;
}
var id = target.id;
if (!id) {
return;
}
var matches = id.match(/M(\d+)D(\d+)/);
if (!matches) {
reutrn;
}
var measure = parseInt(matches[1]);
var dice = parseInt(matches[2]);
if (OutputMeasures[measure] === dice) {
OutputMeasures[measure] = 0;
} else {
OutputMeasures[measure] = dice;
}
LastRollMeasure = measure;
generateScore();
});
// 17 cells in OutputMeasures first cell is unused. The dice number is
// stored in this list, which can then be used to access the measure number
// in the MeasureGrid array.
var OutputMeasures = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
// LastRollMeasure keeps track of the last measure number that a roll
// (click) was done on. This is for using the up/down arrows to scroll
// through the roll options for that measure.
var LastRollMeasure = 0;
// HumdrumDisplay controls display of the Humdrum digital score for the
// realized music. Press the "h" key on the computer keyboard to toggle
// display of the data.
var HumdrumDisplay = 0;
// MeasureGrid contains a list of the 16 measures in the composition, indexed
// from 1. For each measure, there are 11 possible dice rolls, and for each
// measure/roll the measure number from the input score is listed.
var MeasureGrid = [
// dice rolls:
// 2 3 4 5 6 7 8 9 10 11 12
[0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0,0, 96, 32, 69, 40, 148, 104, 152, 119, 98, 3, 54], // measure 1
[0,0, 22, 6, 95, 17, 74, 157, 60, 84, 142, 87, 130], // measure 2
[0,0, 141, 128, 158, 113, 163, 27, 171, 114, 42, 165, 10], // measure 3
[0,0, 41, 63, 13, 85, 45, 167, 53, 50, 156, 61, 103], // measure 4
[0,0, 105, 146, 153, 161, 80, 154, 99, 140, 75, 135, 28], // measure 5
[0,0, 122, 46, 55, 2, 97, 68, 133, 86, 129, 47, 37], // measure 6
[0,0, 11, 134, 110, 159, 36, 118, 21, 169, 62, 147, 106], // measure 7
[0,0, 30, 81, 24, 100, 107, 91, 127, 94, 123, 33, 5], // measure 8
[0,0, 70, 117, 66, 90, 25, 138, 16, 120, 65, 102, 35], // measure 9
[0,0, 121, 39, 139, 176, 143, 71, 155, 88, 77, 4, 20], // measure 10
[0,0, 26, 126, 15, 7, 64, 150, 57, 48, 19, 31, 108], // measure 11
[0,0, 9, 56, 132, 34, 125, 29, 175, 166, 82, 164, 92], // measure 12
[0,0, 112, 174, 73, 67, 76, 101, 43, 51, 137, 144, 12], // measure 13
[0,0, 49, 18, 58, 160, 136, 162, 168, 115, 38, 59, 124], // measure 14
[0,0, 109, 116, 145, 52, 1, 23, 89, 72, 149, 173, 44], // measure 15
[0,0, 14, 83, 79, 170, 93, 151, 172, 111, 8, 78, 131] // measure 16
];
//////////////////////////////
//
// generateScore -- Call this function after changing the measure grid.
//
function generateScore() {
updateTableDisplay(OutputMeasures);
prepareFilter(OutputMeasures, MeasureGrid);
displayHumdrum(HnpOptions);
}
//////////////////////////////
//
// toggleHelpListDisplay --
//
function toggleHelpListDisplay() {
var list = document.querySelector("#command-list");
if (!list) {
return;
}
if (list.style.display === "block") {
list.style.display = "none";
} else {
list.style.display = "block";
}
}
//////////////////////////////
//
// toggleHumdrumDisplay --
//
function toggleHumdrumDisplay() {
HumdrumDisplay = !HumdrumDisplay;
var element = document.querySelector("#humdrum-score");
if (!element) {
return;
}
if (HumdrumDisplay) {
var contents = element.innerHTML;
if (!contents.match(/^\s*$/)) {
element.style.display = "block";
}
} else {
element.style.display = "none";
}
}
//////////////////////////////
//
// clearGrid -- Remove all measure selections from the grid.
//
function clearGrid() {
OutputMeasures.fill(0);
LastRollMeasure = 0;
generateScore();
}
//////////////////////////////
//
// selectNextRollInMeasure -- Select the next larger roll for the
// current measure, wrapping from 12 to 2 if necessary.
//
function selectNextRollInMeasure() {
if (LastRollMeasure <= 0) {
return;
}
var value = OutputMeasures[LastRollMeasure];
if (value == 0) {
return;
}
value++;
if (value > 12) {
value = 2;
}
OutputMeasures[LastRollMeasure] = value;
generateScore();
}
//////////////////////////////
//
// selectPreviousRollInMeasure -- Select the next smaller roll for the
// current measure, wrapping from 2 to 12 if necessary.
//
function selectPreviousRollInMeasure() {
if (LastRollMeasure <= 0) {
return;
}
var value = OutputMeasures[LastRollMeasure];
if (value == 0) {
return;
}
value--;
if (value < 2) {
value = 12;
}
OutputMeasures[LastRollMeasure] = value;
generateScore();
}
//////////////////////////////
//
// goToNextMeasure -- Select the next measure for adjusting the dice roll.
// Only can change the roll in the new measure if there is already a roll.
//
function goToNextMeasure() {
LastRollMeasure++;
if (LastRollMeasure > 16) {
LastRollMeasure = 1;
}
}
//////////////////////////////
//
// goToPreviousMeasure -- Select the previous measure for adjusting the dice roll.
// Only can change the roll in the new measure if there is already a roll.
//
function goToPreviousMeasure() {
LastRollMeasure--;
if (LastRollMeasure < 1) {
LastRollMeasure = 16;
}
}
//////////////////////////////
//
// toggleVhvLinkDisplay -- Show or hide the VHV playback link text.
//
function toggleVhvLinkDisplay() {
var link = document.querySelector(".link");
if (!link) {
return;
}
if (link.style.visibility === "hidden") {
link.style.visibility = "visible";
} else {
link.style.visibility = "hidden";
}
}
//////////////////////////////
//
// makeMusicNotationLarger -- Increase the veorvio scale parameter.
//
function makeMusicNotationLarger() {
HnpOptions.scale += 3;
generateScore();
}
//////////////////////////////
//
// makeMusicNotationSmaller -- Increase the veorvio scale parameter.
//
function makeMusicNotationSmaller() {
HnpOptions.scale -= 3;
if (HnpOptions.scale < 20) {
HnpOptions.scale = 20;
}
generateScore();
}
//////////////////////////////
//
// randomizeGrid -- Create a random score.
//
function randomizeGrid() {
for (var i=1; i<=16; i++) {
var die1 = Math.floor(Math.random() * 6) + 1;
var die2 = Math.floor(Math.random() * 6) + 1;
OutputMeasures[i] = die1 + die2;
}
generateScore();
}
//////////////////////////////
//
// createDiceTable -- Create an HTML table to display on the page
// from the grid of measure numbers in the input score.
//
function createDiceTable(grid) {
var d, m;
var output = "";
output += "<table class='dice-grid'>\n";
// display output measure numbers
output += "<tr>";
output += "<td></td>"; // top left corner of table
for (m=1; m<=16; m++) {
output += "<td class='output-measure'>m" + m + "</td>";
}
output += "</tr>";
for (d=2; d<=12; d++) {
output += "<tr>\n";
output += "<td class='dice'>roll " + d + "</td>";
// display input measure numbers
for (m=1; m<=16; m++) {
output += "<td id='M" + m + "D" + d + "'>";
output += grid[m][d];
output += "</td>";
}
output += "</tr>\n";
}
output += "</table>\n";
return output;
}
//////////////////////////////
//
// createMyankFilter -- Get a list of the selected measures
// and create the myank filter command from them.
//
function createMyankFilter(measures, grid) {
var endmeasure = -1;
var i;
for (i=measures.length-1; i>=0; i--) {
if (measures[i] > 0) {
endmeasure = i;
break;
}
}
var list = [];
var item = "";
var lasti = -1;
for (i=0; i<measures.length; i++) {
if (measures[i] > 0) {
item = grid[i][measures[i]];
if (i == 16) {
item += "==";
}
if (i == 8) {
item += "||";
} else if ((lasti > 0) && (lasti < i - 1)) {
// double barline if missing measure(s)
item = "||" + item;
}
list.push(item.toString());
lasti = i;
}
}
if (measures.length == 0) {
return "";
}
var output = "myank -m '";
var suppress = 0;
for (i=0; i<list.length; i++) {
output += list[i];
if (list[i].match(/\d$/) && (i < list.length - 1) && list[i+1].match(/^\d/)) {
output += ',';
}
}
output += "'";
return output;
}
//////////////////////////////
//
// prepareFilter --
//
function prepareFilter(measures, grid) {
var filter = createMyankFilter(measures, grid);
if (!filter) {
delete HnpOptions.filter;
} else {
HnpOptions.filter = filter;
}
var elink = document.querySelector("#vhv-link");
if (elink) {
// var url = "https://verovio.humdrum.org/?file=dice/mozart/k516f.krn";
var url = "https://verovio.humdrum.org/?file=https://raw.githubusercontent.com/craigsapp/Musikalisches-Wuerfelspiel/main/mozart-kanhc30-01.krn";
if (filter) {
url += "&k=ey&filter=" + encodeURIComponent(filter);
} else {
url += "&k=e";
}
elink.href = url;
}
}
//////////////////////////////
//
// updateTableDisplay -- Highlight all of the selected measures
// in the dice grid.
//
function updateTableDisplay(measures) {
// clear previous markers
var table = document.querySelector("table.dice-grid");
if (!table) {
return;
}
var active = table.querySelectorAll("td.active");
var i;
for (i=0; i<active.length; i++) {
active[i].classList.remove("active");
}
// add new active markers:
for (i=0; i<measures.length; i++) {
if (measures[i] <= 0) {
continue;
}
var measure = i;
var dice = measures[i];
var id = "M" + measure + "D" + dice;
var cell = table.querySelector("#" + id);
if (cell) {
cell.classList.add("active");
}
}
}
//////////////////////////////
//
// prepareMeasureGrid -- Create HTML code for dice grid and display it.
//
function prepareMeasureGrid(grid) {
var element = document.querySelector("#measure-grid");
if (!element) {
return;
}
var contents = createDiceTable(grid);
element.innerHTML = contents;
}
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-175309821-1');
</script>
<style>
body {
margin-top: 100px;
margin-bottom: 100px;
font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif;
color: #333;
}
@media screen and (min-width: 1000px) {
body { margin-left: 100px; margin-right: 100px; }
p { width: 40em; text-align: justify; font-size: 1.25rem;}
table.dice-grid { font-size: 110%; }
table.dice-grid td { padding: 4px; }
}
@media screen and (max-width: 1000px) {
body { margin-left: 80px; margin-right: 80px; }
p { width: 34em; text-align: justify; font-size: 1.20rem;}
table.dice-grid { font-size: 100%; }
table.dice-grid td { padding: 3px; }
}
@media screen and (max-width: 800px) {
body { margin-left: 50px; margin-right: 50px; }
p { width: 30em; text-align: justify; font-size: 1.10rem;}
table.dice-grid { font-size: 80%; }
table.dice-grid td { padding: 2px; }
}
@media screen and (max-width: 600px) {
body { margin-left: 20px; margin-right: 20px; }
p { width: 25em; text-align: justify; font-size: 0.90rem;}
table.dice-grid { font-size: 55%; }
table.dice-grid td { padding: 1px; }
}
@media screen and (max-width: 400px) {
body { margin-left: 3px; margin-right: 3px; }
p { width: 24em; text-align: left; font-size: 0.75rem;}
table.dice-grid { font-size: 25%; }
table.dice-grid td { padding: 0px; }
}
a {
text-decoration: none;
color: gold;
}
div.link {
color: #ccc;
}
p {
margin-left: auto;
margin-right: auto;
word-spacing: 0.5px;
}
h1 {
font-family: 'UnifrakturMaguntia', cursive;
display: block;
color: #003;
font-size: 2.5rem;
letter-spacing: 3px;
text-align: center;
}
.command {
font-family: Courier, fixed-width;
font-weight: bold;
}
#command-list {
width: 38em;
display: none;
margin-left: auto;
margin-right: auto;
}
#humdrum-score {
tab-size: 16;
-moz-tab-size: 16;
width: 30em;
display: none;
margin-left: auto;
margin-right: auto;
background: #fdfdfd;
border: 1px solid #ddd;
padding: 20px;
}
.link {
position: absolute;
right: 0;
margin-top: -20px;
}
table.dice-grid {
margin-top: 30px;
margin-bottom: 30px;
margin-left: auto;
margin-right: auto;
}
table.dice-grid td[id^=M] {
text-align: center;
border-radius: 25px;
border: 1px solid #ffffffff;
cursor: pointer;
font-weight: bold;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
}
table.dice-grid td[id^=M]:hover {
background: tomato;
border: 1px solid gold;
}
table.dice-grid td[id^=M].active:hover {
background: gold;
border: 1px solid tomato;
}
table.dice-grid td:first-child:hover,
table.dice-grid tr:first-child td:hover {
background: transparent;
}
table.dice-grid td.active {
background: gold;
border: 1px solid tomato;
}
table.dice-grid td.dice,
table.dice-grid td.output-measure {
font-style: italic;
font-family: serif;
white-space: nowrap;
color: #999;
}
</style>
</body>
</html>