Archive for the 'code' Category

Drawing into a CGContextRef created using CGBitmapContextCreate

Saturday November 15th, 2008 at 8:23 pm | code

So, if you happen to be doing what the subject line says – drawing anything into a CGContextRef that was created using CGBitmapContextCreate – and you’re seeing nothing but black, the trick is the following: you have to memset the the pixel data to all 0xFF’s before you draw.

For example, to draw a PDF document reference by ‘document’:

// allocate pixels
unsigned char* pixels = new unsigned char[thumb_height*thumb_width*4];

// create context
CGContextRef context = CGBitmapContextCreate(pixels, thumb_width, thumb_height, 8, 4*thumb_width, CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB ), kCGImageAlphaNoneSkipLast );

// clear
memset( pixels, 255, thumb_width*thumb_height*4 );

// render page 1 into the pixels array
CGPDFPageRef page = CGPDFDocumentGetPage( document, 1 );
CGContextDrawPDFPage( context, page );

(For some reason I just wasn’t able to create any kind of RGBA context with CGBitmapContextCreate() – I always had to use the skip-alpha enum. Weird.)

gnuplot from stdin

Saturday August 9th, 2008 at 10:56 pm | code

Since I couldn’t find this in an easy-to-use format anywhere else, here’s how you use gnuplot to plot a list of numbers sent in with stdin:

(echo plot \"-\" with lines; cat numbers.txt) | gnuplot

numbers.txt is a list of numbers, with or without terminating semicolons. Scientific notation is ok.

(30 minutes later) … and, here’s how to write a pdf graph of each text file in the current directory:

ls *.txt | perl -e 'while(<stdin>) { chomp; system "(echo set term pdf; echo set yrange [-1:1]; echo plot \\\"-\\\" with lines; cat $_ ) | gnuplot > $_.pdf"; }

(and so I don’t lose it, here’s how to generate a whole bunch of testunits:

perl -e 'for ( $i=1; $i<60; $i+=5 ) { for ( $j=$i+1; $j<150; $j*=1.5 ) { print "#X obj ".($i*40)." ".(int($j))." testunit $i-".(int($j))." $i ".(int($j))." senergy;\n"; } } ' > 1.actions/testunits.pd.txt

)

ofxOsc

Thursday August 7th, 2008 at 3:14 pm | code

I’ve updated ofxOsc to v0.2 – now with OSC Bundle support. Check it out here.

Save file dialog code – OSX/Carbon

Friday May 2nd, 2008 at 11:25 pm | code

Why is this so hard?

	short fRefNumOut;
	FSRef output_file;

	NavDialogCreationOptions options;
	NavGetDefaultDialogCreationOptions( &options );
	options.modality = kWindowModalityAppModal;

	NavDialogRef dialog;
	err = NavCreatePutFileDialog ( &options, "Midi", 'MIDI', NULL, NULL, &dialog);
	printf("NavCreatePutFileDialog returned %i\n", err );

	err = NavDialogRun( dialog );
	printf("NavDialogRun returned %i\n", err );

	NavUserAction action;
	action = NavDialogGetUserAction( dialog );
	printf("got action %i\n");
	if (action == kNavUserActionNone || action == kNavUserActionCancel) {
		return 2;
	}

	// get dialog reply
	NavReplyRecord reply;
	err = NavDialogGetReply(dialog, &reply);
	if ( err != noErr )
		return 2;

	if ( reply.replacing )
	{
		printf("need to replace\n");
	}

	AEKeyword keyword;
	DescType actual_type;
	Size actual_size;
	FSRef output_dir;
	err = AEGetNthPtr(&(reply.selection), 1, typeFSRef, &keyword, &actual_type,
						  &output_dir, sizeof(output_file), &actual_size);

	printf("AEGetNthPtr returned %i\n", err );

	UInt8 output_dir_name[1024];
	FSRefMakePath(&output_dir, output_dir_name, 1024 );
	printf("got %s\n", output_dir_name );

	// now get filename
	CFIndex len = CFStringGetLength(reply.saveFileName);
	if (len > 255)
		len = 255;
	UniChar output_filename[255];
	CFStringGetCharacters(reply.saveFileName, CFRangeMake(0, len), output_filename);

	// need to unlink the old file
	if ( reply.replacing )
	{
		FSRef oldfile;

		err = FSMakeFSRefUnicode(&output_dir, len, output_filename,
								kTextEncodingUnicodeDefault,
								&oldfile);
		if (err == noErr)
			err = FSDeleteObject(&oldfile);
		if (err != noErr)
		{
			printf( "error %d erasing old file\n", err );
			return 2;
		}
	}

	err = FSCreateFileUnicode( &output_dir, len, output_filename, kFSCatInfoNone,
							   NULL, &output_file, NULL );

	if ( err != noErr )
	{
		fprintf( stderr, "error %i creating file\n", err );
		return 2;
	}

	// cleanup dialog
	NavDialogDispose(dialog);

	HFSUniStr255 data_fork;
	FSGetDataForkName( &data_fork );
	err = FSOpenFork(&output_file,data_fork.length, data_fork.unicode,fsWrPerm,&fRefNumOut);
	if ( err != noErr )
	{
		fprintf( stderr, "error %i opening data fork for writing\n", err );
		return 2;
	}

	char* buf = "hello";
	long bytes_written;
	FSWriteFork( &fRefNumOut, fsAtMark, 0, strlen(buf), buf, &bytes_written );

	FSCloseFork( &fRefNumOut );

Some more history

Wednesday March 26th, 2008 at 1:39 am | code : research

Some history

Friday March 21st, 2008 at 5:05 pm | code : research

These things have all influenced me deeply on some level. Whether or not I’m willing to call them ‘art’ is a different question.


(higher quality video here)

Transistors: BC vs 2N

Tuesday March 11th, 2008 at 8:27 pm | code

PNP
BC556, BC557, BC558
2N3906

NPN
BC546, BC547, BC 548
2N3904, 2N2222

just because i couldn’t find it anywhere else

Sun Run Sun has its own project page

Monday February 4th, 2008 at 7:38 pm | code : participate

at http://www.frey.co.nz/projects/sunrunsun/

enjoy.

Sun Run Sun

Wednesday January 23rd, 2008 at 1:50 am | code : participate

I’m in Amsterdam, NL working at NIMK/Montevideo with Yolande Harris, who is currently Montevideo’s Artist In Residence. She has a project called Sun Run Sun which is about sonifying navigation data, in particular GPS data.

I have been called in to help write some Pure Data patches for this project. The patches need to run with PDa on a Gumstix computer.

Dorkbot Valencia presentation / S5

Saturday December 1st, 2007 at 9:12 pm | code : music

I gave a presentation last night at Dorkbot Valencia. The presentation was called “Performing Music with PureData,” and it included some discussion of my ideas about performance with laptops and why it so often is uninspiring, as well as a little demonstration of my PureData patches, and last a little demonstration of some computer vision tracking used as music control.

The presentation was built using something I found called S5 which calls itself A Simple Standards-Based Slide Show System. It’s basically the entire functionality of PowerPoint in a single HTML file, with CSS to control the look and JavaScript to handle the navigation. Very, very nice. Have a look at my Dorkbot Valencia presentation for a demo…