-
Notifications
You must be signed in to change notification settings - Fork 1
/
eleven.js
147 lines (132 loc) · 4.6 KB
/
eleven.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const Semaphore = require('async-mutex').Semaphore;
const LABSURL = process.env.hasOwnProperty("LABSURL") ? process.env["LABSURL"] : "https://api.elevenlabs.io";
const LABSKEY = process.env.hasOwnProperty("LABSKEY") ? process.env["LABSKEY"] : null;
const PAULCA_VOICE = 'EcOnXAJ3e2odu7bmr9M9';
const YOUTUBE_VOICE = 'LQj2X4OpUuX9YFC5sCDw';
const MICHAEL_VOICE = 'flq6f7yk4E4fJM5XTYuZ';
const MATTHEW_VOICE = 'Yko7PKHZNXotIFUBG7I9';
const DEFAULT_VOICE = MICHAEL_VOICE;
const DEFAULT_MODEL = 'eleven_multilingual_v2';
const DEFAULT_URL = "https://api.elevenlabs.io";
let apiURL = LABSURL ?? DEFAULT_URL;
let apiKey = LABSKEY;
let cachedUser = null;
const semaphore = new Semaphore(1);
function init(url, key) {
apiURL = url ?? apiURL;
apiKey = key ?? apiKey;
return { apiURL, apikey: apiKey};
}
const FORMATS = [
'mp3_44100_64', 'mp3_44100_96', 'mp3_44100_128', 'mp3_44100_192',
'pcm_16000', 'pcm_22050', 'pcm_24000', 'pcm_44100'
];
async function apiCall(method, relativeURL, _headers, body) {
return await semaphore.runExclusive(async () => {
try {
const headers = Object.assign({},
{
"Content-Type": "application/json",
"xi-api-key": apiKey
},
..._headers);
let options = {
method: method || "GET",
headers: headers,
}
if (body) {
options.body = typeof body === "string" ? body : JSON.stringify(body);
}
return await fetch(apiURL + relativeURL, options);
} catch (err) {
console.log("apiCall: " + err.message);
}
});
}
async function getUser() {
try {
const response = await apiCall("GET", "/v1/user", [], null);
const data = await response.json();
// console.log("user: " + JSON.stringify(data, null, 2));
cachedUser = data;
return data;
} catch (err) {
console.log("getUser: " + err.message);
}
cachedUser = null;
return null;
}
async function listVoices() {
try {
const response = await apiCall("GET", "/v1/voices", [], null);
const data = await response.json();
// console.log("listVoices: " + JSON.stringify(data, null, 2));
return data;
} catch (err) {
console.log("listVoices: " + err.message);
}
return null;
}
async function synthesize(ttsOptions) {
try {
const user = await getUser();
const tierLevel = user?.subscription?.tier ?? 'free';
const isMP3 = ttsOptions.output_format.startsWith("mp3_");
if (tierLevel === 'free') {
if (ttsOptions.output_format === 'mp3_44100_192') {
console.log("Free tier is limited to mp3_44100_128 format.");
ttsOptions.output_format = 'mp3_44100_128';
}
}
const acceptType = isMP3 ? 'audio/mpeg' : 'audio/wav';
const headers = [{"Accept": acceptType}];
const output_format = ttsOptions.output_format;
let model_id = ttsOptions.model_id;
switch (ttsOptions.model_id) {
case 'e1': model_id = 'eleven_monolingual_v1'; break;
case 'e2': model_id = 'eleven_monolingual_v2'; break;
case 'm1': model_id = 'eleven_multilingual_v1'; break;
case 'm2': model_id = 'eleven_multilingual_v2'; break;
case '': model_id = DEFAULT_MODEL; break;
case null: model_id = DEFAULT_MODEL; break;
case undefined: model_id = DEFAULT_MODEL; break;
}
console.log("Using model: " + model_id);
// let body = Object.assign({}, ttsOptions, { "voice_id": ttsOptions?.voice_id });
let body = Object.assign({},
ttsOptions,
{
model_id: model_id,
voice_settings : {
"stability": 0.75,
"similarity_boost": 0.75,
"style": 0.0,
}
});
delete body.output_format;
const response = await apiCall("POST", `/v1/text-to-speech/${ttsOptions?.voice_id}?output_format=${output_format}`, headers, body);
if (response.ok) {
// console.log(response.status, response.statusText, response.type, response.bodyUsed)
return response.body;
} else {
if (response.type === 'basic') {
const json = await response.text();
const data = JSON.parse(json);
const detail = data?.detail;
const msg = detail?.message ?? data?.message ?? response.statusText ?? detail ?? json;
// console.log(response.status, response.statusText, response.type, msg);
throw new Error(response.status + " " + msg);
}
// return msg;
throw new Error(response.status + " " + response.statusText);
}
} catch (err) {
// console.log("synthesize: " + err.message);
throw err;
}
// return null;
}
module.exports = {
LABSURL, LABSKEY, FORMATS,
DEFAULT_VOICE, PAULCA_VOICE, YOUTUBE_VOICE,
init, getUser, listVoices, synthesize };