Transpose
This commit is contained in:
@@ -4,6 +4,7 @@ from pathlib import Path
|
||||
|
||||
from generator.score import copy_source_voices, make_arranged_document, make_measure, reidentify, slugify, source_staff, write_json
|
||||
from generator.musicxml import load_musicxml
|
||||
from generator.practice.guitar import transpose_guitar_practice
|
||||
from generator.transposition import TARGET_KEYS, transpose_document
|
||||
|
||||
def practice_document(document: dict) -> dict:
|
||||
@@ -39,23 +40,23 @@ def key_file_name(key: str) -> str:
|
||||
return slugify(key.replace("#", "sharp")) + ".json"
|
||||
|
||||
def generate_chord_practice(source_path: Path, source_root: Path, output_dir: Path) -> int:
|
||||
document = practice_document(load_musicxml(source_path))
|
||||
metadata = document.get("metadata", {})
|
||||
key = str(metadata.get("key_signature", "C") if isinstance(metadata, dict) else "C")
|
||||
relative = source_path.relative_to(source_root)
|
||||
lesson_group = relative.parent.name
|
||||
exercise_id = slugify(source_path.stem)
|
||||
write_json(output_dir / "chord" / lesson_group / exercise_id / key_file_name(key), document)
|
||||
return 1
|
||||
return generate_key_variants(source_path, source_root, output_dir, 'chord')
|
||||
|
||||
def generate_melody_practice(source_path: Path, source_root: Path, output_dir: Path) -> int:
|
||||
return generate_key_variants(source_path, source_root, output_dir, 'melody')
|
||||
|
||||
def generate_key_variants(source_path: Path, source_root: Path, output_dir: Path, practice_type: str) -> int:
|
||||
base_document = practice_document(load_musicxml(source_path))
|
||||
relative = source_path.relative_to(source_root)
|
||||
lesson_group = relative.parent.name
|
||||
exercise_id = slugify(source_path.stem)
|
||||
count = 0
|
||||
for key in TARGET_KEYS:
|
||||
write_json(output_dir / "melody" / lesson_group / exercise_id / key_file_name(key), transpose_document(base_document, key))
|
||||
try:
|
||||
variant = transpose_guitar_practice(base_document, key) if practice_type == 'chord' else transpose_document(base_document, key)
|
||||
except ValueError as error:
|
||||
raise ValueError(f'{source_path}, target key {key}: {error}') from error
|
||||
write_json(output_dir / practice_type / lesson_group / exercise_id / key_file_name(key), variant)
|
||||
count += 1
|
||||
return count
|
||||
|
||||
|
||||
Reference in New Issue
Block a user