From 681590d88835d88e932d2d3f4b8f1f1a7c987d2c Mon Sep 17 00:00:00 2001 From: Nathan Whitehead Date: Mon, 5 Aug 2024 17:05:13 -0600 Subject: [PATCH] Add examples showing MIDI file playing. Fix uninitialized field in Synth, custom_router_callback. This allows MIDI generation without calling Synth.start(). --- fluidsynth.py | 3 ++- test/1080-c01.mid | Bin 0 -> 6952 bytes test/test5.py | 27 +++++++++++++++++++++++++++ test/test6.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 test/1080-c01.mid create mode 100644 test/test5.py create mode 100644 test/test6.py diff --git a/fluidsynth.py b/fluidsynth.py index e4cb430..f2df155 100644 --- a/fluidsynth.py +++ b/fluidsynth.py @@ -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 @@ -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)): diff --git a/test/1080-c01.mid b/test/1080-c01.mid new file mode 100644 index 0000000000000000000000000000000000000000..c366ca8028cb1c228e390b3d56f1acd6ee67cb1e GIT binary patch literal 6952 zcmb_hO>A6O6~1ZGCQT$-Bz_{AhDTabI~o6G{F@uce|2IvZsH`Or3=)F&m?aYd**dK zQ$G+0&5#J8sx1^1sj9?+iY21Jh6PB43O0b)c8|o09TJF@Ip=)$%)Dn$+DJ{M>c0E` zo$q|-oIB3y^_ZMLM{_WZp>en}R2 zUNl2~rgPXbrOUPy(|mkUkC(Me+~JM0XzwizLUaP|8pj$S-uEwu*Xx7xx0CJPjywL!C$1^-pzU%l+2T#??A`&EbnHxxfu|9j%Uu9!)Ca{R)g%Y2RF{~3{wQZ z239VSS5h;;*vUn9|7G%*om*zpRcqpF=Ap=|m&xmpx(f%al4n2R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qE(yXpGnp@HGgE_v7>~g(wt5Pj(L|f%on&s+xIlA%9h~LA_>g{T6yBd|6 zjks3N+&XN?o?5L@Ff$T0y0p1Q6x*k`y7$ZfsMqIAr>At$W zxE$TAY?Qa6bw2S%Il9RQQB-P7XU;fbXMCPPl%LsR6xRlEz?IUia%N@M%(&4=riX^M zx3>p-035o(KO8dqy3F3!AG!uITMGv6%N?gIu|d_+W_emm;lTRv$lzv$|2@n8dB&Zx eJx^ZZA2N}-WI9?c)%gd_=x`w#nVOoA9sdV(Z!SIn literal 0 HcmV?d00001 diff --git a/test/test5.py b/test/test5.py new file mode 100644 index 0000000..b2d2165 --- /dev/null +++ b/test/test5.py @@ -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() diff --git a/test/test6.py b/test/test6.py new file mode 100644 index 0000000..7d3a679 --- /dev/null +++ b/test/test6.py @@ -0,0 +1,42 @@ +import numpy +import pyaudio +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) + +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)