Skip to content

Commit

Permalink
fix web demo button states
Browse files Browse the repository at this point in the history
  • Loading branch information
albho committed Aug 20, 2024
1 parent efebeea commit 37bf239
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions demo/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,19 @@
});

// Streaming Synthesis
let isPlayingStream = false;
let isPlayingAudio = false;
let isStreaming = false;
const audioBuffer = [];
let streamSource;

function playStream() {
if (isPlayingStream) return;
if (isPlayingAudio) return;

if (audioBuffer.length === 0) {
streamPlayBtnEl.disabled = false;
streamCloseBtnEl.disabled = false;
if (!isStreaming) {
streamPlayBtnEl.disabled = false;
streamCloseBtnEl.disabled = false;
}
return;
} else {
streamPlayBtnEl.disabled = true;
Expand All @@ -250,12 +253,12 @@
streamSource.connect(originalAudioGain);

streamSource.onended = async () => {
isPlayingStream = false;
isPlayingAudio = false;
playStream();
};

streamSource.start();
isPlayingStream = true;
isPlayingAudio = true;
}

async function streamOpen() {
Expand All @@ -282,6 +285,7 @@
async function streamPlay() {
writeMessage('Synthesizing and playing speech! Please listen for audio.');
try {
isStreaming = true;
streamTextDisplayEl.innerText = '';
streamSecondsDisplayEl.innerText = '0';

Expand All @@ -298,7 +302,7 @@
const time = curSecs + newSecs;
streamSecondsDisplayEl.innerText = time.toFixed(3);
audioBuffer.push(createBuffer(wordPcm));
if (numIterations === 1 || !isPlayingStream) {
if (numIterations === 1 || !isPlayingAudio) {
playStream();
}
numIterations++;
Expand All @@ -313,10 +317,12 @@
const time = curSecs + newSecs;
streamSecondsDisplayEl.innerText = time.toFixed(3);
audioBuffer.push(createBuffer(flushPcm));
await playStream();
playStream();
}
} catch (err) {
writeMessage(err);
} finally {
isStreaming = false;
}
}

Expand Down

0 comments on commit 37bf239

Please sign in to comment.