-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
82 lines (66 loc) · 3.31 KB
/
main.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
import os
from typing import List
import click
import glob
from sfcreator.decentsampler import DecentSamplerWriter
from sfcreator.sfz import SfzWriter
from sfcreator.soundfont.soundfont import SoundFont, Sample, NoteNameMidiNumberMapper, HighLowKeyDistributor
def list_supported_files(directory):
return glob.glob(directory + "/*.wav")
def map_note_name(note_name: str):
if note_name.isdigit():
return int(note_name)
else:
return NoteNameMidiNumberMapper().map(note_name)
@click.group()
def cli():
pass
@cli.command()
@click.argument('directory')
@click.option('--highkey', required=False, type=str, default="108")
@click.option('--lowkey', required=False, type=str, default="21")
@click.option('--instrument', help="name of the instrument", required=False, type=str)
@click.option('--loopmode',
help="loop mode, no_loop (default), one_shot, loop_continuous or loop_sustain,"
" see https://sfzformat.com/opcodes/loop_mode",
required=False, type=str, default="no_loop")
@click.option('--polyphony',
help="Polyphony voice limit, see https://sfzformat.com/opcodes/polyphony",
required=False, type=str, default=None)
def sfz(directory: str, lowkey: str, highkey: str, instrument: str, loopmode: str, polyphony: str):
soundfont = make_soundfont(directory, map_note_name(lowkey), map_note_name(highkey), instrument, loopmode,
polyphony)
SfzWriter().write(directory, soundfont)
@cli.command()
@click.argument('directory')
@click.option('--highkey', required=False, type=str, default="108")
@click.option('--lowkey', required=False, type=str, default="21")
@click.option('--instrument', help="name of the instrument", required=False, type=str)
@click.option('--image', required=False, type=str)
@click.option('--loopmode',
help="loop mode, only supported for sfz. For compatibility reasons, --loopmode=one_shot will cause"
" --release=20 for decent sampler, to ensure samples are always played until the end.",
required=False, type=str, default="no_loop")
@click.option('--polyphony',
help="Polyphony voice limit, not supported in decent sampler format.",
required=False, type=str, default=None)
def decentsampler(directory: str, lowkey: str, highkey: str, instrument: str, loopmode: str, polyphony: str,
image: str):
soundfont = make_soundfont(directory, map_note_name(lowkey), map_note_name(highkey), instrument, loopmode,
polyphony)
DecentSamplerWriter().write(directory, soundfont, image)
def make_soundfont(directory: str, lowkey: int, highkey: int, instrument: str, loopmode: str, polyphony: str):
files = list_supported_files(directory)
samples: List[Sample] = []
mapper = NoteNameMidiNumberMapper()
for file in files:
samples.append(mapper.mapped_sample(file))
soundfont = SoundFont(samples, loop_mode=loopmode, polyphony=polyphony)
if instrument is None or len(instrument) == 0:
soundfont.instrument_name = os.path.basename(os.path.dirname(directory))
else:
soundfont.instrument_name = instrument
HighLowKeyDistributor().distribute(soundfont, low_key=lowkey, high_key=highkey)
return soundfont
if __name__ == '__main__':
cli()