File size: 1,987 Bytes
05c5e98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5368d5
05c5e98
e941edc
 
05c5e98
ba7f5ba
05c5e98
 
4638322
ceaec5c
05c5e98
 
 
 
88e1dfb
 
4638322
05c5e98
 
 
 
 
 
 
 
 
d0dc54c
 
05c5e98
 
 
 
d0dc54c
05c5e98
d0dc54c
05c5e98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
from __future__ import annotations

import json
from pathlib import Path
from typing import Any

import gradio as gr


ROOT = Path(__file__).resolve().parent
FRONTEND = ROOT / "frontend"


def _read_frontend(name: str) -> str:
    return (FRONTEND / name).read_text(encoding="utf-8")


def initial_studio_value() -> dict[str, Any]:
    return {
        "state": "idle",
        "status": "Upload a recording to begin.",
        "progress": 0,
        "completed_windows": 0,
        "total_windows": 0,
        "audio_name": "",
        "export_stem": "muscriptor",
        "elapsed": 0,
        "duration": 0,
        "available_until": 0,
        "original_audio": "",
        "note_count": 0,
        "tracks": [],
        "full_midi": "",
        "score_svg": "",
        "score_pdf_url": "",
        "score_parts": [],
        "score_page_urls": [],
        "score_pages": 0,
        "notation": {},
    }


class StudioViewer(gr.HTML):
    """MuScriptor result viewer implemented with Gradio's custom HTML API."""

    def __init__(self, value: Any | None = None, **kwargs: Any) -> None:
        synth_bundle = _read_frontend("spessasynth.bundle.js")
        viewer_script = _read_frontend("studio.js")
        super().__init__(
            value=value or initial_studio_value(),
            html_template=_read_frontend("studio.html"),
            css_template=_read_frontend("studio.css"),
            js_on_load=f"{synth_bundle}\n{viewer_script}",
            apply_default_css=False,
            container=False,
            **kwargs,
        )

    def api_info(self) -> dict[str, Any]:
        return {"type": "object"}


def normalize_viewer_value(value: Any) -> dict[str, Any]:
    if isinstance(value, dict):
        return value
    if isinstance(value, str):
        try:
            parsed = json.loads(value)
            if isinstance(parsed, dict):
                return parsed
        except json.JSONDecodeError:
            pass
    return initial_studio_value()