I am trying to emulate a piano in python using mingus as suggested in this question. I am running Ubuntu 14.04, and have already created an audio group and added myself to it. I am using alsa.
I ran the code given in one of the answers to the aforementioned question and it ran fine in shell mode. However, when I wrote a python script and tried to run it, I did not get any sound whatsoever. Here is my code:
#!/usr/bin/env python
from mingus.midi import fluidsynth
DEF_FONT_PATH = '/usr/share/sounds/sf2/FluidR3_GM.sf2'
def main():
fluidsynth.init(DEF_FONT_PATH, 'alsa')
fluidsynth.play_Note(80, 0, 80)
if __name__ == '__main__':
main()
I have checked many other answers, and I cannot seem to find a solution.
I managed to solve it.
It seems the problem was, as I had previously suspected, that it was necessary to wait for some time after calling
fluidsynth.init
. However, since a simpletime.sleep()
had not been able to fix this, I had discarded this possibility.After prompting the user to provide some input to trigger the playing of the note, the sounds play fine.
I am, however, still unsure as to why a delay is required.
I am on a Mac and I ran into the same problem.
Using
time.sleep()
is indeed the correct answer but it does not seem to me that it has to do with waiting forfluidsynth.init
to finish, instead usetime.sleep()
after afluidsynth.play_Note()
or afluidsynth.play_NoteContainer()
call and it should play.While I wish this was mentioned somewhere in the mingus documentation at, https://bspaans.github.io/python-mingus/, they do have examples in GitHub that show that this is exactly what they are doing in order to play the notes.
This example is particularly helpful being that just about every call to
fluidsynth.play_Note()
is followed up by atime.sleep()
call:https://github.com/bspaans/python-mingus/blob/master/mingus_examples/play_progression/play-progression.py