#!/usr/bin/python import string import os import time ########## play functions: def open_port(device_arg): global port port=open(device_arg,'w') def pitch_bend(channel, bend): global port low_byte = (bend & 127) high_byte = (bend >> 7) port.write("%c%c%c" % (0xE0+channel,low_byte,high_byte)) port.flush() def pb(channel, bend): global port # bend = int((bend - 8192.0)/128) port.write("%c%c%c" % (0xE0+channel,0x00, (bend % 128))) port.flush() def note_on(channel,note,volume): global port port.write("%c%c%c" % (0x90+channel, note, volume)) port.flush() def note_off(channel,note): global port port.write("%c%c%c" % (0x80+channel, note, 0)) port.flush() def all_notes_off(): global port for channel in range(16): port.write("%c%c%c" % (0xB0+channel, 123, 0)) port.flush() def close_port(): global port port.close() ############## standard midi file functions: def read_var_length(fp): output = 0 inbyte = ord(fp.read(1)) output = inbyte & 0x7f while inbyte & 0x80 == 0x80: inbyte = ord(fp.read(1)) output = (output << 7 ) + (inbyte & 0x7f) return output def write_var_length(var): from sys import stdout from string import join #### result goes into an array result_array = [] #### we have at least one value, right? : result_array.append(var & 0x7f) var >>= 7 ### shift right #### if 'var' still has any value greater than 0, #### 'and' it with 0x7f and then 'or' with 0x80, #### then put it out to result. Then we shift right #### for the next iteration: while var > 0x0: result_array.append((var & 0x7f) | 0x80) var >>= 7 result_array.reverse() ####for item in result_array: #### print hex(item) hex_string_array=[] for n in result_array: hex_string_array.append(chr(n)) return join(hex_string_array,'') ############## range functions: def bounce(index, max): if index < 0 or index > max: return max-(index%max) else: return index def see_saw(index, max): return bounce(index%max*2,max) ########### mf2t functions def midi_text_open(file,midi_file_type,num_channels, resolution): global text_file text_file=open(file, 'w') text_file.write("MFile %i %i %i\nMTrk\n" % (midi_file_type,num_channels, resolution)) def midi_text_close(): global text_file text_file.close() def midi_text_new_channel(): global text_file text_file.write("TrkEnd\nMTrk\n") def midi_text_final_trkend(): global text_file text_file.write("TrkEnd\n") def midi_text_pitch_bend(time, channel, bend): global text_file text_file.write("%i Pb ch=%i v=%i\n" % (time, channel+1, bend)) def midi_text_note_on(time, channel, note, volume): global text_file text_file.write("%i On ch=%i n=%i v=%i\n" % (time, channel+1, note, volume)) def midi_text_note_off(time, channel, note): global text_file text_file.write("%i Off ch=%i n=%i v=0\n" % (time, channel+1, note)) def midi_text_tempo(time, tempo): global text_file tempo = int((60.0/tempo)*1000000) text_file.write("%i Tempo %i\n" % (time,tempo)) def midi_text_program_change(time, channel, program): global text_file text_file.write("%i PrCh ch=%i p=%i\n" % (time,channel+1,program)) def midi_text_controller(time, channel, parameter, value): global text_file text_file.write("%i Par ch=%i c=%i v=%i\n" % (time, channel+1, parameter, value))