Skip to content

Instantly share code, notes, and snippets.

@bklimt
bklimt / gist:4700288
Last active December 12, 2015 02:38
Objective-C Blocks Example A
void exampleA() {
char a = 'A';
^{
printf("%c\n", a);
}();
}
@bklimt
bklimt / gist:4700897
Last active December 12, 2015 02:39
Objective-C Blocks Example B
void exampleB_addBlockToArray(NSMutableArray *array) {
char b = 'B';
[array addObject:^{
printf("%c\n", b);
}];
}
void exampleB() {
NSMutableArray *array = [NSMutableArray array];
exampleB_addBlockToArray(array);
@bklimt
bklimt / gist:4700913
Created February 3, 2013 08:05
Objective-C Blocks Example C
void exampleC_addBlockToArray(NSMutableArray *array) {
[array addObject:^{
printf("C\n");
}];
}
void exampleC() {
NSMutableArray *array = [NSMutableArray array];
exampleC_addBlockToArray(array);
void (^block)() = [array objectAtIndex:0];
@bklimt
bklimt / gist:4700928
Created February 3, 2013 08:11
Objective-C Blocks Example D
typedef void (^dBlock)();
dBlock exampleD_getBlock() {
char d = 'D';
return ^{
printf("%c\n", d);
};
}
void exampleD() {
@bklimt
bklimt / gist:4700955
Created February 3, 2013 08:33
Objective-C Blocks Example E
typedef void (^eBlock)();
eBlock exampleE_getBlock() {
char e = 'E';
void (^block)() = ^{
printf("%c\n", e);
};
return block;
}
@bklimt
bklimt / gist:5022105
Last active December 14, 2015 03:38
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
@bklimt
bklimt / gist:5022112
Last active December 14, 2015 03:38
BufferWriter for Cloud Code Buffer
/**
* 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() {
@bklimt
bklimt / gist:5022117
Last active December 14, 2015 03:38
WaveWriter for JavaScript
/**
* 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();
@bklimt
bklimt / gist:5022125
Created February 24, 2013 01:01
Generate C# from JavaScript
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();
@bklimt
bklimt / gist:5022739
Created February 24, 2013 05:26
Output Macintosh boot beep as a WAVE file.
/**
* 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;