Skip to content

Commit

Permalink
Merge pull request #63 from nwhitehead/dev-fix-reverb
Browse files Browse the repository at this point in the history
Fix issues related to undefined names for set_chorus_speed and set_ch…
  • Loading branch information
nwhitehead authored Apr 29, 2024
2 parents 9a7332a + 989e739 commit 7dffe78
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 20 deletions.
54 changes: 34 additions & 20 deletions fluidsynth.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
from ctypes.util import find_library
import os

# A short circuited or expression to find the FluidSynth library
# (mostly needed for Windows distributions of libfluidsynth supplied with QSynth)

# DLL search method changed in Python 3.8
# https://docs.python.org/3/library/os.html#os.add_dll_directory
if hasattr(os, 'add_dll_directory'):
Expand All @@ -40,8 +37,10 @@
# Workaround bug in find_library, it doesn't recognize add_dll_directory
os.environ['PATH'] += ';C:\\tools\\fluidsynth\\bin'

# A function to find the FluidSynth library
# (mostly needed for Windows distributions of libfluidsynth supplied with QSynth)
def load_libfluidsynth(debug_print: bool = False) -> str:
"""
r"""
macOS X64:
* 'fluidsynth' was loaded as /usr/local/opt/fluid-synth/lib/libfluidsynth.dylib.
macOS ARM64:
Expand Down Expand Up @@ -80,7 +79,7 @@ def cfunc(name, result, *args):
return None

# Bump this up when changing the interface for users
api_version = '1.3.1'
api_version = '1.3.4'

# Function prototypes for C versions of functions

Expand Down Expand Up @@ -337,9 +336,18 @@ class fluid_synth_channel_info_t(Structure):
('synth', c_void_p, 1),
('level', c_double, 1))

fluid_synth_set_chorus_speed = cfunc('fluid_synth_set_chorus_speed', c_int,
('synth', c_void_p, 1),
('speed', c_double, 1))

fluid_synth_set_chorus_depth = cfunc('fluid_synth_set_chorus_depth', c_int,
('synth', c_void_p, 1),
('depth_ms', c_double, 1))

fluid_synth_set_chorus_type = cfunc('fluid_synth_set_chorus_type', c_int,
('synth', c_void_p, 1),
('type', c_int, 1))

fluid_synth_get_reverb_roomsize = cfunc('fluid_synth_get_reverb_roomsize', c_double,
('synth', c_void_p, 1))

Expand Down Expand Up @@ -769,8 +777,7 @@ def sfpreset_name(self, sfid, bank, prenum):
return None
return fluid_preset_get_name(preset).decode('ascii')
else:
(sfontid, banknum, presetnum, presetname) = self.channel_info(chan)
return presetname
return None
def router_clear(self):
if self.router is not None:
fluid_midi_router_clear_rules(self.router)
Expand Down Expand Up @@ -821,16 +828,16 @@ def set_reverb(self, roomsize=-1.0, damping=-1.0, width=-1.0, level=-1.0):
if fluid_synth_set_reverb is not None:
return fluid_synth_set_reverb(self.synth, roomsize, damping, width, level)
else:
set=0
flags=0
if roomsize>=0:
set+=0b0001
flags+=0b0001
if damping>=0:
set+=0b0010
flags+=0b0010
if width>=0:
set+=0b0100
flags+=0b0100
if level>=0:
set+=0b1000
return fluid_synth_set_reverb_full(self.synth, set, roomsize, damping, width, level)
flags+=0b1000
return fluid_synth_set_reverb_full(self.synth, flags, roomsize, damping, width, level)
def set_chorus(self, nr=-1, level=-1.0, speed=-1.0, depth=-1.0, type=-1):
"""
nr Chorus voice count (0-99, CPU time consumption proportional to this value)
Expand Down Expand Up @@ -889,11 +896,11 @@ def set_chorus_speed(self, speed):
return fluid_synth_set_chorus_speed(self.synth, speed)
else:
return self.set_chorus(speed=speed)
def set_chorus_depth(self, depth):
def set_chorus_depth(self, depth_ms):
if fluid_synth_set_chorus_depth is not None:
return fluid_synth_set_chorus_depth(self.synth, depth)
return fluid_synth_set_chorus_depth(self.synth, depth_ms)
else:
return self.set_chorus(depth=depth)
return self.set_chorus(depth=depth_ms)
def set_chorus_type(self, type):
if fluid_synth_set_chorus_type is not None:
return fluid_synth_set_chorus_type(self.synth, type)
Expand Down Expand Up @@ -945,10 +952,10 @@ def pitch_bend(self, chan, val):
A pitch bend value of 0 is no pitch change from default.
A value of -2048 is 1 semitone down.
A value of 2048 is 1 semitone up.
Maximum values are -8192 to +8192 (transposing by 4 semitones).
Maximum values are -8192 to +8191 (transposing by 4 semitones).
"""
return fluid_synth_pitch_bend(self.synth, chan, val + 8192)
return fluid_synth_pitch_bend(self.synth, chan, max(0, min(val + 8192, 16383)))
def cc(self, chan, ctrl, val):
"""Send control change value
Expand Down Expand Up @@ -998,8 +1005,15 @@ def get_samples(self, len=1024):
"""
return fluid_synth_write_s16_stereo(self.synth, len)
def tuning_dump(self, bank, prog, pitch):
return fluid_synth_tuning_dump(self.synth, bank, prog, name.encode(), len(name), pitch)
def tuning_dump(self, bank, prog):
"""Get tuning information for given bank and preset
Return value is an array of length 128 with tuning factors for each MIDI note.
Tuning factor of 0.0 in each position is standard tuning. Measured in cents.
"""
pitch = (c_double * 128)()
fluid_synth_tuning_dump(self.synth, bank, prog, None, 0, pitch)
return pitch[:]

def midi_event_get_type(self, event):
return fluid_midi_event_get_type(event)
Expand Down
Binary file modified test/example.sf2
Binary file not shown.
43 changes: 43 additions & 0 deletions test/test4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import time
import fluidsynth

def local_file_path(file_name: str) -> str:
"""
Return a file path to a file that is in the same directory as this file.
"""
from os.path import dirname, join

return join(dirname(__file__), file_name)

fs = fluidsynth.Synth()
fs.start()
## Your installation of FluidSynth may require a different driver.
## Use something like:
# fs.start(driver="pulseaudio")

sfid = fs.sfload(local_file_path("example.sf2"))
fs.program_select(0, sfid, 0, 0)

# Note that reverb send amount from presets is also determined by SF2 file.
# If needed you can edit SF2 files with Polytope. To add reverb:
# * Open SF2 file in Polytope.
# * Choose the desired preset in Polytope in left panel.
# * Scroll down to "Reverb (%)".
# * In "Global" column of "Reverb (%)" row set to 100.0 or appropriate percentage.
# * Save SF2 file.
fs.set_reverb(roomsize=0.8, damping=0.2, width=50.0, level=0.5)
fs.noteon(0, 60, 30)
fs.noteon(0, 67, 30)
fs.noteon(0, 76, 30)

time.sleep(2.0)
fs.pitch_bend(0, -8192)
time.sleep(2.0)

fs.noteoff(0, 60)
fs.noteoff(0, 67)
fs.noteoff(0, 76)

time.sleep(2.0)

fs.delete()

0 comments on commit 7dffe78

Please sign in to comment.