| |
| |
| |
|
|
| |
|
|
| |
|
|
| |
| |
|
|
| import argparse |
| from os.path import exists |
| from os import makedirs |
| import logging |
| import numpy as np |
|
|
| from os.path import dirname, realpath |
| import sys |
| sys.path.insert(0, dirname(realpath(__file__))) |
| sys.path.insert(0, dirname(dirname(realpath(__file__)))) |
|
|
| from representation import Indexer, get_encoding, decode |
| import utils |
|
|
| |
|
|
|
|
| |
| |
|
|
| def generated_to_audio(path: str, output_path: str, encoding: dict, vocabulary: dict) -> None: |
| """ |
| Generate audio sample from codes. |
| """ |
|
|
| |
| codes = np.load(file = path) |
|
|
| |
| music = decode(codes = codes, encoding = encoding, vocabulary = vocabulary) |
|
|
| |
| music.write(path = output_path) |
|
|
| return |
|
|
| |
|
|
|
|
| |
| |
|
|
| def parse_args(args = None, namespace = None): |
| """Parse command-line arguments.""" |
| parser = argparse.ArgumentParser(prog = "Generated to Audio", description = "Synthesize generated content as audio.") |
| parser.add_argument("-p", "--path", required = True, type = str, help = "Path to generated sequence (.npy file)") |
| parser.add_argument("-o", "--output_path", default = None, type = str, help = "Output path") |
| return parser.parse_args(args = args, namespace = namespace) |
|
|
| |
|
|
|
|
| |
| |
|
|
| if __name__ == "__main__": |
|
|
| |
| args = parse_args() |
|
|
| |
| logging.basicConfig(level = logging.INFO, format = "%(message)s") |
| |
| |
| encoding = get_encoding() |
| vocabulary = utils.inverse_dict(Indexer(data = encoding["event_code_map"]).get_dict()) |
|
|
| |
| output_path = args.output_path |
| if output_path is None: |
| path_info = args.path[:-len(".npy")].split("/")[-4:] |
| output_dir = f"/home/pnlong/musescore/experiments/generated_audio/{path_info[1]}" |
| if not exists(output_dir): |
| makedirs(output_dir, exist_ok = True) |
| output_path = f"{output_dir}/{path_info[0]}.{path_info[-1]}.wav" |
|
|
| |
| generated_to_audio(path = args.path, output_path = output_path, encoding = encoding, vocabulary = vocabulary) |
| logging.info(f"Saved to {output_path}.") |
|
|
| |