@cartesia/cartesia-js 0.0.3 → 1.0.0-alpha.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.
Files changed (103) hide show
  1. package/.turbo/turbo-build.log +68 -38
  2. package/CHANGELOG.md +12 -0
  3. package/README.md +123 -16
  4. package/dist/chunk-3FL2SNIR.js +17 -0
  5. package/dist/chunk-3GBZUGUD.js +17 -0
  6. package/dist/chunk-4RMSIQLG.js +25 -0
  7. package/dist/chunk-BCQ63627.js +32 -0
  8. package/dist/chunk-JOHSCOLW.js +106 -0
  9. package/dist/chunk-LYPTISWL.js +75 -0
  10. package/dist/chunk-NDNN326Q.js +207 -0
  11. package/dist/chunk-WBK6LLXX.js +58 -0
  12. package/dist/chunk-WE63M7PJ.js +119 -0
  13. package/dist/{chunk-HNLIBHEN.mjs → chunk-WIFMLPT5.js} +31 -16
  14. package/dist/chunk-X7SJMF2R.js +22 -0
  15. package/dist/index.cjs +652 -0
  16. package/dist/index.d.cts +10 -0
  17. package/dist/index.d.ts +10 -0
  18. package/dist/index.js +20 -0
  19. package/dist/lib/client.cjs +89 -0
  20. package/dist/lib/client.d.cts +11 -0
  21. package/dist/lib/client.d.ts +2 -0
  22. package/dist/lib/client.js +7 -42
  23. package/dist/lib/constants.cjs +42 -0
  24. package/dist/lib/constants.d.cts +4 -0
  25. package/dist/lib/constants.d.ts +2 -3
  26. package/dist/lib/constants.js +8 -37
  27. package/dist/lib/index.cjs +531 -0
  28. package/dist/lib/index.d.cts +16 -0
  29. package/dist/lib/index.d.ts +6 -2
  30. package/dist/lib/index.js +13 -409
  31. package/dist/react/index.cjs +846 -0
  32. package/dist/react/index.d.cts +33 -0
  33. package/dist/react/index.d.ts +20 -13
  34. package/dist/react/index.js +161 -501
  35. package/dist/react/utils.cjs +57 -0
  36. package/dist/react/utils.d.cts +7 -0
  37. package/dist/react/utils.d.ts +7 -0
  38. package/dist/react/utils.js +7 -0
  39. package/dist/tts/index.cjs +470 -0
  40. package/dist/tts/index.d.cts +17 -0
  41. package/dist/tts/index.d.ts +17 -0
  42. package/dist/tts/index.js +12 -0
  43. package/dist/tts/player.cjs +198 -0
  44. package/dist/tts/player.d.cts +43 -0
  45. package/dist/tts/player.d.ts +43 -0
  46. package/dist/tts/player.js +8 -0
  47. package/dist/tts/source.cjs +167 -0
  48. package/dist/tts/source.d.cts +53 -0
  49. package/dist/tts/source.d.ts +53 -0
  50. package/dist/tts/source.js +7 -0
  51. package/dist/{audio/utils.js → tts/utils.cjs} +13 -54
  52. package/dist/tts/utils.d.cts +67 -0
  53. package/dist/tts/utils.d.ts +67 -0
  54. package/dist/{audio/utils.mjs → tts/utils.js} +2 -6
  55. package/dist/tts/websocket.cjs +453 -0
  56. package/dist/tts/websocket.d.cts +53 -0
  57. package/dist/tts/websocket.d.ts +53 -0
  58. package/dist/tts/websocket.js +11 -0
  59. package/dist/types/index.cjs +18 -0
  60. package/dist/types/index.d.cts +55 -0
  61. package/dist/types/index.d.ts +50 -1
  62. package/dist/types/index.js +1 -18
  63. package/dist/voices/index.cjs +155 -0
  64. package/dist/voices/index.d.cts +12 -0
  65. package/dist/voices/index.d.ts +12 -0
  66. package/dist/voices/index.js +9 -0
  67. package/package.json +11 -7
  68. package/src/index.ts +4 -0
  69. package/src/lib/client.ts +14 -1
  70. package/src/lib/constants.ts +13 -3
  71. package/src/lib/index.ts +6 -3
  72. package/src/react/index.ts +167 -75
  73. package/src/react/utils.ts +11 -0
  74. package/src/tts/index.ts +17 -0
  75. package/src/tts/player.ts +109 -0
  76. package/src/tts/source.ts +98 -0
  77. package/src/{audio → tts}/utils.ts +19 -97
  78. package/src/tts/websocket.ts +210 -0
  79. package/src/types/index.ts +63 -0
  80. package/src/voices/index.ts +47 -0
  81. package/dist/audio/index.d.mts +0 -5
  82. package/dist/audio/index.d.ts +0 -5
  83. package/dist/audio/index.js +0 -396
  84. package/dist/audio/index.mjs +0 -9
  85. package/dist/audio/utils.d.mts +0 -5
  86. package/dist/audio/utils.d.ts +0 -5
  87. package/dist/chunk-3CYTAFLF.mjs +0 -262
  88. package/dist/chunk-FRIBQZPN.mjs +0 -113
  89. package/dist/chunk-XSFPHPPG.mjs +0 -18
  90. package/dist/index-DSBmfK9-.d.mts +0 -158
  91. package/dist/index-qwAyxV5I.d.ts +0 -158
  92. package/dist/lib/client.d.mts +0 -9
  93. package/dist/lib/client.mjs +0 -7
  94. package/dist/lib/constants.d.mts +0 -5
  95. package/dist/lib/constants.mjs +0 -10
  96. package/dist/lib/index.d.mts +0 -12
  97. package/dist/lib/index.mjs +0 -19
  98. package/dist/react/index.d.mts +0 -26
  99. package/dist/react/index.mjs +0 -130
  100. package/dist/types/index.d.mts +0 -6
  101. package/index.ts +0 -3
  102. package/src/audio/index.ts +0 -282
  103. /package/dist/{types/index.mjs → chunk-FXPGR372.js} +0 -0
@@ -1,113 +0,0 @@
1
- import {
2
- SAMPLE_RATE
3
- } from "./chunk-HNLIBHEN.mjs";
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-HNLIBHEN.mjs";
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,158 +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.mjs';
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
- };
102
- type ConnectionEventData = {
103
- open: never;
104
- close: never;
105
- };
106
- type StreamRequest = {
107
- inputs: object;
108
- options: {
109
- timeout?: number;
110
- };
111
- };
112
- declare class export_default extends Client {
113
- socket?: WebSocket;
114
- isConnected: boolean;
115
- /**
116
- * Stream audio from a model.
117
- *
118
- * @param inputs - Stream options. Includes a `model` key and some `parameters`, which
119
- * are model-specific and can be found in the model's documentation.
120
- * @param options - Options for the stream.
121
- * @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
122
- * If `0`, the stream will not time out.
123
- * @returns An object with a method `play` of type `(bufferDuration: number) => Promise<void>`
124
- * that plays the audio as it arrives, with `bufferDuration` seconds of audio buffered before
125
- * starting playback.
126
- */
127
- stream(inputs: StreamRequest["inputs"], { timeout }?: StreamRequest["options"]): {
128
- on: <Name extends keyof StreamEventData | keyof emittery.OmnipresentEventData>(eventName: Name | readonly Name[], listener: (eventData: (StreamEventData & emittery.OmnipresentEventData)[Name]) => void | Promise<void>) => emittery.UnsubscribeFunction;
129
- off: <Name_1 extends keyof StreamEventData | keyof emittery.OmnipresentEventData>(eventName: Name_1 | readonly Name_1[], listener: (eventData: (StreamEventData & emittery.OmnipresentEventData)[Name_1]) => void | Promise<void>) => void;
130
- once: <Name_2 extends keyof StreamEventData | keyof emittery.OmnipresentEventData>(eventName: Name_2 | readonly Name_2[]) => emittery.EmitteryOncePromise<(StreamEventData & emittery.OmnipresentEventData)[Name_2]>;
131
- events: <Name_3 extends keyof StreamEventData>(eventName: Name_3 | readonly Name_3[]) => AsyncIterableIterator<StreamEventData[Name_3]>;
132
- play: ({ bufferDuration }: {
133
- bufferDuration: number;
134
- }) => Promise<void>;
135
- };
136
- /**
137
- * Generate a unique ID suitable for a streaming context.
138
- *
139
- * Not suitable for security purposes or as a primary key, since
140
- * it lacks the amount of entropy required for those use cases.
141
- *
142
- * @returns A unique ID.
143
- */
144
- generateId(): string;
145
- /**
146
- * Authenticate and connect to a Cartesia streaming WebSocket.
147
- *
148
- * @returns A promise that resolves when the WebSocket is connected.
149
- * @throws {Error} If the WebSocket fails to connect.
150
- */
151
- connect(): Promise<EmitteryCallbacks<ConnectionEventData>>;
152
- /**
153
- * Disconnect from the Cartesia streaming WebSocket.
154
- */
155
- disconnect(): void;
156
- }
157
-
158
- 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 };
@@ -1,158 +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.js';
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
- };
102
- type ConnectionEventData = {
103
- open: never;
104
- close: never;
105
- };
106
- type StreamRequest = {
107
- inputs: object;
108
- options: {
109
- timeout?: number;
110
- };
111
- };
112
- declare class export_default extends Client {
113
- socket?: WebSocket;
114
- isConnected: boolean;
115
- /**
116
- * Stream audio from a model.
117
- *
118
- * @param inputs - Stream options. Includes a `model` key and some `parameters`, which
119
- * are model-specific and can be found in the model's documentation.
120
- * @param options - Options for the stream.
121
- * @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
122
- * If `0`, the stream will not time out.
123
- * @returns An object with a method `play` of type `(bufferDuration: number) => Promise<void>`
124
- * that plays the audio as it arrives, with `bufferDuration` seconds of audio buffered before
125
- * starting playback.
126
- */
127
- stream(inputs: StreamRequest["inputs"], { timeout }?: StreamRequest["options"]): {
128
- on: <Name extends keyof StreamEventData | keyof emittery.OmnipresentEventData>(eventName: Name | readonly Name[], listener: (eventData: (StreamEventData & emittery.OmnipresentEventData)[Name]) => void | Promise<void>) => emittery.UnsubscribeFunction;
129
- off: <Name_1 extends keyof StreamEventData | keyof emittery.OmnipresentEventData>(eventName: Name_1 | readonly Name_1[], listener: (eventData: (StreamEventData & emittery.OmnipresentEventData)[Name_1]) => void | Promise<void>) => void;
130
- once: <Name_2 extends keyof StreamEventData | keyof emittery.OmnipresentEventData>(eventName: Name_2 | readonly Name_2[]) => emittery.EmitteryOncePromise<(StreamEventData & emittery.OmnipresentEventData)[Name_2]>;
131
- events: <Name_3 extends keyof StreamEventData>(eventName: Name_3 | readonly Name_3[]) => AsyncIterableIterator<StreamEventData[Name_3]>;
132
- play: ({ bufferDuration }: {
133
- bufferDuration: number;
134
- }) => Promise<void>;
135
- };
136
- /**
137
- * Generate a unique ID suitable for a streaming context.
138
- *
139
- * Not suitable for security purposes or as a primary key, since
140
- * it lacks the amount of entropy required for those use cases.
141
- *
142
- * @returns A unique ID.
143
- */
144
- generateId(): string;
145
- /**
146
- * Authenticate and connect to a Cartesia streaming WebSocket.
147
- *
148
- * @returns A promise that resolves when the WebSocket is connected.
149
- * @throws {Error} If the WebSocket fails to connect.
150
- */
151
- connect(): Promise<EmitteryCallbacks<ConnectionEventData>>;
152
- /**
153
- * Disconnect from the Cartesia streaming WebSocket.
154
- */
155
- disconnect(): void;
156
- }
157
-
158
- 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 };
@@ -1,9 +0,0 @@
1
- import { ClientOptions } from '../types/index.mjs';
2
-
3
- declare class Client {
4
- apiKey: string;
5
- baseUrl: string;
6
- constructor(options?: ClientOptions);
7
- }
8
-
9
- export { Client };
@@ -1,7 +0,0 @@
1
- import {
2
- Client
3
- } from "../chunk-XSFPHPPG.mjs";
4
- import "../chunk-HNLIBHEN.mjs";
5
- export {
6
- Client
7
- };
@@ -1,5 +0,0 @@
1
- declare const BASE_URL = "https://api.cartesia.ai/v0";
2
- declare const SAMPLE_RATE = 44100;
3
- declare const constructWebsocketUrl: (baseUrl: string) => URL;
4
-
5
- export { BASE_URL, SAMPLE_RATE, constructWebsocketUrl };
@@ -1,10 +0,0 @@
1
- import {
2
- BASE_URL,
3
- SAMPLE_RATE,
4
- constructWebsocketUrl
5
- } from "../chunk-HNLIBHEN.mjs";
6
- export {
7
- BASE_URL,
8
- SAMPLE_RATE,
9
- constructWebsocketUrl
10
- };
@@ -1,12 +0,0 @@
1
- import { e as export_default } from '../index-DSBmfK9-.mjs';
2
- import { ClientOptions } from '../types/index.mjs';
3
- import { Client } from './client.mjs';
4
- import 'emittery';
5
- import 'partysocket';
6
-
7
- declare class Cartesia extends Client {
8
- audio: export_default;
9
- constructor(options?: ClientOptions);
10
- }
11
-
12
- export { Cartesia };
@@ -1,19 +0,0 @@
1
- import {
2
- audio_default
3
- } from "../chunk-3CYTAFLF.mjs";
4
- import "../chunk-FRIBQZPN.mjs";
5
- import {
6
- Client
7
- } from "../chunk-XSFPHPPG.mjs";
8
- import "../chunk-HNLIBHEN.mjs";
9
-
10
- // src/lib/index.ts
11
- var Cartesia = class extends Client {
12
- constructor(options = {}) {
13
- super(options);
14
- this.audio = new audio_default(options);
15
- }
16
- };
17
- export {
18
- Cartesia
19
- };
@@ -1,26 +0,0 @@
1
- import { C as Chunk, S as StreamEventData } from '../index-DSBmfK9-.mjs';
2
- import 'emittery';
3
- import 'partysocket';
4
- import '../lib/client.mjs';
5
- import '../types/index.mjs';
6
-
7
- type UseAudioOptions = {
8
- apiKey: string | null;
9
- baseUrl?: string;
10
- };
11
- interface UseAudioReturn {
12
- stream: (options: object) => void;
13
- play: (bufferDuration?: number) => Promise<void>;
14
- download: () => Blob | null;
15
- isPlaying: boolean;
16
- isConnected: boolean;
17
- isStreamed: boolean;
18
- chunks: Chunk[];
19
- messages: StreamEventData["message"][];
20
- }
21
- /**
22
- * React hook to use the Cartesia audio API.
23
- */
24
- declare function useAudio({ apiKey, baseUrl }: UseAudioOptions): UseAudioReturn;
25
-
26
- export { type UseAudioOptions, useAudio };
@@ -1,130 +0,0 @@
1
- import {
2
- audio_default
3
- } from "../chunk-3CYTAFLF.mjs";
4
- import {
5
- base64ToArray,
6
- bufferToWav
7
- } from "../chunk-FRIBQZPN.mjs";
8
- import "../chunk-XSFPHPPG.mjs";
9
- import {
10
- SAMPLE_RATE,
11
- __async
12
- } from "../chunk-HNLIBHEN.mjs";
13
-
14
- // src/react/index.ts
15
- import { useCallback, useEffect, useMemo, useRef, useState } from "react";
16
- function useAudio({ apiKey, baseUrl }) {
17
- if (typeof window === "undefined") {
18
- return {
19
- stream: () => {
20
- },
21
- play: () => __async(this, null, function* () {
22
- }),
23
- download: () => null,
24
- isConnected: false,
25
- isPlaying: false,
26
- isStreamed: false,
27
- chunks: [],
28
- messages: []
29
- };
30
- }
31
- const audio = useMemo(() => {
32
- if (!apiKey) {
33
- return null;
34
- }
35
- const audio2 = new audio_default({ apiKey, baseUrl });
36
- return audio2;
37
- }, [apiKey, baseUrl]);
38
- const streamReturn = useRef(null);
39
- const [isStreamed, setIsStreamed] = useState(false);
40
- const [isPlaying, setIsPlaying] = useState(false);
41
- const [isConnected, setIsConnected] = useState(false);
42
- const [chunks, setChunks] = useState([]);
43
- const [messages, setMessages] = useState([]);
44
- const stream = useCallback(
45
- (options) => __async(this, null, function* () {
46
- var _a;
47
- streamReturn.current = (_a = audio == null ? void 0 : audio.stream(options)) != null ? _a : null;
48
- if (!streamReturn.current) {
49
- return;
50
- }
51
- setMessages([]);
52
- streamReturn.current.on(
53
- "chunk",
54
- ({ chunks: chunks3 }) => {
55
- setChunks(chunks3);
56
- }
57
- );
58
- streamReturn.current.on(
59
- "message",
60
- (message) => {
61
- setMessages((messages2) => [...messages2, message]);
62
- }
63
- );
64
- const { chunks: chunks2 } = yield streamReturn.current.once("streamed");
65
- setChunks(chunks2);
66
- setIsStreamed(true);
67
- }),
68
- [audio]
69
- );
70
- const download = useCallback(() => {
71
- if (!isStreamed) {
72
- return null;
73
- }
74
- const audio2 = bufferToWav(SAMPLE_RATE, [base64ToArray(chunks)]);
75
- return new Blob([audio2], { type: "audio/wav" });
76
- }, [isStreamed, chunks]);
77
- useEffect(() => {
78
- let cleanup = () => {
79
- };
80
- function setupConnection() {
81
- return __async(this, null, function* () {
82
- try {
83
- const connection = yield audio == null ? void 0 : audio.connect();
84
- if (!connection) {
85
- return;
86
- }
87
- setIsConnected(true);
88
- const unsubscribe = connection.on("close", () => {
89
- setIsConnected(false);
90
- });
91
- return () => {
92
- unsubscribe();
93
- audio == null ? void 0 : audio.disconnect();
94
- };
95
- } catch (e) {
96
- console.error(e);
97
- }
98
- });
99
- }
100
- setupConnection().then((cleanupConnection) => {
101
- cleanup = cleanupConnection;
102
- });
103
- return () => cleanup == null ? void 0 : cleanup();
104
- }, [audio]);
105
- const play = useCallback(
106
- (bufferDuration = 0) => __async(this, null, function* () {
107
- var _a;
108
- if (isPlaying || !streamReturn.current) {
109
- return;
110
- }
111
- setIsPlaying(true);
112
- yield (_a = streamReturn.current) == null ? void 0 : _a.play({ bufferDuration });
113
- setIsPlaying(false);
114
- }),
115
- [isPlaying]
116
- );
117
- return {
118
- stream,
119
- play,
120
- download,
121
- isPlaying,
122
- isConnected,
123
- isStreamed,
124
- chunks,
125
- messages
126
- };
127
- }
128
- export {
129
- useAudio
130
- };
@@ -1,6 +0,0 @@
1
- interface ClientOptions {
2
- apiKey?: string;
3
- baseUrl?: string;
4
- }
5
-
6
- export type { ClientOptions };
package/index.ts DELETED
@@ -1,3 +0,0 @@
1
- export { Cartesia as default } from "./src/lib";
2
- export * from "./src/lib";
3
- export * from "./src/types";