Skip to content

Commit e14155a

Browse files
authored
feat: add new alpha client (#249)
1 parent 0212338 commit e14155a

5 files changed

Lines changed: 81 additions & 0 deletions

File tree

src/DeepgramClient.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ListenClient } from "./packages/ListenClient";
44
import { ManageClient } from "./packages/ManageClient";
55
import { OnPremClient } from "./packages/OnPremClient";
66
import { ReadClient } from "./packages/ReadClient";
7+
import { SpeakClient } from "./packages/SpeakClient";
78

89
/**
910
* Deepgram Client.
@@ -28,6 +29,10 @@ export default class DeepgramClient extends AbstractClient {
2829
return new ReadClient(this.key, this.options);
2930
}
3031

32+
get speak(): SpeakClient {
33+
return new SpeakClient(this.key, this.options);
34+
}
35+
3136
/**
3237
* Major version fallback errors are below
3338
*
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { DeepgramError } from "../errors";
2+
3+
export type DeepgramSpeakResponse = ReadableStream | ErrorResponse;
4+
5+
interface ErrorResponse {
6+
error: DeepgramError;
7+
}

src/lib/types/SpeakSchema.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export interface SpeakSchema extends Record<string, unknown> {
2+
/**
3+
* The model, voice, language, and version of the voice.
4+
* Follows the format of[modelname]-[voicename]-[language]-[version].
5+
*/
6+
model?: string;
7+
8+
/**
9+
* Encoding options for the output audio. Default is 'mp3'.
10+
*/
11+
encoding?: "linear16" | "mulaw" | "alaw" | "mp3" | "opus" | "flac" | "aac";
12+
13+
/**
14+
* File format wrapper for the audio.
15+
*/
16+
container?: string;
17+
18+
/**
19+
* Sample rate of the audio output.
20+
*/
21+
sample_rate?: number;
22+
23+
/**
24+
* Bit rate of the audio output.
25+
*/
26+
bit_rate?: number;
27+
28+
[key: string]: unknown;
29+
}

src/lib/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export type {
4242
AnalyzeSource,
4343
} from "./DeepgramSource";
4444
export type { SendProjectInviteSchema } from "./SendProjectInviteSchema";
45+
export type { SpeakSchema } from "./SpeakSchema";
4546
export type { SpeechStartedEvent } from "./SpeechStartedEvent";
4647
export type { SyncPrerecordedResponse } from "./SyncPrerecordedResponse";
4748
export type { SyncAnalyzeResponse } from "./SyncAnalyzeResponse";

src/packages/SpeakClient.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { AbstractRestfulClient } from "./AbstractRestfulClient";
2+
import { DeepgramError, isDeepgramError } from "../lib/errors";
3+
import { appendSearchParams, isTextSource } from "../lib/helpers";
4+
import { Fetch, SpeakSchema, TextSource } from "../lib/types";
5+
import { DeepgramSpeakResponse } from "../lib/types/DeepgramSpeakResponse";
6+
7+
export class SpeakClient extends AbstractRestfulClient {
8+
/**
9+
* @see https://developers.deepgram.com/reference/text-to-speech-preview-api
10+
*/
11+
async stream(
12+
source: TextSource,
13+
options?: SpeakSchema,
14+
endpoint = "v1/speak"
15+
): Promise<DeepgramSpeakResponse> {
16+
try {
17+
let body;
18+
19+
if (isTextSource(source)) {
20+
body = JSON.stringify(source);
21+
} else {
22+
throw new DeepgramError("Unknown transcription source type");
23+
}
24+
25+
const speakOptions: SpeakSchema = { ...{ model: "alpha-asteria-en" }, ...options };
26+
27+
const url = new URL(endpoint, this.baseUrl);
28+
appendSearchParams(url.searchParams, speakOptions);
29+
30+
return await this.post(this.fetch as Fetch, url, body);
31+
} catch (error) {
32+
if (isDeepgramError(error)) {
33+
return { error };
34+
}
35+
36+
throw error;
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)