Example #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 range(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 #2
0
    def writeToMidi(self, filepath, eventTracks=None):
        if eventTracks == None:
            eventTracks = self.getEventTracks()

        midi = MidiOutFile(filepath)
        midi.header(1, len(self.tracks), 480)  # TODO 480?

        for i in range(len(self.tracks)):
            track = self.tracks[i]
            events = eventTracks[i]
            midi.start_of_track(i)
            midi.tempo(60000000 / self.tempo)  # int(60,000,000.00 / BPM)
            midi.time_signature(self.beatsPerMeasure, 2, 24, 8)  # TODO: ?

            midi.patch_change(i, track.instrument)

            lastPos = 0
            positions = events.keys()
            positions.sort()
            for pos in positions:
                eventArray = events[pos]
                deltaPos = pos - lastPos
                midiTime = int(float(deltaPos) * 300)  # TODO: wtf
                for event in eventArray:
                    midi.update_time(midiTime)
                    pitch = self.noteToPitch(event.note)
                    if event.type == "on":  # TODO on/off
                        midi.note_on(channel=i, note=pitch, velocity=event.note.velocity)
                    else:
                        midi.note_off(channel=i, note=pitch)
                    midiTime = 0  # remainder of events are simultaneous
                lastPos = pos
            midi.update_time(0)
            midi.end_of_track()
        midi.eof()
Example #3
0
def nptensor_to_midi(x_data, out_filename):
    midi = MidiOutFile(out_filename)

    config = nn_config.get_neural_net_configuration()
    model_basename = config['model_basename']
    hidden_dims = config['hidden_dimension_size']
    num_dims = config['num_dims']

    # non optional midi framework
    midi.header(format=0, nTracks=1, division=10)
    midi.start_of_track()

    midi.tempo(500000)

    # musical events

    no = 0
    vel = 0
    last_event_time = 0
    now_playing_note = []
    num_seq_len = len(x_data)
    time_table = [2, 5, 10, 15, 20, 30, 40]

    for i in range(num_seq_len):
        note_vector_index = np.argwhere(x_data[i] == 1)

        if x_data[i][num_dims - 1] == 1:
            no = 0
        else:
            for one_note in note_vector_index:
                time = int(one_note / 720)  #no, vel
                vel = (int(one_note % 720 / 80) + 1) * 10
                no = int(one_note) % 720 % 80 + 21
                midi.update_time(last_event_time)
                midi.note_on(channel=0, note=no, velocity=vel)
                last_event_time = 0
                now_playing_note.append([i, no, time_table[time]])

        for now_note in now_playing_note:
            if i - now_note[0] == now_note[2]:
                midi.update_time(last_event_time)
                midi.note_off(channel=0, note=now_note[1])
                last_event_time = 0
                del now_note

        last_event_time += 1

    # non optional midi framework
    midi.update_time(0)

    midi.end_of_track()  # not optional!

    midi.eof()

    print('good')
Example #4
0
    def write_line_to_midi(self, out_file = 'midiout.mid'):
        """Creates a midi file and write current line to it"""
        midi = MidiOutFile(out_file)

        #format: 0, nTracks: 1, division: 480
        #----------------------------------
        #
        #Start - track #0
        #sequence_name: Type 0
        #tempo: 500000
        #time_signature: 4 2 24 8
        #note_on  - ch:00,  note:48,  vel:64 time:0
        #note_off - ch:00,  note:48,  vel:40 time:480
        #End of track
        #
        #End of file


        midi.header(0, 1, 480)

        midi.start_of_track()
        midi.sequence_name('Type 0')
        midi.tempo(int(60000000. / self.info['bpm']))
        midi.time_signature(4, 2, 24, 8)
        ch = 0
        i = 0
        line = self.lines[self.line_nr]
        for i, segment in enumerate(line.segments[:-1]):
            midi.note_on(ch, segment.pitch+60, 0x64)
            #96 is 4x midi clock which is one hole note
            midi.update_time(96 * segment.duration)
            midi.note_off(ch, segment.pitch+60, 0x40)
            midi.update_time(96	 * (line.segments[i+1].time_stamp \
                            - segment.time_stamp - segment.duration))

        midi.note_on(ch, line.segments[-1].pitch+60, 0x64)
        midi.update_time(96 * line.segments[-1].duration)
        midi.note_off(ch, line.segments[-1].pitch+60, 0x40)
        midi.update_time(0)
        midi.end_of_track()
        midi.eof() # currently optional, should it do the write instead of write??
        midi.write()
Example #5
0
    def toFile(self, filename):

        print "to file", filename

        self.events = sorted(self.events, key=itemgetter(3, 0))

        # count number of unique tracks
        tracks = list(set([i[0] for i in self.events]))

        midi_out = MidiOutFile(filename)

        midi_out.header(nTracks=len(tracks), division=self.ppq)


        for i in tracks:
            midi_out.start_of_track(i)
            midi_out.patch_change(self.channelForTrack[i], self.patchForTrack[i])
            midi_out.update_time(0,relative=False)
            nl=[]
            # for this track, build a note list
            for t,p,s,d,v,channel,patch in [x[1:] for x in self.events if x[0] == i]:
                if t == 'note':
                    print "on", (0,p,s,v,channel,patch)
                    print "off", (1,p,s+d,0,channel,patch)
                    nl.append((0,p,s,v,channel,patch))
                    nl.append((1,p,s+d,0,channel,patch))
                elif t == 'tempo':
                    nl.append((2,p,s,0,channel,patch))
            # for this track, sort the note list by start time and send it to midi file writer
            # t is for type
            for t,p,s,v,channel,patch in sorted(nl,key=itemgetter(2)):
                midi_out.update_time(int(s*1000), relative=False)
                if   t == 0:
                    midi_out.note_on(channel=self.channelForTrack[i], note=p, velocity=v)
                    #print "note on event"
                elif t == 1: midi_out.note_off(channel=self.channelForTrack[i], note=p)
                elif t == 2:
                    print "tempo event"
                    midi_out.tempo(p)
            midi_out.end_of_track()
        midi_out.eof()
Example #6
0
 def toFile(self, fileName):
     self.events = sorted(self.events,key=itemgetter(3,0))
     tracks = list(set([i[0] for i in self.events]))
     midi_out = MidiOutFile(fileName)
     midi_out.header(nTracks=len(tracks),division=self.ppq)
     for i in tracks:
         midi_out.start_of_track(i)
         midi_out.update_time(0,relative=False)
         nl=[]
         for t,p,s,d,v in [x[1:] for x in self.events if x[0] == i]:
             if t == 'note':
                 nl.append((0,p,s,v))
                 nl.append((1,p,s+d,0))
             elif t == 'tempo':
                 nl.append((2,p,s,0))
         for t,p,s,v in sorted(nl,key=itemgetter(2)):
             midi_out.update_time(int(s*1000),relative=False)
             if   t == 0: midi_out.note_on (note=p, velocity=v)
             elif t == 1: midi_out.note_off(note=p)
             elif t == 2: midi_out.tempo(p)
         midi_out.end_of_track()
     midi_out.eof()
Example #7
0
    def write_tone_to_midi(self, line_nr, tone_nr, out_file = 'midiout.mid'):
        """Creates a midi file and write one tone to it

        Keyword arguments:
        line_nr  -- line_nr of the tone
        tone_nr  -- tone_nr in line
        out_file -- output midi file

        """
        midi = MidiOutFile(out_file)

        #format: 0, nTracks: 1, division: 480
        #----------------------------------
        #
        #Start - track #0
        #sequence_name: Type 0
        #tempo: 500000
        #time_signature: 4 2 24 8
        #note_on  - ch:00,  note:48,  vel:64 time:0
        #note_off - ch:00,  note:48,  vel:40 time:480
        #End of track
        #
        #End of file

        midi.header(0, 1, 480)
        midi.start_of_track()
        midi.sequence_name('Type 0')
        midi.tempo(int(60000000. / self.info['bpm']))
        midi.time_signature(4, 2, 24, 8)
        ch = 0
        i = 0
        midi.note_on(ch, self.lines[line_nr].segments[tone_nr].pitch+60, 0x64)
        midi.update_time(96*self.lines[line_nr].segments[tone_nr].duration)
        midi.note_off(ch, self.lines[line_nr].segments[tone_nr].pitch+60, 0x40)
        midi.update_time(0)
        midi.update_time(0)
        midi.end_of_track()
        midi.eof() # currently optional, should it do the write instead of write??
        midi.write()
Example #8
0
 def toFile(self, fileName):
     self.events = sorted(self.events,key=itemgetter(3,0))
     tracks = list(set([i[0] for i in self.events]))
     midi_out = MidiOutFile(fileName)
     midi_out.header(nTracks=len(tracks),division=self.ppq)
     for i in tracks:
         midi_out.start_of_track(i)
         midi_out.update_time(0,relative=False)
         nl=[]
         for t,p,s,d,v in [x[1:] for x in self.events if x[0] == i]:
             if t == 'note':
                 nl.append((0,p,s,v))
                 nl.append((1,p,s+d,0))
             elif t == 'tempo':
                 nl.append((2,p,s,0))
         for t,p,s,v in sorted(nl,key=itemgetter(2)):
             midi_out.update_time(int(s*1000),relative=False)
             if   t == 0: midi_out.note_on (note=p, velocity=v)
             elif t == 1: midi_out.note_off(note=p)
             elif t == 2: midi_out.tempo(p)
         midi_out.end_of_track()
     midi_out.eof()
Example #9
0
positions.sort()
print("melody:")
lastPos = 0
for pos in positions:
    rel = pos - lastPos
    midiTime = int(float(rel) / 16.0 * 192.0*8.0)

    output = "\n" * int(rel * 2 - 1)
    output += "%so" % str(" " * (melody[pos].number+7))
    print(output)
    
    #print("pos: %s note: %s midi: %s" % (str(pos), str(melody[pos]), str(midiTime)))
    midi.update_time(midiTime)
    midi.note_on(channel=0, note=noteToMidi(melody[pos]))

    midi.update_time(48)
    midi.note_off(channel=0, note=noteToMidi(melody[pos]))

    lastPos = pos


    
midi.update_time(0)
midi.end_of_track()

midi.eof() # currently optional, should it do the write instead of write??


os.system('timidity %s' % out_file)

Example #10
0
# non optional midi framework
midi.header(format=1, nTracks=1, division=1024)
midi.start_of_track()
midi.tempo(int(60000000.0/140))
midi.patch_change(0, 1)

# musical events

pen = MidiPen()

for gn in s:
    g =CurrentFont()[gn]
    midi.sequence_name(gn)
    midi.note_on(channel=9, note=36)
    midi.update_time(0)
    midi.note_off(channel=9, note=36)
    midi.update_time(0)
    g.draw(pen)
    print gn,

#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() # not optional!

midi.eof()
from midi.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()