#!/usr/bin/python # Serial Composition script for Aj Klhic B-Fedg # By Kevin Cox > http://www.jibberia.com/ # uses the Python Midi Package # > http://www.mxm.dk/products/public/pythonmidi import MidiOutFile,random random.seed() def inc11(var): var = var + 1 if var > 11: return 0 else: return var # fun config stuff prime = [ 0, 9, 10, 11, 7, 8, 2, 1, 5, 4, 3, 6 ] inver = [ 0, 3, 2, 1, 5, 4, 10, 11, 7, 8, 9, 6 ] rsix = [ 6, 3, 4, 5, 1, 2, 8, 7, 11, 10, 9, 0 ] out_file = 'serial.mid' #name of output file generate = 30 # seconds basepitch = 60+12 # midi note number (60 == middle C) basedur = 192 / 12 # == 16 ticks (192 ticks == 1 second) cpitch, camp, cdur, totaldur, octdev = 0, 0, 0, 0, 0 midi = MidiOutFile.MidiOutFile(out_file) midi.header(format=1, nTracks=3) # track 0 - melody midi.start_of_track(0) while totaldur < (192 * generate): len = basedur * (prime[cdur] + 1) pitch = basepitch + prime[cpitch] midi.update_time(0) midi.note_on(channel=0, note=pitch, velocity=100) midi.update_time(len) midi.note_off(channel=0, note=pitch, velocity=100) if random.randint(0,1): cpitch = inc11(cpitch) if random.randint(0,1): cdur = inc11(cdur) totaldur = totaldur + len midi.update_time(0) midi.end_of_track() # track 2 - bass random.seed() cpitch, camp, cdur, totaldur, octdev = 0, 0, 0, 0, 0 basepitch = 60 - 12 - 12 # two octaves down midi.update_time(new_time=0, relative=0) midi.start_of_track(1) while totaldur < (192 * generate): len = basedur * (rsix[cdur] + 1) pitch = basepitch + rsix[cpitch] midi.update_time(0) midi.note_on(channel=0, note=pitch, velocity=100) midi.update_time(len) midi.note_off(channel=0, note=pitch, velocity=100) if random.randint(0,2): cpitch = inc11(cpitch) if random.randint(0,2): cdur = inc11(cdur) totaldur = totaldur + len midi.update_time(0) midi.end_of_track() # track 3 - chords random.seed() cpitch, camp, cdur, totaldur, octdev = 0, 0, 0, 0, 0 basepitch = 60 basedur = basedur # some long chords. yum. midi.update_time(new_time=0, relative=0) midi.start_of_track(2) while totaldur < (192 * generate): len = basedur * (inver[cdur] + 1) howmany = range(random.randint(2,5)) pitches = [] start = len for x in howmany: pitch = basepitch + inver[cpitch] pitches = pitches + [pitch] midi.update_time(start) start=0 midi.note_on(channel=0, note=pitch, velocity=100) cpitch = inc11(cpitch) to = len for pitch in pitches: midi.update_time(to) to = 0 midi.note_off(channel=0, note=pitch) cdur = inc11(cdur) totaldur = totaldur + len midi.update_time(0) midi.end_of_track() midi.eof()