-
Notifications
You must be signed in to change notification settings - Fork 1
/
tts.js
90 lines (74 loc) · 2.23 KB
/
tts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Long endpoint: /synthesisTasks
// - Up to 500,000 characters
// - Asynchronous, takes ~1s per 800 chars
// - Returns a TaskId (use to check status)
import fetch from "node-fetch";
import dotenv from "dotenv";
dotenv.config();
export const tts = async (text) => {
const data = {
Text: text, // Up to 500,000 characters
VoiceId: "Liv", // Dan, Will, Scarlett, Liv, Amy
Bitrate: "192k", // 320k, 256k, 192k, ...
Speed: "0.2", // -1.0 to 1.0
Pitch: "1", // -0.5 to 1.5
TimestampType: "sentence", // word or sentence
//'CallbackUrl': '<URL>', // pinged when ready
};
try {
const response = await fetch(
"https://api.v6.unrealspeech.com/synthesisTasks",
{
method: "POST",
headers: {
Authorization: "Bearer " + process.env.unreal_voice_key,
"Content-Type": "application/json",
},
body: JSON.stringify(data),
}
);
if (!response.ok) {
throw new Error(
"HTTP error " + response.status + " " + response.statusText
);
}
const responseData = await response.json();
const isDone = async (taskId) => {
const response = await fetch(
"https://api.v6.unrealspeech.com/synthesisTasks/" + taskId,
{
method: "GET",
headers: {
Authorization: "Bearer " + process.env.unreal_voice_key,
"Content-Type": "application/json",
},
}
);
const responseData = await response.json();
if (responseData.SynthesisTask.TaskStatus === "completed") {
return true;
} else {
return false;
}
};
let timesChecked = 0;
const check = async (taskId) => {
const done = await isDone(taskId);
if (done || timesChecked > 6) {
return true;
} else {
await new Promise((resolve) => setTimeout(resolve, 5000));
await check(taskId);
timesChecked++;
}
};
if (responseData.SynthesisTask.TaskStatus === "completed") {
return responseData.SynthesisTask.OutputUri;
}
await check(responseData.SynthesisTask.TaskId);
return responseData.SynthesisTask.OutputUri;
} catch (error) {
console.error("Error:", error);
throw new Error(error);
}
};