Created
March 25, 2023 03:50
-
-
Save damp11113/2488baf7cd1f316f99b61a53e987e973 to your computer and use it in GitHub Desktop.
Python MIDI Player with built in synthesizer
This file contains 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 time | |
import easygui | |
import fluidsynth | |
import os | |
os.system(f'title seclect midi file') | |
midi_file = easygui.fileopenbox(filetypes=['*.mid', '*.midi'], title='Select a MIDI file') | |
# check file type | |
try: | |
if midi_file.endswith('.mid'): | |
pass | |
elif midi_file.endswith('.midi'): | |
pass | |
else: | |
print('File type not supported') | |
exit() | |
except: | |
exit() | |
os.system(f'title init fluidsynth') | |
print('init fluidsynth') | |
fs = fluidsynth.Synth(samplerate=44100.0) | |
fs.start() | |
sfid = fs.sfload("your soundfont") | |
fs.program_select(1, sfid, 0, 0) | |
os.system(f'title reading midi file') | |
print('reading midi file...') | |
midi = MidiFile(midi_file, clip=True) | |
os.system(f'title playing midi file') | |
print('playing midi file...') | |
for i in midi: | |
if i.type == 'program_change': | |
fs.program_change(i.channel, i.program) | |
if i.type == 'note_on': | |
fs.noteon(i.channel, i.note, i.velocity) | |
elif i.type == 'note_off': | |
fs.noteoff(i.channel, i.note) | |
time.sleep(i.time) | |
os.system('title done') | |
os.system('pause') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment