@cartesia/cartesia-js 1.0.0-alpha.4 → 1.0.0
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 +18 -18
- package/CHANGELOG.md +17 -0
- package/README.md +12 -4
- package/dist/{chunk-VK7LBMVI.js → chunk-F4QWVJY3.js} +1 -1
- package/dist/{chunk-IQAXBRHU.js → chunk-FN7BK4PS.js} +9 -3
- package/dist/{chunk-ISRU7PLL.js → chunk-IEN4NCER.js} +1 -1
- package/dist/{chunk-36JBKJUN.js → chunk-JYLAM6VU.js} +18 -5
- package/dist/index.cjs +27 -8
- package/dist/index.js +4 -4
- package/dist/lib/index.cjs +9 -3
- package/dist/lib/index.js +3 -3
- package/dist/react/index.cjs +33 -10
- package/dist/react/index.js +10 -6
- package/dist/tts/index.cjs +9 -3
- package/dist/tts/index.js +2 -2
- package/dist/tts/player.cjs +18 -5
- package/dist/tts/player.d.cts +6 -0
- package/dist/tts/player.d.ts +6 -0
- package/dist/tts/player.js +1 -1
- package/dist/tts/websocket.cjs +9 -3
- package/dist/tts/websocket.d.cts +8 -2
- package/dist/tts/websocket.d.ts +8 -2
- package/dist/tts/websocket.js +1 -1
- package/package.json +1 -1
- package/src/react/index.ts +7 -3
- package/src/tts/player.ts +15 -8
- package/src/tts/websocket.ts +9 -3
package/.turbo/turbo-build.log
CHANGED
|
@@ -8,21 +8,21 @@ $ tsup src/ --format cjs,esm --dts
|
|
|
8
8
|
[32mESM[39m [1mdist/index.js [22m[32m437.00 B[39m
|
|
9
9
|
[32mESM[39m [1mdist/chunk-FXPGR372.js [22m[32m0 B[39m
|
|
10
10
|
[32mESM[39m [1mdist/lib/client.js [22m[32m132.00 B[39m
|
|
11
|
-
[32mESM[39m [1mdist/react/index.js [22m[32m6.
|
|
11
|
+
[32mESM[39m [1mdist/react/index.js [22m[32m6.35 KB[39m
|
|
12
12
|
[32mESM[39m [1mdist/tts/index.js [22m[32m261.00 B[39m
|
|
13
13
|
[32mESM[39m [1mdist/lib/constants.js [22m[32m183.00 B[39m
|
|
14
14
|
[32mESM[39m [1mdist/lib/index.js [22m[32m322.00 B[39m
|
|
15
|
-
[32mESM[39m [1mdist/chunk-
|
|
15
|
+
[32mESM[39m [1mdist/chunk-IEN4NCER.js [22m[32m353.00 B[39m
|
|
16
16
|
[32mESM[39m [1mdist/chunk-SGXUEFII.js [22m[32m1.35 KB[39m
|
|
17
17
|
[32mESM[39m [1mdist/react/utils.js [22m[32m109.00 B[39m
|
|
18
18
|
[32mESM[39m [1mdist/chunk-3FL2SNIR.js [22m[32m337.00 B[39m
|
|
19
|
-
[32mESM[39m [1mdist/chunk-
|
|
20
|
-
[32mESM[39m [1mdist/chunk-
|
|
19
|
+
[32mESM[39m [1mdist/chunk-F4QWVJY3.js [22m[32m439.00 B[39m
|
|
20
|
+
[32mESM[39m [1mdist/chunk-FN7BK4PS.js [22m[32m6.11 KB[39m
|
|
21
21
|
[32mESM[39m [1mdist/chunk-PQ6CIPFW.js [22m[32m4.02 KB[39m
|
|
22
22
|
[32mESM[39m [1mdist/chunk-PQ5EVEEH.js [22m[32m841.00 B[39m
|
|
23
23
|
[32mESM[39m [1mdist/chunk-2BFEKY3F.js [22m[32m366.00 B[39m
|
|
24
24
|
[32mESM[39m [1mdist/tts/player.js [22m[32m143.00 B[39m
|
|
25
|
-
[32mESM[39m [1mdist/chunk-
|
|
25
|
+
[32mESM[39m [1mdist/chunk-JYLAM6VU.js [22m[32m3.76 KB[39m
|
|
26
26
|
[32mESM[39m [1mdist/chunk-RO7TY474.js [22m[32m1.95 KB[39m
|
|
27
27
|
[32mESM[39m [1mdist/chunk-WIFMLPT5.js [22m[32m2.27 KB[39m
|
|
28
28
|
[32mESM[39m [1mdist/types/index.js [22m[32m31.00 B[39m
|
|
@@ -30,32 +30,32 @@ $ tsup src/ --format cjs,esm --dts
|
|
|
30
30
|
[32mESM[39m [1mdist/tts/source.js [22m[32m112.00 B[39m
|
|
31
31
|
[32mESM[39m [1mdist/tts/utils.js [22m[32m395.00 B[39m
|
|
32
32
|
[32mESM[39m [1mdist/tts/websocket.js [22m[32m242.00 B[39m
|
|
33
|
-
[32mESM[39m ⚡️ Build success in
|
|
34
|
-
[32mCJS[39m [1mdist/index.cjs [22m[32m21.
|
|
33
|
+
[32mESM[39m ⚡️ Build success in 108ms
|
|
34
|
+
[32mCJS[39m [1mdist/index.cjs [22m[32m21.48 KB[39m
|
|
35
35
|
[32mCJS[39m [1mdist/lib/client.cjs [22m[32m3.34 KB[39m
|
|
36
36
|
[32mCJS[39m [1mdist/lib/constants.cjs [22m[32m1.43 KB[39m
|
|
37
|
-
[32mCJS[39m [1mdist/lib/index.cjs [22m[32m17.
|
|
38
|
-
[32mCJS[39m [1mdist/react/index.cjs [22m[32m27.
|
|
37
|
+
[32mCJS[39m [1mdist/lib/index.cjs [22m[32m17.45 KB[39m
|
|
38
|
+
[32mCJS[39m [1mdist/react/index.cjs [22m[32m27.85 KB[39m
|
|
39
39
|
[32mCJS[39m [1mdist/react/utils.cjs [22m[32m1.80 KB[39m
|
|
40
|
-
[32mCJS[39m [1mdist/tts/index.cjs [22m[32m15.
|
|
41
|
-
[32mCJS[39m [1mdist/tts/player.cjs [22m[32m6.
|
|
40
|
+
[32mCJS[39m [1mdist/tts/index.cjs [22m[32m15.93 KB[39m
|
|
41
|
+
[32mCJS[39m [1mdist/tts/player.cjs [22m[32m6.87 KB[39m
|
|
42
42
|
[32mCJS[39m [1mdist/tts/source.cjs [22m[32m6.63 KB[39m
|
|
43
43
|
[32mCJS[39m [1mdist/tts/utils.cjs [22m[32m3.87 KB[39m
|
|
44
|
-
[32mCJS[39m [1mdist/tts/websocket.cjs [22m[32m15.
|
|
44
|
+
[32mCJS[39m [1mdist/tts/websocket.cjs [22m[32m15.62 KB[39m
|
|
45
45
|
[32mCJS[39m [1mdist/types/index.cjs [22m[32m764.00 B[39m
|
|
46
46
|
[32mCJS[39m [1mdist/voices/index.cjs [22m[32m5.04 KB[39m
|
|
47
|
-
[32mCJS[39m ⚡️ Build success in
|
|
47
|
+
[32mCJS[39m ⚡️ Build success in 111ms
|
|
48
48
|
[34mDTS[39m Build start
|
|
49
|
-
[32mDTS[39m ⚡️ Build success in
|
|
49
|
+
[32mDTS[39m ⚡️ Build success in 6545ms
|
|
50
50
|
[32mDTS[39m [1mdist/index.d.cts [22m[32m509.00 B[39m
|
|
51
51
|
[32mDTS[39m [1mdist/lib/constants.d.cts [22m[32m564.00 B[39m
|
|
52
52
|
[32mDTS[39m [1mdist/lib/index.d.cts [22m[32m410.00 B[39m
|
|
53
53
|
[32mDTS[39m [1mdist/react/index.d.cts [22m[32m1018.00 B[39m
|
|
54
54
|
[32mDTS[39m [1mdist/react/utils.d.cts [22m[32m240.00 B[39m
|
|
55
55
|
[32mDTS[39m [1mdist/tts/index.d.cts [22m[32m471.00 B[39m
|
|
56
|
-
[32mDTS[39m [1mdist/tts/player.d.cts [22m[32m1.
|
|
56
|
+
[32mDTS[39m [1mdist/tts/player.d.cts [22m[32m1.20 KB[39m
|
|
57
57
|
[32mDTS[39m [1mdist/tts/utils.d.cts [22m[32m2.56 KB[39m
|
|
58
|
-
[32mDTS[39m [1mdist/tts/websocket.d.cts [22m[32m2.
|
|
58
|
+
[32mDTS[39m [1mdist/tts/websocket.d.cts [22m[32m2.66 KB[39m
|
|
59
59
|
[32mDTS[39m [1mdist/tts/source.d.cts [22m[32m2.17 KB[39m
|
|
60
60
|
[32mDTS[39m [1mdist/voices/index.d.cts [22m[32m399.00 B[39m
|
|
61
61
|
[32mDTS[39m [1mdist/lib/client.d.cts [22m[32m267.00 B[39m
|
|
@@ -66,9 +66,9 @@ $ tsup src/ --format cjs,esm --dts
|
|
|
66
66
|
[32mDTS[39m [1mdist/react/index.d.ts [22m[32m1016.00 B[39m
|
|
67
67
|
[32mDTS[39m [1mdist/react/utils.d.ts [22m[32m240.00 B[39m
|
|
68
68
|
[32mDTS[39m [1mdist/tts/index.d.ts [22m[32m467.00 B[39m
|
|
69
|
-
[32mDTS[39m [1mdist/tts/player.d.ts [22m[32m1.
|
|
69
|
+
[32mDTS[39m [1mdist/tts/player.d.ts [22m[32m1.20 KB[39m
|
|
70
70
|
[32mDTS[39m [1mdist/tts/utils.d.ts [22m[32m2.55 KB[39m
|
|
71
|
-
[32mDTS[39m [1mdist/tts/websocket.d.ts [22m[32m2.
|
|
71
|
+
[32mDTS[39m [1mdist/tts/websocket.d.ts [22m[32m2.65 KB[39m
|
|
72
72
|
[32mDTS[39m [1mdist/tts/source.d.ts [22m[32m2.16 KB[39m
|
|
73
73
|
[32mDTS[39m [1mdist/voices/index.d.ts [22m[32m397.00 B[39m
|
|
74
74
|
[32mDTS[39m [1mdist/lib/client.d.ts [22m[32m266.00 B[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# @cartesia/cartesia-js
|
|
2
2
|
|
|
3
|
+
## 1.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- 3ee5bfc: Initial release of Cartesia client with voices and WebSocket support
|
|
8
|
+
|
|
9
|
+
### Minor Changes
|
|
10
|
+
|
|
11
|
+
- e49f73a: Stabilize audio playback in the browser to support play/pause functionality.
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- c98a0c7: Fix typo in README
|
|
16
|
+
- 38af01f: Fix how URLs are constructed, solving WebSocket connection failure
|
|
17
|
+
- 8ecf940: Add provisional Node.js support
|
|
18
|
+
- 585d2c9: Makes JS client compatible with the Cartesia Stable API (2024-06-10)
|
|
19
|
+
|
|
3
20
|
## 1.0.0-alpha.4
|
|
4
21
|
|
|
5
22
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
# JavaScript Client
|
|
1
|
+
# Cartesia JavaScript Client
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
[](https://discord.gg/ZVxavqHB9X)
|
|
5
|
+
|
|
6
|
+
This client provides convenient access to [Cartesia's TTS models](https://cartesia.ai/). Sonic is the fastest text-to-speech model around—it can generate a second of audio in just 650ms, and it can stream out the first audio chunk in just 135ms. Alongside Sonic, we also offer an extensive prebuilt voice library for a variety of use cases.
|
|
7
|
+
|
|
8
|
+
The full API documentation can be found on docs.cartesia.ai.
|
|
9
|
+
|
|
2
10
|
|
|
3
11
|
## Installation
|
|
4
12
|
|
|
@@ -98,10 +106,10 @@ for await (const message of response.events('message')) {
|
|
|
98
106
|
|
|
99
107
|
#### Playing audio in the browser
|
|
100
108
|
|
|
101
|
-
(
|
|
109
|
+
(The `WebPlayer` class only supports playing audio in the browser.)
|
|
102
110
|
|
|
103
111
|
```js
|
|
104
|
-
// If you're using the client in the browser, you can
|
|
112
|
+
// If you're using the client in the browser, you can control audio playback using our WebPlayer:
|
|
105
113
|
import { WebPlayer } from "@cartesia/cartesia-js";
|
|
106
114
|
|
|
107
115
|
console.log("Playing stream...");
|
|
@@ -118,7 +126,7 @@ console.log("Done playing.");
|
|
|
118
126
|
|
|
119
127
|
## React
|
|
120
128
|
|
|
121
|
-
We export a React hook that simplifies the process of using the TTS API. The hook manages the WebSocket connection and provides a simple interface for buffering and
|
|
129
|
+
We export a React hook that simplifies the process of using the TTS API. The hook manages the WebSocket connection and provides a simple interface for buffering, playing, pausing and restarting audio.
|
|
122
130
|
|
|
123
131
|
```jsx
|
|
124
132
|
import { useTTS } from '@cartesia/cartesia-js/react';
|
|
@@ -51,13 +51,15 @@ var WebSocket = class extends Client {
|
|
|
51
51
|
__privateSet(this, _sampleRate, sampleRate);
|
|
52
52
|
}
|
|
53
53
|
/**
|
|
54
|
-
* Send a message over the WebSocket
|
|
54
|
+
* Send a message over the WebSocket to start a stream.
|
|
55
55
|
*
|
|
56
56
|
* @param inputs - Stream options.
|
|
57
57
|
* @param options - Options for the stream.
|
|
58
58
|
* @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
|
|
59
|
-
*
|
|
59
|
+
* If set to `0`, the stream will not time out.
|
|
60
60
|
* @returns A Source object that can be passed to a Player to play the audio.
|
|
61
|
+
* @returns An Emittery instance that emits messages from the WebSocket.
|
|
62
|
+
* @returns An abort function that can be called to cancel the stream.
|
|
61
63
|
*/
|
|
62
64
|
send(inputs, { timeout = 0 } = {}) {
|
|
63
65
|
var _a, _b, _c, _d;
|
|
@@ -128,7 +130,11 @@ var WebSocket = class extends Client {
|
|
|
128
130
|
clearTimeout(timeoutId);
|
|
129
131
|
}
|
|
130
132
|
});
|
|
131
|
-
return __spreadValues({
|
|
133
|
+
return __spreadProps(__spreadValues({
|
|
134
|
+
source
|
|
135
|
+
}, getEmitteryCallbacks(emitter)), {
|
|
136
|
+
stop: streamCompleteController.abort.bind(streamCompleteController)
|
|
137
|
+
});
|
|
132
138
|
}
|
|
133
139
|
/**
|
|
134
140
|
* Authenticate and connect to a Cartesia streaming WebSocket.
|
|
@@ -10,8 +10,7 @@ import {
|
|
|
10
10
|
} from "./chunk-WIFMLPT5.js";
|
|
11
11
|
|
|
12
12
|
// src/tts/player.ts
|
|
13
|
-
|
|
14
|
-
var _context, _startNextPlaybackAt, _bufferDuration, _emitter, _playBuffer, playBuffer_fn;
|
|
13
|
+
var _context, _startNextPlaybackAt, _bufferDuration, _playBuffer, playBuffer_fn;
|
|
15
14
|
var Player = class {
|
|
16
15
|
/**
|
|
17
16
|
* Create a new Player.
|
|
@@ -24,7 +23,6 @@ var Player = class {
|
|
|
24
23
|
__privateAdd(this, _context, null);
|
|
25
24
|
__privateAdd(this, _startNextPlaybackAt, 0);
|
|
26
25
|
__privateAdd(this, _bufferDuration, void 0);
|
|
27
|
-
__privateAdd(this, _emitter, new Emittery());
|
|
28
26
|
__privateSet(this, _bufferDuration, bufferDuration);
|
|
29
27
|
}
|
|
30
28
|
/**
|
|
@@ -46,7 +44,6 @@ var Player = class {
|
|
|
46
44
|
const playableAudio = buffer.subarray(0, read);
|
|
47
45
|
plays.push(__privateMethod(this, _playBuffer, playBuffer_fn).call(this, playableAudio, source.sampleRate));
|
|
48
46
|
if (read < buffer.length) {
|
|
49
|
-
yield __privateGet(this, _emitter).emit("finish");
|
|
50
47
|
break;
|
|
51
48
|
}
|
|
52
49
|
}
|
|
@@ -96,17 +93,33 @@ var Player = class {
|
|
|
96
93
|
}
|
|
97
94
|
});
|
|
98
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* Stop the audio.
|
|
98
|
+
*
|
|
99
|
+
* @returns A promise that resolves when the audio has been stopped.
|
|
100
|
+
*/
|
|
101
|
+
stop() {
|
|
102
|
+
return __async(this, null, function* () {
|
|
103
|
+
var _a;
|
|
104
|
+
if (!__privateGet(this, _context)) {
|
|
105
|
+
throw new Error("AudioContext not initialized.");
|
|
106
|
+
}
|
|
107
|
+
yield (_a = __privateGet(this, _context)) == null ? void 0 : _a.close();
|
|
108
|
+
});
|
|
109
|
+
}
|
|
99
110
|
};
|
|
100
111
|
_context = new WeakMap();
|
|
101
112
|
_startNextPlaybackAt = new WeakMap();
|
|
102
113
|
_bufferDuration = new WeakMap();
|
|
103
|
-
_emitter = new WeakMap();
|
|
104
114
|
_playBuffer = new WeakSet();
|
|
105
115
|
playBuffer_fn = function(buf, sampleRate) {
|
|
106
116
|
return __async(this, null, function* () {
|
|
107
117
|
if (!__privateGet(this, _context)) {
|
|
108
118
|
throw new Error("AudioContext not initialized.");
|
|
109
119
|
}
|
|
120
|
+
if (buf.length === 0) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
110
123
|
const startAt = __privateGet(this, _startNextPlaybackAt);
|
|
111
124
|
const duration = buf.length / sampleRate;
|
|
112
125
|
__privateSet(this, _startNextPlaybackAt, duration + Math.max(__privateGet(this, _context).currentTime, __privateGet(this, _startNextPlaybackAt)));
|
package/dist/index.cjs
CHANGED
|
@@ -336,13 +336,15 @@ var WebSocket = class extends Client {
|
|
|
336
336
|
__privateSet(this, _sampleRate2, sampleRate);
|
|
337
337
|
}
|
|
338
338
|
/**
|
|
339
|
-
* Send a message over the WebSocket
|
|
339
|
+
* Send a message over the WebSocket to start a stream.
|
|
340
340
|
*
|
|
341
341
|
* @param inputs - Stream options.
|
|
342
342
|
* @param options - Options for the stream.
|
|
343
343
|
* @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
|
|
344
|
-
*
|
|
344
|
+
* If set to `0`, the stream will not time out.
|
|
345
345
|
* @returns A Source object that can be passed to a Player to play the audio.
|
|
346
|
+
* @returns An Emittery instance that emits messages from the WebSocket.
|
|
347
|
+
* @returns An abort function that can be called to cancel the stream.
|
|
346
348
|
*/
|
|
347
349
|
send(inputs, { timeout = 0 } = {}) {
|
|
348
350
|
var _a, _b, _c, _d;
|
|
@@ -413,7 +415,11 @@ var WebSocket = class extends Client {
|
|
|
413
415
|
clearTimeout(timeoutId);
|
|
414
416
|
}
|
|
415
417
|
});
|
|
416
|
-
return __spreadValues({
|
|
418
|
+
return __spreadProps(__spreadValues({
|
|
419
|
+
source
|
|
420
|
+
}, getEmitteryCallbacks(emitter)), {
|
|
421
|
+
stop: streamCompleteController.abort.bind(streamCompleteController)
|
|
422
|
+
});
|
|
417
423
|
}
|
|
418
424
|
/**
|
|
419
425
|
* Authenticate and connect to a Cartesia streaming WebSocket.
|
|
@@ -564,8 +570,7 @@ var Cartesia = class extends Client {
|
|
|
564
570
|
};
|
|
565
571
|
|
|
566
572
|
// src/tts/player.ts
|
|
567
|
-
var
|
|
568
|
-
var _context, _startNextPlaybackAt, _bufferDuration, _emitter2, _playBuffer, playBuffer_fn;
|
|
573
|
+
var _context, _startNextPlaybackAt, _bufferDuration, _playBuffer, playBuffer_fn;
|
|
569
574
|
var Player = class {
|
|
570
575
|
/**
|
|
571
576
|
* Create a new Player.
|
|
@@ -578,7 +583,6 @@ var Player = class {
|
|
|
578
583
|
__privateAdd(this, _context, null);
|
|
579
584
|
__privateAdd(this, _startNextPlaybackAt, 0);
|
|
580
585
|
__privateAdd(this, _bufferDuration, void 0);
|
|
581
|
-
__privateAdd(this, _emitter2, new import_emittery3.default());
|
|
582
586
|
__privateSet(this, _bufferDuration, bufferDuration);
|
|
583
587
|
}
|
|
584
588
|
/**
|
|
@@ -600,7 +604,6 @@ var Player = class {
|
|
|
600
604
|
const playableAudio = buffer.subarray(0, read);
|
|
601
605
|
plays.push(__privateMethod(this, _playBuffer, playBuffer_fn).call(this, playableAudio, source.sampleRate));
|
|
602
606
|
if (read < buffer.length) {
|
|
603
|
-
yield __privateGet(this, _emitter2).emit("finish");
|
|
604
607
|
break;
|
|
605
608
|
}
|
|
606
609
|
}
|
|
@@ -650,17 +653,33 @@ var Player = class {
|
|
|
650
653
|
}
|
|
651
654
|
});
|
|
652
655
|
}
|
|
656
|
+
/**
|
|
657
|
+
* Stop the audio.
|
|
658
|
+
*
|
|
659
|
+
* @returns A promise that resolves when the audio has been stopped.
|
|
660
|
+
*/
|
|
661
|
+
stop() {
|
|
662
|
+
return __async(this, null, function* () {
|
|
663
|
+
var _a;
|
|
664
|
+
if (!__privateGet(this, _context)) {
|
|
665
|
+
throw new Error("AudioContext not initialized.");
|
|
666
|
+
}
|
|
667
|
+
yield (_a = __privateGet(this, _context)) == null ? void 0 : _a.close();
|
|
668
|
+
});
|
|
669
|
+
}
|
|
653
670
|
};
|
|
654
671
|
_context = new WeakMap();
|
|
655
672
|
_startNextPlaybackAt = new WeakMap();
|
|
656
673
|
_bufferDuration = new WeakMap();
|
|
657
|
-
_emitter2 = new WeakMap();
|
|
658
674
|
_playBuffer = new WeakSet();
|
|
659
675
|
playBuffer_fn = function(buf, sampleRate) {
|
|
660
676
|
return __async(this, null, function* () {
|
|
661
677
|
if (!__privateGet(this, _context)) {
|
|
662
678
|
throw new Error("AudioContext not initialized.");
|
|
663
679
|
}
|
|
680
|
+
if (buf.length === 0) {
|
|
681
|
+
return;
|
|
682
|
+
}
|
|
664
683
|
const startAt = __privateGet(this, _startNextPlaybackAt);
|
|
665
684
|
const duration = buf.length / sampleRate;
|
|
666
685
|
__privateSet(this, _startNextPlaybackAt, duration + Math.max(__privateGet(this, _context).currentTime, __privateGet(this, _startNextPlaybackAt)));
|
package/dist/index.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import "./chunk-FXPGR372.js";
|
|
2
2
|
import {
|
|
3
3
|
Cartesia
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-IEN4NCER.js";
|
|
5
5
|
import "./chunk-SGXUEFII.js";
|
|
6
|
-
import "./chunk-
|
|
7
|
-
import "./chunk-
|
|
6
|
+
import "./chunk-F4QWVJY3.js";
|
|
7
|
+
import "./chunk-FN7BK4PS.js";
|
|
8
8
|
import "./chunk-PQ6CIPFW.js";
|
|
9
9
|
import "./chunk-PQ5EVEEH.js";
|
|
10
10
|
import "./chunk-2BFEKY3F.js";
|
|
11
11
|
import {
|
|
12
12
|
Player
|
|
13
|
-
} from "./chunk-
|
|
13
|
+
} from "./chunk-JYLAM6VU.js";
|
|
14
14
|
import "./chunk-RO7TY474.js";
|
|
15
15
|
import "./chunk-WIFMLPT5.js";
|
|
16
16
|
export {
|
package/dist/lib/index.cjs
CHANGED
|
@@ -321,13 +321,15 @@ var WebSocket = class extends Client {
|
|
|
321
321
|
__privateSet(this, _sampleRate2, sampleRate);
|
|
322
322
|
}
|
|
323
323
|
/**
|
|
324
|
-
* Send a message over the WebSocket
|
|
324
|
+
* Send a message over the WebSocket to start a stream.
|
|
325
325
|
*
|
|
326
326
|
* @param inputs - Stream options.
|
|
327
327
|
* @param options - Options for the stream.
|
|
328
328
|
* @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
|
|
329
|
-
*
|
|
329
|
+
* If set to `0`, the stream will not time out.
|
|
330
330
|
* @returns A Source object that can be passed to a Player to play the audio.
|
|
331
|
+
* @returns An Emittery instance that emits messages from the WebSocket.
|
|
332
|
+
* @returns An abort function that can be called to cancel the stream.
|
|
331
333
|
*/
|
|
332
334
|
send(inputs, { timeout = 0 } = {}) {
|
|
333
335
|
var _a, _b, _c, _d;
|
|
@@ -398,7 +400,11 @@ var WebSocket = class extends Client {
|
|
|
398
400
|
clearTimeout(timeoutId);
|
|
399
401
|
}
|
|
400
402
|
});
|
|
401
|
-
return __spreadValues({
|
|
403
|
+
return __spreadProps(__spreadValues({
|
|
404
|
+
source
|
|
405
|
+
}, getEmitteryCallbacks(emitter)), {
|
|
406
|
+
stop: streamCompleteController.abort.bind(streamCompleteController)
|
|
407
|
+
});
|
|
402
408
|
}
|
|
403
409
|
/**
|
|
404
410
|
* Authenticate and connect to a Cartesia streaming WebSocket.
|
package/dist/lib/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Cartesia
|
|
3
|
-
} from "../chunk-
|
|
3
|
+
} from "../chunk-IEN4NCER.js";
|
|
4
4
|
import "../chunk-SGXUEFII.js";
|
|
5
|
-
import "../chunk-
|
|
6
|
-
import "../chunk-
|
|
5
|
+
import "../chunk-F4QWVJY3.js";
|
|
6
|
+
import "../chunk-FN7BK4PS.js";
|
|
7
7
|
import "../chunk-PQ6CIPFW.js";
|
|
8
8
|
import "../chunk-PQ5EVEEH.js";
|
|
9
9
|
import "../chunk-2BFEKY3F.js";
|
package/dist/react/index.cjs
CHANGED
|
@@ -335,13 +335,15 @@ var WebSocket = class extends Client {
|
|
|
335
335
|
__privateSet(this, _sampleRate2, sampleRate);
|
|
336
336
|
}
|
|
337
337
|
/**
|
|
338
|
-
* Send a message over the WebSocket
|
|
338
|
+
* Send a message over the WebSocket to start a stream.
|
|
339
339
|
*
|
|
340
340
|
* @param inputs - Stream options.
|
|
341
341
|
* @param options - Options for the stream.
|
|
342
342
|
* @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
|
|
343
|
-
*
|
|
343
|
+
* If set to `0`, the stream will not time out.
|
|
344
344
|
* @returns A Source object that can be passed to a Player to play the audio.
|
|
345
|
+
* @returns An Emittery instance that emits messages from the WebSocket.
|
|
346
|
+
* @returns An abort function that can be called to cancel the stream.
|
|
345
347
|
*/
|
|
346
348
|
send(inputs, { timeout = 0 } = {}) {
|
|
347
349
|
var _a, _b, _c, _d;
|
|
@@ -412,7 +414,11 @@ var WebSocket = class extends Client {
|
|
|
412
414
|
clearTimeout(timeoutId);
|
|
413
415
|
}
|
|
414
416
|
});
|
|
415
|
-
return __spreadValues({
|
|
417
|
+
return __spreadProps(__spreadValues({
|
|
418
|
+
source
|
|
419
|
+
}, getEmitteryCallbacks(emitter)), {
|
|
420
|
+
stop: streamCompleteController.abort.bind(streamCompleteController)
|
|
421
|
+
});
|
|
416
422
|
}
|
|
417
423
|
/**
|
|
418
424
|
* Authenticate and connect to a Cartesia streaming WebSocket.
|
|
@@ -563,8 +569,7 @@ var Cartesia = class extends Client {
|
|
|
563
569
|
};
|
|
564
570
|
|
|
565
571
|
// src/tts/player.ts
|
|
566
|
-
var
|
|
567
|
-
var _context, _startNextPlaybackAt, _bufferDuration, _emitter2, _playBuffer, playBuffer_fn;
|
|
572
|
+
var _context, _startNextPlaybackAt, _bufferDuration, _playBuffer, playBuffer_fn;
|
|
568
573
|
var Player = class {
|
|
569
574
|
/**
|
|
570
575
|
* Create a new Player.
|
|
@@ -577,7 +582,6 @@ var Player = class {
|
|
|
577
582
|
__privateAdd(this, _context, null);
|
|
578
583
|
__privateAdd(this, _startNextPlaybackAt, 0);
|
|
579
584
|
__privateAdd(this, _bufferDuration, void 0);
|
|
580
|
-
__privateAdd(this, _emitter2, new import_emittery3.default());
|
|
581
585
|
__privateSet(this, _bufferDuration, bufferDuration);
|
|
582
586
|
}
|
|
583
587
|
/**
|
|
@@ -599,7 +603,6 @@ var Player = class {
|
|
|
599
603
|
const playableAudio = buffer.subarray(0, read);
|
|
600
604
|
plays.push(__privateMethod(this, _playBuffer, playBuffer_fn).call(this, playableAudio, source.sampleRate));
|
|
601
605
|
if (read < buffer.length) {
|
|
602
|
-
yield __privateGet(this, _emitter2).emit("finish");
|
|
603
606
|
break;
|
|
604
607
|
}
|
|
605
608
|
}
|
|
@@ -649,17 +652,33 @@ var Player = class {
|
|
|
649
652
|
}
|
|
650
653
|
});
|
|
651
654
|
}
|
|
655
|
+
/**
|
|
656
|
+
* Stop the audio.
|
|
657
|
+
*
|
|
658
|
+
* @returns A promise that resolves when the audio has been stopped.
|
|
659
|
+
*/
|
|
660
|
+
stop() {
|
|
661
|
+
return __async(this, null, function* () {
|
|
662
|
+
var _a;
|
|
663
|
+
if (!__privateGet(this, _context)) {
|
|
664
|
+
throw new Error("AudioContext not initialized.");
|
|
665
|
+
}
|
|
666
|
+
yield (_a = __privateGet(this, _context)) == null ? void 0 : _a.close();
|
|
667
|
+
});
|
|
668
|
+
}
|
|
652
669
|
};
|
|
653
670
|
_context = new WeakMap();
|
|
654
671
|
_startNextPlaybackAt = new WeakMap();
|
|
655
672
|
_bufferDuration = new WeakMap();
|
|
656
|
-
_emitter2 = new WeakMap();
|
|
657
673
|
_playBuffer = new WeakSet();
|
|
658
674
|
playBuffer_fn = function(buf, sampleRate) {
|
|
659
675
|
return __async(this, null, function* () {
|
|
660
676
|
if (!__privateGet(this, _context)) {
|
|
661
677
|
throw new Error("AudioContext not initialized.");
|
|
662
678
|
}
|
|
679
|
+
if (buf.length === 0) {
|
|
680
|
+
return;
|
|
681
|
+
}
|
|
663
682
|
const startAt = __privateGet(this, _startNextPlaybackAt);
|
|
664
683
|
const duration = buf.length / sampleRate;
|
|
665
684
|
__privateSet(this, _startNextPlaybackAt, duration + Math.max(__privateGet(this, _context).currentTime, __privateGet(this, _startNextPlaybackAt)));
|
|
@@ -726,10 +745,11 @@ function useTTS({
|
|
|
726
745
|
const [messages, setMessages] = (0, import_react.useState)([]);
|
|
727
746
|
const buffer = (0, import_react.useCallback)(
|
|
728
747
|
(options) => __async(this, null, function* () {
|
|
729
|
-
var _a2;
|
|
748
|
+
var _a2, _b2;
|
|
749
|
+
(_a2 = websocketReturn.current) == null ? void 0 : _a2.stop();
|
|
730
750
|
setMessages([]);
|
|
731
751
|
setBufferStatus("buffering");
|
|
732
|
-
websocketReturn.current = (
|
|
752
|
+
websocketReturn.current = (_b2 = websocket == null ? void 0 : websocket.send(options)) != null ? _b2 : null;
|
|
733
753
|
if (!websocketReturn.current) {
|
|
734
754
|
return;
|
|
735
755
|
}
|
|
@@ -812,6 +832,9 @@ function useTTS({
|
|
|
812
832
|
if (playbackStatus === "playing" || !websocketReturn.current) {
|
|
813
833
|
return;
|
|
814
834
|
}
|
|
835
|
+
if (player.current) {
|
|
836
|
+
yield player.current.stop();
|
|
837
|
+
}
|
|
815
838
|
setPlaybackStatus("playing");
|
|
816
839
|
const unsubscribes = [];
|
|
817
840
|
unsubscribes.push(
|
package/dist/react/index.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Cartesia
|
|
3
|
-
} from "../chunk-
|
|
3
|
+
} from "../chunk-IEN4NCER.js";
|
|
4
4
|
import "../chunk-SGXUEFII.js";
|
|
5
5
|
import {
|
|
6
6
|
pingServer
|
|
7
7
|
} from "../chunk-3FL2SNIR.js";
|
|
8
|
-
import "../chunk-
|
|
9
|
-
import "../chunk-
|
|
8
|
+
import "../chunk-F4QWVJY3.js";
|
|
9
|
+
import "../chunk-FN7BK4PS.js";
|
|
10
10
|
import "../chunk-PQ6CIPFW.js";
|
|
11
11
|
import "../chunk-PQ5EVEEH.js";
|
|
12
12
|
import "../chunk-2BFEKY3F.js";
|
|
13
13
|
import {
|
|
14
14
|
Player
|
|
15
|
-
} from "../chunk-
|
|
15
|
+
} from "../chunk-JYLAM6VU.js";
|
|
16
16
|
import "../chunk-RO7TY474.js";
|
|
17
17
|
import {
|
|
18
18
|
__async
|
|
@@ -68,10 +68,11 @@ function useTTS({
|
|
|
68
68
|
const [messages, setMessages] = useState([]);
|
|
69
69
|
const buffer = useCallback(
|
|
70
70
|
(options) => __async(this, null, function* () {
|
|
71
|
-
var _a2;
|
|
71
|
+
var _a2, _b2;
|
|
72
|
+
(_a2 = websocketReturn.current) == null ? void 0 : _a2.stop();
|
|
72
73
|
setMessages([]);
|
|
73
74
|
setBufferStatus("buffering");
|
|
74
|
-
websocketReturn.current = (
|
|
75
|
+
websocketReturn.current = (_b2 = websocket == null ? void 0 : websocket.send(options)) != null ? _b2 : null;
|
|
75
76
|
if (!websocketReturn.current) {
|
|
76
77
|
return;
|
|
77
78
|
}
|
|
@@ -154,6 +155,9 @@ function useTTS({
|
|
|
154
155
|
if (playbackStatus === "playing" || !websocketReturn.current) {
|
|
155
156
|
return;
|
|
156
157
|
}
|
|
158
|
+
if (player.current) {
|
|
159
|
+
yield player.current.stop();
|
|
160
|
+
}
|
|
157
161
|
setPlaybackStatus("playing");
|
|
158
162
|
const unsubscribes = [];
|
|
159
163
|
unsubscribes.push(
|
package/dist/tts/index.cjs
CHANGED
|
@@ -321,13 +321,15 @@ var WebSocket = class extends Client {
|
|
|
321
321
|
__privateSet(this, _sampleRate2, sampleRate);
|
|
322
322
|
}
|
|
323
323
|
/**
|
|
324
|
-
* Send a message over the WebSocket
|
|
324
|
+
* Send a message over the WebSocket to start a stream.
|
|
325
325
|
*
|
|
326
326
|
* @param inputs - Stream options.
|
|
327
327
|
* @param options - Options for the stream.
|
|
328
328
|
* @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
|
|
329
|
-
*
|
|
329
|
+
* If set to `0`, the stream will not time out.
|
|
330
330
|
* @returns A Source object that can be passed to a Player to play the audio.
|
|
331
|
+
* @returns An Emittery instance that emits messages from the WebSocket.
|
|
332
|
+
* @returns An abort function that can be called to cancel the stream.
|
|
331
333
|
*/
|
|
332
334
|
send(inputs, { timeout = 0 } = {}) {
|
|
333
335
|
var _a, _b, _c, _d;
|
|
@@ -398,7 +400,11 @@ var WebSocket = class extends Client {
|
|
|
398
400
|
clearTimeout(timeoutId);
|
|
399
401
|
}
|
|
400
402
|
});
|
|
401
|
-
return __spreadValues({
|
|
403
|
+
return __spreadProps(__spreadValues({
|
|
404
|
+
source
|
|
405
|
+
}, getEmitteryCallbacks(emitter)), {
|
|
406
|
+
stop: streamCompleteController.abort.bind(streamCompleteController)
|
|
407
|
+
});
|
|
402
408
|
}
|
|
403
409
|
/**
|
|
404
410
|
* Authenticate and connect to a Cartesia streaming WebSocket.
|
package/dist/tts/index.js
CHANGED
package/dist/tts/player.cjs
CHANGED
|
@@ -75,7 +75,6 @@ __export(player_exports, {
|
|
|
75
75
|
default: () => Player
|
|
76
76
|
});
|
|
77
77
|
module.exports = __toCommonJS(player_exports);
|
|
78
|
-
var import_emittery = __toESM(require("emittery"), 1);
|
|
79
78
|
|
|
80
79
|
// src/tts/utils.ts
|
|
81
80
|
var import_base64_js = __toESM(require("base64-js"), 1);
|
|
@@ -94,7 +93,7 @@ function playAudioBuffer(floats, context, startAt, sampleRate) {
|
|
|
94
93
|
}
|
|
95
94
|
|
|
96
95
|
// src/tts/player.ts
|
|
97
|
-
var _context, _startNextPlaybackAt, _bufferDuration,
|
|
96
|
+
var _context, _startNextPlaybackAt, _bufferDuration, _playBuffer, playBuffer_fn;
|
|
98
97
|
var Player = class {
|
|
99
98
|
/**
|
|
100
99
|
* Create a new Player.
|
|
@@ -107,7 +106,6 @@ var Player = class {
|
|
|
107
106
|
__privateAdd(this, _context, null);
|
|
108
107
|
__privateAdd(this, _startNextPlaybackAt, 0);
|
|
109
108
|
__privateAdd(this, _bufferDuration, void 0);
|
|
110
|
-
__privateAdd(this, _emitter, new import_emittery.default());
|
|
111
109
|
__privateSet(this, _bufferDuration, bufferDuration);
|
|
112
110
|
}
|
|
113
111
|
/**
|
|
@@ -129,7 +127,6 @@ var Player = class {
|
|
|
129
127
|
const playableAudio = buffer.subarray(0, read);
|
|
130
128
|
plays.push(__privateMethod(this, _playBuffer, playBuffer_fn).call(this, playableAudio, source.sampleRate));
|
|
131
129
|
if (read < buffer.length) {
|
|
132
|
-
yield __privateGet(this, _emitter).emit("finish");
|
|
133
130
|
break;
|
|
134
131
|
}
|
|
135
132
|
}
|
|
@@ -179,17 +176,33 @@ var Player = class {
|
|
|
179
176
|
}
|
|
180
177
|
});
|
|
181
178
|
}
|
|
179
|
+
/**
|
|
180
|
+
* Stop the audio.
|
|
181
|
+
*
|
|
182
|
+
* @returns A promise that resolves when the audio has been stopped.
|
|
183
|
+
*/
|
|
184
|
+
stop() {
|
|
185
|
+
return __async(this, null, function* () {
|
|
186
|
+
var _a;
|
|
187
|
+
if (!__privateGet(this, _context)) {
|
|
188
|
+
throw new Error("AudioContext not initialized.");
|
|
189
|
+
}
|
|
190
|
+
yield (_a = __privateGet(this, _context)) == null ? void 0 : _a.close();
|
|
191
|
+
});
|
|
192
|
+
}
|
|
182
193
|
};
|
|
183
194
|
_context = new WeakMap();
|
|
184
195
|
_startNextPlaybackAt = new WeakMap();
|
|
185
196
|
_bufferDuration = new WeakMap();
|
|
186
|
-
_emitter = new WeakMap();
|
|
187
197
|
_playBuffer = new WeakSet();
|
|
188
198
|
playBuffer_fn = function(buf, sampleRate) {
|
|
189
199
|
return __async(this, null, function* () {
|
|
190
200
|
if (!__privateGet(this, _context)) {
|
|
191
201
|
throw new Error("AudioContext not initialized.");
|
|
192
202
|
}
|
|
203
|
+
if (buf.length === 0) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
193
206
|
const startAt = __privateGet(this, _startNextPlaybackAt);
|
|
194
207
|
const duration = buf.length / sampleRate;
|
|
195
208
|
__privateSet(this, _startNextPlaybackAt, duration + Math.max(__privateGet(this, _context).currentTime, __privateGet(this, _startNextPlaybackAt)));
|
package/dist/tts/player.d.cts
CHANGED
|
@@ -38,6 +38,12 @@ declare class Player {
|
|
|
38
38
|
* @returns A promise that resolves when the audio has been toggled.
|
|
39
39
|
*/
|
|
40
40
|
toggle(): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Stop the audio.
|
|
43
|
+
*
|
|
44
|
+
* @returns A promise that resolves when the audio has been stopped.
|
|
45
|
+
*/
|
|
46
|
+
stop(): Promise<void>;
|
|
41
47
|
}
|
|
42
48
|
|
|
43
49
|
export { Player as default };
|
package/dist/tts/player.d.ts
CHANGED
|
@@ -38,6 +38,12 @@ declare class Player {
|
|
|
38
38
|
* @returns A promise that resolves when the audio has been toggled.
|
|
39
39
|
*/
|
|
40
40
|
toggle(): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Stop the audio.
|
|
43
|
+
*
|
|
44
|
+
* @returns A promise that resolves when the audio has been stopped.
|
|
45
|
+
*/
|
|
46
|
+
stop(): Promise<void>;
|
|
41
47
|
}
|
|
42
48
|
|
|
43
49
|
export { Player as default };
|
package/dist/tts/player.js
CHANGED
package/dist/tts/websocket.cjs
CHANGED
|
@@ -319,13 +319,15 @@ var WebSocket = class extends Client {
|
|
|
319
319
|
__privateSet(this, _sampleRate2, sampleRate);
|
|
320
320
|
}
|
|
321
321
|
/**
|
|
322
|
-
* Send a message over the WebSocket
|
|
322
|
+
* Send a message over the WebSocket to start a stream.
|
|
323
323
|
*
|
|
324
324
|
* @param inputs - Stream options.
|
|
325
325
|
* @param options - Options for the stream.
|
|
326
326
|
* @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
|
|
327
|
-
*
|
|
327
|
+
* If set to `0`, the stream will not time out.
|
|
328
328
|
* @returns A Source object that can be passed to a Player to play the audio.
|
|
329
|
+
* @returns An Emittery instance that emits messages from the WebSocket.
|
|
330
|
+
* @returns An abort function that can be called to cancel the stream.
|
|
329
331
|
*/
|
|
330
332
|
send(inputs, { timeout = 0 } = {}) {
|
|
331
333
|
var _a, _b, _c, _d;
|
|
@@ -396,7 +398,11 @@ var WebSocket = class extends Client {
|
|
|
396
398
|
clearTimeout(timeoutId);
|
|
397
399
|
}
|
|
398
400
|
});
|
|
399
|
-
return __spreadValues({
|
|
401
|
+
return __spreadProps(__spreadValues({
|
|
402
|
+
source
|
|
403
|
+
}, getEmitteryCallbacks(emitter)), {
|
|
404
|
+
stop: streamCompleteController.abort.bind(streamCompleteController)
|
|
405
|
+
});
|
|
400
406
|
}
|
|
401
407
|
/**
|
|
402
408
|
* Authenticate and connect to a Cartesia streaming WebSocket.
|
package/dist/tts/websocket.d.cts
CHANGED
|
@@ -14,15 +14,21 @@ declare class WebSocket extends Client {
|
|
|
14
14
|
*/
|
|
15
15
|
constructor({ sampleRate }: WebSocketOptions, ...args: ConstructorParameters<typeof Client>);
|
|
16
16
|
/**
|
|
17
|
-
* Send a message over the WebSocket
|
|
17
|
+
* Send a message over the WebSocket to start a stream.
|
|
18
18
|
*
|
|
19
19
|
* @param inputs - Stream options.
|
|
20
20
|
* @param options - Options for the stream.
|
|
21
21
|
* @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
|
|
22
|
-
*
|
|
22
|
+
* If set to `0`, the stream will not time out.
|
|
23
23
|
* @returns A Source object that can be passed to a Player to play the audio.
|
|
24
|
+
* @returns An Emittery instance that emits messages from the WebSocket.
|
|
25
|
+
* @returns An abort function that can be called to cancel the stream.
|
|
24
26
|
*/
|
|
25
27
|
send(inputs: StreamRequest["inputs"], { timeout }?: StreamRequest["options"]): {
|
|
28
|
+
stop: {
|
|
29
|
+
(reason?: any): void;
|
|
30
|
+
(reason?: any): void;
|
|
31
|
+
};
|
|
26
32
|
on: <Name extends keyof emittery.OmnipresentEventData | "message">(eventName: Name | readonly Name[], listener: (eventData: ({
|
|
27
33
|
message: string;
|
|
28
34
|
} & emittery.OmnipresentEventData)[Name]) => void | Promise<void>) => emittery.UnsubscribeFunction;
|
package/dist/tts/websocket.d.ts
CHANGED
|
@@ -14,15 +14,21 @@ declare class WebSocket extends Client {
|
|
|
14
14
|
*/
|
|
15
15
|
constructor({ sampleRate }: WebSocketOptions, ...args: ConstructorParameters<typeof Client>);
|
|
16
16
|
/**
|
|
17
|
-
* Send a message over the WebSocket
|
|
17
|
+
* Send a message over the WebSocket to start a stream.
|
|
18
18
|
*
|
|
19
19
|
* @param inputs - Stream options.
|
|
20
20
|
* @param options - Options for the stream.
|
|
21
21
|
* @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
|
|
22
|
-
*
|
|
22
|
+
* If set to `0`, the stream will not time out.
|
|
23
23
|
* @returns A Source object that can be passed to a Player to play the audio.
|
|
24
|
+
* @returns An Emittery instance that emits messages from the WebSocket.
|
|
25
|
+
* @returns An abort function that can be called to cancel the stream.
|
|
24
26
|
*/
|
|
25
27
|
send(inputs: StreamRequest["inputs"], { timeout }?: StreamRequest["options"]): {
|
|
28
|
+
stop: {
|
|
29
|
+
(reason?: any): void;
|
|
30
|
+
(reason?: any): void;
|
|
31
|
+
};
|
|
26
32
|
on: <Name extends keyof emittery.OmnipresentEventData | "message">(eventName: Name | readonly Name[], listener: (eventData: ({
|
|
27
33
|
message: string;
|
|
28
34
|
} & emittery.OmnipresentEventData)[Name]) => void | Promise<void>) => emittery.UnsubscribeFunction;
|
package/dist/tts/websocket.js
CHANGED
package/package.json
CHANGED
package/src/react/index.ts
CHANGED
|
@@ -86,6 +86,8 @@ export function useTTS({
|
|
|
86
86
|
|
|
87
87
|
const buffer = useCallback(
|
|
88
88
|
async (options: object) => {
|
|
89
|
+
websocketReturn.current?.stop(); // Abort the previous request if it exists.
|
|
90
|
+
|
|
89
91
|
setMessages([]);
|
|
90
92
|
setBufferStatus("buffering");
|
|
91
93
|
websocketReturn.current = websocket?.send(options) ?? null;
|
|
@@ -174,6 +176,11 @@ export function useTTS({
|
|
|
174
176
|
if (playbackStatus === "playing" || !websocketReturn.current) {
|
|
175
177
|
return;
|
|
176
178
|
}
|
|
179
|
+
if (player.current) {
|
|
180
|
+
// Stop the current player if it exists.
|
|
181
|
+
await player.current.stop();
|
|
182
|
+
}
|
|
183
|
+
|
|
177
184
|
setPlaybackStatus("playing");
|
|
178
185
|
|
|
179
186
|
const unsubscribes = [];
|
|
@@ -225,9 +232,6 @@ export function useTTS({
|
|
|
225
232
|
});
|
|
226
233
|
}, []);
|
|
227
234
|
|
|
228
|
-
// TODO:
|
|
229
|
-
// - [] Access the play and buffer cursors.
|
|
230
|
-
// - [] Seek to a specific time.
|
|
231
235
|
return {
|
|
232
236
|
buffer,
|
|
233
237
|
play,
|
package/src/tts/player.ts
CHANGED
|
@@ -1,16 +1,10 @@
|
|
|
1
|
-
import Emittery from "emittery";
|
|
2
1
|
import type Source from "./source";
|
|
3
2
|
import { playAudioBuffer } from "./utils";
|
|
4
3
|
|
|
5
|
-
type PlayEventData = {
|
|
6
|
-
finish: never;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
4
|
export default class Player {
|
|
10
5
|
#context: AudioContext | null = null;
|
|
11
6
|
#startNextPlaybackAt = 0;
|
|
12
7
|
#bufferDuration: number;
|
|
13
|
-
#emitter = new Emittery<PlayEventData>();
|
|
14
8
|
|
|
15
9
|
/**
|
|
16
10
|
* Create a new Player.
|
|
@@ -26,6 +20,9 @@ export default class Player {
|
|
|
26
20
|
if (!this.#context) {
|
|
27
21
|
throw new Error("AudioContext not initialized.");
|
|
28
22
|
}
|
|
23
|
+
if (buf.length === 0) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
29
26
|
|
|
30
27
|
const startAt = this.#startNextPlaybackAt;
|
|
31
28
|
const duration = buf.length / sampleRate;
|
|
@@ -60,11 +57,9 @@ export default class Player {
|
|
|
60
57
|
|
|
61
58
|
if (read < buffer.length) {
|
|
62
59
|
// No more audio to read.
|
|
63
|
-
await this.#emitter.emit("finish");
|
|
64
60
|
break;
|
|
65
61
|
}
|
|
66
62
|
}
|
|
67
|
-
|
|
68
63
|
await Promise.all(plays);
|
|
69
64
|
}
|
|
70
65
|
|
|
@@ -107,4 +102,16 @@ export default class Player {
|
|
|
107
102
|
await this.resume();
|
|
108
103
|
}
|
|
109
104
|
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Stop the audio.
|
|
108
|
+
*
|
|
109
|
+
* @returns A promise that resolves when the audio has been stopped.
|
|
110
|
+
*/
|
|
111
|
+
async stop() {
|
|
112
|
+
if (!this.#context) {
|
|
113
|
+
throw new Error("AudioContext not initialized.");
|
|
114
|
+
}
|
|
115
|
+
await this.#context?.close();
|
|
116
|
+
}
|
|
110
117
|
}
|
package/src/tts/websocket.ts
CHANGED
|
@@ -37,13 +37,15 @@ export default class WebSocket extends Client {
|
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
/**
|
|
40
|
-
* Send a message over the WebSocket
|
|
40
|
+
* Send a message over the WebSocket to start a stream.
|
|
41
41
|
*
|
|
42
42
|
* @param inputs - Stream options.
|
|
43
43
|
* @param options - Options for the stream.
|
|
44
44
|
* @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
|
|
45
|
-
*
|
|
45
|
+
* If set to `0`, the stream will not time out.
|
|
46
46
|
* @returns A Source object that can be passed to a Player to play the audio.
|
|
47
|
+
* @returns An Emittery instance that emits messages from the WebSocket.
|
|
48
|
+
* @returns An abort function that can be called to cancel the stream.
|
|
47
49
|
*/
|
|
48
50
|
send(
|
|
49
51
|
inputs: StreamRequest["inputs"],
|
|
@@ -125,7 +127,11 @@ export default class WebSocket extends Client {
|
|
|
125
127
|
}
|
|
126
128
|
});
|
|
127
129
|
|
|
128
|
-
return {
|
|
130
|
+
return {
|
|
131
|
+
source,
|
|
132
|
+
...getEmitteryCallbacks(emitter),
|
|
133
|
+
stop: streamCompleteController.abort.bind(streamCompleteController),
|
|
134
|
+
};
|
|
129
135
|
}
|
|
130
136
|
|
|
131
137
|
/**
|