FluidSynth() output volume is too low

936 Views Asked by At

in my python script i am using fluidSynth to convert .mid files to .wav files

fs = FluidSynth()
fs.midi_to_audio('myfile.mid', 'myfile.wav')

fluidSynth successfully converts myfile.mid to myfile.wav and saves the output, everything goes well.

the problem is the output volume is too low

i saw in fluidSynth documentation is that i can insert option -g or --gain to fluildSynth to increase the default volume of 0.2

so in the FluidSynth() instance, in the python script, how can i pass the -g option to increace output volume?!

any help would be much appreciated.

4

There are 4 best solutions below

0
On

Depending on the version of pyFluidSynth you can set the gain in different ways. I´m using this
fl = fluidsynth.Synth(20.0,44100) where 20.0 is the gain level. It's minimum level is 0.5.

This code plays a chord. You can set the gain level:

import time
import numpy
import pyaudio
import fluidsynth

pa = pyaudio.PyAudio()
strm = pa.open(
    format = pyaudio.paInt16,
    channels = 2, 
    rate = 44100, 
    output = True)

s = []

fl = fluidsynth.Synth(20.0,44100)
fl.set_chorus(1,5.0,3.0,10,1)

s = numpy.append(s, fl.get_samples(44100 * 1))

sfid = fl.sfload("yamahagrandpiano44.sf2")
fl.program_select(0, sfid, 0, 0)

fl.noteon(0, 60, 30)
fl.noteon(0, 67, 30)
fl.noteon(0, 76, 30)
fl.noteon(0, 96, 30)


s = numpy.append(s, fl.get_samples(44100 * 2))

fl.noteoff(0, 60)
fl.noteoff(0, 67)
fl.noteoff(0, 76)
fl.noteoff(0, 96)


s = numpy.append(s, fl.get_samples(44100 * 1))

fl.delete()

samps = fluidsynth.raw_audio_string(s)

print(len(samps))
print('Starting playback')
strm.write(samps)
0
On

It looks like you're using midi2audio, I'm not sure you're able to pass fluidsynth options in that. Since you have fluidsynth installed you could call it directly using the subprocess module and then you have access to all the options including gain.

Example setting the gain to 5:

subprocess.run(['fluidsynth', '-ni', '-g', '5', 'path to soundfont', 'myfile.mid', '-F', 'myfile.wav'])

This will make it louder. Gain isn't necessarily the same as volume but I don't understand enough about sound engineering to tell the difference.

1
On

This solution works very well. In your python code you also need to import subprocess subprocess is not built into python and needs an import to work successfull.

0
On

Using the pyFluidSynth library, it is possible to set the gain directly.

On your Synth instance, for example:

synth.setting('synth.gain', 0.6)

I am using it through sf2_loader and this solved my low volume issue.