Skip to content

Instantly share code, notes, and snippets.

@bklimt
Last active December 14, 2015 03:38
Show Gist options
  • Save bklimt/5022105 to your computer and use it in GitHub Desktop.
Save bklimt/5022105 to your computer and use it in GitHub Desktop.
Mac Boot Beep translated to JavaScript
/**
* Adapted from http://folklore.org/projects/Macintosh/more/BootBeep.txt
* A direct translation of the m68k asm into JavaScript.
* @returns {Buffer} a Buffer which corresponds to the audio buffer on the
* original Macintosh, repeated for the duration of the boot beep. The Buffer
* contains a series of words, where the lower byte of each word is the
* amplitude of the audio signal at this point.
*/
var bootbeepASM = function() {
d3 = 40; // D3 contains the duration: 40 is boot beep
var output = new Buffer(0);
var buffer = new Buffer(2 * 5 * 4 * 19);
var a0 = 0; // get sound buffer address
var a1 = 0;
console.log("Initializing buffer.");
// repeat 74 byte sequence 5 times
_.times(5, function() {
// 4 byte table to fill buffer with
_.each([0x06, 0xC0, 0x40, 0xFA], function(d1) {
_.times(19, function() { // repeat 19 times
buffer.writeUInt16LE(d1, a0); // move in a byte
a0 += 2; // bump to next buffer location
});
});
});
console.log("Filtering buffer " + d3 + " times.");
// OK, now filter it for a nice fade -- repeat the filtering process D3 times
_.times(d3, function() {
a0 = 0; // point A0 to start of buffer
a1 = a0 + 146; // point A1 74 values in
// process 74 samples
_.times(74, function() {
var d2 = buffer.readUInt8(a1); // get 74th value
a1 += 4; // bump to 76th value
var d1 = buffer.readUInt8(a1); // make it a word value (lol, no)
d2 += d1; // add to the accumulator
a1 -= 2; // point at 75th value
d1 = buffer.readUInt8(a1); // get it
d2 += d1; // add to the accumulator
d2 += d1; // count it twice
d2 = Math.round(d2 / 4); // divide it by 4
buffer.writeUInt8(d2, a0); // store it away
a0 += 2; // bump to next value
});
// 296 values to copy
_.times(296, function() {
buffer.writeUInt8(buffer.readUInt8(a0 - 74), a0);
a0 += 2;
});
// Only the first 370 words is actually used by the Mac audio buffer.
output = Buffer.concat([output, buffer.slice(0, 2 * 370)]);
});
return output;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment