Spaces:
Build error
Build error
Upload 8 files
Browse files- app.py +137 -14
- notation.py +471 -64
- studio_component.py +2 -0
- studio_component.pyi +7 -2
app.py
CHANGED
|
@@ -23,6 +23,7 @@ except ImportError: # Local UI and tests work without the ZeroGPU package.
|
|
| 23 |
|
| 24 |
import base64
|
| 25 |
import hashlib
|
|
|
|
| 26 |
import math
|
| 27 |
import re
|
| 28 |
import sys
|
|
@@ -34,7 +35,7 @@ from typing import Any, Iterator
|
|
| 34 |
|
| 35 |
import gradio as gr
|
| 36 |
|
| 37 |
-
from notation import file_data_uri, generate_notation
|
| 38 |
from studio_component import StudioViewer, initial_studio_value, normalize_viewer_value
|
| 39 |
|
| 40 |
|
|
@@ -327,11 +328,18 @@ def _viewer_payload(
|
|
| 327 |
return value
|
| 328 |
|
| 329 |
|
| 330 |
-
def _served_file_url(path: str | None) -> str:
|
| 331 |
-
"""Expose a temporary result through Gradio's authenticated file route.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
if not path:
|
| 333 |
return ""
|
| 334 |
try:
|
|
|
|
|
|
|
| 335 |
file_data = demo.serve_static_file(path)
|
| 336 |
except (NameError, AttributeError, FileNotFoundError, ValueError):
|
| 337 |
return ""
|
|
@@ -531,7 +539,7 @@ def transcribe_audio(
|
|
| 531 |
track_file_values,
|
| 532 |
viewer,
|
| 533 |
session,
|
| 534 |
-
gr.update(interactive=
|
| 535 |
)
|
| 536 |
except Exception as exc:
|
| 537 |
elapsed = time.perf_counter() - started
|
|
@@ -558,6 +566,70 @@ def transcribe_audio(
|
|
| 558 |
)
|
| 559 |
|
| 560 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 561 |
def reset_score_outputs(audio: Any) -> tuple[Any, ...]:
|
| 562 |
"""Clear the previous session immediately, before ZeroGPU enters its queue."""
|
| 563 |
|
|
@@ -590,6 +662,7 @@ def reset_score_outputs(audio: Any) -> tuple[Any, ...]:
|
|
| 590 |
viewer,
|
| 591 |
{},
|
| 592 |
gr.update(interactive=False),
|
|
|
|
| 593 |
)
|
| 594 |
|
| 595 |
|
|
@@ -599,11 +672,11 @@ def finalize_notation(
|
|
| 599 |
time_signature: str,
|
| 600 |
quantization: str,
|
| 601 |
show_solfege: bool,
|
| 602 |
-
) -> tuple[str | None, str | None, list[str] | None, str | None, dict[str, Any]]:
|
| 603 |
session = session or {}
|
| 604 |
viewer = normalize_viewer_value(session.get("viewer"))
|
| 605 |
if session.get("state") != "complete":
|
| 606 |
-
return None, None, None, None, viewer
|
| 607 |
try:
|
| 608 |
result = generate_notation(
|
| 609 |
session["tracks"],
|
|
@@ -617,20 +690,42 @@ def finalize_notation(
|
|
| 617 |
score_page_urls = [
|
| 618 |
url
|
| 619 |
for page in result.svg_pages
|
| 620 |
-
if (url := _served_file_url(str(page)))
|
| 621 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 622 |
part_files = [
|
| 623 |
str(path)
|
| 624 |
for part in result.parts
|
| 625 |
-
for path in (part.
|
| 626 |
if path is not None
|
| 627 |
]
|
| 628 |
updated = dict(viewer)
|
| 629 |
updated.update(
|
| 630 |
{
|
| 631 |
"state": "ready",
|
| 632 |
-
"status": "
|
| 633 |
"score_svg": "" if score_page_urls else result.preview_svg,
|
|
|
|
|
|
|
| 634 |
"score_page_urls": score_page_urls,
|
| 635 |
"score_pages": len(result.svg_pages),
|
| 636 |
"notation": {
|
|
@@ -639,6 +734,7 @@ def finalize_notation(
|
|
| 639 |
"quantization": quantization,
|
| 640 |
"show_solfege": bool(show_solfege),
|
| 641 |
"part_count": len(result.parts),
|
|
|
|
| 642 |
"musicxml": file_data_uri(result.musicxml, "application/vnd.recordare.musicxml+xml"),
|
| 643 |
"svg": (
|
| 644 |
score_page_urls[0]
|
|
@@ -656,12 +752,20 @@ def finalize_notation(
|
|
| 656 |
part_files,
|
| 657 |
str(result.bundle),
|
| 658 |
updated,
|
|
|
|
| 659 |
)
|
| 660 |
except Exception as exc:
|
| 661 |
updated = dict(viewer)
|
| 662 |
updated["state"] = "notation_error"
|
| 663 |
updated["status"] = f"MIDI is ready, but the score could not be generated: {type(exc).__name__}: {exc}"
|
| 664 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 665 |
|
| 666 |
|
| 667 |
APP_CSS = (ROOT / "frontend" / "app.css").read_text(encoding="utf-8")
|
|
@@ -719,7 +823,7 @@ DECODE_HEADING = """
|
|
| 719 |
"""
|
| 720 |
|
| 721 |
SCORE_HEADING = """
|
| 722 |
-
<div class="control-heading"><span>03</span><div><strong>Score controls</strong><small>
|
| 723 |
"""
|
| 724 |
|
| 725 |
EXPORT_HEADING = """
|
|
@@ -800,6 +904,10 @@ with gr.Blocks(title="MuScriptor Studio") as demo:
|
|
| 800 |
value=False,
|
| 801 |
interactive=True,
|
| 802 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 803 |
generate_score_button = gr.Button(
|
| 804 |
"Generate full score + parts",
|
| 805 |
variant="secondary",
|
|
@@ -853,6 +961,7 @@ with gr.Blocks(title="MuScriptor Studio") as demo:
|
|
| 853 |
viewer,
|
| 854 |
session_state,
|
| 855 |
generate_score_button,
|
|
|
|
| 856 |
],
|
| 857 |
queue=False,
|
| 858 |
show_progress="hidden",
|
|
@@ -879,12 +988,26 @@ with gr.Blocks(title="MuScriptor Studio") as demo:
|
|
| 879 |
concurrency_id="muscriptor-large",
|
| 880 |
show_progress="full",
|
| 881 |
)
|
| 882 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 883 |
finalize_notation,
|
| 884 |
inputs=[session_state, tempo_input, time_signature_input, quantization_input, solfege_input],
|
| 885 |
-
outputs=[musicxml_output, pdf_output, part_score_output, bundle_output, viewer],
|
| 886 |
api_name="generate_score",
|
| 887 |
-
show_progress="
|
| 888 |
)
|
| 889 |
|
| 890 |
demo.queue(default_concurrency_limit=1, max_size=8)
|
|
|
|
| 23 |
|
| 24 |
import base64
|
| 25 |
import hashlib
|
| 26 |
+
import html
|
| 27 |
import math
|
| 28 |
import re
|
| 29 |
import sys
|
|
|
|
| 35 |
|
| 36 |
import gradio as gr
|
| 37 |
|
| 38 |
+
from notation import estimate_score_settings, file_data_uri, generate_notation
|
| 39 |
from studio_component import StudioViewer, initial_studio_value, normalize_viewer_value
|
| 40 |
|
| 41 |
|
|
|
|
| 328 |
return value
|
| 329 |
|
| 330 |
|
| 331 |
+
def _served_file_url(path: str | None, *, inline: bool = False) -> str:
|
| 332 |
+
"""Expose a temporary result through Gradio's authenticated file route.
|
| 333 |
+
|
| 334 |
+
Gradio forces cache-created PDFs to download. Marking only our generated
|
| 335 |
+
score files as static makes their response ``Content-Disposition: inline``,
|
| 336 |
+
which lets the custom viewer embed the complete PDF safely.
|
| 337 |
+
"""
|
| 338 |
if not path:
|
| 339 |
return ""
|
| 340 |
try:
|
| 341 |
+
if inline:
|
| 342 |
+
gr.set_static_paths(path)
|
| 343 |
file_data = demo.serve_static_file(path)
|
| 344 |
except (NameError, AttributeError, FileNotFoundError, ValueError):
|
| 345 |
return ""
|
|
|
|
| 539 |
track_file_values,
|
| 540 |
viewer,
|
| 541 |
session,
|
| 542 |
+
gr.update(interactive=False),
|
| 543 |
)
|
| 544 |
except Exception as exc:
|
| 545 |
elapsed = time.perf_counter() - started
|
|
|
|
| 566 |
)
|
| 567 |
|
| 568 |
|
| 569 |
+
def _score_analysis_html(analysis: dict[str, Any] | None = None, *, state: str = "waiting") -> str:
|
| 570 |
+
if state == "waiting":
|
| 571 |
+
return (
|
| 572 |
+
'<div class="score-analysis" data-state="waiting"><i></i><div>'
|
| 573 |
+
'<strong>Automatic notation presets</strong>'
|
| 574 |
+
'<span>Tempo, meter and grid will be estimated from the decoded performance.</span>'
|
| 575 |
+
'</div></div>'
|
| 576 |
+
)
|
| 577 |
+
if state == "unavailable" or not analysis:
|
| 578 |
+
return (
|
| 579 |
+
'<div class="score-analysis" data-state="unavailable"><i></i><div>'
|
| 580 |
+
'<strong>Preset analysis unavailable</strong>'
|
| 581 |
+
'<span>You can still enter tempo, meter and quantization manually.</span>'
|
| 582 |
+
'</div></div>'
|
| 583 |
+
)
|
| 584 |
+
confidence = html.escape(str(analysis.get("confidence") or "low").title())
|
| 585 |
+
return (
|
| 586 |
+
'<div class="score-analysis" data-state="ready"><i></i><div>'
|
| 587 |
+
'<strong>Suggested from the transcription</strong>'
|
| 588 |
+
f'<span>{float(analysis["tempo_bpm"]):.1f} BPM · {html.escape(str(analysis["time_signature"]))} · '
|
| 589 |
+
f'{html.escape(str(analysis["quantization"]))} grid · {confidence} confidence</span>'
|
| 590 |
+
'</div></div>'
|
| 591 |
+
)
|
| 592 |
+
|
| 593 |
+
|
| 594 |
+
def suggest_score_settings(session: dict[str, Any] | None) -> tuple[Any, Any, Any, str, dict[str, Any]]:
|
| 595 |
+
session = session or {}
|
| 596 |
+
if session.get("state") != "complete" or not session.get("tracks"):
|
| 597 |
+
return (
|
| 598 |
+
gr.update(),
|
| 599 |
+
gr.update(),
|
| 600 |
+
gr.update(),
|
| 601 |
+
_score_analysis_html(state="unavailable"),
|
| 602 |
+
gr.update(interactive=False),
|
| 603 |
+
)
|
| 604 |
+
analysis = estimate_score_settings(session["tracks"])
|
| 605 |
+
return (
|
| 606 |
+
analysis["tempo_bpm"],
|
| 607 |
+
analysis["time_signature"],
|
| 608 |
+
analysis["quantization"],
|
| 609 |
+
_score_analysis_html(analysis),
|
| 610 |
+
gr.update(interactive=True),
|
| 611 |
+
)
|
| 612 |
+
|
| 613 |
+
|
| 614 |
+
def begin_notation(
|
| 615 |
+
session: dict[str, Any] | None,
|
| 616 |
+
current_viewer: dict[str, Any] | str | None = None,
|
| 617 |
+
) -> tuple[dict[str, Any], dict[str, Any]]:
|
| 618 |
+
session = session or {}
|
| 619 |
+
viewer = normalize_viewer_value(current_viewer or session.get("viewer"))
|
| 620 |
+
if session.get("state") != "complete":
|
| 621 |
+
return viewer, gr.update(interactive=False)
|
| 622 |
+
updated = dict(viewer)
|
| 623 |
+
updated.update(
|
| 624 |
+
{
|
| 625 |
+
"state": "generating_score",
|
| 626 |
+
"status": "Engraving the full score and instrument parts…",
|
| 627 |
+
"progress": 0,
|
| 628 |
+
}
|
| 629 |
+
)
|
| 630 |
+
return updated, gr.update(interactive=False, value="Generating score…")
|
| 631 |
+
|
| 632 |
+
|
| 633 |
def reset_score_outputs(audio: Any) -> tuple[Any, ...]:
|
| 634 |
"""Clear the previous session immediately, before ZeroGPU enters its queue."""
|
| 635 |
|
|
|
|
| 662 |
viewer,
|
| 663 |
{},
|
| 664 |
gr.update(interactive=False),
|
| 665 |
+
_score_analysis_html(),
|
| 666 |
)
|
| 667 |
|
| 668 |
|
|
|
|
| 672 |
time_signature: str,
|
| 673 |
quantization: str,
|
| 674 |
show_solfege: bool,
|
| 675 |
+
) -> tuple[str | None, str | None, list[str] | None, str | None, dict[str, Any], dict[str, Any]]:
|
| 676 |
session = session or {}
|
| 677 |
viewer = normalize_viewer_value(session.get("viewer"))
|
| 678 |
if session.get("state") != "complete":
|
| 679 |
+
return None, None, None, None, viewer, gr.update(interactive=False)
|
| 680 |
try:
|
| 681 |
result = generate_notation(
|
| 682 |
session["tracks"],
|
|
|
|
| 690 |
score_page_urls = [
|
| 691 |
url
|
| 692 |
for page in result.svg_pages
|
| 693 |
+
if (url := _served_file_url(str(page), inline=True))
|
| 694 |
]
|
| 695 |
+
score_pdf_url = _served_file_url(str(result.pdf), inline=True) if result.pdf else ""
|
| 696 |
+
score_parts = []
|
| 697 |
+
for part in result.parts:
|
| 698 |
+
part_pdf_url = _served_file_url(str(part.pdf), inline=True) if part.pdf else ""
|
| 699 |
+
part_svg_urls = [
|
| 700 |
+
url
|
| 701 |
+
for page in part.svg_pages
|
| 702 |
+
if (url := _served_file_url(str(page), inline=True))
|
| 703 |
+
]
|
| 704 |
+
score_parts.append(
|
| 705 |
+
{
|
| 706 |
+
"track_id": part.track_id,
|
| 707 |
+
"track_key": part.track_key,
|
| 708 |
+
"name": part.name,
|
| 709 |
+
"pdf": part_pdf_url,
|
| 710 |
+
"musicxml": _served_file_url(str(part.musicxml)),
|
| 711 |
+
"pages": part_svg_urls,
|
| 712 |
+
"page_count": len(part.svg_pages),
|
| 713 |
+
}
|
| 714 |
+
)
|
| 715 |
part_files = [
|
| 716 |
str(path)
|
| 717 |
for part in result.parts
|
| 718 |
+
for path in (part.pdf, part.musicxml)
|
| 719 |
if path is not None
|
| 720 |
]
|
| 721 |
updated = dict(viewer)
|
| 722 |
updated.update(
|
| 723 |
{
|
| 724 |
"state": "ready",
|
| 725 |
+
"status": f"Full score and {len(result.parts)} instrument parts are ready",
|
| 726 |
"score_svg": "" if score_page_urls else result.preview_svg,
|
| 727 |
+
"score_pdf_url": score_pdf_url,
|
| 728 |
+
"score_parts": score_parts,
|
| 729 |
"score_page_urls": score_page_urls,
|
| 730 |
"score_pages": len(result.svg_pages),
|
| 731 |
"notation": {
|
|
|
|
| 734 |
"quantization": quantization,
|
| 735 |
"show_solfege": bool(show_solfege),
|
| 736 |
"part_count": len(result.parts),
|
| 737 |
+
"pdf": score_pdf_url,
|
| 738 |
"musicxml": file_data_uri(result.musicxml, "application/vnd.recordare.musicxml+xml"),
|
| 739 |
"svg": (
|
| 740 |
score_page_urls[0]
|
|
|
|
| 752 |
part_files,
|
| 753 |
str(result.bundle),
|
| 754 |
updated,
|
| 755 |
+
gr.update(interactive=True, value="Regenerate full score + parts"),
|
| 756 |
)
|
| 757 |
except Exception as exc:
|
| 758 |
updated = dict(viewer)
|
| 759 |
updated["state"] = "notation_error"
|
| 760 |
updated["status"] = f"MIDI is ready, but the score could not be generated: {type(exc).__name__}: {exc}"
|
| 761 |
+
return (
|
| 762 |
+
None,
|
| 763 |
+
None,
|
| 764 |
+
None,
|
| 765 |
+
None,
|
| 766 |
+
updated,
|
| 767 |
+
gr.update(interactive=True, value="Retry score generation"),
|
| 768 |
+
)
|
| 769 |
|
| 770 |
|
| 771 |
APP_CSS = (ROOT / "frontend" / "app.css").read_text(encoding="utf-8")
|
|
|
|
| 823 |
"""
|
| 824 |
|
| 825 |
SCORE_HEADING = """
|
| 826 |
+
<div class="control-heading"><span>03</span><div><strong>Score controls</strong><small>Tempo, meter and grid are estimated after transcription; review them before engraving.</small></div></div>
|
| 827 |
"""
|
| 828 |
|
| 829 |
EXPORT_HEADING = """
|
|
|
|
| 904 |
value=False,
|
| 905 |
interactive=True,
|
| 906 |
)
|
| 907 |
+
score_analysis_output = gr.HTML(
|
| 908 |
+
value=_score_analysis_html(),
|
| 909 |
+
elem_id="score-analysis-output",
|
| 910 |
+
)
|
| 911 |
generate_score_button = gr.Button(
|
| 912 |
"Generate full score + parts",
|
| 913 |
variant="secondary",
|
|
|
|
| 961 |
viewer,
|
| 962 |
session_state,
|
| 963 |
generate_score_button,
|
| 964 |
+
score_analysis_output,
|
| 965 |
],
|
| 966 |
queue=False,
|
| 967 |
show_progress="hidden",
|
|
|
|
| 988 |
concurrency_id="muscriptor-large",
|
| 989 |
show_progress="full",
|
| 990 |
)
|
| 991 |
+
transcription.then(
|
| 992 |
+
suggest_score_settings,
|
| 993 |
+
inputs=[session_state],
|
| 994 |
+
outputs=[tempo_input, time_signature_input, quantization_input, score_analysis_output, generate_score_button],
|
| 995 |
+
queue=False,
|
| 996 |
+
api_name="analyze_score_settings",
|
| 997 |
+
)
|
| 998 |
+
notation_preflight = generate_score_button.click(
|
| 999 |
+
begin_notation,
|
| 1000 |
+
inputs=[session_state, viewer],
|
| 1001 |
+
outputs=[viewer, generate_score_button],
|
| 1002 |
+
queue=False,
|
| 1003 |
+
show_progress="hidden",
|
| 1004 |
+
)
|
| 1005 |
+
notation_preflight.then(
|
| 1006 |
finalize_notation,
|
| 1007 |
inputs=[session_state, tempo_input, time_signature_input, quantization_input, solfege_input],
|
| 1008 |
+
outputs=[musicxml_output, pdf_output, part_score_output, bundle_output, viewer, generate_score_button],
|
| 1009 |
api_name="generate_score",
|
| 1010 |
+
show_progress="full",
|
| 1011 |
)
|
| 1012 |
|
| 1013 |
demo.queue(default_concurrency_limit=1, max_size=8)
|
notation.py
CHANGED
|
@@ -4,8 +4,8 @@ import base64
|
|
| 4 |
import copy
|
| 5 |
import io
|
| 6 |
import json
|
|
|
|
| 7 |
import re
|
| 8 |
-
import shutil
|
| 9 |
import tempfile
|
| 10 |
import zipfile
|
| 11 |
from dataclasses import dataclass
|
|
@@ -25,6 +25,8 @@ SOLFEGE_NAMES = ("Do", "Do♯", "Ré", "Mi♭", "Mi", "Fa", "Fa♯", "Sol", "La
|
|
| 25 |
|
| 26 |
@dataclass(frozen=True)
|
| 27 |
class NotationPartResult:
|
|
|
|
|
|
|
| 28 |
name: str
|
| 29 |
slug: str
|
| 30 |
musicxml: Path
|
|
@@ -42,6 +44,190 @@ class NotationResult:
|
|
| 42 |
parts: tuple[NotationPartResult, ...]
|
| 43 |
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
def _safe_slug(value: str) -> str:
|
| 46 |
return re.sub(r"[^a-z0-9]+", "-", value.lower()).strip("-") or "piste"
|
| 47 |
|
|
@@ -57,6 +243,201 @@ def _parse_time_signature(value: str) -> tuple[int, int]:
|
|
| 57 |
return parsed
|
| 58 |
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
def _music21_score(
|
| 61 |
tracks: list[dict[str, Any]],
|
| 62 |
*,
|
|
@@ -65,12 +446,23 @@ def _music21_score(
|
|
| 65 |
time_signature: str,
|
| 66 |
quantization: str,
|
| 67 |
show_solfege: bool,
|
| 68 |
-
):
|
| 69 |
try:
|
| 70 |
-
from music21 import chord, clef, instrument, metadata, meter, note, stream, tempo
|
| 71 |
except ImportError as exc: # pragma: no cover - deployment configuration
|
| 72 |
raise RuntimeError("La dépendance music21 est nécessaire pour générer la partition.") from exc
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
bpm = max(20.0, min(300.0, float(tempo_bpm)))
|
| 75 |
numerator, denominator = _parse_time_signature(time_signature)
|
| 76 |
divisor = QUANTIZATION_DIVISORS.get(quantization, 4)
|
|
@@ -79,69 +471,64 @@ def _music21_score(
|
|
| 79 |
score.metadata = metadata.Metadata()
|
| 80 |
score.metadata.title = title or "Transcription MuScriptor"
|
| 81 |
score.metadata.composer = "Transcription automatique MuScriptor"
|
|
|
|
| 82 |
|
| 83 |
for track_index, track in enumerate(tracks):
|
| 84 |
-
|
| 85 |
-
if not
|
| 86 |
continue
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
midi_instrument.midiChannel = 9
|
| 95 |
-
else:
|
| 96 |
-
midi_instrument.midiProgram = max(0, min(127, int(track.get("program") or 0)))
|
| 97 |
-
part.insert(0, midi_instrument)
|
| 98 |
-
part.insert(0, tempo.MetronomeMark(number=bpm))
|
| 99 |
-
part.insert(0, meter.TimeSignature(f"{numerator}/{denominator}"))
|
| 100 |
-
|
| 101 |
-
pitches = [int(item.get("pitch", 60)) for item in notes]
|
| 102 |
-
if track.get("is_drum"):
|
| 103 |
-
part.insert(0, clef.PercussionClef())
|
| 104 |
-
elif median(pitches) < 55:
|
| 105 |
-
part.insert(0, clef.BassClef())
|
| 106 |
-
else:
|
| 107 |
-
part.insert(0, clef.TrebleClef())
|
| 108 |
-
|
| 109 |
-
grouped: dict[tuple[float, float], list[int]] = {}
|
| 110 |
-
velocities: dict[tuple[float, float], list[int]] = {}
|
| 111 |
-
for item in notes:
|
| 112 |
-
start_seconds = max(0.0, float(item.get("start", 0.0)))
|
| 113 |
-
end_seconds = max(start_seconds + 0.03, float(item.get("end", start_seconds + 0.1)))
|
| 114 |
-
offset = start_seconds * bpm / 60.0
|
| 115 |
-
duration = max(1 / divisor, (end_seconds - start_seconds) * bpm / 60.0)
|
| 116 |
-
key = (round(offset, 5), round(duration, 5))
|
| 117 |
-
grouped.setdefault(key, []).append(max(0, min(127, int(item.get("pitch", 60)))))
|
| 118 |
-
velocities.setdefault(key, []).append(max(1, min(127, int(item.get("velocity", 100)))))
|
| 119 |
-
|
| 120 |
-
for (offset, duration), group_pitches in sorted(grouped.items()):
|
| 121 |
-
if len(group_pitches) == 1:
|
| 122 |
-
musical_item = note.Note(group_pitches[0], quarterLength=duration)
|
| 123 |
-
if show_solfege:
|
| 124 |
-
musical_item.addLyric(SOLFEGE_NAMES[group_pitches[0] % 12])
|
| 125 |
-
else:
|
| 126 |
-
musical_item = chord.Chord(group_pitches, quarterLength=duration)
|
| 127 |
-
if show_solfege:
|
| 128 |
-
musical_item.addLyric(" ".join(SOLFEGE_NAMES[pitch % 12] for pitch in group_pitches))
|
| 129 |
-
musical_item.volume.velocity = round(sum(velocities[(offset, duration)]) / len(velocities[(offset, duration)]))
|
| 130 |
-
part.insert(offset, musical_item)
|
| 131 |
-
|
| 132 |
-
part.quantize(
|
| 133 |
-
quarterLengthDivisors=(divisor,),
|
| 134 |
-
processOffsets=True,
|
| 135 |
-
processDurations=True,
|
| 136 |
-
inPlace=True,
|
| 137 |
)
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
if not score.parts:
|
| 143 |
raise ValueError("Aucune note n’est disponible pour générer une partition.")
|
| 144 |
-
return score
|
| 145 |
|
| 146 |
|
| 147 |
def _render_svg_pages(musicxml: Path, output_dir: Path) -> tuple[tuple[Path, ...], str]:
|
|
@@ -208,7 +595,7 @@ def generate_notation(
|
|
| 208 |
midi_files: list[str] | None = None,
|
| 209 |
) -> NotationResult:
|
| 210 |
output_dir = Path(tempfile.mkdtemp(prefix="muscriptor-notation-"))
|
| 211 |
-
score = _music21_score(
|
| 212 |
tracks,
|
| 213 |
title=title,
|
| 214 |
tempo_bpm=tempo_bpm,
|
|
@@ -222,15 +609,30 @@ def generate_notation(
|
|
| 222 |
|
| 223 |
parts_dir = output_dir / "parties"
|
| 224 |
parts_dir.mkdir()
|
|
|
|
| 225 |
from music21 import stream as music21_stream
|
| 226 |
|
| 227 |
part_results: list[NotationPartResult] = []
|
| 228 |
-
for part_index,
|
| 229 |
-
|
|
|
|
| 230 |
part_slug = f"{part_index:02d}-{_safe_slug(part_name)}"
|
| 231 |
-
part_score = music21_stream.Score(id=f"score-{
|
| 232 |
part_score.metadata = copy.deepcopy(score.metadata)
|
| 233 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
part_musicxml = parts_dir / f"{part_slug}.musicxml"
|
| 235 |
part_score.write("musicxml", fp=str(part_musicxml))
|
| 236 |
|
|
@@ -238,8 +640,13 @@ def generate_notation(
|
|
| 238 |
part_svg_dir.mkdir()
|
| 239 |
part_svg_pages, _ = _render_svg_pages(part_musicxml, part_svg_dir)
|
| 240 |
part_pdf = _render_pdf(part_svg_pages, parts_dir / f"{part_slug}.pdf")
|
|
|
|
|
|
|
|
|
|
| 241 |
part_results.append(
|
| 242 |
NotationPartResult(
|
|
|
|
|
|
|
| 243 |
name=part_name,
|
| 244 |
slug=part_slug,
|
| 245 |
musicxml=part_musicxml,
|
|
|
|
| 4 |
import copy
|
| 5 |
import io
|
| 6 |
import json
|
| 7 |
+
import math
|
| 8 |
import re
|
|
|
|
| 9 |
import tempfile
|
| 10 |
import zipfile
|
| 11 |
from dataclasses import dataclass
|
|
|
|
| 25 |
|
| 26 |
@dataclass(frozen=True)
|
| 27 |
class NotationPartResult:
|
| 28 |
+
track_id: str
|
| 29 |
+
track_key: str
|
| 30 |
name: str
|
| 31 |
slug: str
|
| 32 |
musicxml: Path
|
|
|
|
| 44 |
parts: tuple[NotationPartResult, ...]
|
| 45 |
|
| 46 |
|
| 47 |
+
def estimate_score_settings(tracks: list[dict[str, Any]]) -> dict[str, Any]:
|
| 48 |
+
"""Estimate notation presets from the decoded note onsets.
|
| 49 |
+
|
| 50 |
+
The estimate intentionally runs after transcription: instrument-separated
|
| 51 |
+
note onsets are substantially cleaner than a broadband audio onset curve,
|
| 52 |
+
and this keeps the Space free from another heavyweight audio model.
|
| 53 |
+
"""
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
import numpy as np
|
| 57 |
+
except ImportError: # pragma: no cover - numpy is an app dependency
|
| 58 |
+
return {
|
| 59 |
+
"tempo_bpm": 120.0,
|
| 60 |
+
"time_signature": "4/4",
|
| 61 |
+
"quantization": "1/16",
|
| 62 |
+
"confidence": "low",
|
| 63 |
+
"onset_count": 0,
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
events: list[tuple[float, float, bool]] = []
|
| 67 |
+
durations: list[float] = []
|
| 68 |
+
for track in tracks:
|
| 69 |
+
is_drum = bool(track.get("is_drum"))
|
| 70 |
+
for item in track.get("notes") or []:
|
| 71 |
+
start = max(0.0, float(item.get("start", 0.0)))
|
| 72 |
+
end = max(start + 0.015, float(item.get("end", start + 0.1)))
|
| 73 |
+
velocity = max(1.0, min(127.0, float(item.get("velocity", 90.0)))) / 127.0
|
| 74 |
+
events.append((start, velocity * (1.35 if is_drum else 1.0), is_drum))
|
| 75 |
+
durations.append(end - start)
|
| 76 |
+
|
| 77 |
+
if len(events) < 4:
|
| 78 |
+
return {
|
| 79 |
+
"tempo_bpm": 120.0,
|
| 80 |
+
"time_signature": "4/4",
|
| 81 |
+
"quantization": "1/16",
|
| 82 |
+
"confidence": "low",
|
| 83 |
+
"onset_count": len(events),
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
# Collapse chords and near-simultaneous detections into one accented onset.
|
| 87 |
+
events.sort(key=lambda event: event[0])
|
| 88 |
+
clusters: list[list[tuple[float, float, bool]]] = []
|
| 89 |
+
for event in events:
|
| 90 |
+
if not clusters or event[0] - clusters[-1][-1][0] > 0.035:
|
| 91 |
+
clusters.append([event])
|
| 92 |
+
else:
|
| 93 |
+
clusters[-1].append(event)
|
| 94 |
+
onsets = np.asarray(
|
| 95 |
+
[sum(event[0] * event[1] for event in cluster) / sum(event[1] for event in cluster) for cluster in clusters],
|
| 96 |
+
dtype=float,
|
| 97 |
+
)
|
| 98 |
+
accents = np.asarray(
|
| 99 |
+
[max(event[1] for event in cluster) + 0.18 * sum(event[1] for event in cluster[1:]) for cluster in clusters],
|
| 100 |
+
dtype=float,
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
bpm_axis = np.arange(45.0, 221.0, 0.5)
|
| 104 |
+
tempo_scores = np.zeros_like(bpm_axis)
|
| 105 |
+
beat_hypotheses = ((0.25, 0.22), (0.5, 0.82), (1.0, 1.0), (1.5, 0.62), (2.0, 0.7), (3.0, 0.42), (4.0, 0.3))
|
| 106 |
+
for left in range(len(onsets) - 1):
|
| 107 |
+
for right in range(left + 1, min(len(onsets), left + 9)):
|
| 108 |
+
interval = onsets[right] - onsets[left]
|
| 109 |
+
if interval > 4.5:
|
| 110 |
+
break
|
| 111 |
+
if interval < 0.08:
|
| 112 |
+
continue
|
| 113 |
+
pair_weight = math.sqrt(accents[left] * accents[right]) / math.sqrt(right - left)
|
| 114 |
+
for beats, hypothesis_weight in beat_hypotheses:
|
| 115 |
+
bpm = 60.0 * beats / interval
|
| 116 |
+
if bpm_axis[0] <= bpm <= bpm_axis[-1]:
|
| 117 |
+
distance = (bpm_axis - bpm) / 1.15
|
| 118 |
+
tempo_scores += pair_weight * hypothesis_weight * np.exp(-0.5 * distance * distance)
|
| 119 |
+
|
| 120 |
+
tempo_prior = 0.72 + 0.28 * np.exp(-0.5 * ((bpm_axis - 112.0) / 52.0) ** 2)
|
| 121 |
+
tempo_scores *= tempo_prior
|
| 122 |
+
best_index = int(np.argmax(tempo_scores))
|
| 123 |
+
best_bpm = float(bpm_axis[best_index])
|
| 124 |
+
if best_bpm < 74 and best_bpm * 2 <= bpm_axis[-1]:
|
| 125 |
+
double_index = int(np.argmin(np.abs(bpm_axis - best_bpm * 2)))
|
| 126 |
+
if tempo_scores[double_index] >= tempo_scores[best_index] * 0.78:
|
| 127 |
+
best_index = double_index
|
| 128 |
+
best_bpm = float(bpm_axis[best_index])
|
| 129 |
+
if best_bpm > 178:
|
| 130 |
+
half_index = int(np.argmin(np.abs(bpm_axis - best_bpm / 2)))
|
| 131 |
+
if tempo_scores[half_index] >= tempo_scores[best_index] * 0.88:
|
| 132 |
+
best_index = half_index
|
| 133 |
+
best_bpm = float(bpm_axis[best_index])
|
| 134 |
+
neighborhood = np.abs(bpm_axis - best_bpm) <= 2.0
|
| 135 |
+
if float(tempo_scores[neighborhood].sum()) > 0:
|
| 136 |
+
best_bpm = float(np.average(bpm_axis[neighborhood], weights=tempo_scores[neighborhood]))
|
| 137 |
+
|
| 138 |
+
beat_seconds = 60.0 / max(1.0, best_bpm)
|
| 139 |
+
strong_threshold = float(np.quantile(accents, 0.55))
|
| 140 |
+
strong = accents >= strong_threshold
|
| 141 |
+
phase_candidates = np.linspace(0.0, beat_seconds, 96, endpoint=False)
|
| 142 |
+
phase_scores = []
|
| 143 |
+
for phase in phase_candidates:
|
| 144 |
+
distance = np.abs(((onsets[strong] - phase + beat_seconds / 2) % beat_seconds) - beat_seconds / 2)
|
| 145 |
+
phase_scores.append(float(np.sum(accents[strong] * np.exp(-0.5 * (distance / (beat_seconds * 0.11)) ** 2))))
|
| 146 |
+
beat_phase = float(phase_candidates[int(np.argmax(phase_scores))])
|
| 147 |
+
|
| 148 |
+
beat_indices = np.rint((onsets - beat_phase) / beat_seconds).astype(int)
|
| 149 |
+
minimum_beat = int(beat_indices.min())
|
| 150 |
+
beat_indices -= minimum_beat
|
| 151 |
+
beat_energy = np.zeros(int(beat_indices.max()) + 1, dtype=float)
|
| 152 |
+
for index, accent in zip(beat_indices, accents):
|
| 153 |
+
beat_energy[index] += accent
|
| 154 |
+
|
| 155 |
+
meter_scores: dict[int, float] = {}
|
| 156 |
+
# Common time is deliberately the tie-breaker: sparse arrangements often
|
| 157 |
+
# make 2- and 4-beat periodicities mathematically indistinguishable.
|
| 158 |
+
meter_priors = {2: 0.0, 3: 0.08, 4: 0.32}
|
| 159 |
+
energy_mean = float(beat_energy.mean()) + 1e-9
|
| 160 |
+
for meter_value in (2, 3, 4):
|
| 161 |
+
best_meter_score = -1e9
|
| 162 |
+
for bar_phase in range(meter_value):
|
| 163 |
+
downbeats = beat_energy[bar_phase::meter_value]
|
| 164 |
+
others = np.asarray(
|
| 165 |
+
[value for index, value in enumerate(beat_energy) if index % meter_value != bar_phase],
|
| 166 |
+
dtype=float,
|
| 167 |
+
)
|
| 168 |
+
contrast = (float(downbeats.mean()) - float(others.mean() if len(others) else 0.0)) / energy_mean
|
| 169 |
+
periodicity = 0.0
|
| 170 |
+
if len(beat_energy) > meter_value * 2:
|
| 171 |
+
left = beat_energy[:-meter_value]
|
| 172 |
+
right = beat_energy[meter_value:]
|
| 173 |
+
if float(left.std()) > 1e-6 and float(right.std()) > 1e-6:
|
| 174 |
+
periodicity = float(np.corrcoef(left, right)[0, 1])
|
| 175 |
+
best_meter_score = max(best_meter_score, contrast * 0.72 + periodicity * 0.28)
|
| 176 |
+
meter_scores[meter_value] = best_meter_score + meter_priors[meter_value]
|
| 177 |
+
meter = max(meter_scores, key=meter_scores.get)
|
| 178 |
+
|
| 179 |
+
relative = (onsets - beat_phase) / beat_seconds
|
| 180 |
+
offbeat = np.abs(relative - np.rint(relative)) > 0.08
|
| 181 |
+
compound = False
|
| 182 |
+
if int(np.count_nonzero(offbeat)) >= max(3, len(onsets) // 8):
|
| 183 |
+
triple_error = np.abs(relative[offbeat] * 3 - np.rint(relative[offbeat] * 3))
|
| 184 |
+
binary_error = np.minimum(
|
| 185 |
+
np.abs(relative[offbeat] * 2 - np.rint(relative[offbeat] * 2)),
|
| 186 |
+
np.abs(relative[offbeat] * 4 - np.rint(relative[offbeat] * 4)),
|
| 187 |
+
)
|
| 188 |
+
compound = float(np.median(triple_error)) + 0.035 < float(np.median(binary_error))
|
| 189 |
+
if meter == 3:
|
| 190 |
+
time_signature = "3/4"
|
| 191 |
+
elif compound and meter == 2:
|
| 192 |
+
time_signature = "6/8"
|
| 193 |
+
elif compound and meter == 4:
|
| 194 |
+
time_signature = "12/8"
|
| 195 |
+
else:
|
| 196 |
+
time_signature = f"{meter}/4"
|
| 197 |
+
|
| 198 |
+
starts = np.asarray([event[0] for event in events], dtype=float)
|
| 199 |
+
note_durations = np.asarray(durations, dtype=float)
|
| 200 |
+
quantization = "1/32"
|
| 201 |
+
quantization_fits: dict[str, float] = {}
|
| 202 |
+
for label, divisor in QUANTIZATION_DIVISORS.items():
|
| 203 |
+
grid = beat_seconds / divisor
|
| 204 |
+
start_units = (starts - beat_phase) / grid
|
| 205 |
+
duration_units = note_durations / grid
|
| 206 |
+
start_error = np.abs(start_units - np.rint(start_units))
|
| 207 |
+
duration_error = np.abs(duration_units - np.rint(duration_units))
|
| 208 |
+
# Note-off timing is expressive in real performances; onset placement
|
| 209 |
+
# is the much stronger signal for choosing an engraving grid.
|
| 210 |
+
fit = 0.9 * float(np.mean(start_error <= 0.2)) + 0.1 * float(np.mean(duration_error <= 0.22))
|
| 211 |
+
quantization_fits[label] = fit
|
| 212 |
+
if fit >= 0.78:
|
| 213 |
+
quantization = label
|
| 214 |
+
break
|
| 215 |
+
|
| 216 |
+
peak = float(tempo_scores[best_index])
|
| 217 |
+
baseline = float(np.quantile(tempo_scores, 0.75)) + 1e-9
|
| 218 |
+
tempo_separation = max(0.0, min(1.0, (peak - baseline) / (peak + 1e-9)))
|
| 219 |
+
confidence = "high" if len(onsets) >= 24 and tempo_separation >= 0.55 else "medium" if len(onsets) >= 10 else "low"
|
| 220 |
+
return {
|
| 221 |
+
"tempo_bpm": round(best_bpm, 1),
|
| 222 |
+
"time_signature": time_signature,
|
| 223 |
+
"quantization": quantization,
|
| 224 |
+
"confidence": confidence,
|
| 225 |
+
"onset_count": int(len(onsets)),
|
| 226 |
+
"tempo_confidence": round(tempo_separation, 3),
|
| 227 |
+
"quantization_fit": round(quantization_fits[quantization], 3),
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
|
| 231 |
def _safe_slug(value: str) -> str:
|
| 232 |
return re.sub(r"[^a-z0-9]+", "-", value.lower()).strip("-") or "piste"
|
| 233 |
|
|
|
|
| 243 |
return parsed
|
| 244 |
|
| 245 |
|
| 246 |
+
def _instrument_for_track(track: dict[str, Any], instrument_module: Any, interval_module: Any) -> Any:
|
| 247 |
+
if track.get("is_drum"):
|
| 248 |
+
result = instrument_module.UnpitchedPercussion()
|
| 249 |
+
result.midiChannel = 9
|
| 250 |
+
else:
|
| 251 |
+
program = max(0, min(127, int(track.get("program") or 0)))
|
| 252 |
+
try:
|
| 253 |
+
result = instrument_module.instrumentFromMidiProgram(program)
|
| 254 |
+
except Exception:
|
| 255 |
+
result = instrument_module.Instrument()
|
| 256 |
+
result.midiProgram = program
|
| 257 |
+
octave_transposers = tuple(
|
| 258 |
+
cls
|
| 259 |
+
for cls in (
|
| 260 |
+
getattr(instrument_module, "Guitar", None),
|
| 261 |
+
getattr(instrument_module, "AcousticGuitar", None),
|
| 262 |
+
getattr(instrument_module, "ElectricGuitar", None),
|
| 263 |
+
getattr(instrument_module, "AcousticBass", None),
|
| 264 |
+
getattr(instrument_module, "ElectricBass", None),
|
| 265 |
+
getattr(instrument_module, "Contrabass", None),
|
| 266 |
+
)
|
| 267 |
+
if cls is not None
|
| 268 |
+
)
|
| 269 |
+
if octave_transposers and isinstance(result, octave_transposers):
|
| 270 |
+
result.transposition = interval_module.Interval("P-8")
|
| 271 |
+
name = str(track.get("name") or "Instrument")
|
| 272 |
+
result.partName = name
|
| 273 |
+
result.instrumentName = name
|
| 274 |
+
return result
|
| 275 |
+
|
| 276 |
+
|
| 277 |
+
def _clef_for_track(
|
| 278 |
+
track: dict[str, Any],
|
| 279 |
+
midi_instrument: Any,
|
| 280 |
+
pitches: list[int],
|
| 281 |
+
clef_module: Any,
|
| 282 |
+
instrument_module: Any,
|
| 283 |
+
staff_role: str | None = None,
|
| 284 |
+
) -> Any:
|
| 285 |
+
if track.get("is_drum"):
|
| 286 |
+
return clef_module.PercussionClef()
|
| 287 |
+
if staff_role == "upper":
|
| 288 |
+
return clef_module.TrebleClef()
|
| 289 |
+
if staff_role == "lower":
|
| 290 |
+
return clef_module.BassClef()
|
| 291 |
+
if isinstance(midi_instrument, instrument_module.Viola):
|
| 292 |
+
return clef_module.AltoClef()
|
| 293 |
+
bass_classes = tuple(
|
| 294 |
+
cls
|
| 295 |
+
for cls in (
|
| 296 |
+
getattr(instrument_module, "AcousticBass", None),
|
| 297 |
+
getattr(instrument_module, "ElectricBass", None),
|
| 298 |
+
getattr(instrument_module, "Contrabass", None),
|
| 299 |
+
getattr(instrument_module, "Violoncello", None),
|
| 300 |
+
getattr(instrument_module, "Bassoon", None),
|
| 301 |
+
getattr(instrument_module, "Trombone", None),
|
| 302 |
+
getattr(instrument_module, "BassTrombone", None),
|
| 303 |
+
getattr(instrument_module, "Tuba", None),
|
| 304 |
+
)
|
| 305 |
+
if cls is not None
|
| 306 |
+
)
|
| 307 |
+
if bass_classes and isinstance(midi_instrument, bass_classes):
|
| 308 |
+
return clef_module.BassClef()
|
| 309 |
+
guitar_classes = tuple(
|
| 310 |
+
cls
|
| 311 |
+
for cls in (
|
| 312 |
+
getattr(instrument_module, "Guitar", None),
|
| 313 |
+
getattr(instrument_module, "AcousticGuitar", None),
|
| 314 |
+
getattr(instrument_module, "ElectricGuitar", None),
|
| 315 |
+
)
|
| 316 |
+
if cls is not None
|
| 317 |
+
)
|
| 318 |
+
if guitar_classes and isinstance(midi_instrument, guitar_classes):
|
| 319 |
+
return clef_module.Treble8vbClef()
|
| 320 |
+
return clef_module.BassClef() if median(pitches) < 52 else clef_module.TrebleClef()
|
| 321 |
+
|
| 322 |
+
|
| 323 |
+
def _drum_note(pitch: int, duration: float, note_module: Any, instrument_module: Any) -> Any:
|
| 324 |
+
if pitch in {35, 36}:
|
| 325 |
+
display, class_name = "F4", "BassDrum"
|
| 326 |
+
elif pitch in {37, 38, 39, 40}:
|
| 327 |
+
display, class_name = "C5", "SnareDrum"
|
| 328 |
+
elif pitch in {41, 43, 45, 47, 48, 50}:
|
| 329 |
+
display, class_name = "A4", "TomTom"
|
| 330 |
+
elif pitch in {42, 44, 46}:
|
| 331 |
+
display, class_name = "G5", "HiHatCymbal"
|
| 332 |
+
elif pitch in {49, 51, 52, 53, 55, 57, 59}:
|
| 333 |
+
display, class_name = "A5", "Cymbals"
|
| 334 |
+
elif pitch == 54:
|
| 335 |
+
display, class_name = "E5", "Tambourine"
|
| 336 |
+
elif pitch == 56:
|
| 337 |
+
display, class_name = "D5", "Cowbell"
|
| 338 |
+
elif pitch in {60, 61}:
|
| 339 |
+
display, class_name = "F5", "BongoDrums"
|
| 340 |
+
elif pitch in {62, 63, 64}:
|
| 341 |
+
display, class_name = "E5", "CongaDrum"
|
| 342 |
+
else:
|
| 343 |
+
display, class_name = "B4", "UnpitchedPercussion"
|
| 344 |
+
stored_instrument = getattr(instrument_module, class_name)()
|
| 345 |
+
return note_module.Unpitched(
|
| 346 |
+
displayName=display,
|
| 347 |
+
storedInstrument=stored_instrument,
|
| 348 |
+
quarterLength=duration,
|
| 349 |
+
)
|
| 350 |
+
|
| 351 |
+
|
| 352 |
+
def _build_music21_staff(
|
| 353 |
+
track: dict[str, Any],
|
| 354 |
+
notes: list[dict[str, Any]],
|
| 355 |
+
*,
|
| 356 |
+
staff_id: str,
|
| 357 |
+
staff_role: str | None,
|
| 358 |
+
display_name: str,
|
| 359 |
+
bpm: float,
|
| 360 |
+
numerator: int,
|
| 361 |
+
denominator: int,
|
| 362 |
+
divisor: int,
|
| 363 |
+
show_solfege: bool,
|
| 364 |
+
add_tempo: bool,
|
| 365 |
+
modules: dict[str, Any],
|
| 366 |
+
) -> Any:
|
| 367 |
+
chord = modules["chord"]
|
| 368 |
+
clef = modules["clef"]
|
| 369 |
+
instrument = modules["instrument"]
|
| 370 |
+
interval = modules["interval"]
|
| 371 |
+
meter = modules["meter"]
|
| 372 |
+
note = modules["note"]
|
| 373 |
+
percussion = modules["percussion"]
|
| 374 |
+
stream = modules["stream"]
|
| 375 |
+
tempo = modules["tempo"]
|
| 376 |
+
|
| 377 |
+
part_class = stream.PartStaff if staff_role else stream.Part
|
| 378 |
+
part = part_class(id=staff_id)
|
| 379 |
+
part.partName = display_name
|
| 380 |
+
midi_instrument = _instrument_for_track(track, instrument, interval)
|
| 381 |
+
midi_instrument.partName = display_name
|
| 382 |
+
part.insert(0, midi_instrument)
|
| 383 |
+
if add_tempo:
|
| 384 |
+
part.insert(0, tempo.MetronomeMark(number=bpm))
|
| 385 |
+
part.insert(0, meter.TimeSignature(f"{numerator}/{denominator}"))
|
| 386 |
+
pitches = [max(0, min(127, int(item.get("pitch", 60)))) for item in notes]
|
| 387 |
+
part.insert(0, _clef_for_track(track, midi_instrument, pitches, clef, instrument, staff_role))
|
| 388 |
+
|
| 389 |
+
grouped: dict[tuple[float, float], list[tuple[int, int]]] = {}
|
| 390 |
+
for item in notes:
|
| 391 |
+
start_seconds = max(0.0, float(item.get("start", 0.0)))
|
| 392 |
+
end_seconds = max(start_seconds + 0.03, float(item.get("end", start_seconds + 0.1)))
|
| 393 |
+
offset = start_seconds * bpm / 60.0
|
| 394 |
+
duration = max(1 / divisor, (end_seconds - start_seconds) * bpm / 60.0)
|
| 395 |
+
key = (round(offset, 5), round(duration, 5))
|
| 396 |
+
grouped.setdefault(key, []).append(
|
| 397 |
+
(
|
| 398 |
+
max(0, min(127, int(item.get("pitch", 60)))),
|
| 399 |
+
max(1, min(127, int(item.get("velocity", 100)))),
|
| 400 |
+
)
|
| 401 |
+
)
|
| 402 |
+
|
| 403 |
+
for (offset, duration), values in sorted(grouped.items()):
|
| 404 |
+
group_pitches = [value[0] for value in values]
|
| 405 |
+
velocities = [value[1] for value in values]
|
| 406 |
+
if track.get("is_drum"):
|
| 407 |
+
unpitched = [_drum_note(pitch, duration, note, instrument) for pitch in group_pitches]
|
| 408 |
+
musical_item = (
|
| 409 |
+
unpitched[0]
|
| 410 |
+
if len(unpitched) == 1
|
| 411 |
+
else percussion.PercussionChord(unpitched, quarterLength=duration)
|
| 412 |
+
)
|
| 413 |
+
elif len(group_pitches) == 1:
|
| 414 |
+
musical_item = note.Note(group_pitches[0], quarterLength=duration)
|
| 415 |
+
if show_solfege:
|
| 416 |
+
musical_item.addLyric(SOLFEGE_NAMES[group_pitches[0] % 12])
|
| 417 |
+
else:
|
| 418 |
+
unique_pitches = list(dict.fromkeys(group_pitches))
|
| 419 |
+
musical_item = chord.Chord(unique_pitches, quarterLength=duration)
|
| 420 |
+
if show_solfege:
|
| 421 |
+
musical_item.addLyric(" ".join(SOLFEGE_NAMES[pitch % 12] for pitch in unique_pitches))
|
| 422 |
+
musical_item.volume.velocity = round(sum(velocities) / len(velocities))
|
| 423 |
+
part.insert(offset, musical_item)
|
| 424 |
+
|
| 425 |
+
part.quantize(
|
| 426 |
+
quarterLengthDivisors=(divisor,),
|
| 427 |
+
processOffsets=True,
|
| 428 |
+
processDurations=True,
|
| 429 |
+
inPlace=True,
|
| 430 |
+
)
|
| 431 |
+
measured = part.makeMeasures(inPlace=False)
|
| 432 |
+
measured.makeNotation(inPlace=True)
|
| 433 |
+
# MuScriptor pitches are concert/sounding pitches. Converting here gives
|
| 434 |
+
# clarinet, saxophone, trumpet and horn players proper written parts while
|
| 435 |
+
# keeping MusicXML playback at the original sounding pitch.
|
| 436 |
+
measured.atSoundingPitch = True
|
| 437 |
+
measured.toWrittenPitch(inPlace=True)
|
| 438 |
+
return measured
|
| 439 |
+
|
| 440 |
+
|
| 441 |
def _music21_score(
|
| 442 |
tracks: list[dict[str, Any]],
|
| 443 |
*,
|
|
|
|
| 446 |
time_signature: str,
|
| 447 |
quantization: str,
|
| 448 |
show_solfege: bool,
|
| 449 |
+
) -> tuple[Any, list[dict[str, Any]]]:
|
| 450 |
try:
|
| 451 |
+
from music21 import chord, clef, instrument, interval, layout, metadata, meter, note, percussion, stream, tempo
|
| 452 |
except ImportError as exc: # pragma: no cover - deployment configuration
|
| 453 |
raise RuntimeError("La dépendance music21 est nécessaire pour générer la partition.") from exc
|
| 454 |
|
| 455 |
+
modules = {
|
| 456 |
+
"chord": chord,
|
| 457 |
+
"clef": clef,
|
| 458 |
+
"instrument": instrument,
|
| 459 |
+
"interval": interval,
|
| 460 |
+
"meter": meter,
|
| 461 |
+
"note": note,
|
| 462 |
+
"percussion": percussion,
|
| 463 |
+
"stream": stream,
|
| 464 |
+
"tempo": tempo,
|
| 465 |
+
}
|
| 466 |
bpm = max(20.0, min(300.0, float(tempo_bpm)))
|
| 467 |
numerator, denominator = _parse_time_signature(time_signature)
|
| 468 |
divisor = QUANTIZATION_DIVISORS.get(quantization, 4)
|
|
|
|
| 471 |
score.metadata = metadata.Metadata()
|
| 472 |
score.metadata.title = title or "Transcription MuScriptor"
|
| 473 |
score.metadata.composer = "Transcription automatique MuScriptor"
|
| 474 |
+
built_tracks: list[dict[str, Any]] = []
|
| 475 |
|
| 476 |
for track_index, track in enumerate(tracks):
|
| 477 |
+
track_notes = list(track.get("notes") or [])
|
| 478 |
+
if not track_notes:
|
| 479 |
continue
|
| 480 |
+
track_name = str(track.get("name") or f"Instrument {track_index + 1}")
|
| 481 |
+
key = str(track.get("key") or "").lower()
|
| 482 |
+
pitches = [int(item.get("pitch", 60)) for item in track_notes]
|
| 483 |
+
grand_staff = (
|
| 484 |
+
any(token in key for token in ("piano", "keyboard", "organ", "harp"))
|
| 485 |
+
and min(pitches) < 60 <= max(pitches)
|
| 486 |
+
and len(track_notes) >= 4
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 487 |
)
|
| 488 |
+
parts: list[Any] = []
|
| 489 |
+
if grand_staff:
|
| 490 |
+
staff_specs = (
|
| 491 |
+
("upper", [item for item in track_notes if int(item.get("pitch", 60)) >= 60], track_name),
|
| 492 |
+
("lower", [item for item in track_notes if int(item.get("pitch", 60)) < 60], ""),
|
| 493 |
+
)
|
| 494 |
+
else:
|
| 495 |
+
staff_specs = ((None, track_notes, track_name),)
|
| 496 |
+
|
| 497 |
+
for staff_number, (staff_role, staff_notes, display_name) in enumerate(staff_specs, start=1):
|
| 498 |
+
if not staff_notes:
|
| 499 |
+
continue
|
| 500 |
+
measured = _build_music21_staff(
|
| 501 |
+
track,
|
| 502 |
+
staff_notes,
|
| 503 |
+
staff_id=f"track-{track_index + 1}-staff-{staff_number}",
|
| 504 |
+
staff_role=staff_role,
|
| 505 |
+
display_name=display_name,
|
| 506 |
+
bpm=bpm,
|
| 507 |
+
numerator=numerator,
|
| 508 |
+
denominator=denominator,
|
| 509 |
+
divisor=divisor,
|
| 510 |
+
show_solfege=show_solfege,
|
| 511 |
+
add_tempo=staff_number == 1,
|
| 512 |
+
modules=modules,
|
| 513 |
+
)
|
| 514 |
+
score.insert(0, measured)
|
| 515 |
+
parts.append(measured)
|
| 516 |
+
if len(parts) > 1:
|
| 517 |
+
score.insert(
|
| 518 |
+
0,
|
| 519 |
+
layout.StaffGroup(
|
| 520 |
+
*parts,
|
| 521 |
+
name=track_name,
|
| 522 |
+
abbreviation=track_name[:8],
|
| 523 |
+
symbol="brace",
|
| 524 |
+
barTogether=True,
|
| 525 |
+
),
|
| 526 |
+
)
|
| 527 |
+
built_tracks.append({"track": track, "name": track_name, "parts": tuple(parts)})
|
| 528 |
|
| 529 |
if not score.parts:
|
| 530 |
raise ValueError("Aucune note n’est disponible pour générer une partition.")
|
| 531 |
+
return score, built_tracks
|
| 532 |
|
| 533 |
|
| 534 |
def _render_svg_pages(musicxml: Path, output_dir: Path) -> tuple[tuple[Path, ...], str]:
|
|
|
|
| 595 |
midi_files: list[str] | None = None,
|
| 596 |
) -> NotationResult:
|
| 597 |
output_dir = Path(tempfile.mkdtemp(prefix="muscriptor-notation-"))
|
| 598 |
+
score, built_tracks = _music21_score(
|
| 599 |
tracks,
|
| 600 |
title=title,
|
| 601 |
tempo_bpm=tempo_bpm,
|
|
|
|
| 609 |
|
| 610 |
parts_dir = output_dir / "parties"
|
| 611 |
parts_dir.mkdir()
|
| 612 |
+
from music21 import layout as music21_layout
|
| 613 |
from music21 import stream as music21_stream
|
| 614 |
|
| 615 |
part_results: list[NotationPartResult] = []
|
| 616 |
+
for part_index, built_track in enumerate(built_tracks, start=1):
|
| 617 |
+
source_track = built_track["track"]
|
| 618 |
+
part_name = str(built_track["name"] or f"Instrument {part_index}")
|
| 619 |
part_slug = f"{part_index:02d}-{_safe_slug(part_name)}"
|
| 620 |
+
part_score = music21_stream.Score(id=f"score-track-{part_index}")
|
| 621 |
part_score.metadata = copy.deepcopy(score.metadata)
|
| 622 |
+
copied_parts = [copy.deepcopy(part) for part in built_track["parts"]]
|
| 623 |
+
for copied_part in copied_parts:
|
| 624 |
+
part_score.insert(0, copied_part)
|
| 625 |
+
if len(copied_parts) > 1:
|
| 626 |
+
part_score.insert(
|
| 627 |
+
0,
|
| 628 |
+
music21_layout.StaffGroup(
|
| 629 |
+
*copied_parts,
|
| 630 |
+
name=part_name,
|
| 631 |
+
abbreviation=part_name[:8],
|
| 632 |
+
symbol="brace",
|
| 633 |
+
barTogether=True,
|
| 634 |
+
),
|
| 635 |
+
)
|
| 636 |
part_musicxml = parts_dir / f"{part_slug}.musicxml"
|
| 637 |
part_score.write("musicxml", fp=str(part_musicxml))
|
| 638 |
|
|
|
|
| 640 |
part_svg_dir.mkdir()
|
| 641 |
part_svg_pages, _ = _render_svg_pages(part_musicxml, part_svg_dir)
|
| 642 |
part_pdf = _render_pdf(part_svg_pages, parts_dir / f"{part_slug}.pdf")
|
| 643 |
+
source_id = source_track.get("id")
|
| 644 |
+
if source_id is None:
|
| 645 |
+
source_id = source_track.get("key") or part_index
|
| 646 |
part_results.append(
|
| 647 |
NotationPartResult(
|
| 648 |
+
track_id=str(source_id),
|
| 649 |
+
track_key=str(source_track.get("key") or source_track.get("id") or part_slug),
|
| 650 |
name=part_name,
|
| 651 |
slug=part_slug,
|
| 652 |
musicxml=part_musicxml,
|
studio_component.py
CHANGED
|
@@ -29,6 +29,8 @@ def initial_studio_value() -> dict[str, Any]:
|
|
| 29 |
"tracks": [],
|
| 30 |
"full_midi": "",
|
| 31 |
"score_svg": "",
|
|
|
|
|
|
|
| 32 |
"score_page_urls": [],
|
| 33 |
"score_pages": 0,
|
| 34 |
"notation": {},
|
|
|
|
| 29 |
"tracks": [],
|
| 30 |
"full_midi": "",
|
| 31 |
"score_svg": "",
|
| 32 |
+
"score_pdf_url": "",
|
| 33 |
+
"score_parts": [],
|
| 34 |
"score_page_urls": [],
|
| 35 |
"score_pages": 0,
|
| 36 |
"notation": {},
|
studio_component.pyi
CHANGED
|
@@ -18,15 +18,20 @@ def _read_frontend(name: str) -> str:
|
|
| 18 |
def initial_studio_value() -> dict[str, Any]:
|
| 19 |
return {
|
| 20 |
"state": "idle",
|
| 21 |
-
"status": "
|
| 22 |
"progress": 0,
|
| 23 |
"audio_name": "",
|
| 24 |
"elapsed": 0,
|
| 25 |
"duration": 0,
|
|
|
|
|
|
|
| 26 |
"note_count": 0,
|
| 27 |
"tracks": [],
|
| 28 |
"full_midi": "",
|
| 29 |
"score_svg": "",
|
|
|
|
|
|
|
|
|
|
| 30 |
"score_pages": 0,
|
| 31 |
"notation": {},
|
| 32 |
}
|
|
@@ -68,4 +73,4 @@ def normalize_viewer_value(value: Any) -> dict[str, Any]:
|
|
| 68 |
return parsed
|
| 69 |
except json.JSONDecodeError:
|
| 70 |
pass
|
| 71 |
-
return initial_studio_value()
|
|
|
|
| 18 |
def initial_studio_value() -> dict[str, Any]:
|
| 19 |
return {
|
| 20 |
"state": "idle",
|
| 21 |
+
"status": "Upload a recording to begin.",
|
| 22 |
"progress": 0,
|
| 23 |
"audio_name": "",
|
| 24 |
"elapsed": 0,
|
| 25 |
"duration": 0,
|
| 26 |
+
"available_until": 0,
|
| 27 |
+
"original_audio": "",
|
| 28 |
"note_count": 0,
|
| 29 |
"tracks": [],
|
| 30 |
"full_midi": "",
|
| 31 |
"score_svg": "",
|
| 32 |
+
"score_pdf_url": "",
|
| 33 |
+
"score_parts": [],
|
| 34 |
+
"score_page_urls": [],
|
| 35 |
"score_pages": 0,
|
| 36 |
"notation": {},
|
| 37 |
}
|
|
|
|
| 73 |
return parsed
|
| 74 |
except json.JSONDecodeError:
|
| 75 |
pass
|
| 76 |
+
return initial_studio_value()
|