-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidi2audio.py
58 lines (49 loc) · 2.02 KB
/
midi2audio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import argparse
import os
import subprocess
__all__ = ['FluidSynth']
DEFAULT_SOUND_FONT = './data/.fluidsynth/FluidR3Mono_GM.sf3'
DEFAULT_SAMPLE_RATE = 44100
DEFAULT_GAIN = 0.2
class FluidSynth():
def __init__(self, sound_font=DEFAULT_SOUND_FONT, sample_rate=DEFAULT_SAMPLE_RATE, gain=DEFAULT_GAIN):
self.sample_rate = sample_rate
self.sound_font = os.path.expanduser(sound_font)
self.gain = gain
def midi_to_audio(self, midi_file: str, audio_file: str, verbose=True):
if verbose:
stdout = None
else:
stdout = subprocess.DEVNULL
subprocess.call(
['fluidsynth', '-ni', '-g', str(self.gain), self.sound_font, midi_file, '-F', audio_file, '-r', str(self.sample_rate)],
stdout=stdout,
)
def play_midi(self, midi_file):
subprocess.call(['fluidsynth', '-i', '-g', str(self.gain), 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.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)
parser.add_argument('-r', '--sample-rate', type=int, nargs='?',
default=DEFAULT_SAMPLE_RATE,
help='sample rate in Hz (default: %s)' % 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)
if allow_synth and args.audio_file:
fs.midi_to_audio(args.midi_file, args.audio_file)
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()