Wednesday, December 14, 2011

Quick 'n' Easy Polyphony for the Arduino

Bloominglabs was lucky enough to have Catarina Mota of OpenMaterials, NYCResistor, ITP, and more visit our space last month to give us a demo on open materials - conductive threads and fabrics, materials for modeling parts (e.g., Sugru), quantum tunneling composites, conductive paint - it was very cool and we came away from it inspired to make projects using the things she showed us.

Catarina is partially known as co-creator of the Jeltone Jello piano, which she brought to show us. Later during a health food stop a few of us made at the local Denny’s (the first time I’d eaten at one in at least 15 years) the subject of polyphony using the Arduino came up. Jennet, one of Bloominglabs’ founders, has been working on an IR harp based on a project some students did at Cornell. The site is a great resource of project ideas, and the projects are very well documented.

Anyhow, the code for the IR Harp was written for the ATMega644, and as the user plucks the strings, the notes ring out in some sweet sounding (for 8 bit) chords. This gave us the idea that it could definitely be done, although of course it would require something more than the Tone library.

I took a look at the code to see about porting it. Fortunately the Atmega328 the Arduino Uno is built around has pretty much the same registers and so on as the ATMega644, the main differences being the 644 has a lot more pins, and twice the memory.

I had an AD5330 DAC I had played w/ before, inspired by this tutorial over at Sparkfun, so I decided to use that.

To convert the code, I first switched over from using Timer0 to Timer2. Timer0 is used for millis() on the Arduino, so if you try to compile the code w/ Timer0, it will error out. I also had to add the code to accomadate my DAC (adapting for others shouldn’t be too tricky), and fiddled around a bit with which pins are used for what. I stuck with using the ‘PORT’ approach to I/O instead of the Arduino functions, b/c as we know they are way faster, and generally you want an interrupt routine to do its thing as fast as possible and get out.

Another change was to have a function to load the different waveforms on the fly as needed. These were all being loaded up front, and like I said, the 328 has only half the memory. Also, the waveforms are calculated, so you do not need to store a table.

The demo code goes thru all the waveforms, plucking out the same chord for each. Feel free to use it for whatever instrument you want. Maybe an Arduino-based 8bit clock tower/carillon. Why not?

No comments:

Post a Comment