@cartesia/cartesia-js 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +47 -47
- package/CHANGELOG.md +6 -0
- package/LICENSE.md +21 -0
- package/README.md +92 -19
- package/dist/{chunk-F4QWVJY3.js → chunk-2NA5SEML.js} +2 -2
- package/dist/{chunk-PQ5EVEEH.js → chunk-5M33ZF3Y.js} +1 -1
- package/dist/{chunk-PQ6CIPFW.js → chunk-6YQ6KDIQ.js} +44 -5
- package/dist/{chunk-FN7BK4PS.js → chunk-ASZKHN7Q.js} +44 -26
- package/dist/{chunk-RO7TY474.js → chunk-BHY7MNGT.js} +11 -6
- package/dist/{chunk-WIFMLPT5.js → chunk-GHY2WEOK.js} +13 -0
- package/dist/{chunk-SGXUEFII.js → chunk-KUSVZXDT.js} +2 -2
- package/dist/{chunk-JYLAM6VU.js → chunk-LZO6K34D.js} +2 -2
- package/dist/{chunk-3FL2SNIR.js → chunk-NQVZNVOU.js} +1 -1
- package/dist/{chunk-IEN4NCER.js → chunk-OFH3ML4L.js} +3 -3
- package/dist/index.cjs +102 -31
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +15 -9
- package/dist/lib/client.js +2 -2
- package/dist/lib/constants.js +1 -1
- package/dist/lib/index.cjs +97 -30
- package/dist/lib/index.js +8 -8
- package/dist/react/index.cjs +202 -86
- package/dist/react/index.d.cts +4 -3
- package/dist/react/index.d.ts +4 -3
- package/dist/react/index.js +115 -66
- package/dist/react/utils.js +2 -2
- package/dist/tts/index.cjs +97 -30
- package/dist/tts/index.js +6 -6
- package/dist/tts/player.cjs +5 -0
- package/dist/tts/player.js +4 -3
- package/dist/tts/source.cjs +50 -4
- package/dist/tts/source.d.cts +16 -6
- package/dist/tts/source.d.ts +16 -6
- package/dist/tts/source.js +4 -2
- package/dist/tts/utils.cjs +18 -6
- package/dist/tts/utils.d.cts +7 -5
- package/dist/tts/utils.d.ts +7 -5
- package/dist/tts/utils.js +3 -2
- package/dist/tts/websocket.cjs +97 -30
- package/dist/tts/websocket.d.cts +12 -8
- package/dist/tts/websocket.d.ts +12 -8
- package/dist/tts/websocket.js +5 -5
- package/dist/types/index.d.cts +60 -4
- package/dist/types/index.d.ts +60 -4
- package/dist/voices/index.js +3 -3
- package/package.json +1 -1
- package/src/index.ts +2 -0
- package/src/react/index.ts +114 -63
- package/src/tts/source.ts +53 -7
- package/src/tts/utils.ts +26 -12
- package/src/tts/websocket.ts +33 -16
- package/src/types/index.ts +81 -3
package/dist/react/index.js
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Cartesia
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import "../chunk-
|
|
3
|
+
} from "../chunk-OFH3ML4L.js";
|
|
4
|
+
import "../chunk-KUSVZXDT.js";
|
|
5
5
|
import {
|
|
6
6
|
pingServer
|
|
7
|
-
} from "../chunk-
|
|
8
|
-
import "../chunk-
|
|
9
|
-
import "../chunk-
|
|
10
|
-
import "../chunk-
|
|
11
|
-
import "../chunk-PQ5EVEEH.js";
|
|
7
|
+
} from "../chunk-NQVZNVOU.js";
|
|
8
|
+
import "../chunk-2NA5SEML.js";
|
|
9
|
+
import "../chunk-ASZKHN7Q.js";
|
|
10
|
+
import "../chunk-5M33ZF3Y.js";
|
|
12
11
|
import "../chunk-2BFEKY3F.js";
|
|
13
12
|
import {
|
|
14
13
|
Player
|
|
15
|
-
} from "../chunk-
|
|
16
|
-
import "../chunk-
|
|
14
|
+
} from "../chunk-LZO6K34D.js";
|
|
15
|
+
import "../chunk-BHY7MNGT.js";
|
|
16
|
+
import "../chunk-6YQ6KDIQ.js";
|
|
17
17
|
import {
|
|
18
18
|
__async
|
|
19
|
-
} from "../chunk-
|
|
19
|
+
} from "../chunk-GHY2WEOK.js";
|
|
20
20
|
|
|
21
21
|
// src/react/index.ts
|
|
22
22
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
@@ -25,7 +25,8 @@ var DEFAULT_BUFFER_DURATION = 0.01;
|
|
|
25
25
|
function useTTS({
|
|
26
26
|
apiKey,
|
|
27
27
|
baseUrl,
|
|
28
|
-
sampleRate
|
|
28
|
+
sampleRate,
|
|
29
|
+
onError
|
|
29
30
|
}) {
|
|
30
31
|
var _a, _b;
|
|
31
32
|
if (typeof window === "undefined") {
|
|
@@ -56,7 +57,11 @@ function useTTS({
|
|
|
56
57
|
}
|
|
57
58
|
const cartesia = new Cartesia({ apiKey, baseUrl });
|
|
58
59
|
baseUrl = baseUrl != null ? baseUrl : cartesia.baseUrl;
|
|
59
|
-
return cartesia.tts.websocket({
|
|
60
|
+
return cartesia.tts.websocket({
|
|
61
|
+
container: "raw",
|
|
62
|
+
encoding: "pcm_f32le",
|
|
63
|
+
sampleRate
|
|
64
|
+
});
|
|
60
65
|
}, [apiKey, baseUrl, sampleRate]);
|
|
61
66
|
const websocketReturn = useRef(null);
|
|
62
67
|
const player = useRef(null);
|
|
@@ -70,20 +75,32 @@ function useTTS({
|
|
|
70
75
|
(options) => __async(this, null, function* () {
|
|
71
76
|
var _a2, _b2;
|
|
72
77
|
(_a2 = websocketReturn.current) == null ? void 0 : _a2.stop();
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
+
try {
|
|
79
|
+
setMessages([]);
|
|
80
|
+
setBufferStatus("buffering");
|
|
81
|
+
websocketReturn.current = (_b2 = websocket == null ? void 0 : websocket.send(options)) != null ? _b2 : null;
|
|
82
|
+
if (!websocketReturn.current) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const unsubscribe = websocketReturn.current.on("message", (message) => {
|
|
86
|
+
const parsedMessage = JSON.parse(message);
|
|
87
|
+
setMessages((messages2) => [...messages2, parsedMessage]);
|
|
88
|
+
if (parsedMessage.error) {
|
|
89
|
+
onError == null ? void 0 : onError(new Error(parsedMessage.error));
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
yield websocketReturn.current.source.once("close");
|
|
93
|
+
setBufferStatus("buffered");
|
|
94
|
+
unsubscribe();
|
|
95
|
+
} catch (error) {
|
|
96
|
+
if (error instanceof Error) {
|
|
97
|
+
onError == null ? void 0 : onError(error);
|
|
98
|
+
} else {
|
|
99
|
+
console.error(error);
|
|
100
|
+
}
|
|
78
101
|
}
|
|
79
|
-
const unsubscribe = websocketReturn.current.on("message", (message) => {
|
|
80
|
-
setMessages((messages2) => [...messages2, JSON.parse(message)]);
|
|
81
|
-
});
|
|
82
|
-
yield websocketReturn.current.source.once("close");
|
|
83
|
-
setBufferStatus("buffered");
|
|
84
|
-
unsubscribe();
|
|
85
102
|
}),
|
|
86
|
-
[websocket]
|
|
103
|
+
[websocket, onError]
|
|
87
104
|
);
|
|
88
105
|
const metrics = useMemo(() => {
|
|
89
106
|
var _a2;
|
|
@@ -152,56 +169,88 @@ function useTTS({
|
|
|
152
169
|
return () => cleanup == null ? void 0 : cleanup();
|
|
153
170
|
}, [websocket, baseUrl]);
|
|
154
171
|
const play = useCallback(() => __async(this, null, function* () {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
unsubscribe
|
|
172
|
+
try {
|
|
173
|
+
if (playbackStatus === "playing" || !websocketReturn.current) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
if (player.current) {
|
|
177
|
+
yield player.current.stop();
|
|
178
|
+
}
|
|
179
|
+
setPlaybackStatus("playing");
|
|
180
|
+
const unsubscribes = [];
|
|
181
|
+
unsubscribes.push(
|
|
182
|
+
websocketReturn.current.source.on("wait", () => {
|
|
183
|
+
setIsWaiting(true);
|
|
184
|
+
})
|
|
185
|
+
);
|
|
186
|
+
unsubscribes.push(
|
|
187
|
+
websocketReturn.current.source.on("read", () => {
|
|
188
|
+
setIsWaiting(false);
|
|
189
|
+
})
|
|
190
|
+
);
|
|
191
|
+
player.current = new Player({
|
|
192
|
+
bufferDuration: bufferDuration != null ? bufferDuration : DEFAULT_BUFFER_DURATION
|
|
193
|
+
});
|
|
194
|
+
yield player.current.play(websocketReturn.current.source);
|
|
195
|
+
for (const unsubscribe of unsubscribes) {
|
|
196
|
+
unsubscribe();
|
|
197
|
+
}
|
|
198
|
+
setPlaybackStatus("finished");
|
|
199
|
+
} catch (error) {
|
|
200
|
+
if (error instanceof Error) {
|
|
201
|
+
onError == null ? void 0 : onError(error);
|
|
202
|
+
} else {
|
|
203
|
+
console.error(error);
|
|
204
|
+
}
|
|
179
205
|
}
|
|
180
|
-
|
|
181
|
-
}), [playbackStatus, bufferDuration]);
|
|
206
|
+
}), [playbackStatus, bufferDuration, onError]);
|
|
182
207
|
const pause = useCallback(() => __async(this, null, function* () {
|
|
183
208
|
var _a2;
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
209
|
+
try {
|
|
210
|
+
yield (_a2 = player.current) == null ? void 0 : _a2.pause();
|
|
211
|
+
setPlaybackStatus("paused");
|
|
212
|
+
} catch (error) {
|
|
213
|
+
if (error instanceof Error) {
|
|
214
|
+
onError == null ? void 0 : onError(error);
|
|
215
|
+
} else {
|
|
216
|
+
console.error(error);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}), [onError]);
|
|
187
220
|
const resume = useCallback(() => __async(this, null, function* () {
|
|
188
221
|
var _a2;
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
222
|
+
try {
|
|
223
|
+
yield (_a2 = player.current) == null ? void 0 : _a2.resume();
|
|
224
|
+
setPlaybackStatus("playing");
|
|
225
|
+
} catch (error) {
|
|
226
|
+
if (error instanceof Error) {
|
|
227
|
+
onError == null ? void 0 : onError(error);
|
|
228
|
+
} else {
|
|
229
|
+
console.error(error);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}), [onError]);
|
|
192
233
|
const toggle = useCallback(() => __async(this, null, function* () {
|
|
193
234
|
var _a2;
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
235
|
+
try {
|
|
236
|
+
yield (_a2 = player.current) == null ? void 0 : _a2.toggle();
|
|
237
|
+
setPlaybackStatus((status) => {
|
|
238
|
+
if (status === "playing") {
|
|
239
|
+
return "paused";
|
|
240
|
+
}
|
|
241
|
+
if (status === "paused") {
|
|
242
|
+
return "playing";
|
|
243
|
+
}
|
|
244
|
+
return status;
|
|
245
|
+
});
|
|
246
|
+
} catch (error) {
|
|
247
|
+
if (error instanceof Error) {
|
|
248
|
+
onError == null ? void 0 : onError(error);
|
|
249
|
+
} else {
|
|
250
|
+
console.error(error);
|
|
201
251
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
}), []);
|
|
252
|
+
}
|
|
253
|
+
}), [onError]);
|
|
205
254
|
return {
|
|
206
255
|
buffer,
|
|
207
256
|
play,
|
package/dist/react/utils.js
CHANGED
package/dist/tts/index.cjs
CHANGED
|
@@ -22,6 +22,18 @@ var __spreadValues = (a, b) => {
|
|
|
22
22
|
return a;
|
|
23
23
|
};
|
|
24
24
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
25
|
+
var __objRest = (source, exclude) => {
|
|
26
|
+
var target = {};
|
|
27
|
+
for (var prop in source)
|
|
28
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
29
|
+
target[prop] = source[prop];
|
|
30
|
+
if (source != null && __getOwnPropSymbols)
|
|
31
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
32
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
33
|
+
target[prop] = source[prop];
|
|
34
|
+
}
|
|
35
|
+
return target;
|
|
36
|
+
};
|
|
25
37
|
var __export = (target, all) => {
|
|
26
38
|
for (var name in all)
|
|
27
39
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -134,7 +146,13 @@ var import_partysocket = require("partysocket");
|
|
|
134
146
|
|
|
135
147
|
// src/tts/source.ts
|
|
136
148
|
var import_emittery = __toESM(require("emittery"), 1);
|
|
137
|
-
var
|
|
149
|
+
var ENCODING_MAP = {
|
|
150
|
+
pcm_f32le: { arrayType: Float32Array, bytesPerElement: 4 },
|
|
151
|
+
pcm_s16le: { arrayType: Int16Array, bytesPerElement: 2 },
|
|
152
|
+
pcm_alaw: { arrayType: Uint8Array, bytesPerElement: 1 },
|
|
153
|
+
pcm_mulaw: { arrayType: Uint8Array, bytesPerElement: 1 }
|
|
154
|
+
};
|
|
155
|
+
var _emitter, _buffer, _readIndex, _writeIndex, _closed, _sampleRate, _encoding, _container, _createBuffer, createBuffer_fn;
|
|
138
156
|
var Source = class {
|
|
139
157
|
/**
|
|
140
158
|
* Create a new Source.
|
|
@@ -142,23 +160,44 @@ var Source = class {
|
|
|
142
160
|
* @param options - Options for the Source.
|
|
143
161
|
* @param options.sampleRate - The sample rate of the audio.
|
|
144
162
|
*/
|
|
145
|
-
constructor({
|
|
163
|
+
constructor({
|
|
164
|
+
sampleRate,
|
|
165
|
+
encoding,
|
|
166
|
+
container
|
|
167
|
+
}) {
|
|
168
|
+
/**
|
|
169
|
+
* Create a new buffer for the source.
|
|
170
|
+
*
|
|
171
|
+
* @param size - The size of the buffer to create.
|
|
172
|
+
* @returns The new buffer as a TypedArray based on the encoding.
|
|
173
|
+
*/
|
|
174
|
+
__privateAdd(this, _createBuffer);
|
|
146
175
|
__privateAdd(this, _emitter, new import_emittery.default());
|
|
147
176
|
__privateAdd(this, _buffer, void 0);
|
|
148
177
|
__privateAdd(this, _readIndex, 0);
|
|
149
178
|
__privateAdd(this, _writeIndex, 0);
|
|
150
179
|
__privateAdd(this, _closed, false);
|
|
151
180
|
__privateAdd(this, _sampleRate, void 0);
|
|
181
|
+
__privateAdd(this, _encoding, void 0);
|
|
182
|
+
__privateAdd(this, _container, void 0);
|
|
152
183
|
this.on = __privateGet(this, _emitter).on.bind(__privateGet(this, _emitter));
|
|
153
184
|
this.once = __privateGet(this, _emitter).once.bind(__privateGet(this, _emitter));
|
|
154
185
|
this.events = __privateGet(this, _emitter).events.bind(__privateGet(this, _emitter));
|
|
155
186
|
this.off = __privateGet(this, _emitter).off.bind(__privateGet(this, _emitter));
|
|
156
187
|
__privateSet(this, _sampleRate, sampleRate);
|
|
157
|
-
__privateSet(this,
|
|
188
|
+
__privateSet(this, _encoding, encoding);
|
|
189
|
+
__privateSet(this, _container, container);
|
|
190
|
+
__privateSet(this, _buffer, __privateMethod(this, _createBuffer, createBuffer_fn).call(this, 1024));
|
|
158
191
|
}
|
|
159
192
|
get sampleRate() {
|
|
160
193
|
return __privateGet(this, _sampleRate);
|
|
161
194
|
}
|
|
195
|
+
get encoding() {
|
|
196
|
+
return __privateGet(this, _encoding);
|
|
197
|
+
}
|
|
198
|
+
get container() {
|
|
199
|
+
return __privateGet(this, _container);
|
|
200
|
+
}
|
|
162
201
|
/**
|
|
163
202
|
* Append audio to the buffer.
|
|
164
203
|
*
|
|
@@ -172,7 +211,7 @@ var Source = class {
|
|
|
172
211
|
while (newCapacity < requiredCapacity) {
|
|
173
212
|
newCapacity *= 2;
|
|
174
213
|
}
|
|
175
|
-
const newBuffer =
|
|
214
|
+
const newBuffer = __privateMethod(this, _createBuffer, createBuffer_fn).call(this, newCapacity);
|
|
176
215
|
newBuffer.set(__privateGet(this, _buffer));
|
|
177
216
|
__privateSet(this, _buffer, newBuffer);
|
|
178
217
|
}
|
|
@@ -220,6 +259,9 @@ var Source = class {
|
|
|
220
259
|
get readIndex() {
|
|
221
260
|
return __privateGet(this, _readIndex);
|
|
222
261
|
}
|
|
262
|
+
get writeIndex() {
|
|
263
|
+
return __privateGet(this, _writeIndex);
|
|
264
|
+
}
|
|
223
265
|
/**
|
|
224
266
|
* Close the source. This signals that no more audio will be enqueued.
|
|
225
267
|
*
|
|
@@ -241,19 +283,27 @@ _readIndex = new WeakMap();
|
|
|
241
283
|
_writeIndex = new WeakMap();
|
|
242
284
|
_closed = new WeakMap();
|
|
243
285
|
_sampleRate = new WeakMap();
|
|
286
|
+
_encoding = new WeakMap();
|
|
287
|
+
_container = new WeakMap();
|
|
288
|
+
_createBuffer = new WeakSet();
|
|
289
|
+
createBuffer_fn = function(size) {
|
|
290
|
+
const { arrayType: ArrayType } = ENCODING_MAP[__privateGet(this, _encoding)];
|
|
291
|
+
return new ArrayType(size);
|
|
292
|
+
};
|
|
244
293
|
|
|
245
294
|
// src/tts/utils.ts
|
|
246
295
|
var import_base64_js = __toESM(require("base64-js"), 1);
|
|
247
|
-
function base64ToArray(b64) {
|
|
296
|
+
function base64ToArray(b64, encoding) {
|
|
248
297
|
const byteArrays = filterSentinel(b64).map((b) => import_base64_js.default.toByteArray(b));
|
|
298
|
+
const { arrayType: ArrayType, bytesPerElement } = ENCODING_MAP[encoding];
|
|
249
299
|
const totalLength = byteArrays.reduce(
|
|
250
|
-
(acc, arr) => acc + arr.length /
|
|
300
|
+
(acc, arr) => acc + arr.length / bytesPerElement,
|
|
251
301
|
0
|
|
252
302
|
);
|
|
253
|
-
const result = new
|
|
303
|
+
const result = new ArrayType(totalLength);
|
|
254
304
|
let offset = 0;
|
|
255
305
|
for (const arr of byteArrays) {
|
|
256
|
-
const floats = new
|
|
306
|
+
const floats = new ArrayType(arr.buffer);
|
|
257
307
|
result.set(floats, offset);
|
|
258
308
|
offset += floats.length;
|
|
259
309
|
}
|
|
@@ -271,10 +321,10 @@ function createMessageHandlerForContextId(contextId, handler) {
|
|
|
271
321
|
let chunk;
|
|
272
322
|
if (message.done) {
|
|
273
323
|
chunk = getSentinel();
|
|
274
|
-
} else {
|
|
324
|
+
} else if (message.type === "chunk") {
|
|
275
325
|
chunk = message.data;
|
|
276
326
|
}
|
|
277
|
-
handler({ chunk, message: event.data });
|
|
327
|
+
handler({ chunk, message: event.data, data: message });
|
|
278
328
|
};
|
|
279
329
|
}
|
|
280
330
|
function getSentinel() {
|
|
@@ -298,14 +348,14 @@ function getEmitteryCallbacks(emitter) {
|
|
|
298
348
|
}
|
|
299
349
|
|
|
300
350
|
// src/tts/websocket.ts
|
|
301
|
-
var _isConnected, _sampleRate2, _generateId, generateId_fn;
|
|
351
|
+
var _isConnected, _sampleRate2, _container2, _encoding2, _generateId, generateId_fn;
|
|
302
352
|
var WebSocket = class extends Client {
|
|
303
353
|
/**
|
|
304
354
|
* Create a new WebSocket client.
|
|
305
355
|
*
|
|
306
356
|
* @param args - Arguments to pass to the Client constructor.
|
|
307
357
|
*/
|
|
308
|
-
constructor({ sampleRate }, ...args) {
|
|
358
|
+
constructor({ sampleRate, container, encoding }, ...args) {
|
|
309
359
|
super(...args);
|
|
310
360
|
/**
|
|
311
361
|
* Generate a unique ID suitable for a streaming context.
|
|
@@ -318,12 +368,16 @@ var WebSocket = class extends Client {
|
|
|
318
368
|
__privateAdd(this, _generateId);
|
|
319
369
|
__privateAdd(this, _isConnected, false);
|
|
320
370
|
__privateAdd(this, _sampleRate2, void 0);
|
|
371
|
+
__privateAdd(this, _container2, void 0);
|
|
372
|
+
__privateAdd(this, _encoding2, void 0);
|
|
321
373
|
__privateSet(this, _sampleRate2, sampleRate);
|
|
374
|
+
__privateSet(this, _container2, container != null ? container : "raw");
|
|
375
|
+
__privateSet(this, _encoding2, encoding != null ? encoding : "pcm_f32le");
|
|
322
376
|
}
|
|
323
377
|
/**
|
|
324
378
|
* Send a message over the WebSocket to start a stream.
|
|
325
379
|
*
|
|
326
|
-
* @param inputs - Stream options.
|
|
380
|
+
* @param inputs - Stream options. Defined in the StreamRequest type.
|
|
327
381
|
* @param options - Options for the stream.
|
|
328
382
|
* @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
|
|
329
383
|
* If set to `0`, the stream will not time out.
|
|
@@ -331,26 +385,30 @@ var WebSocket = class extends Client {
|
|
|
331
385
|
* @returns An Emittery instance that emits messages from the WebSocket.
|
|
332
386
|
* @returns An abort function that can be called to cancel the stream.
|
|
333
387
|
*/
|
|
334
|
-
send(
|
|
335
|
-
var
|
|
388
|
+
send(_a, { timeout = 0 } = {}) {
|
|
389
|
+
var inputs = __objRest(_a, []);
|
|
390
|
+
var _a2, _b, _c, _d;
|
|
336
391
|
if (!__privateGet(this, _isConnected)) {
|
|
337
392
|
throw new Error("Not connected to WebSocket. Call .connect() first.");
|
|
338
393
|
}
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
394
|
+
if (!inputs.context_id) {
|
|
395
|
+
inputs.context_id = __privateMethod(this, _generateId, generateId_fn).call(this);
|
|
396
|
+
}
|
|
397
|
+
if (!inputs.output_format) {
|
|
398
|
+
inputs.output_format = {
|
|
399
|
+
container: __privateGet(this, _container2),
|
|
400
|
+
encoding: __privateGet(this, _encoding2),
|
|
401
|
+
sample_rate: __privateGet(this, _sampleRate2)
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
(_a2 = this.socket) == null ? void 0 : _a2.send(
|
|
405
|
+
JSON.stringify(__spreadValues({}, inputs))
|
|
350
406
|
);
|
|
351
407
|
const emitter = new import_emittery2.default();
|
|
352
408
|
const source = new Source({
|
|
353
|
-
sampleRate: __privateGet(this, _sampleRate2)
|
|
409
|
+
sampleRate: __privateGet(this, _sampleRate2),
|
|
410
|
+
encoding: __privateGet(this, _encoding2),
|
|
411
|
+
container: __privateGet(this, _container2)
|
|
354
412
|
});
|
|
355
413
|
const streamCompleteController = new AbortController();
|
|
356
414
|
let timeoutId = null;
|
|
@@ -358,19 +416,26 @@ var WebSocket = class extends Client {
|
|
|
358
416
|
timeoutId = setTimeout(streamCompleteController.abort, timeout);
|
|
359
417
|
}
|
|
360
418
|
const handleMessage = createMessageHandlerForContextId(
|
|
361
|
-
|
|
362
|
-
(_0) => __async(this, [_0], function* ({ chunk, message }) {
|
|
419
|
+
inputs.context_id,
|
|
420
|
+
(_0) => __async(this, [_0], function* ({ chunk, message, data }) {
|
|
363
421
|
emitter.emit("message", message);
|
|
422
|
+
if (data.type === "timestamps") {
|
|
423
|
+
emitter.emit("timestamps", data.word_timestamps);
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
364
426
|
if (isSentinel(chunk)) {
|
|
365
427
|
yield source.close();
|
|
366
428
|
streamCompleteController.abort();
|
|
367
429
|
return;
|
|
368
430
|
}
|
|
369
|
-
yield source.enqueue(base64ToArray([chunk]));
|
|
370
431
|
if (timeoutId) {
|
|
371
432
|
clearTimeout(timeoutId);
|
|
372
433
|
timeoutId = setTimeout(streamCompleteController.abort, timeout);
|
|
373
434
|
}
|
|
435
|
+
if (!chunk) {
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
yield source.enqueue(base64ToArray([chunk], __privateGet(this, _encoding2)));
|
|
374
439
|
})
|
|
375
440
|
);
|
|
376
441
|
(_b = this.socket) == null ? void 0 : _b.addEventListener("message", handleMessage, {
|
|
@@ -474,6 +539,8 @@ var WebSocket = class extends Client {
|
|
|
474
539
|
};
|
|
475
540
|
_isConnected = new WeakMap();
|
|
476
541
|
_sampleRate2 = new WeakMap();
|
|
542
|
+
_container2 = new WeakMap();
|
|
543
|
+
_encoding2 = new WeakMap();
|
|
477
544
|
_generateId = new WeakSet();
|
|
478
545
|
generateId_fn = function() {
|
|
479
546
|
return (0, import_human_id.humanId)({
|
package/dist/tts/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
TTS
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import "../chunk-
|
|
5
|
-
import "../chunk-
|
|
6
|
-
import "../chunk-PQ5EVEEH.js";
|
|
3
|
+
} from "../chunk-2NA5SEML.js";
|
|
4
|
+
import "../chunk-ASZKHN7Q.js";
|
|
5
|
+
import "../chunk-5M33ZF3Y.js";
|
|
7
6
|
import "../chunk-2BFEKY3F.js";
|
|
8
|
-
import "../chunk-
|
|
9
|
-
import "../chunk-
|
|
7
|
+
import "../chunk-BHY7MNGT.js";
|
|
8
|
+
import "../chunk-6YQ6KDIQ.js";
|
|
9
|
+
import "../chunk-GHY2WEOK.js";
|
|
10
10
|
export {
|
|
11
11
|
TTS as default
|
|
12
12
|
};
|
package/dist/tts/player.cjs
CHANGED
|
@@ -78,6 +78,11 @@ module.exports = __toCommonJS(player_exports);
|
|
|
78
78
|
|
|
79
79
|
// src/tts/utils.ts
|
|
80
80
|
var import_base64_js = __toESM(require("base64-js"), 1);
|
|
81
|
+
|
|
82
|
+
// src/tts/source.ts
|
|
83
|
+
var import_emittery = __toESM(require("emittery"), 1);
|
|
84
|
+
|
|
85
|
+
// src/tts/utils.ts
|
|
81
86
|
function playAudioBuffer(floats, context, startAt, sampleRate) {
|
|
82
87
|
const source = context.createBufferSource();
|
|
83
88
|
const buffer = context.createBuffer(1, floats.length, sampleRate);
|
package/dist/tts/player.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Player
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import "../chunk-
|
|
5
|
-
import "../chunk-
|
|
3
|
+
} from "../chunk-LZO6K34D.js";
|
|
4
|
+
import "../chunk-BHY7MNGT.js";
|
|
5
|
+
import "../chunk-6YQ6KDIQ.js";
|
|
6
|
+
import "../chunk-GHY2WEOK.js";
|
|
6
7
|
export {
|
|
7
8
|
Player as default
|
|
8
9
|
};
|
package/dist/tts/source.cjs
CHANGED
|
@@ -44,6 +44,10 @@ var __privateSet = (obj, member, value, setter) => {
|
|
|
44
44
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
45
45
|
return value;
|
|
46
46
|
};
|
|
47
|
+
var __privateMethod = (obj, member, method) => {
|
|
48
|
+
__accessCheck(obj, member, "access private method");
|
|
49
|
+
return method;
|
|
50
|
+
};
|
|
47
51
|
var __async = (__this, __arguments, generator) => {
|
|
48
52
|
return new Promise((resolve, reject) => {
|
|
49
53
|
var fulfilled = (value) => {
|
|
@@ -68,11 +72,18 @@ var __async = (__this, __arguments, generator) => {
|
|
|
68
72
|
// src/tts/source.ts
|
|
69
73
|
var source_exports = {};
|
|
70
74
|
__export(source_exports, {
|
|
75
|
+
ENCODING_MAP: () => ENCODING_MAP,
|
|
71
76
|
default: () => Source
|
|
72
77
|
});
|
|
73
78
|
module.exports = __toCommonJS(source_exports);
|
|
74
79
|
var import_emittery = __toESM(require("emittery"), 1);
|
|
75
|
-
var
|
|
80
|
+
var ENCODING_MAP = {
|
|
81
|
+
pcm_f32le: { arrayType: Float32Array, bytesPerElement: 4 },
|
|
82
|
+
pcm_s16le: { arrayType: Int16Array, bytesPerElement: 2 },
|
|
83
|
+
pcm_alaw: { arrayType: Uint8Array, bytesPerElement: 1 },
|
|
84
|
+
pcm_mulaw: { arrayType: Uint8Array, bytesPerElement: 1 }
|
|
85
|
+
};
|
|
86
|
+
var _emitter, _buffer, _readIndex, _writeIndex, _closed, _sampleRate, _encoding, _container, _createBuffer, createBuffer_fn;
|
|
76
87
|
var Source = class {
|
|
77
88
|
/**
|
|
78
89
|
* Create a new Source.
|
|
@@ -80,23 +91,44 @@ var Source = class {
|
|
|
80
91
|
* @param options - Options for the Source.
|
|
81
92
|
* @param options.sampleRate - The sample rate of the audio.
|
|
82
93
|
*/
|
|
83
|
-
constructor({
|
|
94
|
+
constructor({
|
|
95
|
+
sampleRate,
|
|
96
|
+
encoding,
|
|
97
|
+
container
|
|
98
|
+
}) {
|
|
99
|
+
/**
|
|
100
|
+
* Create a new buffer for the source.
|
|
101
|
+
*
|
|
102
|
+
* @param size - The size of the buffer to create.
|
|
103
|
+
* @returns The new buffer as a TypedArray based on the encoding.
|
|
104
|
+
*/
|
|
105
|
+
__privateAdd(this, _createBuffer);
|
|
84
106
|
__privateAdd(this, _emitter, new import_emittery.default());
|
|
85
107
|
__privateAdd(this, _buffer, void 0);
|
|
86
108
|
__privateAdd(this, _readIndex, 0);
|
|
87
109
|
__privateAdd(this, _writeIndex, 0);
|
|
88
110
|
__privateAdd(this, _closed, false);
|
|
89
111
|
__privateAdd(this, _sampleRate, void 0);
|
|
112
|
+
__privateAdd(this, _encoding, void 0);
|
|
113
|
+
__privateAdd(this, _container, void 0);
|
|
90
114
|
this.on = __privateGet(this, _emitter).on.bind(__privateGet(this, _emitter));
|
|
91
115
|
this.once = __privateGet(this, _emitter).once.bind(__privateGet(this, _emitter));
|
|
92
116
|
this.events = __privateGet(this, _emitter).events.bind(__privateGet(this, _emitter));
|
|
93
117
|
this.off = __privateGet(this, _emitter).off.bind(__privateGet(this, _emitter));
|
|
94
118
|
__privateSet(this, _sampleRate, sampleRate);
|
|
95
|
-
__privateSet(this,
|
|
119
|
+
__privateSet(this, _encoding, encoding);
|
|
120
|
+
__privateSet(this, _container, container);
|
|
121
|
+
__privateSet(this, _buffer, __privateMethod(this, _createBuffer, createBuffer_fn).call(this, 1024));
|
|
96
122
|
}
|
|
97
123
|
get sampleRate() {
|
|
98
124
|
return __privateGet(this, _sampleRate);
|
|
99
125
|
}
|
|
126
|
+
get encoding() {
|
|
127
|
+
return __privateGet(this, _encoding);
|
|
128
|
+
}
|
|
129
|
+
get container() {
|
|
130
|
+
return __privateGet(this, _container);
|
|
131
|
+
}
|
|
100
132
|
/**
|
|
101
133
|
* Append audio to the buffer.
|
|
102
134
|
*
|
|
@@ -110,7 +142,7 @@ var Source = class {
|
|
|
110
142
|
while (newCapacity < requiredCapacity) {
|
|
111
143
|
newCapacity *= 2;
|
|
112
144
|
}
|
|
113
|
-
const newBuffer =
|
|
145
|
+
const newBuffer = __privateMethod(this, _createBuffer, createBuffer_fn).call(this, newCapacity);
|
|
114
146
|
newBuffer.set(__privateGet(this, _buffer));
|
|
115
147
|
__privateSet(this, _buffer, newBuffer);
|
|
116
148
|
}
|
|
@@ -158,6 +190,9 @@ var Source = class {
|
|
|
158
190
|
get readIndex() {
|
|
159
191
|
return __privateGet(this, _readIndex);
|
|
160
192
|
}
|
|
193
|
+
get writeIndex() {
|
|
194
|
+
return __privateGet(this, _writeIndex);
|
|
195
|
+
}
|
|
161
196
|
/**
|
|
162
197
|
* Close the source. This signals that no more audio will be enqueued.
|
|
163
198
|
*
|
|
@@ -179,3 +214,14 @@ _readIndex = new WeakMap();
|
|
|
179
214
|
_writeIndex = new WeakMap();
|
|
180
215
|
_closed = new WeakMap();
|
|
181
216
|
_sampleRate = new WeakMap();
|
|
217
|
+
_encoding = new WeakMap();
|
|
218
|
+
_container = new WeakMap();
|
|
219
|
+
_createBuffer = new WeakSet();
|
|
220
|
+
createBuffer_fn = function(size) {
|
|
221
|
+
const { arrayType: ArrayType } = ENCODING_MAP[__privateGet(this, _encoding)];
|
|
222
|
+
return new ArrayType(size);
|
|
223
|
+
};
|
|
224
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
225
|
+
0 && (module.exports = {
|
|
226
|
+
ENCODING_MAP
|
|
227
|
+
});
|