Exemplo n.º 1
0
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()
Exemplo n.º 2
0
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()
Exemplo n.º 3
0
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
Exemplo n.º 4
0
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
Exemplo n.º 5
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)
midi.update_time(0)
midi.note_off(channel=0, note=0x44)
midi.update_time(0)
midi.note_off(channel=0, note=0x45)
midi.update_time(0)
midi.note_off(channel=0, note=0x46)
midi.update_time(0)
midi.note_off(channel=0, note=0x47)
midi.update_time(0)
midi.note_off(channel=0, note=0x48)
midi.update_time(0)
midi.note_off(channel=0, note=0x49)
midi.update_time(0)


# non optional midi framework
midi.update_time(0)
midi.end_of_track()

midi.eof()
Exemplo n.º 6
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!

midi.eof()

midi_in = MidiInFile(event_handler, in_file)
midi_in.read()
Exemplo n.º 7
0

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!

midi.eof()


midi_in = MidiInFile(event_handler, in_file)
midi_in.read()


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 = 'midiout/minimal_type0.mid'
midi = MidiOutFile(out_file)

# non optional midi framework
midi.header()
midi.start_of_track() 


# musical events

midi.update_time(0)
midi.note_on(channel=0, note=0x40)

midi.update_time(192)
midi.note_off(channel=0, note=0x40)


# non optional midi framework
midi.update_time(0)
midi.end_of_track()

midi.eof()