-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_model.py
49 lines (37 loc) · 1.25 KB
/
generate_model.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
import encoder
from pathlib import Path
from typing import Optional
import argparse
import markovify
import mido
def main(directory: Path, state_size: int = 2, output: Optional[Path] = None):
dataset = []
for filename in directory.iterdir():
with mido.MidiFile(filename) as file:
messages = encoder.encode(file)
dataset.append(messages)
print(f"Processed {filename}")
chain = markovify.Chain(dataset, state_size=state_size)
with open(output, "w") as file:
file.write(chain.to_json())
print(f"Successfully generated model from {directory}! Saved to {output}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate a Markov chain from a directory of MIDI files.")
parser.add_argument(
"directory",
help="The path to a directory of MIDI files.",
type=Path
)
parser.add_argument(
"--state-size", "-s",
help="The state size of the model. Defaults to 2.",
default=2,
type=int
)
parser.add_argument(
"--output", "-o",
help="The path to the output file. Defaults to 'model.json'",
default=Path("model.json"),
type=Path
)
main(**vars(parser.parse_args()))