nwhitehead / pyfluidsynth

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

A working code for Jupiter Notebooks/Google Colabs and Windows Env. #24

Closed asigalov61 closed 3 years ago

asigalov61 commented 4 years ago

Hey guys,

Since the fluidsynth and pyfluidsynth can't play output on Windows/Google Colabs, here is a little code that works just fine. Reposting here from fluidsynth issues as recommended by fluidsynth contributors. I hope it is ok.

Its actually more of a hack/workaround but it does the job. Enjoy :)

!pip install pyFluidSynth
!apt install fluidsynth #Pip does not work for some reason. Only apt works
!pip install midi2audio

!cp /usr/share/sounds/sf2/FluidR3_GM.sf2 /content/font.sf2

from midi2audio import FluidSynth
from google.colab import output
from IPython.display import display, Javascript, HTML, Audio

FluidSynth("/content/font.sf2").midi_to_audio('output_midi.mid','output_wav.wav')
# set the src and play
Audio("output_wav.wav")
albedozero commented 3 years ago

Finally got a chance to try this out on colab. In the second line of your code you have to use apt - the aptitude package manager - as this is installing the fluidsynth software synthesizer, which is a binary package written in C. The last line of your code gives me a "ValueError: could not convert string to float: 'output_wav.wav'" - I think you probably have to open the _outputwav.wav file first and read its contents into a list of floats, then pass that list to Audio().

This is interesting, but your python code doesn't actually use pyfluidsynth. You install it using pip on line 1 but you never import it in your python code - you've instead imported a FluidSynth module that's part of the midi2audio library.