def load_file(filename): pygame.midi.init() player = pygame.midi.Output(0) pattern = midi.read_midifile(filename) pattern.make_ticks_abs() events = [] for track in pattern: for event in track: events.append(event) events.sort() delta = 0 tickTime = 0 secPerTick = .0005 # set it until we see a tempo event for event in events: delta = event.tick - tickTime tickTime = event.tick if delta: time.sleep(delta * secPerTick) if isinstance(event, midi.NoteOnEvent): player.note_on(event.pitch, event.velocity, event.channel) elif isinstance(event, midi.NoteOffEvent): player.note_off(event.pitch, event.velocity, event.channel) elif isinstance(event, midi.ProgramChangeEvent): player.set_instrument(event.value, event.channel) elif isinstance(event, midi.SetTempoEvent): # convert tempo to secPerTick mpb = 60.0 * 1000000.0 / event.bpm mpt = mpb / pattern.resolution secPerTick = mpt / 1000000.0 player.close()
def create_tracks(midifile): z = midi.read_midifile(midifile) z.make_ticks_abs() data=[] for i in range(0,len(z)): data.append(data_from_midi(z[i])) data = filter(lambda x: len(x)!=0,data) pprint(z[0]) return data
def load_file(self, filename): self.pattern = midi.read_midifile(filename) self.pattern.make_ticks_abs() self.inistialResolution = self.pattern.resolution self.events = [] for track in self.pattern: for event in track: self.events.append(event) self.events.sort()
def read(self, filename="Steinway Grand Piano.mid"): pattern = midi.read_midifile("midi/%s" % filename) #print(pattern) track = pattern[0] note_events = [] for e in track[2:-1]: time, note, attack, on = e.tick, e.data[0], e.data[1], None if e.name == 'Note On': on = True elif e.name == 'Note Off': on = False n = {"time": time, "note": note, "attack": attack, "on": on} note_events.append(n) #note_events = sorted(note_events, key=lambda k: k['time']) #pprint(note_events) total_time = 0 for n in note_events: total_time += n["time"] n["time"] = total_time #pprint(note_events) on_events = [] off_events = [] for n in note_events: if n["on"]: on_events.append(n) else: off_events.append(n) events = [] for n in on_events: start, note, attack = n["time"], n["note"], n["attack"] for i, m in enumerate(off_events): if m["note"] == note: off_events.pop(i) #print(m["time"]) duration = m["time"] - start break new = {"start": start, "note": note, "attack": attack, "duration": duration} events.append(new) #pprint(events) return self.extract_information(events)
def get_clock_divisor(midifile): z = midi.read_midifile(midifile) return z[0][1].mpqn/(z.resolution*1000.)