def midiwrite(filename, piano_roll, r=(21, 109), dt=32, patch=0): midi = MidiOutFile(filename) midi.header(division=128) midi.start_of_track() midi.patch_change(channel=0, patch=patch) t = 0 samples = [i.nonzero()[0] + r[0] for i in piano_roll] for i in xrange(len(samples)): for f in samples[i]: if (i==0 or f not in samples[i-1] or i%4 == 0): midi.update_time(t) midi.note_on(channel=0, note=f, velocity=90) t = 0 t += int(dt) for f in samples[i]: if (i==len(samples)-1 or f not in samples[i+1] or i%4 == 3): midi.update_time(t) midi.note_off(channel=0, note=f, velocity=0) t = 0 midi.update_time(0) midi.end_of_track() midi.eof()
def midiwrite(filename, piano_roll, r=(21, 109), dt=0.2, patch=0): midi = MidiOutFile(filename) midi.header(division=100) midi.start_of_track() midi.patch_change(channel=0, patch=patch) t = 0 samples = [i.nonzero()[0] + r[0] for i in piano_roll] for i in xrange(len(samples)): for f in samples[i]: if i==0 or f not in samples[i-1]: midi.update_time(t) midi.note_on(channel=0, note=f, velocity=90) t = 0 t += int(dt*200) for f in samples[i]: if i==len(samples)-1 or f not in samples[i+1]: midi.update_time(t) midi.note_off(channel=0, note=f, velocity=0) t = 0 midi.update_time(0) midi.end_of_track() midi.eof()
class MidiWrapper: def __init__(self): self.file = None self.eventQueue = [] def createFile(self, filename): self.file = MidiOutFile(filename) self.file.header() self.file.start_of_track() def addNote(self, pitch, octave, time, duration, channel=0): if not self.file: print "Error - outfile has not been created yet! Use MidiWrapper.createFile(1) to make the file first." return file = self.file octaveOffset = ((octave + 1) * 12) - 3 # Middle C (C4) is 0x3C = 60 noteNum = self._pitchToHex(pitch) + octaveOffset self.eventQueue.append((time, "on", (noteNum, channel))) self.eventQueue.append((time + duration, "off", (noteNum, channel))) def _pitchToHex(self, pitch): choices = {"A": 0, "B": 2, "C": 3, "D": 5, "E": 7, "F": 8, "G": 10} accidental = None if len(pitch) > 1: accidental = pitch[1] pitch = pitch[0] return choices[pitch] + (1 if accidental else 0) def finalize(self): if not self.file: print "Error - outfile has not been created yet! Use MidiWrapper.createFile(1) to make the file first." return self.eventQueue.sort(cmp=lambda x, y: int.__cmp__(x[0], y[0])) file = self.file for tuple in self.eventQueue: time, op, dataTuple = tuple file.update_time(time, 0) if op == "on": file.update_time(time, 0) noteNum, channel = dataTuple file.note_on(channel, noteNum) else: noteNum, channel = dataTuple file.note_off(channel, noteNum) # non optional midi framework self.file.update_time(0) self.file.end_of_track() # not optional! self.file.eof() return self.file
class MidiWrapper: def __init__(self): self.file = None self.eventQueue = [] def createFile(self, filename): self.file = MidiOutFile(filename) self.file.header() self.file.start_of_track() def addNote(self, pitch, octave, time, duration, channel=0): if not self.file: print "Error - outfile has not been created yet! Use MidiWrapper.createFile(1) to make the file first." return file = self.file octaveOffset = ((octave + 1) * 12) - 3 # Middle C (C4) is 0x3C = 60 noteNum = self._pitchToHex(pitch) + octaveOffset self.eventQueue.append((time, "on", (noteNum, channel))) self.eventQueue.append((time+duration, "off", (noteNum, channel))) def _pitchToHex(self, pitch): choices = {"A": 0, "B": 2, "C":3, "D":5, "E":7, "F":8, "G":10} accidental = None if len(pitch) > 1: accidental = pitch[1] pitch = pitch[0] return choices[pitch] + (1 if accidental else 0) def finalize(self): if not self.file: print "Error - outfile has not been created yet! Use MidiWrapper.createFile(1) to make the file first." return self.eventQueue.sort(cmp=lambda x, y : int.__cmp__(x[0], y[0])) file = self.file for tuple in self.eventQueue: time, op, dataTuple = tuple file.update_time(time,0) if op == "on": file.update_time(time,0) noteNum, channel= dataTuple file.note_on(channel, noteNum) else: noteNum, channel = dataTuple file.note_off(channel, noteNum) # non optional midi framework self.file.update_time(0) self.file.end_of_track() # not optional! self.file.eof() return self.file
from MidiOutFile import MidiOutFile """ This is an example of the smallest possible type 0 midi file, where all the midi events are in the same track. """ out_file = '../minimal_type0.mid' midi = MidiOutFile(out_file) # non optional midi framework midi.header(format=1) midi.start_of_track() # musical events import ipdb;ipdb.set_trace() midi.update_time(10) midi.note_on(channel=0, note=0x40) midi.update_time(0) midi.note_on(channel=0, note=0x41) midi.update_time(192) midi.note_off(channel=0, note=0x40) midi.update_time(0) midi.note_off(channel=0, note=0x41) midi.update_time(0) midi.note_off(channel=0, note=0x42) midi.update_time(0) midi.note_off(channel=0, note=0x43)
def note_off(self, channel=0, note=0, velocity=0): if channel == 0: print "Off with note ", note, " with duration ", self.currTime - self.notes[ note] self.notes[note] = 0 event_handler = NoteOnPrinter() in_file = 'test.mid' out_file = 'test.mid' midi = MidiOutFile(out_file) # non optional midi framework midi.header(division=96) midi.start_of_track() # musical events midi.tempo(60000000 / 60) midi.update_time(0) midi.note_on(channel=0, note=69) midi.update_time(96) midi.note_on(channel=0, note=73) midi.update_time(96) midi.note_off(channel=0, note=69) midi.update_time(96) midi.note_off(channel=0, note=73) # non optional midi framework midi.update_time(0) midi.end_of_track() # not optional!
""" This is an example of the smallest possible type 0 midi file, where all the midi events are in the same track. """ out_file = '../prueba.mid' ntracks= 3 midi = MidiOutFile(out_file) # non optional midi framework midi.header(format= 1, nTracks=ntracks) # musical events #import ipdb;ipdb.set_trace() for i in xrange(0,ntracks): midi.start_of_track(i) #midi.continuous_controller(i,RESET_ALL_CONTROLLERS,0x01) #midi.continuous_controller(i,CHANNEL_VOLUME,0x32) midi.update_time(192*i) midi.note_on(channel=i, note=0x40) midi.update_time(192*(i+1)) midi.note_off(channel=i, note=0x40) #midi.update_time(0) midi.end_of_track() midi.eof()