Skip to content

Commit

Permalink
Merge pull request #85 from madderscientist/master
Browse files Browse the repository at this point in the history
enable midi -> audio (re pull)
  • Loading branch information
nwhitehead authored Dec 11, 2024
2 parents 28b8e66 + bf8775a commit 3ab8465
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
22 changes: 22 additions & 0 deletions 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 @@ -1071,6 +1081,18 @@ def play_midi_stop(self):
def player_set_tempo(self, tempo_type, tempo):
return fluid_player_set_tempo(self.player, tempo_type, tempo)

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
19 changes: 19 additions & 0 deletions test/test7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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"))

fs.delete()

0 comments on commit 3ab8465

Please sign in to comment.