From ce206d152f96bb8335c71997a42c64c7883fe13f Mon Sep 17 00:00:00 2001 From: Sandra Rodgers Date: Wed, 3 Jul 2024 16:18:38 -0500 Subject: [PATCH] feat: add SpeakClient and example --- examples/node-speak-live/index.js | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 examples/node-speak-live/index.js diff --git a/examples/node-speak-live/index.js b/examples/node-speak-live/index.js new file mode 100644 index 0000000..d544111 --- /dev/null +++ b/examples/node-speak-live/index.js @@ -0,0 +1,44 @@ +const { createClient, LiveTTSEvents } = require("../../dist/main/index"); +const fetch = require("cross-fetch"); + +const live = async () => { + const text = "Hello, how can I help you today?"; + + const deepgram = createClient(process.env.DEEPGRAM_API_KEY, { + global: { fetch: { options: { url: "https://api.beta.deepgram.com" } } }, + }); + + const connection = deepgram.speak.live({ text }, { model: "aura-asteria-en" }); + + connection.on(LiveTTSEvents.Open, () => { + connection.on(LiveTTSEvents.Close, () => { + console.log("Connection closed."); + }); + + connection.on(LiveTTSEvents.Metadata, (data) => { + console.log(`Deepgram Metadata: ${data}`); + }); + + connection.on(LiveTTSEvents.Audio, (data) => { + console.log(`Deepgram Audio: ${data}`); + }); + + connection.on(LiveTTSEvents.Flushed, (data) => { + console.log("Deepgram Flushed"); + }); + + connection.on(LiveTTSEvents.Error, (err) => { + console.error(err); + }); + + fetch(url) + .then((r) => r.body) + .then((res) => { + res.on("readable", () => { + connection.send(res.read()); + }); + }); + }); +}; + +live();