Thursday 8 June 2017

PYTHON RHYTHM

Dick Pountain/ Idealog 270/10 January 2017 10:54

I may have mentioned here a few dozen times how deeply I'm into music. I play guitar, bass and dobro (for amusement rather than reward); I regularly attend chamber and orchestral concerts at South Bank and Wigmore Hall; I listen to classical, jazz, blues, rock, bluegrass, country, dubstep, reggae, EDM and much, much  more on Spotify, both at home and on Bluetooth earbuds while walking on heath or park. But on top of all that, for the last 20-odd years I've been working on my own system of computerised composition.

It started in the mid-1990s, using Turbo Pascal to write my own music library that let me generate MIDI files from Pascal programs, and play them on any General MIDI synth. Before too long the memory management limitations of PC-DOS became an obstacle to writing big programs though. I've picked up and dropped this project many times over the intervening years, planned and failed to rewrite it in Ruby, planned and succeeded in rewriting it in the blessed Python. And I recently cracked a couple of remaining knotty problems (both related to the grim unfriendliness of the MIDI protocol) to produce a system that automatically generates tunes which sometimes sound convincingly like human music. A sort of musical Turing Test - not intended as yet another tool to help you write pop or dance music, because there are plenty of good ones already like Ableton and Sibelius.

Instead my system lets me dissect music into its atomic parts, then reassemble them to generate tunes few humans could play. One of the few real regrets of my life is that as a child I turned down the offer of piano lessons, and therefore didn't learn to sight-read musical notation at the best age. Instead I took up guitar in my teens and taught myself blues and ragtime by listening to records. This has had a profound effect on my understanding of music because the guitar is a fundamentally chromatic instrument: each fret is one semitone so the difference between white and black notes means nothing. I did painfully teach myself to read notation eventually but I'm far from fluent, and more to the point it actively irritates me. I now think chromatic.

Hence when designing my system I decided not to make the 'note' its fundamental data structure, but rather - like MIDI itself - I treat pitch, time, duration and volume as separate musical 'atoms'. Of course it's trivial in Python to strap these atoms together as (pitch, time, duration, volume) tuples, then manipulate those as units, and it enables me to translate ASCII strings into sequences of these atoms. The process of 'composing' a tune then becomes a matter of writing strings that define melody, rhythm and dynamics, each of which which I can alter separately. More significantly, the *program* can manipulate them by splicing and chopping, applying functions, adding randomness, say to change the rhythm or the timing of the same melodic fragment.

All the compositional tricks like fugues, canons, rondos, arpeggios are easy to achieve and automate. Play the same fragment in a different scale - major, minor, cycles of thirds, fourths, fifths, wholetone, Bartok's Golden Section pentatony - by choosing from a tuple of scale functions. I can even create new scales on the fly using  lambda function parameters. It's strictly a system for programmers, interfaced via a single Python function called 'phrase' that takes five ASCII strings as parameters. Each invocation, like:

S.phrase(1, 1, scale[mx], Key[2], Acoustic_Bass, S.nSeq(inv(p),t,d,v,m))

writes a sequence of notes, length defined by those strings, to one MIDI track. Programs tend to be short, forty or fifty lines with lots of loops. Using ASCII permits silly tricks like turning a name, let's say Donald Trump, into a tune (and yes, I have, as a sinister bass riff). MIDI is quite limiting of course, the available instruments aren't that great, so any tunes I like I whisk into Abelton and play them with proper samples into an MP3 file.

Though I do like some electronic dance music (especially Brandt, Brauer, Frick) that's really not what this system is meant for. I'm more into sort of jazz/classical fusions that mess with tricksy rhythms and harmonies that would be hard to play on real intruments. Everyone worries nowadays that robots are going to put them out of a job, but neither Adele nor Coldplay need have any fear on my account. In any case my competitors would more likely be software suites developed at France's IRCAM and INRIA, and they needn't panic either. And I don't intend to build a graphical user interface, because for me writing program code is just as much fun as playing with music.

[Dick Pountain would like to offer you a jaunty little tune based on the words BREXIT and TRUMP, by way of demonstration: file brexit bounce.mp3]

No comments:

Post a Comment

SOCIAL UNEASE

Dick Pountain /Idealog 350/ 07 Sep 2023 10:58 Ten years ago this column might have listed a handful of online apps that assist my everyday...