This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void exampleA() { | |
char a = 'A'; | |
^{ | |
printf("%c\n", a); | |
}(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void exampleB_addBlockToArray(NSMutableArray *array) { | |
char b = 'B'; | |
[array addObject:^{ | |
printf("%c\n", b); | |
}]; | |
} | |
void exampleB() { | |
NSMutableArray *array = [NSMutableArray array]; | |
exampleB_addBlockToArray(array); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void exampleC_addBlockToArray(NSMutableArray *array) { | |
[array addObject:^{ | |
printf("C\n"); | |
}]; | |
} | |
void exampleC() { | |
NSMutableArray *array = [NSMutableArray array]; | |
exampleC_addBlockToArray(array); | |
void (^block)() = [array objectAtIndex:0]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef void (^dBlock)(); | |
dBlock exampleD_getBlock() { | |
char d = 'D'; | |
return ^{ | |
printf("%c\n", d); | |
}; | |
} | |
void exampleD() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef void (^eBlock)(); | |
eBlock exampleE_getBlock() { | |
char e = 'E'; | |
void (^block)() = ^{ | |
printf("%c\n", e); | |
}; | |
return block; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Lets you fill a Buffer without having to know its size beforehand. | |
*/ | |
var BufferWriter = function() { | |
this._size = 0; | |
this._buffer = new Buffer(100); | |
}; | |
_.extend(BufferWriter.prototype, { | |
buffer: function() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Creates a helper to write a WAVE file to a Buffer. | |
* @param {Number} seconds - The length of the WAVE file to write. | |
* @param {Number} sampleRate - The sample rate in Hz, such as 44100 for a CD. | |
* @param {Number} bitsPerSample - Either 8 or 16. | |
*/ | |
var WaveWriter = function(seconds, sampleRate, bitsPerSample) { | |
this._sampleRate = sampleRate; | |
this._bitsPerSample = bitsPerSample; | |
this._writer = new BufferWriter(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sineWave = function(frequency) { | |
var writer = new WaveWriter(2.0, 11025, 8); | |
var time = 0; | |
var sample = 0; | |
do { | |
sample = Math.sin(frequency * time * 2 * 3.14159); | |
time = time + 1/11025; | |
} while (writer.writeSample(sample)); | |
writer.close(); | |
return writer.buffer(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Runs the bootbeepASM methods and then converts the audio data to a WAVE file. | |
* @returns {Buffer} a Buffer containing the contents of a WAVE file. | |
*/ | |
var bootbeep = function() { | |
// 22050 Hz is an approximation of the Mac's 370 * 60.15 Hz = 22255.5 Hz. | |
var writer = new WaveWriter(1.5, 22050, 8); | |
var samples = bootbeepASM(); | |
console.log("Copying samples to WAVE."); | |
var i = 0; |
OlderNewer