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

Update Usage #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ It provides a Python and command-line interface to the [FluidSynth](http://www.f
First, FluidSynth has a CLI which is not so straightforward to use. The goal was to make it easy to use as possible by making some parameters implicit.

```
fluidsynth -ni sound_font.sf2 input.mid -F output.wav -r 44100
fluidsynth -ni -F output.wav -r 44100 sound_font.sf2 input.mid
```

vs.
Expand Down
22 changes: 15 additions & 7 deletions midi2audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,36 @@
DEFAULT_SOUND_FONT = '~/.fluidsynth/default_sound_font.sf2'
DEFAULT_SAMPLE_RATE = 44100


class FluidSynth():
def __init__(self, sound_font=DEFAULT_SOUND_FONT, sample_rate=DEFAULT_SAMPLE_RATE):
self.sample_rate = sample_rate
self.sound_font = os.path.expanduser(sound_font)

def midi_to_audio(self, midi_file, audio_file):
subprocess.call(['fluidsynth', '-ni', self.sound_font, midi_file, '-F', audio_file, '-r', str(self.sample_rate)])
subprocess.call(['fluidsynth', '-ni', '-F', audio_file, '-r', str(self.sample_rate),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did the behavior of fluidsynth change? In which version? Did you make any tests?

self.sound_font, midi_file])

def play_midi(self, midi_file):
subprocess.call(['fluidsynth', '-i', self.sound_font, midi_file, '-r', str(self.sample_rate)])
subprocess.call(['fluidsynth', '-i', self.sound_font,
midi_file, '-r', str(self.sample_rate)])


def parse_args(allow_synth=True):
parser = argparse.ArgumentParser(description='Convert MIDI to audio via FluidSynth')
parser = argparse.ArgumentParser(
description='Convert MIDI to audio via FluidSynth')
parser.add_argument('midi_file', metavar='MIDI', type=str)
if allow_synth:
parser.add_argument('audio_file', metavar='AUDIO', type=str, nargs='?')
parser.add_argument('-s', '--sound-font', type=str,
default=DEFAULT_SOUND_FONT,
help='path to a SF2 sound font (default: %s)' % DEFAULT_SOUND_FONT)
default=DEFAULT_SOUND_FONT,
help='path to a SF2 sound font (default: {})'.format(DEFAULT_SOUND_FONT))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are unrelated, should not be in the commit.

parser.add_argument('-r', '--sample-rate', type=int, nargs='?',
default=DEFAULT_SAMPLE_RATE,
help='sample rate in Hz (default: %s)' % DEFAULT_SAMPLE_RATE)
default=DEFAULT_SAMPLE_RATE,
help='sample rate in Hz (default: {})'.format(DEFAULT_SAMPLE_RATE))
return parser.parse_args()


def main(allow_synth=True):
args = parse_args(allow_synth)
fs = FluidSynth(args.sound_font, args.sample_rate)
Expand All @@ -69,11 +75,13 @@ def main(allow_synth=True):
else:
fs.play_midi(args.midi_file)


def main_play():
"""
A method for the `midiplay` entry point. It omits the audio file from args.
"""
main(allow_synth=False)


if __name__ == '__main__':
main()