No sound using fluidsynth and mingus except in shell
Asked Answered
V

3

2

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.

Vintage answered 13/12, 2015 at 18:18 Comment(0)
V
1

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 simple time.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.

Vintage answered 14/12, 2015 at 16:4 Comment(0)
P
1

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 for fluidsynth.init to finish, instead use time.sleep() after a fluidsynth.play_Note() or a fluidsynth.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 a time.sleep() call:

https://github.com/bspaans/python-mingus/blob/master/mingus_examples/play_progression/play-progression.py

Pye answered 20/2, 2017 at 5:36 Comment(0)
S
0

It's also possible with Bar:

#!/usr/bin/env python

from mingus.midi import fluidsynth
from mingus.containers import Note, Bar
from mingus.midi.fluidsynth import play_Bar
DEF_FONT_PATH = '/usr/share/sounds/sf2/FluidR3_GM.sf2'

def main():
    fluidsynth.init(DEF_FONT_PATH, 'alsa')

    note = Note("C-3",
                velocity=127)
    bar = Bar()
    bar.place_notes(notes=note,
                    duration=1)
    play_Bar(bar=bar,
             bpm=60)


if __name__ == '__main__':
    main()
Symbolist answered 13/5, 2023 at 4:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.