Skip to content

Instantly share code, notes, and snippets.

@ff8c00
Created March 18, 2018 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ff8c00/ab9d1413760fec4eecd6e71338595c76 to your computer and use it in GitHub Desktop.
Save ff8c00/ab9d1413760fec4eecd6e71338595c76 to your computer and use it in GitHub Desktop.
Reddit Daily Programmer 343 Intermediate
<html>
<head>
<script>
var mozart = [
[ 96, 32, 69, 40, 148, 104, 152, 119, 98, 3, 54],
[ 22, 6, 95, 17, 74, 157, 60, 84, 142, 87, 130],
[141, 128, 158, 113, 163, 27, 171, 114, 42, 165, 10],
[ 41, 63, 13, 85, 45, 167, 53, 50, 156, 61, 103],
[105, 146, 153, 161, 80, 154, 99, 140, 75, 135, 28],
[122, 46, 55, 2, 97, 68, 133, 86, 129, 47, 37],
[ 11, 134, 110, 159, 36, 118, 21, 169, 62, 147, 106],
[ 30, 81, 24, 100, 107, 91, 127, 94, 123, 33, 5],
[ 70, 117, 66, 90, 25, 138, 16, 120, 65, 102, 35],
[121, 39, 136, 176, 143, 71, 155, 88, 77, 4, 20],
[ 26, 126, 15, 7, 64, 150, 57, 48, 19, 31, 108],
[ 9, 56, 132, 34, 125, 29, 175, 166, 82, 164, 92],
[112, 174, 73, 67, 76, 101, 43, 51, 137, 144, 12],
[ 49, 18, 58, 160, 136, 162, 168, 115, 38, 59, 124],
[109, 116, 145, 52, 1, 23, 89, 72, 149, 173, 44],
[ 14, 83, 79, 170, 93, 151, 172, 111, 8, 78, 131]
];
function open(uri, callback) {
var xhttp;
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
callback(this.responseText);
}
};
xhttp.open("GET", uri, true);
xhttp.send();
}
function parse(file) {
var measures, i, items, item, measure, j;
measures = [];
for (i = 0; i < 176; i++) {
measures[i] = [];
}
items = file.split("\r\n");
for (i = 0; i < items.length; i++) {
item = items[i].split(" ");
measure = {
note: item[0],
start: parseFloat(item[1]),
duration: parseFloat(item[2])
};
j = Math.floor(measure.start / 3);
measure.start = measure.start - (j * 3);
measures[j].push(measure);
}
return measures;
}
function random(measures) {
var composition, i, j, items, k, item, measure;
composition = [];
for (i = 0; i < 16; i++) {
j = Math.floor(Math.random() * 6) + Math.floor(Math.random() * 6);
items = measures[mozart[i][j] - 1];
for (k = 0; k < items.length; k++) {
item = items[k];
measure = {
note: item.note,
start: item.start + (i * 3),
duration: item.duration
};
composition.push(measure);
}
}
return composition;
}
function print (composition) {
var i, measure;
for (i = 0; i < composition.length; i++) {
measure = composition[i];
document.write(measure.note + " ");
document.write(measure.start + " ");
document.write(measure.duration + " ");
document.write("<br>");
}
}
function convert(note) {
var notes, i;
notes = [
"C", "", "D", "", "E", "F", "", "G", "", "A", "", "B"
];
i = notes.indexOf(note[0]);
if (note.length == 2) {
i = i + ((1 + parseInt(note[1])) * 12);
} else {
i = i + ((1 + parseInt(note[2])) * 12) + 1;
}
return Math.pow(2, (i - 69) / 12) * 440;
}
function music (composition) {
var audio, volume, sound, index, i, measure, frequency, start, duration;
var tempo = 60 / 120, fade = 0.25 * tempo;
audio = new AudioContext();
volume = [];
sound = [];
for (i = 0; i < 12; i++) {
volume[i] = audio.createGain();
volume[i].gain.setValueAtTime(0, audio.currentTime);
volume[i].connect(audio.destination);
sound[i] = audio.createOscillator();
sound[i].type = "sawtooth";
sound[i].connect(volume[i]);
sound[i].start();
}
index = 0;
for (i = 0; i < composition.length; i++) {
measure = composition[i];
frequency = convert(measure.note);
start = measure.start * tempo;
duration = measure.duration * tempo;
volume[index].gain.setValueAtTime(0.5, start);
volume[index].gain.setTargetAtTime(0, start + duration - fade, fade);
sound[index].frequency.setValueAtTime(frequency, start);
index = index + 1;
if (index == sound.length) {
index = 0;
}
}
}
function run() {
open("mozart.txt", function(file) {
var measures, composition;
measures = parse(file);
composition = random(measures);
print(composition);
music(composition);
});
}
</script>
</head>
<body onload="run()">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment