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)
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)
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)
def setUpClass(cls): """Create new/empty MidiFile, populate it, save to `cls.bio`. """ with MidiFile() as midi: track = MidiTrack() midi.tracks.append(track) track.append(Message('program_change', program=12, time=0)) track.append(Message('note_on', note=64, velocity=64, time=32)) track.append(Message('note_off', note=64, velocity=127, time=128)) # midi.save('new_song.mid') midi.save(file=cls.bio)
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))
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)
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
tolerance = 0.8 pitch_o = pitch("yin", win_s, hop_s, samplerate) pitch_o.set_unit("midi") pitch_o.set_tolerance(tolerance) pitches = [] confidences = [] # Open file output_file_name = "./data/pitches.txt" output_file = open(output_file_name, "w") with MidiFile() as mid: track = MidiTrack() mid.tracks.append(track) track.append(mido.Message('program_change', program=12, time=0)) # total number of frames read total_frames = 0 time = -1 while True: samples, read = s() pitch = pitch_o(samples)[0] #pitch = int(round(pitch)) confidence = pitch_o.get_confidence() #if confidence < 0.8: pitch = 0. print("%f %f %f" % (total_frames / float(samplerate), pitch, confidence))
#print pitches import os.path from numpy import array, ma import matplotlib.pyplot as plt from demo_waveform_plot import get_waveform_plot, set_xlabels_sample2time skip = 1 pitches = array(pitches[skip:]) confidences = array(confidences[skip:]) times = [t * hop_s for t in range(len(pitches))] time = -1 with MidiFile() as mid: track = MidiTrack() mid.tracks.append(track) track.append(mido.Message('program_change', program=12, time=0)) for t in range(len(pitches)): # # Write times and cleaned_pitches to a file (to use feed to MIDI synthesizer) # output_file.write("%f\t%f\t%f\n" % (total_frames / float(samplerate), pitch, confidence)) # Note if time == -1: time = total_frames / float(samplerate) else: time = (total_frames / float(samplerate)) - time
from demo_waveform_plot import get_waveform_plot, set_xlabels_sample2time skip = 1 pitches = array(pitches[skip:]) confidences = array(confidences[skip:]) times = [t * hop_s for t in range(len(pitches))] time = -1 with MidiFile() as mid: track = MidiTrack() mid.tracks.append(track) track.append(mido.Message('program_change', program=12, time=0)) for t in range(len(pitches)): # # Write times and cleaned_pitches to a file (to use feed to MIDI synthesizer) # output_file.write("%f\t%f\t%f\n" % (total_frames / float(samplerate), pitch, confidence)) # Note if time == -1: time = total_frames / float(samplerate) else: time = (total_frames / float(samplerate)) - time
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) '''
def writeNoteListToMidiFile(self): with MidiFile(type=0) as mid: track = MidiTrack() mid.tracks.append(track) track.append(MetaMessage('set_tempo', tempo=mido.bpm2tempo(self.tempo))) track.append(MetaMessage('time_signature', numerator=4, denominator=4)) if self.noteList[0][0][0] == 144: track.append(Message('note_on', note=self.noteList[0][0][1], velocity=self.noteList[0][0][2], time=0)) for i in range(1, len(self.noteList)): noteEvent = self.noteList[i] lastEvent = self.noteList[i-1] if noteEvent[0][0] == 144: track.append(Message('note_on', note=noteEvent[0][1], velocity=noteEvent[0][2], \ time=(int(milliSecondsToTicks(noteEvent[1], mido.bpm2tempo(self.tempo), mid.ticks_per_beat))) - \ int(milliSecondsToTicks(lastEvent[1], mido.bpm2tempo(self.tempo), mid.ticks_per_beat)))) elif noteEvent[0][0] == 128: track.append(Message('note_off', note=noteEvent[0][1], velocity=noteEvent[0][2], \ time=(int(milliSecondsToTicks(noteEvent[1], mido.bpm2tempo(self.tempo), mid.ticks_per_beat))) - \ int(milliSecondsToTicks(lastEvent[1], mido.bpm2tempo(self.tempo), mid.ticks_per_beat)))) lickFileName = 'lick' + str(self.numberOfLicks) + '-' + str(self.tempo) mid.save(MIDI_DIR + lickFileName + '.mid') self.midiFileDisplayList.addItem(lickFileName) self.midiFileList.append((lickFileName, self.tempo)) self.numberOfLicks += 1 self.noteList = []
from mido import MidiFile from mido.midifiles import MidiTrack from mido import Message, MetaMessage import time import datetime with MidiFile() as new_mid: new_track = MidiTrack() filename = "generated-music/round2-2.txt" with open(filename) as f: new_track.append(MetaMessage('set_tempo', tempo=500000 * 3)) for line in f: parts = line.split() #c~0 n!1 v@2 t#3 if parts[0] == "pw": if abs(int(float(parts[2].split('=')[1]))) < 8191: new_track.append( Message('pitchwheel', channel=0, pitch=int(float(parts[1].split('*')[1])), time=int(float(parts[2].split('#')[1])))) elif parts[0] == "no": velocity = int(float(parts[2].split('%')[1])) velocity = velocity if velocity <= 127 else 127 t = int(float(parts[3].split('#')[1])) t = t if t <= 127 else 127 new_track.append(
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)
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)
from mido.midifiles import MidiTrack, MidiFile import mido with MidiFile() as mid: track = MidiTrack() mid.tracks.append(track) track.append(mido.Message('program_change', program=12, time=0)) # Note track.append(mido.Message('note_on', note=64, velocity=64, time=32)) track.append(mido.Message('note_off', note=64, velocity=127, time=1000)) track.append(mido.Message('note_on', note=64, velocity=64, time=32)) track.append(mido.Message('note_off', note=64, velocity=127, time=32)) track.append(mido.Message('note_on', note=64, velocity=64, time=32)) track.append(mido.Message('note_off', note=64, velocity=127, time=32)) track.append(mido.Message('note_on', note=64, velocity=64, time=32)) track.append(mido.Message('note_off', note=64, velocity=127, time=32)) track.append(mido.Message('note_on', note=64, velocity=64, time=32)) track.append(mido.Message('note_off', note=64, velocity=127, time=32)) track.append(mido.Message('note_on', note=64, velocity=64, time=32)) track.append(mido.Message('note_off', note=64, velocity=127, time=32)) track.append(mido.Message('note_on', note=64, velocity=64, time=32)) track.append(mido.Message('note_off', note=64, velocity=127, time=1000)) # Pause track.append(mido.Message('note_on', note=64, velocity=64, time=32)) track.append(mido.Message('note_off', note=64, velocity=127, time=32)) mid.save('new_song.mid')
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)
from mido import MidiFile from mido.midifiles import MidiTrack from mido import Message, MetaMessage import time import datetime with MidiFile() as new_mid: new_track = MidiTrack() filename = "generated-music/round2-2.txt" with open(filename) as f: new_track.append(MetaMessage('set_tempo', tempo=500000*3)) for line in f: parts = line.split() #c~0 n!1 v@2 t#3 if parts[0] == "pw": if abs(int(float(parts[2].split('=')[1]))) < 8191: new_track.append(Message('pitchwheel', channel=0, pitch=int(float(parts[1].split('*')[1])), time=int(float(parts[2].split('#')[1])))) elif parts[0] == "no": velocity = int(float(parts[2].split('%')[1])) velocity = velocity if velocity <= 127 else 127 t = int(float(parts[3].split('#')[1])) t = t if t <= 127 else 127 new_track.append(Message('note_on', channel=0, note=int(float(parts[1].split('!')[1])), velocity=velocity,
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')
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)
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)
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) """