Skip to content

Commit 39a535d

Browse files
SandraRodgersnaomi-lgbt
authored andcommitted
feat: add SpeakLiveClient and LiveTTSEvents
1 parent 630387d commit 39a535d

3 files changed

Lines changed: 162 additions & 0 deletions

File tree

src/lib/enums/LiveTTSEvents.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Enumeration of events related to live text-to-speech synthesis.
3+
*
4+
* - `Open`: Built-in socket event for when the connection is opened.
5+
* - `Close`: Built-in socket event for when the connection is closed.
6+
* - `Error`: Built-in socket event for when an error occurs.
7+
* - `Metadata`: Event for when metadata is received.
8+
* - `Flushed`: Event for when the server has flushed the buffer.
9+
* - `Warning`: Event for when a warning is received.
10+
* - `Unhandled`: Catch-all event for any other message event.
11+
*/
12+
export enum LiveTTSEvents {
13+
/**
14+
* Built in socket events.
15+
*/
16+
Open = "Open",
17+
Close = "Close",
18+
Error = "Error",
19+
20+
/**
21+
* Message { type: string }
22+
*/
23+
Metadata = "Metadata",
24+
Flushed = "Flushed",
25+
Warning = "Warning",
26+
27+
/**
28+
* Catch all for any other message event
29+
*/
30+
Unhandled = "Unhandled",
31+
}

src/lib/enums/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./LiveConnectionState";
22
export * from "./LiveTranscriptionEvents";
3+
export * from "./LiveTTSEvents";

src/packages/SpeakLiveClient.ts

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import { AbstractLiveClient } from "./AbstractLiveClient";
2+
import { LiveTTSEvents } from "../lib/enums";
3+
import type { SpeakSchema, DeepgramClientOptions } from "../lib/types";
4+
5+
/**
6+
* The `SpeakLiveClient` class extends the `AbstractLiveClient` class and provides functionality for setting up and managing a WebSocket connection for live text-to-speech synthesis.
7+
*
8+
* The constructor takes in `DeepgramClientOptions` and an optional `SpeakSchema` object, as well as an optional `endpoint` string. It then calls the `connect` method of the parent `AbstractLiveClient` class to establish the WebSocket connection.
9+
*
10+
* The `setupConnection` method is responsible for handling the various events that can occur on the WebSocket connection, such as opening, closing, and receiving messages. It sets up event handlers for these events and emits the appropriate events based on the message type.
11+
*
12+
* The `configure` method allows you to send additional configuration options to the connected session.
13+
*
14+
*
15+
* The `requestClose` method requests the server to close the connection.
16+
*
17+
*/
18+
export class SpeakLiveClient extends AbstractLiveClient {
19+
public namespace: string = "speak";
20+
21+
/**
22+
* Constructs a new `SpeakLiveClient` instance with the provided options.
23+
*
24+
* @param options - The `DeepgramClientOptions` to use for the client connection.
25+
* @param speakOptions - An optional `SpeakSchema` object containing additional configuration options for the text-to-speech.
26+
* @param endpoint - An optional string representing the WebSocket endpoint to connect to. Defaults to `:version/speak`.
27+
*/
28+
constructor(
29+
options: DeepgramClientOptions,
30+
speakOptions: SpeakSchema = {},
31+
endpoint: string = ":version/speak"
32+
) {
33+
super(options);
34+
35+
this.connect(speakOptions, endpoint);
36+
}
37+
38+
/**
39+
* Sets up the connection event handlers.
40+
* This method is responsible for handling the various events that can occur on the WebSocket connection, such as opening, closing, and receiving data.
41+
* - When the connection is opened, it emits the `LiveTTSEvents.Open` event.
42+
* - When the connection is closed, it emits the `LiveTTSEvents.Close` event.
43+
* - When an error occurs on the connection, it emits the `LiveTTSEvents.Error` event.
44+
* - When a message is received, it parses the message and emits the appropriate event based on the message type, such as `LiveTTSEvents.Metadata`, `LiveTTSEvents.Flushed`, and `LiveTTSEvents.Warning`.
45+
*/
46+
public setupConnection(): void {
47+
if (this.conn) {
48+
this.conn.onopen = () => {
49+
this.emit(LiveTTSEvents.Open, this);
50+
};
51+
52+
this.conn.onclose = (event: any) => {
53+
this.emit(LiveTTSEvents.Close, event);
54+
};
55+
56+
this.conn.onerror = (event: ErrorEvent) => {
57+
this.emit(LiveTTSEvents.Error, event);
58+
};
59+
60+
this.conn.onmessage = (event: MessageEvent) => {
61+
try {
62+
const data: any = JSON.parse(event.data.toString());
63+
64+
if (data.type === LiveTTSEvents.Metadata) {
65+
this.emit(LiveTTSEvents.Metadata, data);
66+
} else if (data.type === LiveTTSEvents.Flushed) {
67+
this.emit(LiveTTSEvents.Flushed, data);
68+
} else if (data.type === "Warning") {
69+
this.emit(LiveTTSEvents.Warning, data);
70+
} else {
71+
this.emit(LiveTTSEvents.Unhandled, data);
72+
}
73+
} catch (error) {
74+
this.emit(LiveTTSEvents.Error, {
75+
event,
76+
message: "Unable to parse `data` as JSON.",
77+
error,
78+
});
79+
}
80+
};
81+
}
82+
}
83+
84+
/**
85+
* Sends a text input message to the server.
86+
*
87+
* @param text - The text to convert to speech.
88+
*/
89+
public sendText(text: string): void {
90+
this.send(
91+
JSON.stringify({
92+
type: "Speak",
93+
text,
94+
})
95+
);
96+
}
97+
98+
/**
99+
* Requests the server flush the current buffer and return generated audio.
100+
*/
101+
public flush(): void {
102+
this.send(
103+
JSON.stringify({
104+
type: "Flush",
105+
})
106+
);
107+
}
108+
/**
109+
* Requests the server reset the current buffer.
110+
*/
111+
public reset(): void {
112+
this.send(
113+
JSON.stringify({
114+
type: "Reset",
115+
})
116+
);
117+
}
118+
/**
119+
* Requests the server close the connection.
120+
*/
121+
public requestClose(): void {
122+
this.send(
123+
JSON.stringify({
124+
type: "Close",
125+
})
126+
);
127+
}
128+
}
129+
130+
export { SpeakLiveClient as Liveclient };

0 commit comments

Comments
 (0)