@cartesia/cartesia-js 0.0.4-alpha.0 → 1.0.0-alpha.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.
Files changed (89) hide show
  1. package/.turbo/turbo-build.log +63 -45
  2. package/CHANGELOG.md +12 -0
  3. package/README.md +123 -16
  4. package/dist/chunk-36JBKJUN.js +119 -0
  5. package/dist/chunk-3F5E46FT.js +212 -0
  6. package/dist/{chunk-XPIMIAAE.js → chunk-3FL2SNIR.js} +1 -1
  7. package/dist/chunk-JGP5BIUV.js +34 -0
  8. package/dist/chunk-KWBSQZTY.js +25 -0
  9. package/dist/chunk-PQ6CIPFW.js +120 -0
  10. package/dist/chunk-RO7TY474.js +81 -0
  11. package/dist/chunk-T3RG6WV4.js +22 -0
  12. package/dist/{chunk-R4P7LWVZ.js → chunk-WIFMLPT5.js} +31 -6
  13. package/dist/chunk-WVTITUXX.js +58 -0
  14. package/dist/chunk-XHTDPLFR.js +19 -0
  15. package/dist/index.cjs +425 -166
  16. package/dist/index.d.cts +7 -3
  17. package/dist/index.d.ts +7 -3
  18. package/dist/index.js +13 -6
  19. package/dist/lib/client.cjs +49 -1
  20. package/dist/lib/client.d.cts +2 -0
  21. package/dist/lib/client.d.ts +2 -0
  22. package/dist/lib/client.js +3 -3
  23. package/dist/lib/constants.cjs +15 -8
  24. package/dist/lib/constants.d.cts +4 -4
  25. package/dist/lib/constants.d.ts +4 -4
  26. package/dist/lib/constants.js +6 -6
  27. package/dist/lib/index.cjs +310 -171
  28. package/dist/lib/index.d.cts +6 -2
  29. package/dist/lib/index.d.ts +6 -2
  30. package/dist/lib/index.js +9 -6
  31. package/dist/react/index.cjs +573 -290
  32. package/dist/react/index.d.cts +20 -14
  33. package/dist/react/index.d.ts +20 -14
  34. package/dist/react/index.js +157 -105
  35. package/dist/react/utils.js +2 -2
  36. package/dist/tts/index.cjs +496 -0
  37. package/dist/tts/index.d.cts +17 -0
  38. package/dist/tts/index.d.ts +17 -0
  39. package/dist/tts/index.js +12 -0
  40. package/dist/tts/player.cjs +198 -0
  41. package/dist/tts/player.d.cts +43 -0
  42. package/dist/tts/player.d.ts +43 -0
  43. package/dist/tts/player.js +8 -0
  44. package/dist/tts/source.cjs +181 -0
  45. package/dist/tts/source.d.cts +53 -0
  46. package/dist/tts/source.d.ts +53 -0
  47. package/dist/tts/source.js +7 -0
  48. package/dist/{audio → tts}/utils.cjs +25 -60
  49. package/dist/tts/utils.d.cts +67 -0
  50. package/dist/tts/utils.d.ts +67 -0
  51. package/dist/{audio → tts}/utils.js +2 -7
  52. package/dist/tts/websocket.cjs +479 -0
  53. package/dist/tts/websocket.d.cts +53 -0
  54. package/dist/tts/websocket.d.ts +53 -0
  55. package/dist/tts/websocket.js +11 -0
  56. package/dist/types/index.d.cts +50 -1
  57. package/dist/types/index.d.ts +50 -1
  58. package/dist/voices/index.cjs +157 -0
  59. package/dist/voices/index.d.cts +12 -0
  60. package/dist/voices/index.d.ts +12 -0
  61. package/dist/voices/index.js +9 -0
  62. package/package.json +2 -1
  63. package/src/index.ts +1 -0
  64. package/src/lib/client.ts +15 -1
  65. package/src/lib/constants.ts +15 -4
  66. package/src/lib/index.ts +6 -3
  67. package/src/react/index.ts +176 -110
  68. package/src/tts/index.ts +17 -0
  69. package/src/tts/player.ts +110 -0
  70. package/src/tts/source.ts +115 -0
  71. package/src/tts/utils.ts +150 -0
  72. package/src/tts/websocket.ts +214 -0
  73. package/src/types/index.ts +63 -0
  74. package/src/voices/index.ts +47 -0
  75. package/dist/audio/index.cjs +0 -404
  76. package/dist/audio/index.d.cts +0 -5
  77. package/dist/audio/index.d.ts +0 -5
  78. package/dist/audio/index.js +0 -10
  79. package/dist/audio/utils.d.cts +0 -5
  80. package/dist/audio/utils.d.ts +0 -5
  81. package/dist/chunk-4MHF74A7.js +0 -272
  82. package/dist/chunk-5TSWLYOW.js +0 -113
  83. package/dist/chunk-MJIFZWHS.js +0 -18
  84. package/dist/chunk-OVI3W3GG.js +0 -12
  85. package/dist/chunk-S6A27RQL.js +0 -18
  86. package/dist/index-C2_3XFxn.d.cts +0 -163
  87. package/dist/index-DgwnZezj.d.ts +0 -163
  88. package/src/audio/index.ts +0 -297
  89. package/src/audio/utils.ts +0 -220
@@ -1,272 +0,0 @@
1
- import {
2
- createMessageHandlerForContextId,
3
- getBufferDuration,
4
- getEmitteryCallbacks,
5
- isComplete,
6
- isSentinel,
7
- playAudioBuffer
8
- } from "./chunk-5TSWLYOW.js";
9
- import {
10
- Client
11
- } from "./chunk-MJIFZWHS.js";
12
- import {
13
- SAMPLE_RATE,
14
- constructWebsocketUrl
15
- } from "./chunk-OVI3W3GG.js";
16
- import {
17
- __async,
18
- __forAwait,
19
- __spreadValues
20
- } from "./chunk-R4P7LWVZ.js";
21
-
22
- // src/audio/index.ts
23
- import Emittery from "emittery";
24
- import { humanId } from "human-id";
25
- import { WebSocket } from "partysocket";
26
- var audio_default = class extends Client {
27
- constructor() {
28
- super(...arguments);
29
- this.isConnected = false;
30
- }
31
- /**
32
- * Stream audio from a model.
33
- *
34
- * @param inputs - Stream options. Includes a `model` key and some `parameters`, which
35
- * are model-specific and can be found in the model's documentation.
36
- * @param options - Options for the stream.
37
- * @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
38
- * If `0`, the stream will not time out.
39
- * @returns An object with a method `play` of type `(bufferDuration: number) => Promise<void>`
40
- * that plays the audio as it arrives, with `bufferDuration` seconds of audio buffered before
41
- * starting playback.
42
- */
43
- stream(inputs, { timeout = 0 } = {}) {
44
- var _a, _b, _c, _d;
45
- if (!this.isConnected) {
46
- throw new Error("Not connected to WebSocket. Call .connect() first.");
47
- }
48
- const contextId = this.generateId();
49
- (_a = this.socket) == null ? void 0 : _a.send(
50
- JSON.stringify({
51
- data: inputs,
52
- context_id: contextId
53
- })
54
- );
55
- const streamCompleteController = new AbortController();
56
- let timeoutId = null;
57
- if (timeout > 0) {
58
- timeoutId = setTimeout(streamCompleteController.abort, timeout);
59
- }
60
- const chunks = [];
61
- const emitter = new Emittery();
62
- const handleMessage = createMessageHandlerForContextId(
63
- contextId,
64
- (_0) => __async(this, [_0], function* ({ chunk, message }) {
65
- chunks.push(chunk);
66
- yield emitter.emit("chunk", {
67
- chunk,
68
- chunks
69
- });
70
- yield emitter.emit("message", message);
71
- if (isSentinel(chunk)) {
72
- yield emitter.emit("streamed", {
73
- chunks
74
- });
75
- streamCompleteController.abort();
76
- } else if (timeoutId) {
77
- clearTimeout(timeoutId);
78
- timeoutId = setTimeout(streamCompleteController.abort, timeout);
79
- }
80
- })
81
- );
82
- (_b = this.socket) == null ? void 0 : _b.addEventListener("message", handleMessage, {
83
- signal: streamCompleteController.signal
84
- });
85
- (_c = this.socket) == null ? void 0 : _c.addEventListener(
86
- "close",
87
- () => {
88
- streamCompleteController.abort();
89
- },
90
- {
91
- once: true
92
- }
93
- );
94
- (_d = this.socket) == null ? void 0 : _d.addEventListener(
95
- "error",
96
- () => {
97
- streamCompleteController.abort();
98
- },
99
- {
100
- once: true
101
- }
102
- );
103
- streamCompleteController.signal.addEventListener("abort", () => {
104
- if (timeoutId) {
105
- clearTimeout(timeoutId);
106
- }
107
- emitter.clearListeners();
108
- });
109
- const play = (_0) => __async(this, [_0], function* ({ bufferDuration }) {
110
- const context = new AudioContext({
111
- sampleRate: SAMPLE_RATE
112
- });
113
- let startNextPlaybackAt = 0;
114
- const playLatestChunk = (chunk) => {
115
- if (isSentinel(chunk)) {
116
- return true;
117
- }
118
- startNextPlaybackAt = playAudioBuffer([chunk], context, startNextPlaybackAt) + Math.max(context.currentTime, startNextPlaybackAt);
119
- return false;
120
- };
121
- const playChunks = (chunks2) => {
122
- startNextPlaybackAt += playAudioBuffer(
123
- chunks2,
124
- context,
125
- startNextPlaybackAt
126
- );
127
- if (isComplete(chunks2)) {
128
- return;
129
- }
130
- };
131
- const tryStart = (chunks2) => __async(this, null, function* () {
132
- startNextPlaybackAt = context.currentTime;
133
- if (isComplete(chunks2) || streamCompleteController.signal.aborted) {
134
- emitter.emit("buffered");
135
- playChunks(chunks2);
136
- return true;
137
- }
138
- if (getBufferDuration(chunks2) > bufferDuration) {
139
- emitter.emit("buffered");
140
- playChunks(chunks2);
141
- try {
142
- for (var iter2 = __forAwait(emitter.events("chunk")), more2, temp2, error2; more2 = !(temp2 = yield iter2.next()).done; more2 = false) {
143
- const { chunk } = temp2.value;
144
- if (playLatestChunk(chunk)) {
145
- break;
146
- }
147
- }
148
- } catch (temp2) {
149
- error2 = [temp2];
150
- } finally {
151
- try {
152
- more2 && (temp2 = iter2.return) && (yield temp2.call(iter2));
153
- } finally {
154
- if (error2)
155
- throw error2[0];
156
- }
157
- }
158
- return true;
159
- }
160
- emitter.emit("buffering");
161
- return false;
162
- });
163
- if (!(yield tryStart(chunks))) {
164
- try {
165
- for (var iter = __forAwait(emitter.events("chunk")), more, temp, error; more = !(temp = yield iter.next()).done; more = false) {
166
- const { chunks: chunks2 } = temp.value;
167
- if (yield tryStart(chunks2)) {
168
- const playbackEndsIn = Math.max(0, startNextPlaybackAt - context.currentTime) * 1e3;
169
- emitter.emit("scheduled", { playbackEndsIn });
170
- break;
171
- }
172
- }
173
- } catch (temp) {
174
- error = [temp];
175
- } finally {
176
- try {
177
- more && (temp = iter.return) && (yield temp.call(iter));
178
- } finally {
179
- if (error)
180
- throw error[0];
181
- }
182
- }
183
- } else {
184
- const playbackEndsIn = Math.max(0, startNextPlaybackAt - context.currentTime) * 1e3;
185
- emitter.emit("scheduled", { playbackEndsIn });
186
- }
187
- });
188
- return __spreadValues({
189
- play
190
- }, getEmitteryCallbacks(emitter));
191
- }
192
- /**
193
- * Generate a unique ID suitable for a streaming context.
194
- *
195
- * Not suitable for security purposes or as a primary key, since
196
- * it lacks the amount of entropy required for those use cases.
197
- *
198
- * @returns A unique ID.
199
- */
200
- generateId() {
201
- return humanId({
202
- separator: "-",
203
- capitalize: false
204
- });
205
- }
206
- /**
207
- * Authenticate and connect to a Cartesia streaming WebSocket.
208
- *
209
- * @returns A promise that resolves when the WebSocket is connected.
210
- * @throws {Error} If the WebSocket fails to connect.
211
- */
212
- connect() {
213
- const url = constructWebsocketUrl(this.baseUrl);
214
- url.searchParams.set("api_key", this.apiKey);
215
- const emitter = new Emittery();
216
- this.socket = new WebSocket(url.toString());
217
- this.socket.onopen = () => {
218
- this.isConnected = true;
219
- emitter.emit("open");
220
- };
221
- this.socket.onclose = () => {
222
- this.isConnected = false;
223
- emitter.emit("close");
224
- };
225
- return new Promise(
226
- (resolve, reject) => {
227
- var _a, _b, _c;
228
- (_a = this.socket) == null ? void 0 : _a.addEventListener(
229
- "open",
230
- () => {
231
- resolve(getEmitteryCallbacks(emitter));
232
- },
233
- {
234
- once: true
235
- }
236
- );
237
- const aborter = new AbortController();
238
- (_b = this.socket) == null ? void 0 : _b.addEventListener(
239
- "error",
240
- () => {
241
- aborter.abort();
242
- reject(new Error("WebSocket failed to connect."));
243
- },
244
- {
245
- signal: aborter.signal
246
- }
247
- );
248
- (_c = this.socket) == null ? void 0 : _c.addEventListener(
249
- "close",
250
- () => {
251
- aborter.abort();
252
- reject(new Error("WebSocket closed before it could connect."));
253
- },
254
- {
255
- signal: aborter.signal
256
- }
257
- );
258
- }
259
- );
260
- }
261
- /**
262
- * Disconnect from the Cartesia streaming WebSocket.
263
- */
264
- disconnect() {
265
- var _a;
266
- (_a = this.socket) == null ? void 0 : _a.close();
267
- }
268
- };
269
-
270
- export {
271
- audio_default
272
- };
@@ -1,113 +0,0 @@
1
- import {
2
- SAMPLE_RATE
3
- } from "./chunk-OVI3W3GG.js";
4
-
5
- // src/audio/utils.ts
6
- import base64 from "base64-js";
7
- function getBufferDuration(b64) {
8
- const floats = base64ToArray(b64);
9
- return floats.length / SAMPLE_RATE;
10
- }
11
- function base64ToArray(b64) {
12
- return filterSentinel(b64).reduce((acc, b) => {
13
- const floats = new Float32Array(base64.toByteArray(b).buffer);
14
- const newAcc = new Float32Array(acc.length + floats.length);
15
- newAcc.set(acc, 0);
16
- newAcc.set(floats, acc.length);
17
- return newAcc;
18
- }, new Float32Array(0));
19
- }
20
- function playAudioBuffer(b64, context, maybeStartAt = null, onEnded = null) {
21
- const startAt = maybeStartAt != null ? maybeStartAt : context.currentTime;
22
- const floats = base64ToArray(b64);
23
- const source = context.createBufferSource();
24
- const buffer = context.createBuffer(1, floats.length, SAMPLE_RATE);
25
- buffer.getChannelData(0).set(floats);
26
- source.buffer = buffer;
27
- source.connect(context.destination);
28
- source.start(startAt);
29
- source.onended = onEnded;
30
- return buffer.duration;
31
- }
32
- function createMessageHandlerForContextId(contextId, handler) {
33
- return (event) => {
34
- const message = JSON.parse(event.data);
35
- if (message.context_id !== contextId) {
36
- return;
37
- }
38
- let chunk;
39
- if (message.done) {
40
- chunk = getSentinel();
41
- } else {
42
- chunk = message.data;
43
- }
44
- handler({ chunk, message });
45
- };
46
- }
47
- function getSentinel() {
48
- return null;
49
- }
50
- function isSentinel(x) {
51
- return x === getSentinel();
52
- }
53
- function filterSentinel(collection) {
54
- return collection.filter(
55
- (x) => !isSentinel(x)
56
- );
57
- }
58
- function isComplete(chunks) {
59
- return isSentinel(chunks[chunks.length - 1]);
60
- }
61
- function getEmitteryCallbacks(emitter) {
62
- return {
63
- on: emitter.on.bind(emitter),
64
- off: emitter.off.bind(emitter),
65
- once: emitter.once.bind(emitter),
66
- events: emitter.events.bind(emitter)
67
- };
68
- }
69
- function bufferToWav(sampleRate, channelBuffers) {
70
- const totalSamples = channelBuffers[0].length * channelBuffers.length;
71
- const buffer = new ArrayBuffer(44 + totalSamples * 2);
72
- const view = new DataView(buffer);
73
- const writeString = (view2, offset2, string) => {
74
- for (let i = 0; i < string.length; i++) {
75
- view2.setUint8(offset2 + i, string.charCodeAt(i));
76
- }
77
- };
78
- writeString(view, 0, "RIFF");
79
- view.setUint32(4, 36 + totalSamples * 2, true);
80
- writeString(view, 8, "WAVE");
81
- writeString(view, 12, "fmt ");
82
- view.setUint32(16, 16, true);
83
- view.setUint16(20, 1, true);
84
- view.setUint16(22, channelBuffers.length, true);
85
- view.setUint32(24, sampleRate, true);
86
- view.setUint32(28, sampleRate * 4, true);
87
- view.setUint16(32, channelBuffers.length * 2, true);
88
- view.setUint16(34, 16, true);
89
- writeString(view, 36, "data");
90
- view.setUint32(40, totalSamples * 2, true);
91
- let offset = 44;
92
- for (let i = 0; i < channelBuffers[0].length; i++) {
93
- for (let channel = 0; channel < channelBuffers.length; channel++) {
94
- const s = Math.max(-1, Math.min(1, channelBuffers[channel][i]));
95
- view.setInt16(offset, s < 0 ? s * 32768 : s * 32767, true);
96
- offset += 2;
97
- }
98
- }
99
- return buffer;
100
- }
101
-
102
- export {
103
- getBufferDuration,
104
- base64ToArray,
105
- playAudioBuffer,
106
- createMessageHandlerForContextId,
107
- getSentinel,
108
- isSentinel,
109
- filterSentinel,
110
- isComplete,
111
- getEmitteryCallbacks,
112
- bufferToWav
113
- };
@@ -1,18 +0,0 @@
1
- import {
2
- BASE_URL
3
- } from "./chunk-OVI3W3GG.js";
4
-
5
- // src/lib/client.ts
6
- var Client = class {
7
- constructor(options = {}) {
8
- if (!(options.apiKey || process.env.CARTESIA_API_KEY)) {
9
- throw new Error("Missing Cartesia API key.");
10
- }
11
- this.apiKey = options.apiKey || process.env.CARTESIA_API_KEY;
12
- this.baseUrl = options.baseUrl || BASE_URL;
13
- }
14
- };
15
-
16
- export {
17
- Client
18
- };
@@ -1,12 +0,0 @@
1
- // src/lib/constants.ts
2
- var BASE_URL = "https://api.cartesia.ai/v0";
3
- var SAMPLE_RATE = 44100;
4
- var constructWebsocketUrl = (baseUrl) => {
5
- return new URL(`${baseUrl.replace(/^http/, "ws")}/audio/websocket`);
6
- };
7
-
8
- export {
9
- BASE_URL,
10
- SAMPLE_RATE,
11
- constructWebsocketUrl
12
- };
@@ -1,18 +0,0 @@
1
- import {
2
- audio_default
3
- } from "./chunk-4MHF74A7.js";
4
- import {
5
- Client
6
- } from "./chunk-MJIFZWHS.js";
7
-
8
- // src/lib/index.ts
9
- var Cartesia = class extends Client {
10
- constructor(options = {}) {
11
- super(options);
12
- this.audio = new audio_default(options);
13
- }
14
- };
15
-
16
- export {
17
- Cartesia
18
- };
@@ -1,163 +0,0 @@
1
- import * as emittery from 'emittery';
2
- import emittery__default from 'emittery';
3
- import { WebSocket } from 'partysocket';
4
- import { Client } from './lib/client.cjs';
5
-
6
- /**
7
- * Get the duration of base64-encoded audio buffer(s) in seconds.
8
- *
9
- * @param b64 The base64-encoded audio buffer, or an array of base64-encoded
10
- * audio buffers.
11
- * @returns The duration of the buffer(s) in seconds.
12
- */
13
- declare function getBufferDuration(b64: Chunk[]): number;
14
- /**
15
- * Convert base64-encoded audio buffer(s) to a Float32Array.
16
- *
17
- * @param b64 The base64-encoded audio buffer, or an array of base64-encoded
18
- * audio buffers.
19
- * @returns The audio buffer(s) as a Float32Array.
20
- */
21
- declare function base64ToArray(b64: Chunk[]): Float32Array;
22
- /**
23
- * Schedule an audio buffer to play at a given time in the passed context.
24
- *
25
- * @param b64 The base64-encoded audio buffer to play.
26
- * @param context The audio context to play the buffer in.
27
- * @param maybeStartAt The time to start playing the buffer at, or null to play
28
- * immediately.
29
- * @param onEnded The callback to call when the buffer has finished playing.
30
- * @returns The duration of the buffer in seconds.
31
- */
32
- declare function playAudioBuffer(b64: Chunk[], context: AudioContext, maybeStartAt?: number | null, onEnded?: AudioScheduledSourceNode["onended"]): number;
33
- /**
34
- * Unwraps a chunk of audio data from a message event and calls the
35
- * handler with it if the context ID matches.
36
- *
37
- * @param contextId The context ID to listen for.
38
- * @param handler The handler to call with the chunk of audio data.
39
- * @returns A message event handler.
40
- */
41
- declare function createMessageHandlerForContextId(contextId: string, handler: ({ chunk, message, }: {
42
- chunk: Chunk;
43
- message: StreamEventData["message"];
44
- }) => void): (event: MessageEvent) => void;
45
- type Sentinel = null;
46
- /**
47
- * Get a sentinel value that indicates the end of a stream.
48
- * @returns A sentinel value to indicate the end of a stream.
49
- */
50
- declare function getSentinel(): Sentinel;
51
- /**
52
- * Check if a chunk is a sentinel value (i.e. null).
53
- *
54
- * @param chunk
55
- * @returns Whether the chunk is a sentinel value.
56
- */
57
- declare function isSentinel(x: unknown): x is Sentinel;
58
- /**
59
- * Filter out null values from a collection.
60
- *
61
- * @param collection The collection to filter.
62
- * @returns The collection with null values removed.
63
- */
64
- declare function filterSentinel<T>(collection: T[]): Exclude<T, Sentinel>[];
65
- /**
66
- * Check if an array of chunks is complete by testing if the last chunk is a sentinel
67
- * value (i.e. null).
68
- * @param chunk
69
- * @returns Whether the array of chunks is complete.
70
- */
71
- declare function isComplete(chunks: Chunk[]): boolean;
72
- type EmitteryCallbacks<T> = {
73
- on: emittery__default<T>["on"];
74
- off: emittery__default<T>["off"];
75
- once: emittery__default<T>["once"];
76
- events: emittery__default<T>["events"];
77
- };
78
- /**
79
- * Get user-facing emitter callbacks for an Emittery instance.
80
- * @param emitter The Emittery instance to get callbacks for.
81
- * @returns User-facing emitter callbacks.
82
- */
83
- declare function getEmitteryCallbacks<T>(emitter: emittery__default<T>): EmitteryCallbacks<T>;
84
- /**
85
- * Converts a base64-encoded audio buffer to a WAV file.
86
- * Source: https://gist.github.com/Daninet/22edc59cf2aee0b9a90c18e553e49297
87
- * @param b64 The base64-encoded audio buffer to convert to a WAV file.
88
- */
89
- declare function bufferToWav(sampleRate: number, channelBuffers: Float32Array[]): ArrayBuffer;
90
-
91
- type Chunk = string | Sentinel;
92
- type StreamEventData = {
93
- chunk: {
94
- chunk: Chunk;
95
- chunks: Chunk[];
96
- };
97
- streamed: {
98
- chunks: Chunk[];
99
- };
100
- message: unknown;
101
- buffering: never;
102
- buffered: never;
103
- scheduled: {
104
- playbackEndsIn: number;
105
- };
106
- };
107
- type ConnectionEventData = {
108
- open: never;
109
- close: never;
110
- };
111
- type StreamRequest = {
112
- inputs: object;
113
- options: {
114
- timeout?: number;
115
- };
116
- };
117
- declare class export_default extends Client {
118
- socket?: WebSocket;
119
- isConnected: boolean;
120
- /**
121
- * Stream audio from a model.
122
- *
123
- * @param inputs - Stream options. Includes a `model` key and some `parameters`, which
124
- * are model-specific and can be found in the model's documentation.
125
- * @param options - Options for the stream.
126
- * @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
127
- * If `0`, the stream will not time out.
128
- * @returns An object with a method `play` of type `(bufferDuration: number) => Promise<void>`
129
- * that plays the audio as it arrives, with `bufferDuration` seconds of audio buffered before
130
- * starting playback.
131
- */
132
- stream(inputs: StreamRequest["inputs"], { timeout }?: StreamRequest["options"]): {
133
- on: <Name extends keyof emittery.OmnipresentEventData | keyof StreamEventData>(eventName: Name | readonly Name[], listener: (eventData: (StreamEventData & emittery.OmnipresentEventData)[Name]) => void | Promise<void>) => emittery.UnsubscribeFunction;
134
- off: <Name_1 extends keyof emittery.OmnipresentEventData | keyof StreamEventData>(eventName: Name_1 | readonly Name_1[], listener: (eventData: (StreamEventData & emittery.OmnipresentEventData)[Name_1]) => void | Promise<void>) => void;
135
- once: <Name_2 extends keyof emittery.OmnipresentEventData | keyof StreamEventData>(eventName: Name_2 | readonly Name_2[]) => emittery.EmitteryOncePromise<(StreamEventData & emittery.OmnipresentEventData)[Name_2]>;
136
- events: <Name_3 extends keyof StreamEventData>(eventName: Name_3 | readonly Name_3[]) => AsyncIterableIterator<StreamEventData[Name_3]>;
137
- play: ({ bufferDuration }: {
138
- bufferDuration: number;
139
- }) => Promise<void>;
140
- };
141
- /**
142
- * Generate a unique ID suitable for a streaming context.
143
- *
144
- * Not suitable for security purposes or as a primary key, since
145
- * it lacks the amount of entropy required for those use cases.
146
- *
147
- * @returns A unique ID.
148
- */
149
- generateId(): string;
150
- /**
151
- * Authenticate and connect to a Cartesia streaming WebSocket.
152
- *
153
- * @returns A promise that resolves when the WebSocket is connected.
154
- * @throws {Error} If the WebSocket fails to connect.
155
- */
156
- connect(): Promise<EmitteryCallbacks<ConnectionEventData>>;
157
- /**
158
- * Disconnect from the Cartesia streaming WebSocket.
159
- */
160
- disconnect(): void;
161
- }
162
-
163
- export { type Chunk as C, type EmitteryCallbacks as E, type StreamEventData as S, type Sentinel as a, base64ToArray as b, createMessageHandlerForContextId as c, getSentinel as d, export_default as e, filterSentinel as f, getBufferDuration as g, isComplete as h, isSentinel as i, getEmitteryCallbacks as j, bufferToWav as k, type ConnectionEventData as l, type StreamRequest as m, playAudioBuffer as p };