-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmidi2audio.py
88 lines (68 loc) · 2.67 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""
Allows to play and synthesize MIDI to audio using a sound font via the
FluidSynth synthesizer.
Input: MIDI file
Output: audio file (WAV, FLAC, ...) - type determined from the extension
Usage in shell:
$ midiplay input.mid
# use some default sound font
$ midi2audio input.mid output.flac
# use a custom sound font
$ midi2audio -s sound_font.sf2 input.mid output.flac
Usage in Python:
from midi2audio import FluidSynth
fs = FluidSynth()
fs.play_midi('input.mid')
fs.midi_to_audio('input.mid', 'output.flac')
FluidSynth('sound_font.sf2').midi_to_audio('input.mid', 'output.flac')
Default sound font path is `~/.fluidsynth/default_sound_font.sf2`.
"""
import argparse
import os
import subprocess
__all__ = ['FluidSynth']
DEFAULT_SOUND_FONT = '~/.fluidsynth/default_sound_font.sf2'
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()