generated from deepgram/oss-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: tie together namespace config with fetch and websocket options
- Loading branch information
1 parent
f42ece3
commit 957dbb1
Showing
11 changed files
with
179 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,22 @@ | ||
const { createClient } = require("../../dist/main/index"); | ||
const fs = require("fs"); | ||
const { pipeline } = require("stream"); | ||
const { promisify } = require("util"); | ||
const streamPipeline = promisify(pipeline); | ||
|
||
const deepgram = createClient(process.env.DEEPGRAM_API_KEY); | ||
|
||
const text = "Hello, how can I help you today?"; | ||
|
||
const getAudio = async () => { | ||
const response = await deepgram.speak.request({ text }, { model: "aura-asteria-en" }); | ||
const stream = await response.getStream(); | ||
const headers = await response.getHeaders(); | ||
if (stream) { | ||
const buffer = await getAudioBuffer(stream); | ||
const { result } = await deepgram.speak.request({ text }, { model: "aura-asteria-en" }); | ||
|
||
fs.writeFile("audio.wav", buffer, (err) => { | ||
if (err) { | ||
console.error("Error writing audio to file:", err); | ||
} else { | ||
console.log("Audio file written to audio.wav"); | ||
} | ||
}); | ||
} else { | ||
console.error("Error generating audio:", stream); | ||
if (!result.ok) { | ||
throw new Error(`HTTP error! Status: ${result}`); | ||
} | ||
|
||
if (headers) { | ||
console.log("Headers:", headers); | ||
} | ||
}; | ||
|
||
// helper function to convert stream to audio buffer | ||
const getAudioBuffer = async (response) => { | ||
const reader = response.getReader(); | ||
const chunks = []; | ||
|
||
while (true) { | ||
const { done, value } = await reader.read(); | ||
if (done) break; | ||
|
||
chunks.push(value); | ||
} | ||
|
||
const dataArray = chunks.reduce( | ||
(acc, chunk) => Uint8Array.from([...acc, ...chunk]), | ||
new Uint8Array(0) | ||
); | ||
|
||
return Buffer.from(dataArray.buffer); | ||
const fileStream = fs.createWriteStream("audio.wav"); | ||
await streamPipeline(result.body, fileStream); | ||
}; | ||
|
||
getAudio(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,5 @@ | ||
export type Fetch = typeof fetch; | ||
|
||
export interface FetchOptions { | ||
method?: RequestMethodType; | ||
headers?: Record<string, string>; | ||
cache?: "default" | "no-cache" | "reload" | "force-cache" | "only-if-cached"; // default, no-cache, reload, force-cache, only-if-cached | ||
credentials?: "include" | "same-origin" | "omit"; // include, same-origin, omit | ||
redirect?: "manual" | "follow" | "error"; // manual, follow, error | ||
referrerPolicy?: // no-referrer, no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url | ||
| "no-referrer" | ||
| "no-referrer-when-downgrade" | ||
| "origin" | ||
| "origin-when-cross-origin" | ||
| "same-origin" | ||
| "strict-origin" | ||
| "strict-origin-when-cross-origin" | ||
| "unsafe-url"; | ||
} | ||
export type FetchOptions = RequestInit & { method?: RequestMethodType; duplex?: string }; | ||
|
||
export type RequestMethodType = "GET" | "POST" | "PUT" | "DELETE" | "PATCH"; // GET, POST, PUT, DELETE, etc. | ||
|
||
export interface FetchParameters { | ||
/** | ||
* Pass in an AbortController's signal to cancel the request. | ||
*/ | ||
signal?: AbortSignal; | ||
} | ||
export type RequestMethodType = "GET" | "POST" | "PUT" | "DELETE" | "PATCH"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.