Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep ws connection live #52

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ async function textToSpeechInputStreaming(text: string, config: Config): Promise
let firstByteTime: number | undefined;
let startTime: number | undefined;
let firstByte = true;
let lastSentTime: number | undefined;

const uri = `wss://api.elevenlabs.io/v1/text-to-speech/${config.voiceId}/stream-input?model_id=${config.model}`;
const websocket = new WebSocket(uri, {
Expand Down Expand Up @@ -63,8 +64,18 @@ async function textToSpeechInputStreaming(text: string, config: Config): Promise
startTime = new Date().getTime()

websocket.send(JSON.stringify({ text: text }));

websocket.send(JSON.stringify({ text: '' }));
lastSentTime = new Date().getTime();

// set repeated time out to keep the connection alive
setInterval(() => {
const elapsed_ = new Date().getTime();
if (typeof lastSentTime === 'undefined') {
throw new Error('Last sent time is not recorded.');
}

console.log(`Time since last sent: ${elapsed_ - lastSentTime} ms`);
websocket.send(JSON.stringify({ text: ' ' }));
}, 10000);
});

// Log received data and the time elapsed since the connection started.
Expand Down Expand Up @@ -162,7 +173,7 @@ async function main() {
apiKey: argv.api_key || "",
model: argv.model || 'eleven_turbo_v2',
voiceId: argv.voiceId || 'Xb7hH8MSUJpSbSDYk0k2',
numOfTrials: 5
numOfTrials: 1
} satisfies Config;

console.log('Using model:', config.model);
Expand Down
Loading