Esempio n. 1
0
def changeRhythm(fname,result_name):
    """
        Change rhythm of two instruments
    """
    mid = MidiFile(fname)
    messages = []
    
    with MidiFile() as new_mid:
        new_mid.ticks_per_beat = 960
        new_mid.tracks.append(mid.tracks[0])

        #track1
        new_track1 = MidiTrack()
        for message in mid.tracks[1]:
            if message.type == 'note_off':
                new_track1.append(mido.Message('note_off',note=message.note,velocity=message.velocity,time=message.time/2))
            elif message.type == 'note_on':
                new_track1.append(mido.Message('note_on',note=message.note,velocity=message.velocity,time=0))
            else:
                new_track1.append(message)
        new_mid.tracks.append(new_track1)

        #track2
        new_track2 = MidiTrack()
        for message in mid.tracks[2]:
            if message.type == 'note_off':
                new_track2.append(mido.Message('note_off',note=message.note,velocity=message.velocity,time=message.time*2))
            elif message.type == 'note_on':
                new_track2.append(mido.Message('note_on',note=message.note,velocity=message.velocity,time=0))
            else:
                new_track2.append(message)
        new_mid.tracks.append(new_track2)

        new_mid.save(result_name)
Esempio n. 2
0
def half_up_every_two(fname,result_name):
    """
        Raises every second note by half step
    """
    mid = MidiFile(fname)
    messages = []
    for message in mid.tracks[1]:
        if not isinstance(message, MetaMessage):
            messages.append(message)

    with MidiFile() as new_mid:
        track = MidiTrack()
        new_mid.tracks.append(mid.tracks[0]) #append first track with all meta data
        new_mid.tracks.append(track)

    new_messages = []
    note_counter = 0
    for message in messages:
        if message.type == 'note_off':
            continue
        elif message.type == 'note_on':
            if note_counter % 2 == 0:
                new_messages.append(mido.Message('note_on', note=message.note+1, velocity=message.velocity, time=960))
                new_messages.append(mido.Message('note_off', note=message.note+1, velocity=message.velocity, time=960))
                note_counter += 1
            else:
                new_messages.append(mido.Message('note_on', note=message.note, velocity=message.velocity, time=960))
                new_messages.append(mido.Message('note_off', note=message.note, velocity=message.velocity, time=960))
                note_counter += 1           
 
    for message in new_messages:
        track.append(message)

    new_mid.save(result_name)
Esempio n. 3
0
def changeGuitar(fname,result_name):
    """
        Changes first 100 notes of guitar by raising half step
    """
    mid = MidiFile(fname)
    
    with MidiFile() as new_mid:
        new_mid.ticks_per_beat = 960
        new_mid.tracks.append(mid.tracks[0])

        guitar_track = mid.tracks[1]
        new_track = MidiTrack()
        note_counter = 0
        #go through first 100 notes and raise notes by half step
        for i in range(len(guitar_track)):
            if note_counter<100:
                if guitar_track[i].type == 'note_on':
                    new_track.append(mido.Message('note_on',note=guitar_track[i].note+1,velocity=guitar_track[i].velocity,time=0))
                    note_counter += 1
                elif guitar_track[i].type == 'note_off':
                    new_track.append(mido.Message('note_off',note=guitar_track[i].note+1,velocity=guitar_track[i].velocity,time=guitar_track[i].time))
                else:
                    new_track.append(guitar_track[i])
            else:
                new_track.append(guitar_track[i])
        new_mid.tracks.append(new_track)
        new_mid.save(result_name)
Esempio n. 4
0
def changePercussion(fname,result_name):
    mid = MidiFile(fname)

    with MidiFile() as new_mid:
        new_mid.ticks_per_beat = 960
        new_mid.tracks.append(mid.tracks[0])
        
        #change marimba (track 3), raise each note by half step, speed up by factor of 2
        marimba = mid.tracks[3]
        new_marimba = MidiTrack()
        for message in marimba:
            if message.type == 'note_on':
                new_marimba.append(mido.Message('note_on',note=message.note+1, velocity=message.velocity, time=message.time))
            elif message.type == 'note_off':
                new_marimba.append(mido.Message('note_off', note=message.note+1, velocity=message.velocity, time=message.time/2))
            else:
                new_marimba.append(message)
        new_mid.tracks.append(new_marimba)

        #change snare drum (track 1), speed it by factor of 4
        snare_drum = mid.tracks[1]
        new_snare_drum = MidiTrack()
        for message in snare_drum:
            if message.type == 'note_on':
                new_snare_drum.append(mido.Message('note_on', note=message.note, velocity=message.velocity, time=message.time,channel=9))
            elif message.type == 'note_off':
                new_snare_drum.append(mido.Message('note_off', note=message.note, velocity=message.velocity, time=message.time/4, channel=9))
            else:
                new_snare_drum.append(message)
        new_mid.tracks.append(new_snare_drum)
              
        #change cowbell (track 2), slow it by a factor of 2
        cowbell = mid.tracks[2]
        new_cowbell = MidiTrack()
        for message in cowbell:
            if message.type == 'track_name':
                new_cowbell.append(MetaMessage('track_name', name=u'Triangle'))
            elif message.type == 'note_on':
                new_cowbell.append(mido.Message('note_on', note=81, velocity=message.velocity, time=message.time,channel=9))
            elif message.type == 'note_off':
                new_cowbell.append(mido.Message('note_off', note=81, velocity=message.velocity, time=message.time*2, channel=9))
            else:
                new_cowbell.append(message)
        new_mid.tracks.append(new_cowbell)
                 

        new_mid.save(result_name)
Esempio n. 5
0
    def write_file(self, filename=None):
        with MidiFile() as midi_song:
            unused_notes = []
            current_time = 0
            new_track = MidiTrack()
            new_track.append(
                mido.Message('program_change', channel=1, program=29, time=5))
            new_track.append(
                mido.Message('program_change', channel=2, program=30, time=5))
            new_track.append(
                mido.Message('program_change', channel=3, program=31, time=5))
            new_track.append(
                mido.Message('program_change', channel=4, program=32, time=5))
            new_track.append(
                mido.Message('program_change', channel=5, program=33, time=5))
            new_track.append(
                mido.Message('program_change', channel=6, program=34, time=5))
            new_track.append(
                mido.Message('program_change', channel=7, program=35, time=5))
            new_track.append(
                mido.Message('program_change', channel=8, program=36, time=5))
            new_track.append(
                mido.Message('program_change', channel=9, program=37, time=5))

            for note in self.notes:
                note.absolute_start = current_time + note.time_delta
                note_start = note.absolute_start

                best_end = float('inf')
                best_end_note = None

                for unused_note in unused_notes:
                    this_end = unused_note.get_absolute_end()

                    if this_end < best_end:
                        best_end = this_end
                        best_end_note = unused_note

                if best_end < note_start:
                    new_track.append(
                        best_end_note.get_note_off(best_end - current_time))
                    unused_notes.remove(note)
                    current_time = best_end
                else:
                    new_track.append(note.get_note_on())
                    unused_notes.append(note)
                    current_time = note_start

            midi_song.tracks.append(new_track)
            midi_song.save(filename)
Esempio n. 6
0
def create_midi_file_with_notes(filename, notes, bpm):
    with MidiFile() as midifile:
        track = MidiTrack()
        midifile.tracks.append(track)

        track.append(Message('program_change', program=12, time=0))

        tempo = int((60.0 / bpm) * 1000000)
        track.append(MetaMessage('set_tempo', tempo=tempo))

        sec_per_tick = tempo / 1000000.0 / midifile.ticks_per_beat
        add_notes(track, notes, sec_per_tick)

        midifile.save('{}.mid'.format(filename))
Esempio n. 7
0
def indiv_rhythm_change(fname,result_name):
    """
        Change the rhythm of individual note randomly.
    """   
    mid = MidiFile(fname)
    rhythm_conversion = [0.25,0.5,1,2,4] #rhythm change factor   
 
    with MidiFile() as new_mid:
        new_mid.tricks_per_beat = 960
        new_mid.tracks.append(mid.tracks[0])

    #track1
    new_track1 = MidiTrack()
    new_track1.append(MetaMessage('time_signature',numerator=3,denominator=4))
    for message in mid.tracks[1]:
        if message.type == 'note_off':
            new_track1.append(mido.Message('note_off',note=message.note,velocity=message.velocity,time=int(message.time*random.choice(rhythm_conversion))))
        elif message.type == 'note_on':
            new_track1.append(mido.Message('note_on',note=message.note,velocity=message.velocity,time=message.time))
        else:
            new_track1.append(message)
    new_mid.tracks.append(new_track1)

    #track2
    new_track2 = MidiTrack()
    for message in mid.tracks[2]:
        if message.type == 'note_off':
            new_track2.append(mido.Message('note_off',note=message.note,velocity=message.velocity,time=int(message.time*random.choice(rhythm_conversion))))
        elif message.type == 'note_on':
            new_track2.append(mido.Message('note_on',note=message.note,velocity=message.velocity,time=message.time))
        else:
            new_track2.append(message)
    new_mid.tracks.append(new_track2)
    

    new_mid.save(result_name)
Esempio n. 8
0
def transpose2(fname,result_name):
    """
        Transpose two instruments up by M2
    """
    mid = MidiFile(fname)
    messages = []
    
    with MidiFile() as new_mid:
        new_mid.ticks_per_beat = 960
        new_mid.tracks.append(mid.tracks[0])
        for track in mid.tracks[1:]:
            new_track = MidiTrack()
            for message in track:
                if message.type == 'note_off':
                    continue
                elif message.type == 'note_on':
                    new_track.append(mido.Message('note_on',note=message.note+2,velocity=message.velocity,time=0))
                    new_track.append(mido.Message('note_off',note=message.note+2,velocity=message.velocity,time=960))
                else:
                    new_track.append(message)
            new_mid.tracks.append(new_track)
        new_mid.save(result_name)
Esempio n. 9
0
def reverse_mid(fname,result_name):

    mid = MidiFile(fname)

    """
    for i,track in enumerate(mid.tracks):
        print('Track {}: {}'.format(i, track.name))
        for message in track:
            if not isinstance(message, MetaMessage):
                print(message)  
    """
    messages = []
    for message in mid.tracks[1]:
        if not isinstance(message, MetaMessage):
            messages.append(message)

    with MidiFile() as new_mid:
        track = MidiTrack()
        new_mid.tracks.append(mid.tracks[0]) #append first track with all meta data
        new_mid.tracks.append(track)
        reversed_msg = []
        
        msg_velocity = 0
        msg_time = 960
        for message in messages:
            if message.type == 'note_on':
                new_message = mido.Message('note_off', note=message.note,velocity=0, time=msg_time) 
                reversed_msg.insert(0,new_message)
                msg_velocity = message.velocity #update velocity
            elif message.type == 'note_off':
                new_message = mido.Message('note_on', note=message.note, velocity=msg_velocity, time=0)
                reversed_msg.insert(0,new_message)
            else:
                continue
       
        for message in reversed_msg:
            track.append(message)
        
        new_mid.save(result_name)
Esempio n. 10
0
from mido import MidiFile
from mido.midifiles import MidiTrack

with MidiFile() as new_mid:
    new_track = MidiTrack()

    mid = MidiFile('midi/someone-like-you.mid')

    print('mid', mid)
    for i, track in enumerate(mid.tracks):
        print('len(track)', len(track))
        for message in track:
            print('message', message)
            new_track.append(message)

    new_mid.tracks.append(new_track)

    print('ALL TRACKS APPENDED')
    new_mid.save('new_song.mid')
Esempio n. 11
0
from mido.midifiles import MidiFile, MidiTrack, MetaMessage
from mido.messages import Message
"""
    track.append(Message('note_on', note=value_code, velocity=80, time=32))
    track.append(Message('note_on', note=value_code, velocity=0, time=32))
"""

if __name__ == '__main__':
    out_path = "midi_files/Test1.mid"

    mid = MidiFile()
    track = MidiTrack()
    mid.tracks.append(track)

    # Tempo
    track.append(MetaMessage('set_tempo', tempo=800000, time=0))

    # Start A
    track.append(Message('note_on', note=23, velocity=80, time=0))
    # End A
    track.append(Message('note_on', note=23, velocity=0, time=560))

    # Start B
    track.append(Message('note_on', note=56, velocity=80, time=40))
    # End B
    track.append(Message('note_on', note=56, velocity=0, time=100))

    # Tempo
    track.append(MetaMessage('set_tempo', tempo=500000, time=0))

    # Start C
import mido
from mido import MidiFile
from mido.midifiles import MidiTrack
from mido import Message

#pattern = MidiFile('Songs/Suteki-Da-Ne.mid')
pattern = MidiFile('Songs/twinkle_twinkle.mid')
mid = MidiFile()

tracks = MidiTrack()
tracks.append(tracks)
'''
for message in pattern:
    
    
    if message.type == 'note_on' or message.type == 'note_off':
        #print message
        mid.tracks.append(mid.Message(message.type, note=message.note, velocity=message.velocity, time=message.time))
    #elif message.type == 'control_change':
    #    mid.tracks.append(Message(message.type, control=message.control, value=message.value, time=message.time))
    
    #else:
    #    print message
    #    print message.type
    
    
    #tracks.append(Message(message.type, note=message.note, velocity=message.velocity, time=message.time))
    #tracks.append(message)

'''