Skip to content

Commit

Permalink
enable midi -> audio (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
madderscientist authored Dec 6, 2024
1 parent 21e30cc commit 4c07da9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
26 changes: 25 additions & 1 deletion fluidsynth.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,16 @@ class fluid_midi_router_t(Structure):
('rule', c_void_p, 1),
('type', c_int, 1))

# fluid file renderer
new_fluid_file_renderer = cfunc('new_fluid_file_renderer', c_void_p,
('synth', c_void_p, 1))

delete_fluid_file_renderer = cfunc('delete_fluid_file_renderer', None,
('renderer', c_void_p, 1))

fluid_file_renderer_process_block = cfunc('fluid_file_renderer_process_block', c_int,
('render', c_void_p, 1))

# fluidsynth 2.x
new_fluid_cmd_handler=cfunc('new_fluid_cmd_handler', c_void_p,
('synth', c_void_p, 1),
Expand Down Expand Up @@ -693,6 +703,8 @@ def __init__(self, gain=0.2, samplerate=44100, channels=256, **kwargs):
self.midi_driver = None
self.router = None
self.custom_router_callback = None
def __del__(self):
self.delete()
def setting(self, opt, val):
"""change an arbitrary synth setting, type-smart"""
if isinstance(val, (str, bytes)):
Expand Down Expand Up @@ -1070,7 +1082,19 @@ def play_midi_stop(self):

def player_set_tempo(self, tempo_type, tempo):
return fluid_player_set_tempo(self.player, tempo_type, tempo)


Check failure on line 1085 in fluidsynth.py

View workflow job for this annotation

GitHub Actions / codespell_and_ruff

Ruff (W293)

fluidsynth.py:1085:1: W293 Blank line contains whitespace
def midi2audio(self, midifile, audiofile = "output.wav"):
"""Convert a midi file to an audio file"""
self.setting("audio.file.name", audiofile)
player = new_fluid_player(self.synth)
fluid_player_add(player, midifile.encode())
fluid_player_play(player)
renderer = new_fluid_file_renderer(self.synth)
while(fluid_player_get_status(player) == FLUID_PLAYER_PLAYING):
if(fluid_file_renderer_process_block(renderer) != FLUID_OK):
break
delete_fluid_file_renderer(renderer)
delete_fluid_player(player)


class Sequencer:
Expand Down
17 changes: 17 additions & 0 deletions test/test7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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()
sfid = fs.sfload(local_file_path("example.sf2"))
fs.midi2audio(local_file_path("1080-c01.mid"), local_file_path("1080-c01.wav"))
# A Synth object can synthesize multiple files. For example:
# fs.midi2audio(local_file_path("1080-c02.mid"), local_file_path("1080-c02.wav"))
# fs.midi2audio(local_file_path("1080-c03.mid"), local_file_path("1080-c03.wav"))

0 comments on commit 4c07da9

Please sign in to comment.