@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.
- package/.turbo/turbo-build.log +63 -45
- package/CHANGELOG.md +12 -0
- package/README.md +123 -16
- package/dist/chunk-36JBKJUN.js +119 -0
- package/dist/chunk-3F5E46FT.js +212 -0
- package/dist/{chunk-XPIMIAAE.js → chunk-3FL2SNIR.js} +1 -1
- package/dist/chunk-JGP5BIUV.js +34 -0
- package/dist/chunk-KWBSQZTY.js +25 -0
- package/dist/chunk-PQ6CIPFW.js +120 -0
- package/dist/chunk-RO7TY474.js +81 -0
- package/dist/chunk-T3RG6WV4.js +22 -0
- package/dist/{chunk-R4P7LWVZ.js → chunk-WIFMLPT5.js} +31 -6
- package/dist/chunk-WVTITUXX.js +58 -0
- package/dist/chunk-XHTDPLFR.js +19 -0
- package/dist/index.cjs +425 -166
- package/dist/index.d.cts +7 -3
- package/dist/index.d.ts +7 -3
- package/dist/index.js +13 -6
- package/dist/lib/client.cjs +49 -1
- package/dist/lib/client.d.cts +2 -0
- package/dist/lib/client.d.ts +2 -0
- package/dist/lib/client.js +3 -3
- package/dist/lib/constants.cjs +15 -8
- package/dist/lib/constants.d.cts +4 -4
- package/dist/lib/constants.d.ts +4 -4
- package/dist/lib/constants.js +6 -6
- package/dist/lib/index.cjs +310 -171
- package/dist/lib/index.d.cts +6 -2
- package/dist/lib/index.d.ts +6 -2
- package/dist/lib/index.js +9 -6
- package/dist/react/index.cjs +573 -290
- package/dist/react/index.d.cts +20 -14
- package/dist/react/index.d.ts +20 -14
- package/dist/react/index.js +157 -105
- package/dist/react/utils.js +2 -2
- package/dist/tts/index.cjs +496 -0
- package/dist/tts/index.d.cts +17 -0
- package/dist/tts/index.d.ts +17 -0
- package/dist/tts/index.js +12 -0
- package/dist/tts/player.cjs +198 -0
- package/dist/tts/player.d.cts +43 -0
- package/dist/tts/player.d.ts +43 -0
- package/dist/tts/player.js +8 -0
- package/dist/tts/source.cjs +181 -0
- package/dist/tts/source.d.cts +53 -0
- package/dist/tts/source.d.ts +53 -0
- package/dist/tts/source.js +7 -0
- package/dist/{audio → tts}/utils.cjs +25 -60
- package/dist/tts/utils.d.cts +67 -0
- package/dist/tts/utils.d.ts +67 -0
- package/dist/{audio → tts}/utils.js +2 -7
- package/dist/tts/websocket.cjs +479 -0
- package/dist/tts/websocket.d.cts +53 -0
- package/dist/tts/websocket.d.ts +53 -0
- package/dist/tts/websocket.js +11 -0
- package/dist/types/index.d.cts +50 -1
- package/dist/types/index.d.ts +50 -1
- package/dist/voices/index.cjs +157 -0
- package/dist/voices/index.d.cts +12 -0
- package/dist/voices/index.d.ts +12 -0
- package/dist/voices/index.js +9 -0
- package/package.json +2 -1
- package/src/index.ts +1 -0
- package/src/lib/client.ts +15 -1
- package/src/lib/constants.ts +15 -4
- package/src/lib/index.ts +6 -3
- package/src/react/index.ts +176 -110
- package/src/tts/index.ts +17 -0
- package/src/tts/player.ts +110 -0
- package/src/tts/source.ts +115 -0
- package/src/tts/utils.ts +150 -0
- package/src/tts/websocket.ts +214 -0
- package/src/types/index.ts +63 -0
- package/src/voices/index.ts +47 -0
- package/dist/audio/index.cjs +0 -404
- package/dist/audio/index.d.cts +0 -5
- package/dist/audio/index.d.ts +0 -5
- package/dist/audio/index.js +0 -10
- package/dist/audio/utils.d.cts +0 -5
- package/dist/audio/utils.d.ts +0 -5
- package/dist/chunk-4MHF74A7.js +0 -272
- package/dist/chunk-5TSWLYOW.js +0 -113
- package/dist/chunk-MJIFZWHS.js +0 -18
- package/dist/chunk-OVI3W3GG.js +0 -12
- package/dist/chunk-S6A27RQL.js +0 -18
- package/dist/index-C2_3XFxn.d.cts +0 -163
- package/dist/index-DgwnZezj.d.ts +0 -163
- package/src/audio/index.ts +0 -297
- package/src/audio/utils.ts +0 -220
|
@@ -27,14 +27,12 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
27
27
|
));
|
|
28
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
29
|
|
|
30
|
-
// src/
|
|
30
|
+
// src/tts/utils.ts
|
|
31
31
|
var utils_exports = {};
|
|
32
32
|
__export(utils_exports, {
|
|
33
33
|
base64ToArray: () => base64ToArray,
|
|
34
|
-
bufferToWav: () => bufferToWav,
|
|
35
34
|
createMessageHandlerForContextId: () => createMessageHandlerForContextId,
|
|
36
35
|
filterSentinel: () => filterSentinel,
|
|
37
|
-
getBufferDuration: () => getBufferDuration,
|
|
38
36
|
getEmitteryCallbacks: () => getEmitteryCallbacks,
|
|
39
37
|
getSentinel: () => getSentinel,
|
|
40
38
|
isComplete: () => isComplete,
|
|
@@ -43,38 +41,39 @@ __export(utils_exports, {
|
|
|
43
41
|
});
|
|
44
42
|
module.exports = __toCommonJS(utils_exports);
|
|
45
43
|
var import_base64_js = __toESM(require("base64-js"), 1);
|
|
46
|
-
|
|
47
|
-
// src/lib/constants.ts
|
|
48
|
-
var SAMPLE_RATE = 44100;
|
|
49
|
-
|
|
50
|
-
// src/audio/utils.ts
|
|
51
|
-
function getBufferDuration(b64) {
|
|
52
|
-
const floats = base64ToArray(b64);
|
|
53
|
-
return floats.length / SAMPLE_RATE;
|
|
54
|
-
}
|
|
55
44
|
function base64ToArray(b64) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
45
|
+
const byteArrays = filterSentinel(b64).map((b) => import_base64_js.default.toByteArray(b));
|
|
46
|
+
const totalLength = byteArrays.reduce(
|
|
47
|
+
(acc, arr) => acc + arr.length / Float32Array.BYTES_PER_ELEMENT,
|
|
48
|
+
0
|
|
49
|
+
);
|
|
50
|
+
const result = new Float32Array(totalLength);
|
|
51
|
+
let offset = 0;
|
|
52
|
+
for (const arr of byteArrays) {
|
|
53
|
+
const floats = new Float32Array(arr.buffer);
|
|
54
|
+
result.set(floats, offset);
|
|
55
|
+
offset += floats.length;
|
|
56
|
+
}
|
|
57
|
+
return result;
|
|
63
58
|
}
|
|
64
|
-
function playAudioBuffer(
|
|
65
|
-
const startAt = maybeStartAt != null ? maybeStartAt : context.currentTime;
|
|
66
|
-
const floats = base64ToArray(b64);
|
|
59
|
+
function playAudioBuffer(floats, context, startAt, sampleRate) {
|
|
67
60
|
const source = context.createBufferSource();
|
|
68
|
-
const buffer = context.createBuffer(1, floats.length,
|
|
61
|
+
const buffer = context.createBuffer(1, floats.length, sampleRate);
|
|
69
62
|
buffer.getChannelData(0).set(floats);
|
|
70
63
|
source.buffer = buffer;
|
|
71
64
|
source.connect(context.destination);
|
|
72
65
|
source.start(startAt);
|
|
73
|
-
|
|
74
|
-
|
|
66
|
+
return new Promise((resolve) => {
|
|
67
|
+
source.onended = () => {
|
|
68
|
+
resolve();
|
|
69
|
+
};
|
|
70
|
+
});
|
|
75
71
|
}
|
|
76
72
|
function createMessageHandlerForContextId(contextId, handler) {
|
|
77
73
|
return (event) => {
|
|
74
|
+
if (typeof event.data !== "string") {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
78
77
|
const message = JSON.parse(event.data);
|
|
79
78
|
if (message.context_id !== contextId) {
|
|
80
79
|
return;
|
|
@@ -85,7 +84,7 @@ function createMessageHandlerForContextId(contextId, handler) {
|
|
|
85
84
|
} else {
|
|
86
85
|
chunk = message.data;
|
|
87
86
|
}
|
|
88
|
-
handler({ chunk, message });
|
|
87
|
+
handler({ chunk, message: event.data });
|
|
89
88
|
};
|
|
90
89
|
}
|
|
91
90
|
function getSentinel() {
|
|
@@ -110,45 +109,11 @@ function getEmitteryCallbacks(emitter) {
|
|
|
110
109
|
events: emitter.events.bind(emitter)
|
|
111
110
|
};
|
|
112
111
|
}
|
|
113
|
-
function bufferToWav(sampleRate, channelBuffers) {
|
|
114
|
-
const totalSamples = channelBuffers[0].length * channelBuffers.length;
|
|
115
|
-
const buffer = new ArrayBuffer(44 + totalSamples * 2);
|
|
116
|
-
const view = new DataView(buffer);
|
|
117
|
-
const writeString = (view2, offset2, string) => {
|
|
118
|
-
for (let i = 0; i < string.length; i++) {
|
|
119
|
-
view2.setUint8(offset2 + i, string.charCodeAt(i));
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
writeString(view, 0, "RIFF");
|
|
123
|
-
view.setUint32(4, 36 + totalSamples * 2, true);
|
|
124
|
-
writeString(view, 8, "WAVE");
|
|
125
|
-
writeString(view, 12, "fmt ");
|
|
126
|
-
view.setUint32(16, 16, true);
|
|
127
|
-
view.setUint16(20, 1, true);
|
|
128
|
-
view.setUint16(22, channelBuffers.length, true);
|
|
129
|
-
view.setUint32(24, sampleRate, true);
|
|
130
|
-
view.setUint32(28, sampleRate * 4, true);
|
|
131
|
-
view.setUint16(32, channelBuffers.length * 2, true);
|
|
132
|
-
view.setUint16(34, 16, true);
|
|
133
|
-
writeString(view, 36, "data");
|
|
134
|
-
view.setUint32(40, totalSamples * 2, true);
|
|
135
|
-
let offset = 44;
|
|
136
|
-
for (let i = 0; i < channelBuffers[0].length; i++) {
|
|
137
|
-
for (let channel = 0; channel < channelBuffers.length; channel++) {
|
|
138
|
-
const s = Math.max(-1, Math.min(1, channelBuffers[channel][i]));
|
|
139
|
-
view.setInt16(offset, s < 0 ? s * 32768 : s * 32767, true);
|
|
140
|
-
offset += 2;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
return buffer;
|
|
144
|
-
}
|
|
145
112
|
// Annotate the CommonJS export names for ESM import in node:
|
|
146
113
|
0 && (module.exports = {
|
|
147
114
|
base64ToArray,
|
|
148
|
-
bufferToWav,
|
|
149
115
|
createMessageHandlerForContextId,
|
|
150
116
|
filterSentinel,
|
|
151
|
-
getBufferDuration,
|
|
152
117
|
getEmitteryCallbacks,
|
|
153
118
|
getSentinel,
|
|
154
119
|
isComplete,
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import emittery__default from 'emittery';
|
|
2
|
+
import { Chunk, Sentinel, EmitteryCallbacks } from '../types/index.cjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Convert base64-encoded audio buffer(s) to a Float32Array.
|
|
6
|
+
*
|
|
7
|
+
* @param b64 The base64-encoded audio buffer, or an array of base64-encoded
|
|
8
|
+
* audio buffers.
|
|
9
|
+
* @returns The audio buffer(s) as a Float32Array.
|
|
10
|
+
*/
|
|
11
|
+
declare function base64ToArray(b64: Chunk[]): Float32Array;
|
|
12
|
+
/**
|
|
13
|
+
* Schedule an audio buffer to play at a given time in the passed context.
|
|
14
|
+
*
|
|
15
|
+
* @param floats The audio buffer to play.
|
|
16
|
+
* @param context The audio context to play the buffer in.
|
|
17
|
+
* @param startAt The time to start playing the buffer at.
|
|
18
|
+
* @param sampleRate The sample rate of the audio.
|
|
19
|
+
* @returns A promise that resolves when the audio has finished playing.
|
|
20
|
+
*/
|
|
21
|
+
declare function playAudioBuffer(floats: Float32Array, context: AudioContext, startAt: number, sampleRate: number): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Unwraps a chunk of audio data from a message event and calls the
|
|
24
|
+
* handler with it if the context ID matches.
|
|
25
|
+
*
|
|
26
|
+
* @param contextId The context ID to listen for.
|
|
27
|
+
* @param handler The handler to call with the chunk of audio data.
|
|
28
|
+
* @returns A message event handler.
|
|
29
|
+
*/
|
|
30
|
+
declare function createMessageHandlerForContextId(contextId: string, handler: ({ chunk, message, }: {
|
|
31
|
+
chunk: Chunk;
|
|
32
|
+
message: string;
|
|
33
|
+
}) => void): (event: MessageEvent) => void;
|
|
34
|
+
/**
|
|
35
|
+
* Get a sentinel value that indicates the end of a stream.
|
|
36
|
+
* @returns A sentinel value to indicate the end of a stream.
|
|
37
|
+
*/
|
|
38
|
+
declare function getSentinel(): Sentinel;
|
|
39
|
+
/**
|
|
40
|
+
* Check if a chunk is a sentinel value (i.e. null).
|
|
41
|
+
*
|
|
42
|
+
* @param chunk
|
|
43
|
+
* @returns Whether the chunk is a sentinel value.
|
|
44
|
+
*/
|
|
45
|
+
declare function isSentinel(x: unknown): x is Sentinel;
|
|
46
|
+
/**
|
|
47
|
+
* Filter out null values from a collection.
|
|
48
|
+
*
|
|
49
|
+
* @param collection The collection to filter.
|
|
50
|
+
* @returns The collection with null values removed.
|
|
51
|
+
*/
|
|
52
|
+
declare function filterSentinel<T>(collection: T[]): Exclude<T, Sentinel>[];
|
|
53
|
+
/**
|
|
54
|
+
* Check if an array of chunks is complete by testing if the last chunk is a sentinel
|
|
55
|
+
* value (i.e. null).
|
|
56
|
+
* @param chunk
|
|
57
|
+
* @returns Whether the array of chunks is complete.
|
|
58
|
+
*/
|
|
59
|
+
declare function isComplete(chunks: Chunk[]): boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Get user-facing emitter callbacks for an Emittery instance.
|
|
62
|
+
* @param emitter The Emittery instance to get callbacks for.
|
|
63
|
+
* @returns User-facing emitter callbacks.
|
|
64
|
+
*/
|
|
65
|
+
declare function getEmitteryCallbacks<T>(emitter: emittery__default<T>): EmitteryCallbacks<T>;
|
|
66
|
+
|
|
67
|
+
export { base64ToArray, createMessageHandlerForContextId, filterSentinel, getEmitteryCallbacks, getSentinel, isComplete, isSentinel, playAudioBuffer };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import emittery__default from 'emittery';
|
|
2
|
+
import { Chunk, Sentinel, EmitteryCallbacks } from '../types/index.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Convert base64-encoded audio buffer(s) to a Float32Array.
|
|
6
|
+
*
|
|
7
|
+
* @param b64 The base64-encoded audio buffer, or an array of base64-encoded
|
|
8
|
+
* audio buffers.
|
|
9
|
+
* @returns The audio buffer(s) as a Float32Array.
|
|
10
|
+
*/
|
|
11
|
+
declare function base64ToArray(b64: Chunk[]): Float32Array;
|
|
12
|
+
/**
|
|
13
|
+
* Schedule an audio buffer to play at a given time in the passed context.
|
|
14
|
+
*
|
|
15
|
+
* @param floats The audio buffer to play.
|
|
16
|
+
* @param context The audio context to play the buffer in.
|
|
17
|
+
* @param startAt The time to start playing the buffer at.
|
|
18
|
+
* @param sampleRate The sample rate of the audio.
|
|
19
|
+
* @returns A promise that resolves when the audio has finished playing.
|
|
20
|
+
*/
|
|
21
|
+
declare function playAudioBuffer(floats: Float32Array, context: AudioContext, startAt: number, sampleRate: number): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Unwraps a chunk of audio data from a message event and calls the
|
|
24
|
+
* handler with it if the context ID matches.
|
|
25
|
+
*
|
|
26
|
+
* @param contextId The context ID to listen for.
|
|
27
|
+
* @param handler The handler to call with the chunk of audio data.
|
|
28
|
+
* @returns A message event handler.
|
|
29
|
+
*/
|
|
30
|
+
declare function createMessageHandlerForContextId(contextId: string, handler: ({ chunk, message, }: {
|
|
31
|
+
chunk: Chunk;
|
|
32
|
+
message: string;
|
|
33
|
+
}) => void): (event: MessageEvent) => void;
|
|
34
|
+
/**
|
|
35
|
+
* Get a sentinel value that indicates the end of a stream.
|
|
36
|
+
* @returns A sentinel value to indicate the end of a stream.
|
|
37
|
+
*/
|
|
38
|
+
declare function getSentinel(): Sentinel;
|
|
39
|
+
/**
|
|
40
|
+
* Check if a chunk is a sentinel value (i.e. null).
|
|
41
|
+
*
|
|
42
|
+
* @param chunk
|
|
43
|
+
* @returns Whether the chunk is a sentinel value.
|
|
44
|
+
*/
|
|
45
|
+
declare function isSentinel(x: unknown): x is Sentinel;
|
|
46
|
+
/**
|
|
47
|
+
* Filter out null values from a collection.
|
|
48
|
+
*
|
|
49
|
+
* @param collection The collection to filter.
|
|
50
|
+
* @returns The collection with null values removed.
|
|
51
|
+
*/
|
|
52
|
+
declare function filterSentinel<T>(collection: T[]): Exclude<T, Sentinel>[];
|
|
53
|
+
/**
|
|
54
|
+
* Check if an array of chunks is complete by testing if the last chunk is a sentinel
|
|
55
|
+
* value (i.e. null).
|
|
56
|
+
* @param chunk
|
|
57
|
+
* @returns Whether the array of chunks is complete.
|
|
58
|
+
*/
|
|
59
|
+
declare function isComplete(chunks: Chunk[]): boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Get user-facing emitter callbacks for an Emittery instance.
|
|
62
|
+
* @param emitter The Emittery instance to get callbacks for.
|
|
63
|
+
* @returns User-facing emitter callbacks.
|
|
64
|
+
*/
|
|
65
|
+
declare function getEmitteryCallbacks<T>(emitter: emittery__default<T>): EmitteryCallbacks<T>;
|
|
66
|
+
|
|
67
|
+
export { base64ToArray, createMessageHandlerForContextId, filterSentinel, getEmitteryCallbacks, getSentinel, isComplete, isSentinel, playAudioBuffer };
|
|
@@ -1,23 +1,18 @@
|
|
|
1
1
|
import {
|
|
2
2
|
base64ToArray,
|
|
3
|
-
bufferToWav,
|
|
4
3
|
createMessageHandlerForContextId,
|
|
5
4
|
filterSentinel,
|
|
6
|
-
getBufferDuration,
|
|
7
5
|
getEmitteryCallbacks,
|
|
8
6
|
getSentinel,
|
|
9
7
|
isComplete,
|
|
10
8
|
isSentinel,
|
|
11
9
|
playAudioBuffer
|
|
12
|
-
} from "../chunk-
|
|
13
|
-
import "../chunk-
|
|
14
|
-
import "../chunk-R4P7LWVZ.js";
|
|
10
|
+
} from "../chunk-RO7TY474.js";
|
|
11
|
+
import "../chunk-WIFMLPT5.js";
|
|
15
12
|
export {
|
|
16
13
|
base64ToArray,
|
|
17
|
-
bufferToWav,
|
|
18
14
|
createMessageHandlerForContextId,
|
|
19
15
|
filterSentinel,
|
|
20
|
-
getBufferDuration,
|
|
21
16
|
getEmitteryCallbacks,
|
|
22
17
|
getSentinel,
|
|
23
18
|
isComplete,
|