Example #1
0
 def sequence_name(self, text):
     """
     Sequence/track name
     text: string
     """
     try:
         text = text.decode(self.from_enc).encode(self.to_enc)
     except UnicodeDecodeError:
         print >> sys.stderr, "Error decoding", text
         pass
     except UnicodeEncodeError:
         text = text.decode(self.from_enc).encode(self.to_enc, "replace")
         text = text.replace("?", "_")
     MidiOutFile.sequence_name(self, text)
Example #2
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
Example #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
 def note_off(self, channel=0, note=0x40, velocity=0x40):
     note = self._transp(channel, note)
     MidiOutFile.note_off(self, channel, note, velocity)
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()
Example #6
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()
Example #7
0
        if channel == 0:
            print channel, note, velocity, self.abs_time(), self.currTime
            self.notes[note] = self.currTime * 1.0

    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)
Example #8
0
 def update_time(self, new_time, relative):
     if new_time > self.time_to_move:
         new_time -= self.time_to_move
     MidiOutFile.update_time(self, new_time, relative)
Example #9
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()
 def note_off(self, channel=0, note=0x40, velocity=0x40):
     note = self._transp(channel, note)
     MidiOutFile.note_off(self, channel, note, velocity)
Example #11
0
 def createFile(self, filename):
     self.file = MidiOutFile(filename)
     self.file.header()
     self.file.start_of_track()
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()
Example #13
0
from MidiOutFile import MidiOutFile
from midistuff.constants import *

"""
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() 

Example #14
0
 def note_on(self, channel=0, note=0x40, velocity=0x40):
     #note = self._transp(channel, note)
     note = self.reduct_note(channel, note)
     velocity = self.reduct_velocity(channel, velocity)
     MidiOutFile.note_on(self, channel, note, velocity)
Example #15
0
 def sysex_event(self, data):
     MidiOutFile.system_exclusive(self, data)
Example #16
0
 def createFile(self, filename):
     self.file = MidiOutFile(filename)
     self.file.header()
     self.file.start_of_track()
Example #17
0
 def __init__(self, raw_out="", from_enc="sjis", to_enc="gbk"):
     MidiOutFile.__init__(self, raw_out)
     self.from_enc = from_enc
     self.to_enc = to_enc
Example #18
0
        if channel == 0:
            print channel, note, velocity, self.abs_time(), self.currTime
            self.notes[note]=self.currTime*1.0

    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)
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)
Example #20
0
 def __init__(self, raw_out="", channels=None, time_to_move=0):
     MidiOutFile.__init__(self, raw_out, channels)
     self.time_to_move = time_to_move