Skip to content

Instantly share code, notes, and snippets.

@damp11113
Created March 25, 2023 03:50
Show Gist options
  • Save damp11113/2488baf7cd1f316f99b61a53e987e973 to your computer and use it in GitHub Desktop.
Save damp11113/2488baf7cd1f316f99b61a53e987e973 to your computer and use it in GitHub Desktop.
Python MIDI Player with built in synthesizer
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