Skip to content

Commit

Permalink
chore: update example and readme to be consistent with live STT
Browse files Browse the repository at this point in the history
  • Loading branch information
SandraRodgers authored and naomi-lgbt committed Sep 18, 2024
1 parent 2e0eb3d commit 12a38c3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,21 @@ const { result } = await deepgram.speak.request({ text }, { model: "aura-asteria
## Websocket

```js
const connection = deepgram.speak.live({ model: "aura-asteria-en" });
const dgConnection = deepgram.speak.live({ model: "aura-asteria-en" });

dgConnection.on(LiveTTSEvents.Open, () => {
console.log("Connection opened");

// Send text data for TTS synthesis
dgConnection.sendText(text);

// Send Flush message to the server after sending the text
dgConnection.flush();

dgConnection.on(LiveTTSEvents.Close, () => {
console.log("Connection closed");
});
});
```

[See our API reference for more info](https://developers.deepgram.com/reference/text-to-speech-api).
Expand Down
18 changes: 9 additions & 9 deletions examples/node-speak-live/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,41 @@ const live = async () => {

const deepgram = createClient(process.env.DEEPGRAM_API_KEY);

const connection = deepgram.speak.live({ model: "aura-asteria-en" });
const dgConnection = deepgram.speak.live({ model: "aura-asteria-en" });

let audioBuffer = Buffer.alloc(0);

connection.on(LiveTTSEvents.Open, () => {
dgConnection.on(LiveTTSEvents.Open, () => {
console.log("Connection opened");

// Send text data for TTS synthesis
connection.sendText(text);
dgConnection.sendText(text);

// Send Flush message to the server after sending the text
connection.flush();
dgConnection.flush();

connection.on(LiveTTSEvents.Close, () => {
dgConnection.on(LiveTTSEvents.Close, () => {
console.log("Connection closed");
});

connection.on(LiveTTSEvents.Metadata, (data) => {
dgConnection.on(LiveTTSEvents.Metadata, (data) => {
console.dir(data, { depth: null });
});

connection.on(LiveTTSEvents.Audio, (data) => {
dgConnection.on(LiveTTSEvents.Audio, (data) => {
console.log("Deepgram audio data received");
// Concatenate the audio chunks into a single buffer
const buffer = Buffer.from(data);
audioBuffer = Buffer.concat([audioBuffer, buffer]);
});

connection.on(LiveTTSEvents.Flushed, () => {
dgConnection.on(LiveTTSEvents.Flushed, () => {
console.log("Deepgram Flushed");
// Write the buffered audio data to a file when the flush event is received
writeFile();
});

connection.on(LiveTTSEvents.Error, (err) => {
dgConnection.on(LiveTTSEvents.Error, (err) => {
console.error(err);
});
});
Expand Down

0 comments on commit 12a38c3

Please sign in to comment.