Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable midi -> audio (re pull) #85

Merged
merged 3 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading