File size: 7,819 Bytes
bbac2cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
"""
tiny-swara ONNX inference.

Expected repository layout:
    tiny-swara/
      inference.py
      config.json
      inventory.json
      lexicon.json
      tiny_voca/
        __init__.py
        text.py          # patched Bengali tiny-voca frontend
      onnx/
        tiny-swara-int8.onnx
        tiny-swara.onnx  # optional FP32 model

System dependency: espeak-ng
Python dependencies: numpy, onnxruntime

The frontend module must be the Bengali-patched tiny_voca.text from the
training/export environment. The ONNX graph itself accepts phoneme IDs,
not raw text.
"""

from __future__ import annotations

import argparse
import json
import os
import re
import wave
from pathlib import Path
from typing import Iterator

import numpy as np
import onnxruntime as ort

ROOT = Path(__file__).resolve().parent

# Set these BEFORE importing tiny_voca.text because it loads the inventory
# and lexicon when the module is imported.
os.environ["TINY_VOCA_INVENTORY"] = str(ROOT / "inventory.json")
os.environ["TINY_VOCA_LEXICON"] = str(ROOT / "lexicon.json")
os.environ.setdefault("TINY_VOCA_FRONTEND", "espeak")

from tiny_voca import text as frontend  # noqa: E402


def load_config() -> dict:
    path = ROOT / "config.json"
    if not path.exists():
        raise FileNotFoundError(f"Missing model config: {path}")
    return json.loads(path.read_text(encoding="utf-8"))


CONFIG = load_config()
SPEAKERS = CONFIG.get("speakers", ["BF", "BM"])
SAMPLE_RATE = int(CONFIG.get("sample_rate", 24000))


def resolve_model(model: str | Path | None = None) -> Path:
    if model:
        path = Path(model)
        if not path.is_absolute():
            path = ROOT / path
        if path.is_file():
            return path
        raise FileNotFoundError(f"Model file not found: {path}")

    candidates = [
        ROOT / "onnx" / "tiny-swara-int8.onnx",
        ROOT / "tiny-swara-int8.onnx",
        ROOT / "onnx" / "tiny-swara.onnx",
        ROOT / "tiny-swara.onnx",
        ROOT / "tiny-bn-int8.onnx",
        ROOT / "tiny-bn.onnx",
    ]
    for path in candidates:
        if path.is_file():
            return path
    raise FileNotFoundError(
        "Could not find an ONNX model. Put tiny-swara-int8.onnx in onnx/ "
        "or pass --model PATH."
    )


def split_sentences(text: str) -> list[str]:
    """Split at sentence endings while retaining punctuation."""
    text = re.sub(r"\s+", " ", text).strip()
    if not text:
        return []

    parts = re.split(r"(?<=[.!?।…])\s+", text)
    result = []
    for part in parts:
        part = part.strip()
        if not part:
            continue
        if not re.search(r"[.!?।…][\"'’”)\]]*$", part):
            part += "."
        result.append(part)
    return result


class TinySwara:
    def __init__(
        self,
        model: str | Path | None = None,
        voice: str = "BF",
        speed: float = 1.0,
        pitch_shift: float = 0.0,
        pitch_range: float = 1.0,
        providers: list[str] | None = None,
    ):
        if voice not in SPEAKERS:
            raise ValueError(
                f"Unknown voice {voice!r}. Available voices: {', '.join(SPEAKERS)}"
            )

        self.voice = voice
        self.voice_id = SPEAKERS.index(voice)
        self.speed = float(speed)
        self.pitch_shift = float(pitch_shift)
        self.pitch_range = float(pitch_range)
        self.model_path = resolve_model(model)

        self.session = ort.InferenceSession(
            str(self.model_path),
            providers=providers or ["CPUExecutionProvider"],
        )

        required_inputs = {
            "tokens", "stress", "voice", "speed", "pitch_shift", "pitch_range"
        }
        actual_inputs = {item.name for item in self.session.get_inputs()}
        missing = required_inputs - actual_inputs
        if missing:
            raise RuntimeError(
                f"ONNX model is missing expected inputs: {sorted(missing)}. "
                f"Found: {sorted(actual_inputs)}"
            )

    def synthesize_chunk(self, text: str) -> np.ndarray:
        """Generate one sentence/utterance and return mono float32 audio."""
        phones = frontend.phonemize(text)

        if phones.unknown:
            raise ValueError(
                f"Unmapped phonemes in {text!r}: {phones.unknown}. "
                "Check inventory.json and lexicon.json."
            )

        inputs = {
            "tokens": np.asarray([phones.ids], dtype=np.int64),
            "stress": np.asarray([phones.stress], dtype=np.int64),
            "voice": np.asarray([self.voice_id], dtype=np.int64),
            "speed": np.asarray([self.speed], dtype=np.float32),
            "pitch_shift": np.asarray([self.pitch_shift], dtype=np.float32),
            "pitch_range": np.asarray([self.pitch_range], dtype=np.float32),
        }

        audio, _durations = self.session.run(["audio", "durations"], inputs)
        wave = np.asarray(audio[0], dtype=np.float32).reshape(-1)

        if wave.size == 0 or not np.isfinite(wave).all():
            raise RuntimeError("ONNX model returned empty or non-finite audio.")

        peak = float(np.max(np.abs(wave)))
        if peak > 0:
            wave = wave * (0.9 / peak)

        return wave

    def stream(self, text: str) -> Iterator[np.ndarray]:
        """
        Yield one generated sentence at a time.
        This is sentence-level streaming: each sentence is generated fully
        before it is yielded; it is not frame-by-frame neural streaming.
        """
        sentences = split_sentences(text)
        if not sentences:
            raise ValueError("Text is empty.")

        for sentence in sentences:
            yield self.synthesize_chunk(sentence)

    def synthesize(self, text: str) -> np.ndarray:
        """Generate a complete utterance by joining sentence audio chunks."""
        chunks = list(self.stream(text))
        return np.concatenate(chunks) if len(chunks) > 1 else chunks[0]

    def save(self, text: str, output_path: str | Path) -> Path:
        output_path = Path(output_path)
        if not output_path.is_absolute():
            output_path = Path.cwd() / output_path
        output_path.parent.mkdir(parents=True, exist_ok=True)
        audio = self.synthesize(text)
        pcm = np.clip(audio * 32767.0, -32768, 32767).astype("<i2")
        with wave.open(str(output_path), "wb") as f:
            f.setnchannels(1)
            f.setsampwidth(2)
            f.setframerate(SAMPLE_RATE)
            f.writeframes(pcm.tobytes())
        return output_path


def main() -> None:
    parser = argparse.ArgumentParser(description="tiny-swara Bengali ONNX TTS")
    parser.add_argument("--text", help="Text to synthesize")
    parser.add_argument("--text-file", help="UTF-8 text file to synthesize")
    parser.add_argument("--output", default="tiny-swara.wav")
    parser.add_argument("--model", help="Optional ONNX model path")
    parser.add_argument("--voice", default="BF", choices=SPEAKERS)
    parser.add_argument("--speed", type=float, default=1.0)
    parser.add_argument("--pitch-shift", type=float, default=0.0)
    parser.add_argument("--pitch-range", type=float, default=1.0)
    args = parser.parse_args()

    if bool(args.text) == bool(args.text_file):
        parser.error("Provide exactly one of --text or --text-file.")

    text = (
        Path(args.text_file).read_text(encoding="utf-8")
        if args.text_file
        else args.text
    )

    tts = TinySwara(
        model=args.model,
        voice=args.voice,
        speed=args.speed,
        pitch_shift=args.pitch_shift,
        pitch_range=args.pitch_range,
    )
    saved = tts.save(text, args.output)
    print(f"Saved: {saved} ({SAMPLE_RATE} Hz, voice={args.voice})")
    print(f"Model: {tts.model_path}")


if __name__ == "__main__":
    main()