31 lines
2.0 KiB
JavaScript
31 lines
2.0 KiB
JavaScript
// Der Testsatz kommt ausschließlich vom Server; hier wird nur die Stimme gewählt.
|
|
(() => {
|
|
const status = document.getElementById('probe-status');
|
|
const csrf = document.querySelector('input[name=csrf]')?.value;
|
|
document.querySelectorAll('button.probe').forEach(button => button.addEventListener('click', async () => {
|
|
const select = button.parentElement.querySelector('.stimmen-modell');
|
|
const saved = button.dataset.gespeichert === '1';
|
|
const matches = o => o.dataset.art === (saved ? 'gespeichert' : 'eingebaut') || o.dataset.art === 'alle';
|
|
const model = matches(select.selectedOptions[0]) ? select.selectedOptions[0] : Array.from(select.options).find(matches);
|
|
if (!model) { status.textContent = 'Kein passendes installiertes Sprachmodell verfügbar.'; return; }
|
|
button.disabled = true;
|
|
status.textContent = 'Probehören wird geladen …';
|
|
try {
|
|
const response = await fetch('/ui/modelle/probe', {method: 'POST', headers: {'Content-Type': 'application/json', 'X-CSRF-Token': csrf}, credentials: 'same-origin', body: JSON.stringify({model: model.value, voice: button.dataset.stimme})});
|
|
if (!response.ok) throw new Error('Die Stimme konnte nicht probegehört werden.');
|
|
// PCM 24 kHz / 16 Bit / Mono, wie bei der bestehenden Vorlese-Route.
|
|
const pcm = await response.arrayBuffer();
|
|
const samples = new Int16Array(pcm);
|
|
const ctx = new AudioContext({sampleRate: 24000});
|
|
await ctx.resume();
|
|
const buffer = ctx.createBuffer(1, samples.length, 24000);
|
|
const channel = buffer.getChannelData(0);
|
|
for (let i = 0; i < samples.length; i++) channel[i] = samples[i] / 32768;
|
|
const source = ctx.createBufferSource(); source.buffer = buffer; source.connect(ctx.destination);
|
|
source.onended = () => ctx.close(); source.start();
|
|
status.textContent = 'Probehören läuft.';
|
|
} catch (_) { status.textContent = 'Die Stimme konnte nicht probegehört werden.'; }
|
|
finally { button.disabled = false; }
|
|
}));
|
|
})();
|