Spaces:
Running
Running
File size: 5,041 Bytes
e699214 | 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 | <!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>Make chant audio · Reading Coach</title>
<style>
body{font:15px/1.5 system-ui,sans-serif;max-width:760px;margin:24px auto;padding:0 16px;color:#222}
h1{font-size:20px}.muted{color:#666}.btn{padding:8px 14px;border-radius:8px;border:1px solid #ccc;background:#fff;cursor:pointer}
.btn.primary{background:#2f6f5e;color:#fff;border-color:#2f6f5e}li{margin:2px 0}.ok{color:#2f6f5e}.bad{color:#b33}
.v{font-family:'Noto Sans Devanagari',sans-serif}
</style></head><body>
<h1>🎙 Make chant audio for a Sanskrit book</h1>
<p class="muted">Renders each verse with Vāgdhenu (IISc’s Sanskrit chant voice) inside this tab and stores it in the browser,
so the Reading Coach’s <b>🎵 Hear verse</b> button can play it. Verses that already have audio are skipped.
It is slow on a CPU (roughly 1–4 minutes per verse; faster if the browser has WebGPU). Keep this tab open; you can keep practising in the other tab.
The first run downloads the voice model (a few hundred MB) from huggingface.co.</p>
<p><select id="book" class="btn"></select> <button id="go" class="btn primary">Start</button> <button id="stop" class="btn">Stop</button></p>
<p id="status" class="muted"></p>
<ol id="list"></ol>
<script type="module">
const $ = id => document.getElementById(id);
// Reuse the app's own passage splitter and audio keys so the cache keys match exactly.
const html = await (await fetch('../index.html')).text();
new Function(html.match(/<script id="rc-core">([\s\S]*?)<\/script>/)[1])();
const C = window.RCCore;
const lib = JSON.parse(html.match(/<script type="application\/json" id="rc-library">([\s\S]*?)<\/script>/)[1].replace(/<\\\//g, '</'));
const audKey = s => String(s).normalize('NFC').replace(/[-‐–—\u200c\u200d]/g, '').replace(/\s+/g, ' ').trim();
const fnv = str => { let h = 0x811c9dc5; for (const b of new TextEncoder().encode(str)) { h ^= b; h = Math.imul(h, 0x01000193) >>> 0; } return h.toString(16).padStart(8, '0'); };
let man = { verses: {} }; try { man = await (await fetch('../audio/sa/manifest.json', { cache: 'no-cache' })).json(); } catch (e) {}
const books = lib.books.filter(b => /^sa_/.test(b.file));
$('book').innerHTML = books.map(b => `<option value="${b.file}">${b.title}</option>`).join('');
const want = new URLSearchParams(location.search).get('book'); if (want) $('book').value = want;
let stopping = false, voice = null;
$('stop').onclick = () => { stopping = true; $('status').textContent = 'Stopping after the current verse…'; };
async function show() {
const b = books.find(x => x.file === $('book').value); const P = C.splitBook(b.text).passages;
const cache = await caches.open('rc-verse-audio');
const items = [];
for (let i = 0; i < P.length; i++) {
const h = fnv(audKey(P[i].text)); const url = new URL('../verse-cache/' + h + '.wav', location.href).href;
const have = (man.verses && man.verses[h]) ? 'included' : (await cache.match(url)) ? 'made' : '';
items.push({ i, h, url, text: P[i].text, have });
}
$('list').innerHTML = items.map(it => `<li id="v${it.i}"><span class="v">${it.text.split('\n')[0].slice(0, 60)}…</span> <span class="${it.have ? 'ok' : 'muted'}">${it.have ? '✓ ' + it.have : 'no audio yet'}</span></li>`).join('');
return { items, cache };
}
$('book').onchange = show; await show();
$('go').onclick = async () => {
stopping = false; $('go').disabled = true;
const { items, cache } = await show();
const todo = items.filter(it => !it.have);
if (!todo.length) { $('status').textContent = 'Every verse of this book already has audio.'; $('go').disabled = false; return; }
try {
if (!voice) {
$('status').textContent = 'Loading the Vāgdhenu voice (first time: downloading the model)…';
const { default: sanskritTts } = await import('../vendor/vagdhenu/index.mjs');
voice = await sanskritTts(navigator.gpu ? 'onnx' : 'wasm');
}
for (const [n, it] of todo.entries()) {
if (stopping) break;
const t0 = performance.now();
$('status').textContent = `Rendering verse ${it.i + 1} (${n + 1} of ${todo.length} to do)…`;
const li = $('v' + it.i); li.lastElementChild.textContent = '⏳ rendering…';
const res = await voice.chant(it.text.replace(/[-‐–]/g, ''));
let blob = res.wav instanceof Blob ? res.wav : res.wav ? new Blob([res.wav.buffer || res.wav], { type: 'audio/wav' }) : await (await fetch(res.url)).blob();
await cache.put(it.url, new Response(blob, { headers: { 'Content-Type': 'audio/wav' } }));
li.lastElementChild.className = 'ok'; li.lastElementChild.textContent = `✓ made (${((performance.now() - t0) / 1000).toFixed(0)} s)`;
}
$('status').textContent = stopping ? 'Stopped.' : 'Done. Reload the Reading Coach tab to use the new audio.';
} catch (e) { $('status').innerHTML = '<span class="bad">Failed: ' + (e.message || e) + '</span>'; }
$('go').disabled = false;
};
</script></body></html>
|