nwhitehead / pyfluidsynth

Python bindings for FluidSynth
GNU Lesser General Public License v2.1
197 stars 56 forks source link

How to implement play_midi_file? #45

Closed emilycardwell closed 2 months ago

emilycardwell commented 1 year ago

I've tried the below code but only get '0' as output and no sound. I feel like I'm missing something but couldn't figure it out from the documentation.

fs = fluidsynth.Synth()
fs.start()

sfid = fs.sfload(sound1)
fs.program_select(0, sfid, 0, 0)

fs.play_midi_file(midi_path)
fs.play_midi_stop()

The below code did work for me:

fs = fluidsynth.Synth()
fs.start()

sfid = fs.sfload(sound1)
fs.program_select(0, sfid, 0, 0)

fs.noteon(0, 60, 100)
fs.noteon(0, 67, 100)
fs.noteon(0, 76, 100)

time.sleep(1.0)

fs.noteoff(0, 60)
fs.noteoff(0, 67)
fs.noteoff(0, 76)

time.sleep(1.0)
fs.delete()
astrolozano commented 11 months ago

Same problem, but discovered the problem is script ending soon. Avoid it with a sleep() or doing something else (playing notes, etc). This works for me.

fs = fluidsynth.Synth()
fs.start()
sfid = fs.sfload(sound1)

fs.play_midi_file(midi_path)
time.sleep(midi_duration) #or something else that avoid script end

Sorry if something is wrong, my first post on github and i'm not english-speaker.

cclauss commented 4 months ago

@emilycardwell Does the solution proposed by @astrolozano work for you?

nwhitehead commented 2 months ago

Closing this as inactive. Feel free to re-open if you think there is still an issue.