Abusing Bach
Sun Aug 17 20:28:21 BST 2003 Permalink
Via a recent Tech Tip i've learned a little bit about Java's sound capabilites and, in particular, using Java to play MIDI. It was fun typing in a little java program and hearing piano's playing a few notes. Something to while away a few minutes on a hot afternoon.
Then I started thinking about algorithmically generating music. I am deuced unmusical (lacking both rhythmn and a good pitch ear) which has lead, in the past, to some frustrating attempts to use professional software to make compositions. I can program Java though.
I have a program which I use to generate random pronounceable passwords. Although it's hit rate for generating memorable words is about 1 in 40, I can remember the good passwords some 6 years after first using them. It works by analysing a body of text and calculating the frequency of each 3 letter combination that appears. Then it uses some simple rules to combine these 3 letter combinations into words.
So I started to wonder if the same thing could be done with music. Could you stitch together 3 note combinations into something resembling music? (for the moment let's set aside the question of why on earth would you do this?)
Java has a very simple call:
MidiSystem.getSequence( file )
which loads a MIDI file into an array of Track objects from which you can access the events which play the various notes of the piece. There are equivalent calls for creating & playing tracks (which use your sound card like a synthesizer).
I started with some Bach that I found on the net. My aim was to do a frequency analysis of the 3-note combinations. Somewhat to my surprise though I discovered there weren't any repetitions. That is, no exact 3 note combination was ever repeated. At least, unless I got my program wrong. The resulting noise led my housemates to question whether I was safe to be left home alone.
Not to be discouraged I tried a second approach, analysing, for each note played the range of notes which could follow and probability of each. This, coupled with some simple selection logic, allows me to play something that sounds almost totally unlike music (and certainly unlike Bach's music). Mostly it has taught me that music is vastly more complex in structure than words.
Still, it's been a diverting way to spend an afternoon AND i've learned something.
Some other notes. My development environment is IDEA by Intellij. I've tried pretty much every Java IDE going and this one is the best by far. If you haven't tried Intention actions yet, well... The GUI was built using Peter Eastman's Buoy widget set. Peter is also responsible for the Java based 3D rendering suite ArtOfIllusion.

