midi_processor.py#

midi_processor.py#

This file provides functionalities to process and play MIDI files using MIDO.

Classes:

MIDIProcessor

Functions:

main

Authors: Devin Martin and Wesley Jake Anding

class midi_processor.MIDIProcessor(file_path)#

Bases: object

A class that processes MIDI files and plays the tracks in real time.

Attributes#

file_pathstr

The path to the MIDI file.

midi_fileMidiFile

The MidiFile object representing the MIDI file.

extract_global_tempo()#

Extracts global tempo changes from the MIDI file, storing them in our list self.global_tempo_changes. This list is used to accurately apply tempo changes to each track message. Helper function for extract_track_messages.

extract_track_messages(track_number)#

Extracts messages from the specified track, applying global tempo changes accurately, calculating the correct delays for each event, ensuring the playback tempo feels correct.

Parameters#

track_numberint

The track number to extract messages from. (0-indexed)

Returns#

list

A list of (message, delay) tuples representing the messages in the track.

play_track(messages, outport)#

Plays messages from a MIDI track in real time. This is used for testing our extract_track_messages function. We can listen to how our interpretation of the MIDI file sounds compared to the original. THIS METHOD IS SLIGHTLY INNACURATE DUE TO THE FACT WE ARE USING TIME.SLEEP TO DELAY THE MESSAGES; HOWEVER, IT IS USEFUL FOR TESTING AND DEBUGGING. THE SONGS SHOULD SOUND SIMILAR, BUT NOT EXACTLY THE SAME… BUT CORRECT IN GAME!

To hear the song perfectly, use the test_midi_file.py script.

Parameters#

messageslist

A list of (message, delay) tuples representing the messages in the track. Obtain this list using the extract_track_messages function.

outportstr

The MIDI output port name.

read_midi_file()#

Reads a MIDI file and returns the MidiFile object.

Returns#

MidiFile

The MidiFile object representing the MIDI file, or None if an error occurs.