-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add examples showing MIDI file playing.
Fix uninitialized field in Synth, custom_router_callback. This allows MIDI generation without calling Synth.start().
- Loading branch information
1 parent
56f6006
commit 681590d
Showing
4 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |