Last active
July 23, 2022 14:08
-
-
Save nemanjan00/591bc3aedbb09a6f8fb623e862232870 to your computer and use it in GitHub Desktop.
MIDI to FMF (flipper music file)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mido import MidiFile | |
import math | |
NOTES = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'] | |
OCTAVES = list(range(11)) | |
NOTES_IN_OCTAVE = len(NOTES) | |
def number_to_note(number: int) -> tuple: | |
octave = number // NOTES_IN_OCTAVE | |
note = NOTES[number % NOTES_IN_OCTAVE] | |
return note, octave | |
mid = MidiFile('./HarryPotter.mid', clip=True) | |
notes = "" | |
bpm = 130 | |
for msg in mid: | |
msg_dict = msg.dict() | |
if msg_dict["type"] == "set_tempo": | |
bpm = math.floor(60000000 / msg_dict["tempo"]) | |
if msg_dict["type"] == "note_on" and msg_dict["time"] != 0: | |
pause = str(round(3 / msg_dict["time"])) | |
notes = notes + pause + "P, " | |
if msg_dict["type"] == "note_off": | |
pause = str(round(3 / msg_dict["time"])) | |
note = number_to_note(msg_dict["note"]) | |
notes = notes + pause + note[0] + str(note[1]) + ", " | |
notes = notes[:-2] | |
format = """Filetype: Flipper Music Format | |
Version: 0 | |
BPM: """ + str(bpm) + """ | |
Duration: 12 | |
Octave: 5 | |
Notes: """ + notes | |
print(format) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment