nwhitehead / pyfluidsynth

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

Please implement gain (trivial patch included) #32

Closed gd2shoe closed 3 years ago

gd2shoe commented 3 years ago

Midi velocity doesn't just change volume of a rendered note. It also changes the attack. Having some way to change the volume without changing how the note sounds is important. As far as I can tell in my tinkering, this is as simple as exposing the functionality that fluidsynth already includes.

# If I understand what's going on correctly: 
...
fluid_synth_get_gain = cfunc('fluid_synth_get_gain', c_float,
                                    ('synth', c_void_p, 1))

fluid_synth_set_gain = cfunc('fluid_synth_set_gain', c_void_p,
                                    ('synth', c_void_p, 1),
                                    ('gain', c_float, 1))
...
class Synth:
...
    def get_gain(self) :
        return fluid_synth_get_gain(self.synth)
    def set_gain(self, gain) :
        return fluid_synth_set_gain(self.synth, float(gain))
...
albedozero commented 3 years ago

The way to do this is by changing the synth.gain setting using Synth.setting() e.g.

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

It seems like the FluidSynth team have been steadily moving individual get_XXX, set_XXX functions out of the public API in favor of using the fluid_settings_XXX functions, since these handle the synthesis thread in a safer, more consistent way. You can set any of the parameters in fluidsettings.xml using Synth.setting().

Incidentally, if you're trying to change overall volume mid-song/-performance, a better way is probably to send a value on CC 7 (MIDI volume).