Your function getStream fetches data as a stream and attempts to process that data as it arrives. Your description indicates that you have an issue because your talkVis function, which uses the Web Speech API, isn't able to speak the incoming text fast enough to keep up with the stream. This is a common issue when dealing with streaming data and text-to-speech (TTS): the TTS service may not be able to speak as quickly as data arrives, leading to potentially missed information or buffer overflows.
Here's how you can potentially handle this:
-
Queue the incoming data: Rather than trying to immediately send all incoming text segments to the talkVis function, you can queue them and make sure that only one piece of text is spoken at a time.
-
Handle talkVis scheduling: Use a mechanism to determine when the TTS has finished speaking the current text, and only then deque the next available text segment.
Here's an example of how your code might be augmented to handle this with a basic queue system:
function getStream() {
const apiUrl = 'https://pandora-s-playground-api.onrender.com/get_talk';
let txt = document.querySelector('.overlay-section p').textContent.toLowerCase();
const data = {
history: [['user', txt]],
};
const speechQueue = [];
let speaking = false;
async function fetchStreamPost() {
// ... same as before ...
const reader = response.body.getReader();
let fullData = "";
while (true) {
const { done, value } = await reader.read();
if (done) break;
const fragment = new TextDecoder().decode(value);
fullData += fragment;
console.log('Streamed fragment:', fragment);
if (fullData.endsWith('.') || fullData.endsWith(',') || fullData.endsWith('?') || fullData.endsWith('!')) {
speechQueue.push(fullData);
fullData = "";
if (!speaking) speakNextSegment();
}
}
} catch (error) {
// ... same as before ...
}
// Call this once to start the fetch operation
fetchStreamPost();
}
function speakNextSegment() {
if (speechQueue.length === 0) {
speaking = false;
return;
}
speaking = true;
const textSegment = speechQueue.shift();
talkVis(textSegment, function() {
// This callback is called after the speech is finished
speakNextSegment();
});
}
// Modify your talkVis to accept a callback when finished speaking
function talkVis(text, callback) {
const utterance = new SpeechSynthesisUtterance(text);
utterance.onend = function(event) {
console.log('SpeechSynthesisUtterance.onend');
if (typeof callback === "function") callback();
};
utterance.onerror = function(event) {
console.error('SpeechSynthesisUtterance.onerror');
if (typeof callback === "function") callback();
};
speechSynthesis.speak(utterance);
}
This code adds a speechQueue and ensures that talkVis is called one by one by using speakNextSegment. It also modifies the talkVis function to take a callback that is used to chain the speaking of the queued texts.