@cartesia/cartesia-js 1.0.0 → 1.0.2
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 +50 -50
- package/CHANGELOG.md +12 -0
- package/LICENSE.md +21 -0
- package/README.md +92 -19
- package/dist/{chunk-PQ6CIPFW.js → chunk-6YQ6KDIQ.js} +44 -5
- package/dist/{chunk-RO7TY474.js → chunk-BHY7MNGT.js} +11 -6
- package/dist/{chunk-F4QWVJY3.js → chunk-EDAAHENY.js} +2 -2
- package/dist/{chunk-WIFMLPT5.js → chunk-GHY2WEOK.js} +13 -0
- package/dist/{chunk-FN7BK4PS.js → chunk-IZBPLCGW.js} +97 -75
- 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-NVOCUUOF.js} +3 -3
- package/dist/chunk-PISCPZK4.js +40 -0
- package/dist/{chunk-SGXUEFII.js → chunk-UCYL2SOX.js} +18 -15
- package/dist/index.cjs +186 -103
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +15 -9
- package/dist/lib/client.cjs +35 -10
- package/dist/lib/client.d.cts +2 -2
- package/dist/lib/client.d.ts +2 -2
- package/dist/lib/client.js +2 -2
- package/dist/lib/constants.js +1 -1
- package/dist/lib/index.cjs +181 -102
- package/dist/lib/index.js +8 -8
- package/dist/react/index.cjs +286 -158
- package/dist/react/index.d.cts +5 -4
- package/dist/react/index.d.ts +5 -4
- package/dist/react/index.js +115 -66
- package/dist/react/utils.js +2 -2
- package/dist/tts/index.cjs +165 -89
- 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 +165 -89
- 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 +65 -5
- package/dist/types/index.d.ts +65 -5
- package/dist/voices/index.cjs +31 -23
- package/dist/voices/index.d.cts +2 -1
- package/dist/voices/index.d.ts +2 -1
- package/dist/voices/index.js +3 -3
- package/package.json +1 -1
- package/src/index.ts +2 -0
- package/src/lib/client.ts +10 -10
- package/src/react/index.ts +115 -64
- package/src/tts/source.ts +53 -7
- package/src/tts/utils.ts +26 -12
- package/src/tts/websocket.ts +42 -23
- package/src/types/index.ts +89 -4
- package/src/voices/index.ts +22 -15
- package/dist/chunk-PQ5EVEEH.js +0 -34
package/dist/react/index.d.cts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import Source from '../tts/source.cjs';
|
|
2
|
+
import { StreamRequest } from '../types/index.cjs';
|
|
2
3
|
import 'emittery';
|
|
3
|
-
import '../types/index.cjs';
|
|
4
4
|
|
|
5
5
|
type UseTTSOptions = {
|
|
6
|
-
apiKey: string | null;
|
|
6
|
+
apiKey: string | (() => Promise<string>) | null;
|
|
7
7
|
baseUrl?: string;
|
|
8
8
|
sampleRate: number;
|
|
9
|
+
onError?: (error: Error) => void;
|
|
9
10
|
};
|
|
10
11
|
type PlaybackStatus = "inactive" | "playing" | "paused" | "finished";
|
|
11
12
|
type BufferStatus = "inactive" | "buffering" | "buffered";
|
|
@@ -13,7 +14,7 @@ type Metrics = {
|
|
|
13
14
|
modelLatency: number | null;
|
|
14
15
|
};
|
|
15
16
|
interface UseTTSReturn {
|
|
16
|
-
buffer: (options:
|
|
17
|
+
buffer: (options: StreamRequest) => Promise<void>;
|
|
17
18
|
play: (bufferDuration?: number) => Promise<void>;
|
|
18
19
|
pause: () => Promise<void>;
|
|
19
20
|
resume: () => Promise<void>;
|
|
@@ -28,6 +29,6 @@ interface UseTTSReturn {
|
|
|
28
29
|
/**
|
|
29
30
|
* React hook to use the Cartesia audio API.
|
|
30
31
|
*/
|
|
31
|
-
declare function useTTS({ apiKey, baseUrl, sampleRate, }: UseTTSOptions): UseTTSReturn;
|
|
32
|
+
declare function useTTS({ apiKey, baseUrl, sampleRate, onError, }: UseTTSOptions): UseTTSReturn;
|
|
32
33
|
|
|
33
34
|
export { type BufferStatus, type Metrics, type PlaybackStatus, type UseTTSOptions, type UseTTSReturn, useTTS };
|
package/dist/react/index.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import Source from '../tts/source.js';
|
|
2
|
+
import { StreamRequest } from '../types/index.js';
|
|
2
3
|
import 'emittery';
|
|
3
|
-
import '../types/index.js';
|
|
4
4
|
|
|
5
5
|
type UseTTSOptions = {
|
|
6
|
-
apiKey: string | null;
|
|
6
|
+
apiKey: string | (() => Promise<string>) | null;
|
|
7
7
|
baseUrl?: string;
|
|
8
8
|
sampleRate: number;
|
|
9
|
+
onError?: (error: Error) => void;
|
|
9
10
|
};
|
|
10
11
|
type PlaybackStatus = "inactive" | "playing" | "paused" | "finished";
|
|
11
12
|
type BufferStatus = "inactive" | "buffering" | "buffered";
|
|
@@ -13,7 +14,7 @@ type Metrics = {
|
|
|
13
14
|
modelLatency: number | null;
|
|
14
15
|
};
|
|
15
16
|
interface UseTTSReturn {
|
|
16
|
-
buffer: (options:
|
|
17
|
+
buffer: (options: StreamRequest) => Promise<void>;
|
|
17
18
|
play: (bufferDuration?: number) => Promise<void>;
|
|
18
19
|
pause: () => Promise<void>;
|
|
19
20
|
resume: () => Promise<void>;
|
|
@@ -28,6 +29,6 @@ interface UseTTSReturn {
|
|
|
28
29
|
/**
|
|
29
30
|
* React hook to use the Cartesia audio API.
|
|
30
31
|
*/
|
|
31
|
-
declare function useTTS({ apiKey, baseUrl, sampleRate, }: UseTTSOptions): UseTTSReturn;
|
|
32
|
+
declare function useTTS({ apiKey, baseUrl, sampleRate, onError, }: UseTTSOptions): UseTTSReturn;
|
|
32
33
|
|
|
33
34
|
export { type BufferStatus, type Metrics, type PlaybackStatus, type UseTTSOptions, type UseTTSReturn, useTTS };
|
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-NVOCUUOF.js";
|
|
4
|
+
import "../chunk-UCYL2SOX.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-EDAAHENY.js";
|
|
9
|
+
import "../chunk-IZBPLCGW.js";
|
|
10
|
+
import "../chunk-PISCPZK4.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,
|