Skip to content

Commit

Permalink
Add examples showing MIDI file playing.
Browse files Browse the repository at this point in the history
Fix uninitialized field in Synth, custom_router_callback.
This allows MIDI generation without calling Synth.start().
  • Loading branch information
nwhitehead committed Aug 5, 2024
1 parent 56f6006 commit 681590d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
3 changes: 2 additions & 1 deletion fluidsynth.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def cfunc(name, result, *args):
return None

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

# Function prototypes for C versions of functions

Expand Down Expand Up @@ -684,6 +684,7 @@ def __init__(self, gain=0.2, samplerate=44100, channels=256, **kwargs):
self.audio_driver = None
self.midi_driver = None
self.router = None
self.custom_router_callback = None
def setting(self, opt, val):
"""change an arbitrary synth setting, type-smart"""
if isinstance(val, (str, bytes)):
Expand Down
Binary file added test/1080-c01.mid
Binary file not shown.
27 changes: 27 additions & 0 deletions test/test5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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)

fs.play_midi_file(local_file_path("1080-c01.mid"))
while fluidsynth.fluid_player_get_status(fs.player) == fluidsynth.FLUID_PLAYER_PLAYING:
time.sleep(1)

fs.delete()
42 changes: 42 additions & 0 deletions test/test6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import numpy
import pyaudio
import time

Check failure on line 3 in test/test6.py

View workflow job for this annotation

GitHub Actions / codespell_and_ruff

Ruff (F401)

test/test6.py:3:8: F401 `time` imported but unused

import fluidsynth


def local_file_path(file_name: str) -> str:

Check failure on line 8 in test/test6.py

View workflow job for this annotation

GitHub Actions / codespell_and_ruff

Ruff (I001)

test/test6.py:1:1: I001 Import block is un-sorted or un-formatted
"""
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)

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

fs = fluidsynth.Synth()
fs.custom_router_callback = None

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

fs.play_midi_file(local_file_path("1080-c01.mid"))

# Generate 10 seconds of audio into s
s = []
for _ in range(10):
s = numpy.append(s, fs.get_samples(44100))
if fluidsynth.fluid_player_get_status(fs.player) != fluidsynth.FLUID_PLAYER_PLAYING:
break

fs.delete()

samps = fluidsynth.raw_audio_string(s)
print('Starting playback')
strm.write(samps)

0 comments on commit 681590d

Please sign in to comment.