nwhitehead/pyfluidsynth

pitch_bend doesn't work from Python

theosib opened this issue · 2 comments

I have fluid-synth installed from homebrew and pyfluidsynth installed from pip (plus I had to edit the pyfluidsynth code so it could find the fluid-synth library).

I have verified that fluid-synth itself supports pitch bending by using its command line tool and giving it commands.

But the pitch_bend API doesn't seem to work in python. Here's a code sample that demonstrates the problem. I should hear two different notes, but this plays the same one twice.

import time
import fluidsynth

fs = fluidsynth.Synth(samplerate=44100.0)
fs.start() 

sfid = fs.sfload('FluidR3_GM.sf2')  # replace path as needed
fs.program_select(0, sfid, 0, 0)

time.sleep(1.0)

fs.pitch_bend(0, 8192)
fs.noteon(0, 60, 127)

time.sleep(1.0)
fs.noteoff(0, 60)

fs.pitch_bend(0, 10000)
fs.noteon(0, 60, 127)

time.sleep(3.0)

In pull request #63 I added a new test, test/test4.py that shows pitch bend. It also fixes some bugs.

The main thing is that the pyfluidsynth pitch_bend measures from 0, so you pass 0 to get no pitch bend, 8191 to get maximal upwards bend, and -8192 to get maximal downwards pitch bend. So in your code above I would change the constants from 8192 and 10000 to 0 and 1808.

There was also a problem with out-of-range values. Prior to PR #63 if you gave out of range pitch bend values, fluidsynth would ignore them. The docstring suggested that 8192 was the maximum value, but actually 8191 is the maximum value. With PR #63 I fixed it to clamp the value to the legal range.

@theosib Could you try with updated values to see if you get the pitch bend?