Skip to content

Instantly share code, notes, and snippets.

@veeenu
Created December 4, 2014 17:07
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 veeenu/af48a4834ebc33306412 to your computer and use it in GitHub Desktop.
Save veeenu/af48a4834ebc33306412 to your computer and use it in GitHub Desktop.
Thema Regium
<!DOCTYPE html>
<html>
<head>
<link href='http://fonts.googleapis.com/css?family=Ubuntu+Mono' rel='stylesheet' type='text/css'>
<style>
body {
padding: 9rem;
font-size: 4rem;
font-family: 'Ubuntu Mono', monospace;
line-height: 6rem;
text-align: center;
}
</style>
</head>
<body>
<script>
(function() {
var ac = new window.AudioContext(),
osc = ac.createOscillator(),
notes = {
'c': 261.6,
'c#': 277.2,
'db': 277.2,
'd': 293.7,
'd#': 311.1,
'eb': 311.1,
'e': 329.6,
'f': 349.2,
'f#': 370.0,
'gb': 370.0,
'g': 392.0,
'g#': 415.3,
'ab': 415.3,
'a': 440.0,
'a#': 466.2,
'bb': 466.2,
'b': 493.9,
'--': 1
},
toPrint = function(note) {
var out = note.charAt(0).toUpperCase();
if(out === '-') return '';
if(note.length > 1) {
if(note.charAt(1) === 'b')
out += '&#9837;';
else
out += '&#9839;';
}
return out;
},
themaRegium;
// "Thema Regium" beat by Bach feat. Frederich II of Prussia
// note octave duration note octave duration ...
themaRegium =
'c 1 2 eb 1 2 g 1 2 ab 1 2 b 0.5 2 -- 1 1 g 1 1 ' +
'f# 1 2 f 1 2 e 1 2 eb 1 3 d 1 1 db 1 1 c 1 1 ' +
'b .5 1 a .5 .5 g .5 .5 c 1 1 f 1 1 eb 1 2 d 1 2 ' +
'c 1 4';
themaRegium = themaRegium.split(/\s+/g);
var nextNote = function() {
if(themaRegium.length < 1) {
document.body.innerHTML += '!';
return osc.stop();
}
var note = themaRegium.shift(),
oct = parseFloat(themaRegium.shift()),
dur = parseFloat(themaRegium.shift()) * 250;
console.log(note, dur);
osc.frequency.value = notes[note] * oct;
setTimeout(nextNote, dur);
document.body.innerHTML += ' ' + toPrint(note);
}
osc.connect(ac.destination);
osc.type = 'triangle';
osc.start(0);
nextNote();
}());
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment