Zelda - Band.js - JSON Load Alternative
The Zelda Overworld theme recreated with Band.js loaded via JSON using an alternative JSON syntax.
HTML
<script src="http://remotesynthesis.com/projects/8bit/js/band.min.js"></script>
<script src="http://remotesynthesis.com/projects/8bit/zelda-band-reformat.js"></script>
<button id="playButton">Play</button>
<button id="pauseButton" disabled>Pause</button>
<button id="stopButton" disabled>Stop</button>
<p>Image via <a href="http://8bitcity.blogspot.com/2011/12/awesome-8-bit-zelda-pic.html">http://8bitcity.blogspot.com/2011/12/awesome-8-bit-zelda-pic.html</a></p>
CSS
body {
background-image: url(http://remotesynthesis.com/projects/8bit/img/The_Legend_of_Zelda_8_Bit_by_gamingaddictmike125.png);
color: white;
font-size: .8em;
}
a, a:visited, a:hover {
color: white;
}
JavaScript
// 8bit.js https://github.com/meenie/8bit.js
// arrangement via http://herbalcell.com/static/sheets/legend-of-zelda/overworld-labels.pdf
var music = new BandJS();
music.load = function(json) {
if (! json) {
throw new Error('JSON is required for this method to work.');
}
// Need to have at least instruments and notes
if (typeof json['instruments'] === 'undefined') {
throw new Error('You must define at least one instrument');
}
if (typeof json['notes'] === 'undefined') {
throw new Error('You must define notes for each instrument');
}
// Shall we set a time signature?
if (typeof json['timeSignature'] !== 'undefined') {
this.setTimeSignature(json['timeSignature'][0], json['timeSignature'][1]);
}
// Maybe some tempo?
if (typeof json['tempo'] !== 'undefined') {
this.setTempo(json['tempo']);
}
// Lets create some instruments
var instrumentList = {};
for (var instrument in json['instruments']) {
if (! json['instruments'].hasOwnProperty(instrument)) {
continue;
}
instrumentList[instrument] = this.createInstrument(
json['instruments'][instrument].name,
json['instruments'][instrument].pack
);
}
// Now lets add in each of the notes
var measureDuration;
console.log(this.dottedEighth);
for (var instrument in json['notes']) {
if (! json['notes'].hasOwnProperty(instrument)) {
continue;
}
json['notes'][instrument].forEach(function(note) {
note.forEach(function(measure) {
if (measure.length === 1) {
instrumentList[instrument].rest(measure[0]);
}
else {
instrumentList[instrument].note(measure[0], measure[1], measure[2]);
}
});
});
...