-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathnote_sequence_utils.py
61 lines (53 loc) · 2.39 KB
/
note_sequence_utils.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
"""
Common utilities for the book's code.
"""
import os
import time
from typing import Union, List, Optional
import magenta.music as mm
from magenta.protobuf.music_pb2 import NoteSequence
from visual_midi import Plotter
def save_midi(sequences: Union[NoteSequence, List[NoteSequence]],
output_dir: Optional[str] = None,
prefix: str = "sequence"):
"""
Writes the sequences as MIDI files to the "output" directory, with the
filename pattern "<prefix>_<index>_<date_time>" and "mid" as extension.
:param sequences: a NoteSequence or list of NoteSequence to be saved
:param output_dir: an optional subdirectory in the output directory
:param prefix: an optional prefix for each file
"""
output_dir = os.path.join("output", output_dir) if output_dir else "output"
os.makedirs(output_dir, exist_ok=True)
if not isinstance(sequences, list):
sequences = [sequences]
for (index, sequence) in enumerate(sequences):
date_and_time = time.strftime("%Y-%m-%d_%H%M%S")
filename = f"{prefix}_{index:02}_{date_and_time}.mid"
path = os.path.join(output_dir, filename)
mm.midi_io.note_sequence_to_midi_file(sequence, path)
print(f"Generated midi file: {os.path.abspath(path)}")
def save_plot(sequences: Union[NoteSequence, List[NoteSequence]],
output_dir: Optional[str] = None,
prefix: str = "sequence",
**kwargs):
"""
Writes the sequences as HTML plot files to the "output" directory, with the
filename pattern "<prefix>_<index>_<date_time>" and "html" as extension.
:param sequences: a NoteSequence or list of NoteSequence to be saved
:param output_dir: an optional subdirectory in the output directory
:param prefix: an optional prefix for each file
:param kwargs: the keyword arguments to pass to the Plotter instance
"""
output_dir = os.path.join("output", output_dir) if output_dir else "output"
os.makedirs(output_dir, exist_ok=True)
if not isinstance(sequences, list):
sequences = [sequences]
for (index, sequence) in enumerate(sequences):
date_and_time = time.strftime("%Y-%m-%d_%H%M%S")
filename = f"{prefix}_{index:02}_{date_and_time}.html"
path = os.path.join(output_dir, filename)
midi = mm.midi_io.note_sequence_to_pretty_midi(sequence)
plotter = Plotter(**kwargs)
plotter.save(midi, path)
print(f"Generated plot file: {os.path.abspath(path)}")