Improve tts with codex extra high

This commit is contained in:
Xin Wang
2026-02-09 15:04:34 +08:00
parent a42dd4c712
commit cd68ebe306
3 changed files with 164 additions and 56 deletions

View File

@@ -400,6 +400,7 @@
let interimAiText = "";
const targetSampleRate = 16000;
const playbackStopRampSec = 0.008;
function logLine(type, text, data) {
const time = new Date().toLocaleTimeString();
@@ -456,10 +457,18 @@
function stopPlayback() {
discardAudio = true;
playbackTime = audioCtx ? audioCtx.currentTime : 0;
playbackSources.forEach((s) => {
const now = audioCtx ? audioCtx.currentTime : 0;
playbackTime = now;
playbackSources.forEach((node) => {
try {
s.stop();
if (audioCtx && node.gainNode && node.source) {
node.gainNode.gain.cancelScheduledValues(now);
node.gainNode.gain.setValueAtTime(node.gainNode.gain.value || 1, now);
node.gainNode.gain.linearRampToValueAtTime(0, now + playbackStopRampSec);
node.source.stop(now + playbackStopRampSec + 0.002);
} else if (node.source) {
node.source.stop();
}
} catch (err) {}
});
playbackSources = [];
@@ -527,14 +536,18 @@
const buffer = audioCtx.createBuffer(1, float32.length, targetSampleRate);
buffer.copyToChannel(float32, 0);
const source = audioCtx.createBufferSource();
const gainNode = audioCtx.createGain();
source.buffer = buffer;
source.connect(playbackDest);
source.connect(gainNode);
gainNode.connect(playbackDest);
const startTime = Math.max(audioCtx.currentTime + 0.02, playbackTime);
gainNode.gain.setValueAtTime(1, startTime);
source.start(startTime);
playbackTime = startTime + buffer.duration;
playbackSources.push(source);
const playbackNode = { source, gainNode };
playbackSources.push(playbackNode);
source.onended = () => {
playbackSources = playbackSources.filter((s) => s !== source);
playbackSources = playbackSources.filter((s) => s !== playbackNode);
};
}