Skip to content
This repository was archived by the owner on Apr 13, 2025. It is now read-only.

Commit 10e1281

Browse files
authored
Fix OpenTTS getWavData, format and add doc comments (#761)
1 parent 25719e9 commit 10e1281

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

samples/opentts/extension/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ async function playTTSInGraphic(client: OpenTTSClient, nodecg: NodeCG) {
1313
if (voiceName === undefined) throw new Error("no voice available");
1414

1515
const helloWorldUrl = client.generateWavUrl("Hello World", voiceName);
16-
await nodecg.sendMessage("setSrc", helloWorldUrl);
16+
nodecg.sendMessage("setSrc", helloWorldUrl);
1717
}
1818

1919
module.exports = function (nodecg: NodeCG) {
@@ -23,9 +23,10 @@ module.exports = function (nodecg: NodeCG) {
2323

2424
nodecg.listenFor("ready", () => {
2525
const client = opentts?.getClient();
26-
if (client !== undefined){
27-
playTTSInGraphic(client, nodecg)
28-
.catch(err => nodecg.log.error(`Error while trying to play tts message: ${err.messages}`));
26+
if (client !== undefined) {
27+
playTTSInGraphic(client, nodecg).catch((err) =>
28+
nodecg.log.error(`Error while trying to play tts message: ${err.messages}`),
29+
);
2930
}
3031
});
3132

samples/opentts/graphics/index.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<!DOCTYPE html>
22
<html lang="en">
3-
<head>
4-
<title>OpenTTS Sample</title>
5-
</head>
6-
<body>
7-
<audio controls id="opentts-audio"></audio>
8-
<script src="index.js"></script>
9-
</body>
10-
</html>
3+
<head>
4+
<title>OpenTTS Sample</title>
5+
</head>
6+
<body>
7+
<audio controls id="opentts-audio"></audio>
8+
<script src="index.js"></script>
9+
</body>
10+
</html>

services/nodecg-io-opentts/extension/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ class OpenTTSService extends ServiceBundle<OpenTTSConfig, OpenTTSClient> {
2525
return success(client);
2626
}
2727

28-
stopClient(_: OpenTTSClient, logger: Logger): void {
28+
stopClient(_: OpenTTSClient, _logger: Logger): void {
2929
// Client is stateless, no need to stop anything
3030
}
3131
}
32-

services/nodecg-io-opentts/extension/openTtsClient.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class OpenTTSClient {
2929
private async executeRequest(path: string): Promise<Response> {
3030
const response = await fetch(this.buildBaseURL() + path);
3131
if (!response.ok) {
32-
throw new Error("Failed to execute opentts request: " + (await response.text()));
32+
throw new Error(`Failed to execute opentts request: ${await response.text()}`);
3333
}
3434

3535
return response;
@@ -57,6 +57,9 @@ export class OpenTTSClient {
5757
return await response.json();
5858
}
5959

60+
/**
61+
* Generates a URL to a .wav file with the spoken text that is generated using OpenTTS:
62+
*/
6063
generateWavUrl(
6164
text: string,
6265
voice: string,
@@ -65,15 +68,24 @@ export class OpenTTSClient {
6568
cache?: boolean,
6669
): string {
6770
const params = new URLSearchParams({ text, voice });
68-
if(vocoder) params.set("vocoder", vocoder);
69-
if(denoiserStrength) params.set("denoiserStrength", denoiserStrength.toString());
70-
if(cache !== undefined) params.set("cache", cache.toString());
71+
if (vocoder) params.set("vocoder", vocoder);
72+
if (denoiserStrength) params.set("denoiserStrength", denoiserStrength.toString());
73+
if (cache !== undefined) params.set("cache", cache.toString());
7174

7275
return `${this.buildBaseURL()}/api/tts?${params}`;
7376
}
7477

78+
/**
79+
* Downloads the .wav file from the given URL and returns it as a Buffer.
80+
* @param url the url generated by {@link generateWavUrl}
81+
* @returns the wav file
82+
*/
7583
async getWavData(url: string): Promise<ArrayBuffer> {
76-
const response = await this.executeRequest(url);
84+
const response = await fetch(url);
85+
if (!response.ok) {
86+
throw new Error(`Failed to fetch wav audio data: ${await response.text()}`);
87+
}
88+
7789
return await response.arrayBuffer();
7890
}
7991

0 commit comments

Comments
 (0)