2026-11-13 10:48:13 +00:00
|
|
|
function tones() {
|
|
|
|
|
return {sounds: radioSet.sounds, squelch: radioSet.squelch, music: settings.music};
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-06 08:28:04 +00:00
|
|
|
function loudness(kind) {
|
2026-11-13 10:48:13 +00:00
|
|
|
return Voice.loudness(kind, tones());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function keepRadio(change) {
|
|
|
|
|
const before = radioSet.mode;
|
|
|
|
|
radioSet = RadioSet.valid({...radioSet, ...change});
|
|
|
|
|
keep(RadioSet.KEY, radioSet);
|
|
|
|
|
hush(before !== radioSet.mode);
|
|
|
|
|
drawRadioSet();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function hush(switched) {
|
|
|
|
|
const {item, source} = speech;
|
|
|
|
|
if (source && item && item.row && !allows(rendered(item, item.at), item.playing)) {
|
|
|
|
|
item.cut = true;
|
|
|
|
|
source.stop();
|
|
|
|
|
}
|
|
|
|
|
if (switched && radioSet.mode === "silence") {
|
|
|
|
|
silenceBeeps();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {object|undefined} step the step the radio set plans now at the index that plays.
|
|
|
|
|
* @param {string} kind word, speech or burst: what plays or is about to play there.
|
|
|
|
|
* @returns {boolean} whether that sound may play: the planned kind, or the burst standing in for a planned speech line.
|
|
|
|
|
*/
|
|
|
|
|
function allows(step, kind) {
|
|
|
|
|
return Boolean(step) && (step.kind === kind || (kind === "burst" && step.kind === "speech"));
|
2026-11-06 08:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function speechFailed(lines) {
|
|
|
|
|
if (Date.now() - speech.warned > SPEECH_QUIET) {
|
|
|
|
|
speech.warned = Date.now();
|
2026-11-08 17:26:51 +00:00
|
|
|
toast("Reading radio aloud failed", lines, false);
|
2026-11-06 08:28:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-14 21:23:12 +00:00
|
|
|
/**
|
|
|
|
|
* @param {...object} items the rows to read aloud.
|
|
|
|
|
*/
|
|
|
|
|
function say(...items) {
|
|
|
|
|
speech.pending = items.reduce((pending, item) => Voice.enqueue(pending, item, SPEECH_QUEUE), speech.pending);
|
2026-11-15 09:46:05 +00:00
|
|
|
preempt();
|
2026-11-06 08:28:04 +00:00
|
|
|
pump();
|
2026-11-11 23:39:17 +00:00
|
|
|
drawBubbles();
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-15 09:46:05 +00:00
|
|
|
function preempt() {
|
|
|
|
|
const {item, source} = speech;
|
|
|
|
|
const [first] = speech.pending;
|
|
|
|
|
if (item && source && item.playing === "morse" && first && Voice.rank(first) < Voice.rank(item)) {
|
|
|
|
|
speech.item = null;
|
|
|
|
|
speech.pending = Voice.enqueue(speech.pending, item, SPEECH_QUEUE);
|
|
|
|
|
source.stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-14 09:58:46 +00:00
|
|
|
/**
|
|
|
|
|
* @param {object} item the row that sounds now.
|
|
|
|
|
* @param {number} span how many of its unread words this sound says: Infinity for the spoken rest, 0 for a procedure word.
|
|
|
|
|
* @param {number} seconds how long the sound plays.
|
|
|
|
|
*/
|
|
|
|
|
function voicePace(item, span, seconds) {
|
|
|
|
|
const floor = wordsRead(item);
|
|
|
|
|
Object.assign(item, {floor, span: Math.min(span, saidOf(item).length - floor), started: performance.now(), duration: seconds, reading: false});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function readingPace(item) {
|
|
|
|
|
if (!item.reading) {
|
|
|
|
|
const floor = wordsRead(item);
|
|
|
|
|
const span = item.words.length - floor;
|
|
|
|
|
Object.assign(item, {floor, span, started: performance.now(), duration: Bubbles.reading(span), reading: true});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-11 23:39:17 +00:00
|
|
|
function readSilently(item, next) {
|
2026-11-14 09:58:46 +00:00
|
|
|
readingPace(item);
|
2026-11-11 23:39:17 +00:00
|
|
|
speech.current = item;
|
|
|
|
|
speech.timer = window.setTimeout(() => {
|
2026-11-14 09:58:46 +00:00
|
|
|
Object.assign(speech, {current: null, timer: 0, item: null});
|
2026-11-14 21:23:12 +00:00
|
|
|
try {
|
|
|
|
|
drawSpeaker();
|
|
|
|
|
} finally {
|
|
|
|
|
next();
|
|
|
|
|
}
|
2026-11-14 09:58:46 +00:00
|
|
|
}, Math.max(0, item.started + item.duration * 1000 - performance.now()));
|
2026-11-11 23:39:17 +00:00
|
|
|
drawSpeaker();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function endSilent() {
|
|
|
|
|
window.clearTimeout(speech.timer);
|
2026-11-14 09:58:46 +00:00
|
|
|
Object.assign(speech, {current: null, timer: 0, busy: false, item: null});
|
2026-11-11 23:39:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function dropSilent() {
|
|
|
|
|
speech.pending = speech.pending.filter((item) => !item.silent);
|
|
|
|
|
if (speech.timer) {
|
|
|
|
|
endSilent();
|
|
|
|
|
drawSpeaker();
|
|
|
|
|
pump();
|
|
|
|
|
}
|
2026-11-06 08:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-13 10:48:13 +00:00
|
|
|
function sounded(item, source, seconds) {
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
let timer = 0;
|
|
|
|
|
const ended = () => {
|
|
|
|
|
window.clearTimeout(timer);
|
2026-11-14 09:58:46 +00:00
|
|
|
try {
|
|
|
|
|
if (speech.source === source) {
|
|
|
|
|
Object.assign(speech, {current: null, source: null});
|
|
|
|
|
drawSpeaker();
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
resolve();
|
2026-11-13 10:48:13 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
source.addEventListener("ended", ended, {once: true});
|
|
|
|
|
timer = window.setTimeout(() => {
|
|
|
|
|
source.stop();
|
|
|
|
|
ended();
|
|
|
|
|
}, (seconds + SOUND_SLACK) * 1000);
|
|
|
|
|
duck(seconds);
|
|
|
|
|
Object.assign(speech, {current: item, source});
|
|
|
|
|
drawSpeaker();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function burst(sound, item) {
|
|
|
|
|
const shape = RadioSet.burst(loudness(item.tone));
|
|
|
|
|
if (shape.gain <= 0) {
|
|
|
|
|
return Promise.resolve();
|
|
|
|
|
}
|
|
|
|
|
const frames = Math.ceil(sound.sampleRate * shape.seconds);
|
|
|
|
|
const buffer = sound.createBuffer(1, frames, sound.sampleRate);
|
|
|
|
|
const data = buffer.getChannelData(0);
|
|
|
|
|
for (let index = 0; index < frames; index += 1) {
|
|
|
|
|
data[index] = Math.random() * 2 - 1;
|
|
|
|
|
}
|
|
|
|
|
const source = sound.createBufferSource();
|
|
|
|
|
const filter = sound.createBiquadFilter();
|
|
|
|
|
const gain = sound.createGain();
|
|
|
|
|
const start = sound.currentTime;
|
|
|
|
|
filter.type = "bandpass";
|
|
|
|
|
filter.frequency.value = shape.frequency;
|
|
|
|
|
filter.Q.value = shape.q;
|
|
|
|
|
gain.gain.setValueAtTime(shape.gain, start);
|
|
|
|
|
gain.gain.exponentialRampToValueAtTime(0.0001, start + shape.seconds);
|
|
|
|
|
source.buffer = buffer;
|
|
|
|
|
source.connect(filter).connect(gain).connect(sound.destination);
|
2026-11-14 09:58:46 +00:00
|
|
|
if (item.words) {
|
|
|
|
|
readingPace(item);
|
|
|
|
|
} else {
|
|
|
|
|
voicePace(item, Infinity, shape.seconds);
|
|
|
|
|
}
|
2026-11-13 10:48:13 +00:00
|
|
|
const ended = sounded(item, source, shape.seconds);
|
|
|
|
|
source.start(start);
|
|
|
|
|
source.stop(start + shape.seconds);
|
|
|
|
|
return ended;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function spoken(sound, item, step, done) {
|
|
|
|
|
speech.loading = item;
|
|
|
|
|
drawBubbles();
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch("/api/tts", {method: "POST", headers: {"X-Kvb-Orchestrator": "1", "Content-Type": "application/json"}, body: JSON.stringify({text: step.text, voice: item.voice}), cache: "no-store"});
|
|
|
|
|
const held = response.status === 503 && (response.headers.get("Content-Type") || "").startsWith("application/json");
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
return held ? "held" : `${response.status} ${(await response.text()).trim()}`;
|
|
|
|
|
}
|
|
|
|
|
const buffer = await sound.decodeAudioData(await response.arrayBuffer());
|
|
|
|
|
if (speech.item !== item) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
if (!allows(rendered(item, done), step.kind)) {
|
|
|
|
|
return "dropped";
|
|
|
|
|
}
|
|
|
|
|
const source = sound.createBufferSource();
|
|
|
|
|
const gain = sound.createGain();
|
|
|
|
|
gain.gain.value = loudness(item.tone);
|
|
|
|
|
source.buffer = buffer;
|
|
|
|
|
source.connect(gain).connect(sound.destination);
|
2026-11-14 09:58:46 +00:00
|
|
|
voicePace(item, step.kind === "speech" ? Infinity : 0, buffer.duration);
|
2026-11-13 10:48:13 +00:00
|
|
|
const ended = sounded(item, source, buffer.duration);
|
|
|
|
|
source.start();
|
|
|
|
|
await ended;
|
|
|
|
|
return speech.item === item && item.cut ? "dropped" : "";
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return String(error.message || error);
|
|
|
|
|
} finally {
|
|
|
|
|
if (speech.loading === item) {
|
|
|
|
|
speech.loading = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function liveSpeech() {
|
|
|
|
|
return {tts: state.speech.tts, open: state.speech.gate.open};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function stepOf(item, done, live) {
|
|
|
|
|
return (item.row ? RadioSet.plan(item.row, radioSet, live || liveSpeech()) : item.steps)[done];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function rendered(item, done) {
|
|
|
|
|
return item.row ? stepOf(item, done, {...liveSpeech(), open: true}) : item.steps[done];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function transmit(item, audio) {
|
|
|
|
|
let voiced = false;
|
|
|
|
|
for (let done = 0; ; done += 1) {
|
|
|
|
|
const step = stepOf(item, done);
|
|
|
|
|
if (!step || speech.item !== item) {
|
|
|
|
|
return voiced;
|
|
|
|
|
}
|
|
|
|
|
Object.assign(item, {at: done, playing: step.kind, cut: false});
|
|
|
|
|
if (step.kind === "burst") {
|
|
|
|
|
await burst(audio, item);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-11-15 09:46:05 +00:00
|
|
|
if (step.kind === "morse") {
|
|
|
|
|
voiced = (await keyMorse(audio, item, step)) || voiced;
|
|
|
|
|
done -= Number(item.cut);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-11-13 10:48:13 +00:00
|
|
|
const failed = await spoken(audio, item, step, done);
|
|
|
|
|
if (failed === "dropped") {
|
|
|
|
|
done -= 1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
voiced = voiced || (!failed && step.kind === "speech");
|
2026-11-15 09:46:05 +00:00
|
|
|
await fellBack(audio, item, step, done, failed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function fellBack(audio, item, step, done, failed) {
|
|
|
|
|
if (failed && failed !== "held") {
|
|
|
|
|
speechFailed([failed]);
|
|
|
|
|
}
|
|
|
|
|
if (failed && step.kind === "speech" && speech.item === item && allows(stepOf(item, done), "burst")) {
|
|
|
|
|
item.playing = "burst";
|
|
|
|
|
await burst(audio, item);
|
2026-11-13 10:48:13 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-06 08:28:04 +00:00
|
|
|
async function pump() {
|
|
|
|
|
if (speech.busy || !speech.pending.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-11-11 23:39:17 +00:00
|
|
|
const [item, ...rest] = speech.pending;
|
2026-11-13 10:48:13 +00:00
|
|
|
const audio = item.silent ? null : context();
|
|
|
|
|
if (!item.silent && !audio) {
|
2026-11-06 08:28:04 +00:00
|
|
|
speech.pending = [];
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-11-13 10:48:13 +00:00
|
|
|
Object.assign(speech, {pending: rest, busy: true, item});
|
2026-11-06 08:28:04 +00:00
|
|
|
const next = () => {
|
|
|
|
|
speech.busy = false;
|
|
|
|
|
pump();
|
|
|
|
|
};
|
2026-11-11 23:39:17 +00:00
|
|
|
if (item.silent) {
|
|
|
|
|
readSilently(item, next);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-11-14 09:58:46 +00:00
|
|
|
let voiced = false;
|
|
|
|
|
try {
|
|
|
|
|
voiced = await transmit(item, audio);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
speechFailed([String(error.message || error)]);
|
|
|
|
|
}
|
2026-11-13 10:48:13 +00:00
|
|
|
if (speech.item !== item) {
|
2026-11-06 08:28:04 +00:00
|
|
|
next();
|
2026-11-13 10:48:13 +00:00
|
|
|
return;
|
2026-11-06 08:28:04 +00:00
|
|
|
}
|
2026-11-13 10:48:13 +00:00
|
|
|
Object.assign(speech, {current: null, source: null, item: null});
|
|
|
|
|
if (!voiced && settings.bubbles && item.words) {
|
|
|
|
|
readSilently(item, next);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-11-14 21:23:12 +00:00
|
|
|
try {
|
|
|
|
|
drawSpeaker();
|
|
|
|
|
} finally {
|
|
|
|
|
next();
|
|
|
|
|
}
|
2026-11-06 08:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-07 19:59:47 +00:00
|
|
|
function cancelSpeech() {
|
2026-11-11 23:39:17 +00:00
|
|
|
if (speech.timer) {
|
|
|
|
|
endSilent();
|
|
|
|
|
}
|
2026-11-13 10:48:13 +00:00
|
|
|
const playing = speech.source;
|
|
|
|
|
Object.assign(speech, {pending: [], current: null, loading: null, source: null, item: null});
|
|
|
|
|
if (playing) {
|
|
|
|
|
playing.stop();
|
2026-11-07 19:59:47 +00:00
|
|
|
}
|
2026-11-11 23:39:17 +00:00
|
|
|
clearBubbles();
|
|
|
|
|
drawSpeaker();
|
2026-11-07 19:59:47 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-14 21:23:12 +00:00
|
|
|
/**
|
|
|
|
|
* @param {Array<object>} rows the radio rows the page has not announced yet.
|
|
|
|
|
* @param {boolean} arrived whether they arrived while the page polled without a gap.
|
|
|
|
|
*/
|
|
|
|
|
function speakRows(rows, arrived) {
|
|
|
|
|
const [spoken, quiet] = [[], []];
|
|
|
|
|
for (const row of rows) {
|
|
|
|
|
const fresh = row.ts >= state.now - SPEECH_FRESH;
|
2026-11-14 22:00:05 +00:00
|
|
|
const steps = fresh && settings.channels.aloud ? RadioSet.plan(row, radioSet, liveSpeech()) : [];
|
2026-11-14 21:23:12 +00:00
|
|
|
const line = short(row.text.split("\n").map((part) => part.trim()).find(Boolean) || "");
|
|
|
|
|
if (fresh && (steps.length || (settings.bubbles && Bubbles.picked(row, radioSet)))) {
|
|
|
|
|
spoken.push({row, voice: row.voice, tone: Voice.toneOf(row, HUMAN), cls: row.class, who: row.source, sent: Boolean(RadioSet.ledgerOf(row, state.messages)), line, words: Bubbles.words(row.text), silent: !steps.length});
|
|
|
|
|
} else if (settings.bubbles && arrived && row.ts >= state.now - LIVE_GAP) {
|
|
|
|
|
quiet.push({who: row.source, cls: row.class, line, words: Bubbles.words(line), quiet: true});
|
2026-11-11 23:39:17 +00:00
|
|
|
}
|
2026-11-06 08:28:04 +00:00
|
|
|
}
|
2026-11-14 21:23:12 +00:00
|
|
|
quietBubbles(quiet);
|
|
|
|
|
say(...spoken);
|
2026-11-06 08:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-07 19:59:47 +00:00
|
|
|
function micGap() {
|
|
|
|
|
return !window.isSecureContext || !navigator.mediaDevices
|
2026-11-08 17:26:51 +00:00
|
|
|
? "The microphone needs a secure context: open the hub via 127.0.0.1 or localhost"
|
|
|
|
|
: !window.MediaRecorder ? "This browser cannot record" : !state.speech.stt ? `Voice input is off: ${state.speech.stt_off}` : "";
|
2026-11-07 19:59:47 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-14 21:37:26 +00:00
|
|
|
async function wavOf(blob, sound, seconds) {
|
2026-11-07 19:59:47 +00:00
|
|
|
const decoded = await sound.decodeAudioData(await blob.arrayBuffer());
|
|
|
|
|
const rate = state.speech.rate;
|
2026-11-14 21:37:26 +00:00
|
|
|
const length = Voice.frames(decoded.duration, rate, seconds);
|
2026-11-07 19:59:47 +00:00
|
|
|
if (length < rate / 4) {
|
2026-11-08 17:26:51 +00:00
|
|
|
throw new Error("too short: speak for at least a quarter second");
|
2026-11-07 19:59:47 +00:00
|
|
|
}
|
|
|
|
|
const offline = new OfflineAudioContext(1, length, rate);
|
|
|
|
|
const source = offline.createBufferSource();
|
|
|
|
|
source.buffer = decoded;
|
|
|
|
|
source.connect(offline.destination);
|
|
|
|
|
source.start();
|
|
|
|
|
const rendered = await offline.startRendering();
|
|
|
|
|
return Voice.wav(rendered.getChannelData(0), rate);
|
|
|
|
|
}
|