@cartesia/cartesia-js 0.0.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.
- package/.turbo/turbo-build.log +47 -0
- package/CHANGELOG.md +7 -0
- package/README.md +38 -0
- package/dist/audio/index.d.mts +4 -0
- package/dist/audio/index.d.ts +4 -0
- package/dist/audio/index.js +354 -0
- package/dist/audio/index.mjs +9 -0
- package/dist/audio/utils.d.mts +4 -0
- package/dist/audio/utils.d.ts +4 -0
- package/dist/audio/utils.js +113 -0
- package/dist/audio/utils.mjs +21 -0
- package/dist/chunk-35HX6ML3.mjs +39 -0
- package/dist/chunk-5RMUZJV7.mjs +240 -0
- package/dist/chunk-BTFHUVNH.mjs +71 -0
- package/dist/chunk-ERFCRIWU.mjs +18 -0
- package/dist/index-Ds4LDkmk.d.ts +127 -0
- package/dist/index-Dt9A_pEb.d.mts +127 -0
- package/dist/lib/client.d.mts +9 -0
- package/dist/lib/client.d.ts +9 -0
- package/dist/lib/client.js +43 -0
- package/dist/lib/client.mjs +7 -0
- package/dist/lib/constants.d.mts +5 -0
- package/dist/lib/constants.d.ts +5 -0
- package/dist/lib/constants.js +38 -0
- package/dist/lib/constants.mjs +10 -0
- package/dist/lib/index.d.mts +11 -0
- package/dist/lib/index.d.ts +11 -0
- package/dist/lib/index.js +368 -0
- package/dist/lib/index.mjs +19 -0
- package/dist/react/index.d.mts +22 -0
- package/dist/react/index.d.ts +22 -0
- package/dist/react/index.js +431 -0
- package/dist/react/index.mjs +82 -0
- package/dist/types/index.d.mts +6 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.js +18 -0
- package/dist/types/index.mjs +0 -0
- package/index.ts +3 -0
- package/package.json +50 -0
- package/src/audio/index.ts +242 -0
- package/src/audio/utils.ts +138 -0
- package/src/lib/client.ts +17 -0
- package/src/lib/constants.ts +6 -0
- package/src/lib/index.ts +13 -0
- package/src/react/index.ts +91 -0
- package/src/types/index.ts +4 -0
- package/tsconfig.json +3 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
|
|
2
|
+
[0m[2m[35m$[0m [2m[1mtsup src/ --format cjs,esm --dts --clean[0m
|
|
3
|
+
[34mCLI[39m Building entry: src/audio/index.ts, src/audio/utils.ts, src/types/index.ts, src/lib/client.ts, src/lib/constants.ts, src/lib/index.ts, src/react/index.ts
|
|
4
|
+
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
5
|
+
[34mCLI[39m tsup v8.0.2
|
|
6
|
+
[34mCLI[39m Target: es6
|
|
7
|
+
[34mCLI[39m Cleaning output folder
|
|
8
|
+
[34mCJS[39m Build start
|
|
9
|
+
[34mESM[39m Build start
|
|
10
|
+
[32mCJS[39m [1mdist/types/index.js [22m[32m764.00 B[39m
|
|
11
|
+
[32mCJS[39m [1mdist/lib/index.js [22m[32m11.67 KB[39m
|
|
12
|
+
[32mCJS[39m [1mdist/lib/constants.js [22m[32m1.33 KB[39m
|
|
13
|
+
[32mCJS[39m [1mdist/audio/utils.js [22m[32m3.71 KB[39m
|
|
14
|
+
[32mCJS[39m [1mdist/lib/client.js [22m[32m1.40 KB[39m
|
|
15
|
+
[32mCJS[39m [1mdist/react/index.js [22m[32m13.52 KB[39m
|
|
16
|
+
[32mCJS[39m [1mdist/audio/index.js [22m[32m11.41 KB[39m
|
|
17
|
+
[32mCJS[39m ⚡️ Build success in 44ms
|
|
18
|
+
[32mESM[39m [1mdist/types/index.mjs [22m[32m0 B[39m
|
|
19
|
+
[32mESM[39m [1mdist/audio/utils.mjs [22m[32m391.00 B[39m
|
|
20
|
+
[32mESM[39m [1mdist/lib/client.mjs [22m[32m103.00 B[39m
|
|
21
|
+
[32mESM[39m [1mdist/lib/constants.mjs [22m[32m155.00 B[39m
|
|
22
|
+
[32mESM[39m [1mdist/lib/index.mjs [22m[32m355.00 B[39m
|
|
23
|
+
[32mESM[39m [1mdist/chunk-5RMUZJV7.mjs [22m[32m7.01 KB[39m
|
|
24
|
+
[32mESM[39m [1mdist/chunk-BTFHUVNH.mjs [22m[32m1.79 KB[39m
|
|
25
|
+
[32mESM[39m [1mdist/react/index.mjs [22m[32m2.08 KB[39m
|
|
26
|
+
[32mESM[39m [1mdist/audio/index.mjs [22m[32m192.00 B[39m
|
|
27
|
+
[32mESM[39m [1mdist/chunk-ERFCRIWU.mjs [22m[32m386.00 B[39m
|
|
28
|
+
[32mESM[39m [1mdist/chunk-35HX6ML3.mjs [22m[32m1.32 KB[39m
|
|
29
|
+
[32mESM[39m ⚡️ Build success in 44ms
|
|
30
|
+
[34mDTS[39m Build start
|
|
31
|
+
[32mDTS[39m ⚡️ Build success in 3158ms
|
|
32
|
+
[32mDTS[39m [1mdist/lib/constants.d.ts [22m[32m211.00 B[39m
|
|
33
|
+
[32mDTS[39m [1mdist/lib/index.d.ts [22m[32m302.00 B[39m
|
|
34
|
+
[32mDTS[39m [1mdist/react/index.d.ts [22m[32m587.00 B[39m
|
|
35
|
+
[32mDTS[39m [1mdist/lib/client.d.ts [22m[32m180.00 B[39m
|
|
36
|
+
[32mDTS[39m [1mdist/types/index.d.ts [22m[32m103.00 B[39m
|
|
37
|
+
[32mDTS[39m [1mdist/audio/utils.d.ts [22m[32m306.00 B[39m
|
|
38
|
+
[32mDTS[39m [1mdist/audio/index.d.ts [22m[32m161.00 B[39m
|
|
39
|
+
[32mDTS[39m [1mdist/index-Ds4LDkmk.d.ts [22m[32m5.28 KB[39m
|
|
40
|
+
[32mDTS[39m [1mdist/lib/constants.d.mts [22m[32m211.00 B[39m
|
|
41
|
+
[32mDTS[39m [1mdist/lib/index.d.mts [22m[32m305.00 B[39m
|
|
42
|
+
[32mDTS[39m [1mdist/react/index.d.mts [22m[32m590.00 B[39m
|
|
43
|
+
[32mDTS[39m [1mdist/lib/client.d.mts [22m[32m181.00 B[39m
|
|
44
|
+
[32mDTS[39m [1mdist/types/index.d.mts [22m[32m103.00 B[39m
|
|
45
|
+
[32mDTS[39m [1mdist/audio/utils.d.mts [22m[32m309.00 B[39m
|
|
46
|
+
[32mDTS[39m [1mdist/audio/index.d.mts [22m[32m164.00 B[39m
|
|
47
|
+
[32mDTS[39m [1mdist/index-Dt9A_pEb.d.mts [22m[32m5.29 KB[39m
|
package/CHANGELOG.md
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# JavaScript Client for Cartesia
|
|
2
|
+
|
|
3
|
+
Usage:
|
|
4
|
+
|
|
5
|
+
```javascript
|
|
6
|
+
import Cartesia from "@cartesia/cartesia-js";
|
|
7
|
+
|
|
8
|
+
const cartesia = new Cartesia();
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
await cartesia.audio.connect();
|
|
12
|
+
} catch (error) {
|
|
13
|
+
console.error(`Failed to connect to Cartesia: ${error}`);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const stream = await cartesia.audio.stream({
|
|
17
|
+
model: "echo_tts_v0.0.6",
|
|
18
|
+
options: {
|
|
19
|
+
transcript: "Hello, world!",
|
|
20
|
+
chunk_time: 0.1,
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
console.log(`Created stream ${stream.id}.`);
|
|
25
|
+
|
|
26
|
+
stream.on("chunk", ({ chunk, chunks }) => {
|
|
27
|
+
console.log("Received chunk:", chunk);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
stream.on("message", ({ message }) => {
|
|
31
|
+
// Raw message.
|
|
32
|
+
console.log("Received message:", message);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
console.log("Playing stream...");
|
|
36
|
+
await stream.play();
|
|
37
|
+
console.log("Done playing.");
|
|
38
|
+
```
|
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __knownSymbol = (name, symbol) => {
|
|
9
|
+
return (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
|
|
10
|
+
};
|
|
11
|
+
var __export = (target, all) => {
|
|
12
|
+
for (var name in all)
|
|
13
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
+
};
|
|
15
|
+
var __copyProps = (to, from, except, desc) => {
|
|
16
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
17
|
+
for (let key of __getOwnPropNames(from))
|
|
18
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
19
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
20
|
+
}
|
|
21
|
+
return to;
|
|
22
|
+
};
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
24
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
29
|
+
mod
|
|
30
|
+
));
|
|
31
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
32
|
+
var __async = (__this, __arguments, generator) => {
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
var fulfilled = (value) => {
|
|
35
|
+
try {
|
|
36
|
+
step(generator.next(value));
|
|
37
|
+
} catch (e) {
|
|
38
|
+
reject(e);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
var rejected = (value) => {
|
|
42
|
+
try {
|
|
43
|
+
step(generator.throw(value));
|
|
44
|
+
} catch (e) {
|
|
45
|
+
reject(e);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
49
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
var __forAwait = (obj, it, method) => (it = obj[__knownSymbol("asyncIterator")]) ? it.call(obj) : (obj = obj[__knownSymbol("iterator")](), it = {}, method = (key, fn) => (fn = obj[key]) && (it[key] = (arg) => new Promise((yes, no, done) => (arg = fn.call(obj, arg), done = arg.done, Promise.resolve(arg.value).then((value) => yes({ value, done }), no)))), method("next"), method("return"), it);
|
|
53
|
+
|
|
54
|
+
// src/audio/index.ts
|
|
55
|
+
var audio_exports = {};
|
|
56
|
+
__export(audio_exports, {
|
|
57
|
+
default: () => audio_default
|
|
58
|
+
});
|
|
59
|
+
module.exports = __toCommonJS(audio_exports);
|
|
60
|
+
var import_emittery = __toESM(require("emittery"));
|
|
61
|
+
var import_human_id = require("human-id");
|
|
62
|
+
|
|
63
|
+
// src/lib/constants.ts
|
|
64
|
+
var BASE_URL = "https://api.cartesia.ai/v0";
|
|
65
|
+
var SAMPLE_RATE = 44100;
|
|
66
|
+
var constructWebsocketUrl = (baseUrl) => {
|
|
67
|
+
return new URL(`${baseUrl.replace(/^http/, "ws")}/ws`);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// src/lib/client.ts
|
|
71
|
+
var Client = class {
|
|
72
|
+
constructor(options = {}) {
|
|
73
|
+
if (!(options.apiKey || process.env.CARTESIA_API_KEY)) {
|
|
74
|
+
throw new Error("Missing Cartesia API key.");
|
|
75
|
+
}
|
|
76
|
+
this.apiKey = options.apiKey || process.env.CARTESIA_API_KEY;
|
|
77
|
+
this.baseUrl = options.baseUrl || BASE_URL;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// src/audio/utils.ts
|
|
82
|
+
var import_base64_js = __toESM(require("base64-js"));
|
|
83
|
+
function getBufferDuration(b64) {
|
|
84
|
+
const floats = base64ToArray(b64);
|
|
85
|
+
return floats.length / SAMPLE_RATE;
|
|
86
|
+
}
|
|
87
|
+
function base64ToArray(b64) {
|
|
88
|
+
return filterSentinel(b64).reduce((acc, b) => {
|
|
89
|
+
const floats = new Float32Array(import_base64_js.default.toByteArray(b).buffer);
|
|
90
|
+
const newAcc = new Float32Array(acc.length + floats.length);
|
|
91
|
+
newAcc.set(acc, 0);
|
|
92
|
+
newAcc.set(floats, acc.length);
|
|
93
|
+
return newAcc;
|
|
94
|
+
}, new Float32Array(0));
|
|
95
|
+
}
|
|
96
|
+
function playAudioBuffer(b64, context, maybeStartAt = null, onEnded = null) {
|
|
97
|
+
const startAt = maybeStartAt != null ? maybeStartAt : context.currentTime;
|
|
98
|
+
const floats = base64ToArray(b64);
|
|
99
|
+
const source = context.createBufferSource();
|
|
100
|
+
const buffer = context.createBuffer(1, floats.length, SAMPLE_RATE);
|
|
101
|
+
buffer.getChannelData(0).set(floats);
|
|
102
|
+
source.buffer = buffer;
|
|
103
|
+
source.connect(context.destination);
|
|
104
|
+
source.start(startAt);
|
|
105
|
+
source.onended = onEnded;
|
|
106
|
+
return buffer.duration;
|
|
107
|
+
}
|
|
108
|
+
function createMessageHandlerForContextId(contextId, handler) {
|
|
109
|
+
return (event) => {
|
|
110
|
+
const message = JSON.parse(event.data);
|
|
111
|
+
if (message.context_id !== contextId) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
let chunk;
|
|
115
|
+
if (message.done) {
|
|
116
|
+
chunk = getSentinel();
|
|
117
|
+
} else {
|
|
118
|
+
chunk = message.data;
|
|
119
|
+
}
|
|
120
|
+
handler({ chunk, message });
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
function getSentinel() {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
function isSentinel(x) {
|
|
127
|
+
return x === getSentinel();
|
|
128
|
+
}
|
|
129
|
+
function filterSentinel(collection) {
|
|
130
|
+
return collection.filter(
|
|
131
|
+
(x) => !isSentinel(x)
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
function isComplete(chunks) {
|
|
135
|
+
return isSentinel(chunks[chunks.length - 1]);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// src/audio/index.ts
|
|
139
|
+
var audio_default = class extends Client {
|
|
140
|
+
constructor() {
|
|
141
|
+
super(...arguments);
|
|
142
|
+
this.isConnected = false;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Stream audio from a model.
|
|
146
|
+
*
|
|
147
|
+
* @param inputs - Stream options. Includes a `model` key and some `parameters`, which
|
|
148
|
+
* are model-specific and can be found in the model's documentation.
|
|
149
|
+
* @param options - Options for the stream.
|
|
150
|
+
* @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
|
|
151
|
+
* If `0`, the stream will not time out.
|
|
152
|
+
* @returns An object with a method `play` of type `(bufferDuration: number) => Promise<void>`
|
|
153
|
+
* that plays the audio as it arrives, with `bufferDuration` seconds of audio buffered before
|
|
154
|
+
* starting playback.
|
|
155
|
+
*/
|
|
156
|
+
stream(inputs, { timeout = 0 } = {}) {
|
|
157
|
+
var _a, _b, _c, _d;
|
|
158
|
+
if (!this.isConnected) {
|
|
159
|
+
throw new Error("Not connected to WebSocket. Call .connect() first.");
|
|
160
|
+
}
|
|
161
|
+
const contextId = this.generateId();
|
|
162
|
+
(_a = this.socket) == null ? void 0 : _a.send(
|
|
163
|
+
JSON.stringify({
|
|
164
|
+
data: inputs,
|
|
165
|
+
context_id: contextId
|
|
166
|
+
})
|
|
167
|
+
);
|
|
168
|
+
const streamCompleteController = new AbortController();
|
|
169
|
+
let timeoutId = null;
|
|
170
|
+
if (timeout > 0) {
|
|
171
|
+
timeoutId = setTimeout(streamCompleteController.abort, timeout);
|
|
172
|
+
}
|
|
173
|
+
const chunks = [];
|
|
174
|
+
const emitter = new import_emittery.default();
|
|
175
|
+
const handleMessage = createMessageHandlerForContextId(
|
|
176
|
+
contextId,
|
|
177
|
+
(_0) => __async(this, [_0], function* ({ chunk, message }) {
|
|
178
|
+
chunks.push(chunk);
|
|
179
|
+
yield emitter.emit("chunk", {
|
|
180
|
+
chunk,
|
|
181
|
+
chunks
|
|
182
|
+
});
|
|
183
|
+
yield emitter.emit("message", message);
|
|
184
|
+
if (isSentinel(chunk)) {
|
|
185
|
+
streamCompleteController.abort();
|
|
186
|
+
} else if (timeoutId) {
|
|
187
|
+
clearTimeout(timeoutId);
|
|
188
|
+
timeoutId = setTimeout(streamCompleteController.abort, timeout);
|
|
189
|
+
}
|
|
190
|
+
})
|
|
191
|
+
);
|
|
192
|
+
(_b = this.socket) == null ? void 0 : _b.addEventListener("message", handleMessage, {
|
|
193
|
+
signal: streamCompleteController.signal
|
|
194
|
+
});
|
|
195
|
+
(_c = this.socket) == null ? void 0 : _c.addEventListener("close", streamCompleteController.abort, {
|
|
196
|
+
once: true
|
|
197
|
+
});
|
|
198
|
+
(_d = this.socket) == null ? void 0 : _d.addEventListener("error", streamCompleteController.abort, {
|
|
199
|
+
once: true
|
|
200
|
+
});
|
|
201
|
+
streamCompleteController.signal.addEventListener("abort", () => {
|
|
202
|
+
emitter.clearListeners();
|
|
203
|
+
});
|
|
204
|
+
const play = (_0) => __async(this, [_0], function* ({ bufferDuration }) {
|
|
205
|
+
const context = new AudioContext({
|
|
206
|
+
sampleRate: SAMPLE_RATE
|
|
207
|
+
});
|
|
208
|
+
let startNextPlaybackAt = 0;
|
|
209
|
+
const playLatestChunk = (chunk) => {
|
|
210
|
+
if (isSentinel(chunk)) {
|
|
211
|
+
return true;
|
|
212
|
+
}
|
|
213
|
+
startNextPlaybackAt = playAudioBuffer([chunk], context, startNextPlaybackAt) + Math.max(context.currentTime, startNextPlaybackAt);
|
|
214
|
+
return false;
|
|
215
|
+
};
|
|
216
|
+
const playChunks = (chunks2) => {
|
|
217
|
+
startNextPlaybackAt += playAudioBuffer(
|
|
218
|
+
chunks2,
|
|
219
|
+
context,
|
|
220
|
+
startNextPlaybackAt
|
|
221
|
+
);
|
|
222
|
+
if (isComplete(chunks2)) {
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
const tryStart = (chunks2) => __async(this, null, function* () {
|
|
227
|
+
startNextPlaybackAt = context.currentTime;
|
|
228
|
+
if (isComplete(chunks2) || streamCompleteController.signal.aborted) {
|
|
229
|
+
playChunks(chunks2);
|
|
230
|
+
return true;
|
|
231
|
+
}
|
|
232
|
+
if (getBufferDuration(chunks2) > bufferDuration) {
|
|
233
|
+
playChunks(chunks2);
|
|
234
|
+
try {
|
|
235
|
+
for (var iter2 = __forAwait(emitter.events("chunk")), more2, temp2, error2; more2 = !(temp2 = yield iter2.next()).done; more2 = false) {
|
|
236
|
+
const { chunk } = temp2.value;
|
|
237
|
+
if (playLatestChunk(chunk)) {
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
} catch (temp2) {
|
|
242
|
+
error2 = [temp2];
|
|
243
|
+
} finally {
|
|
244
|
+
try {
|
|
245
|
+
more2 && (temp2 = iter2.return) && (yield temp2.call(iter2));
|
|
246
|
+
} finally {
|
|
247
|
+
if (error2)
|
|
248
|
+
throw error2[0];
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return true;
|
|
252
|
+
}
|
|
253
|
+
return false;
|
|
254
|
+
});
|
|
255
|
+
if (!(yield tryStart(chunks))) {
|
|
256
|
+
try {
|
|
257
|
+
for (var iter = __forAwait(emitter.events("chunk")), more, temp, error; more = !(temp = yield iter.next()).done; more = false) {
|
|
258
|
+
const { chunks: chunks2 } = temp.value;
|
|
259
|
+
if (yield tryStart(chunks2)) {
|
|
260
|
+
break;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
} catch (temp) {
|
|
264
|
+
error = [temp];
|
|
265
|
+
} finally {
|
|
266
|
+
try {
|
|
267
|
+
more && (temp = iter.return) && (yield temp.call(iter));
|
|
268
|
+
} finally {
|
|
269
|
+
if (error)
|
|
270
|
+
throw error[0];
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
return {
|
|
276
|
+
play,
|
|
277
|
+
on: emitter.on.bind(emitter),
|
|
278
|
+
off: emitter.off.bind(emitter),
|
|
279
|
+
once: emitter.once.bind(emitter),
|
|
280
|
+
events: emitter.events.bind(emitter)
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Generate a unique ID suitable for a streaming context.
|
|
285
|
+
*
|
|
286
|
+
* Not suitable for security purposes or as a primary key, since
|
|
287
|
+
* it lacks the amount of entropy required for those use cases.
|
|
288
|
+
*
|
|
289
|
+
* @returns A unique ID.
|
|
290
|
+
*/
|
|
291
|
+
generateId() {
|
|
292
|
+
return (0, import_human_id.humanId)({
|
|
293
|
+
separator: "-",
|
|
294
|
+
capitalize: false
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Authenticate and connect to a Cartesia streaming WebSocket.
|
|
299
|
+
*
|
|
300
|
+
* @returns A promise that resolves when the WebSocket is connected.
|
|
301
|
+
* @throws {Error} If the WebSocket fails to connect.
|
|
302
|
+
*/
|
|
303
|
+
connect() {
|
|
304
|
+
const url = constructWebsocketUrl(this.baseUrl);
|
|
305
|
+
url.searchParams.set("api_key", this.apiKey);
|
|
306
|
+
this.socket = new WebSocket(url);
|
|
307
|
+
this.socket.onopen = () => {
|
|
308
|
+
this.isConnected = true;
|
|
309
|
+
};
|
|
310
|
+
this.socket.onclose = () => {
|
|
311
|
+
this.isConnected = false;
|
|
312
|
+
};
|
|
313
|
+
return new Promise((resolve, reject) => {
|
|
314
|
+
var _a, _b, _c;
|
|
315
|
+
(_a = this.socket) == null ? void 0 : _a.addEventListener(
|
|
316
|
+
"open",
|
|
317
|
+
() => {
|
|
318
|
+
resolve();
|
|
319
|
+
},
|
|
320
|
+
{
|
|
321
|
+
once: true
|
|
322
|
+
}
|
|
323
|
+
);
|
|
324
|
+
const aborter = new AbortController();
|
|
325
|
+
(_b = this.socket) == null ? void 0 : _b.addEventListener(
|
|
326
|
+
"error",
|
|
327
|
+
() => {
|
|
328
|
+
aborter.abort();
|
|
329
|
+
reject(new Error("WebSocket failed to connect."));
|
|
330
|
+
},
|
|
331
|
+
{
|
|
332
|
+
signal: aborter.signal
|
|
333
|
+
}
|
|
334
|
+
);
|
|
335
|
+
(_c = this.socket) == null ? void 0 : _c.addEventListener(
|
|
336
|
+
"close",
|
|
337
|
+
() => {
|
|
338
|
+
aborter.abort();
|
|
339
|
+
reject(new Error("WebSocket closed before it could connect."));
|
|
340
|
+
},
|
|
341
|
+
{
|
|
342
|
+
signal: aborter.signal
|
|
343
|
+
}
|
|
344
|
+
);
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Disconnect from the Cartesia streaming WebSocket.
|
|
349
|
+
*/
|
|
350
|
+
disconnect() {
|
|
351
|
+
var _a;
|
|
352
|
+
(_a = this.socket) == null ? void 0 : _a.close();
|
|
353
|
+
}
|
|
354
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { a as Sentinel, b as base64ToArray, c as createMessageHandlerForContextId, f as filterSentinel, g as getBufferDuration, d as getSentinel, h as isComplete, i as isSentinel, p as playAudioBuffer } from '../index-Dt9A_pEb.mjs';
|
|
2
|
+
import 'emittery';
|
|
3
|
+
import '../lib/client.mjs';
|
|
4
|
+
import '../types/index.mjs';
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { a as Sentinel, b as base64ToArray, c as createMessageHandlerForContextId, f as filterSentinel, g as getBufferDuration, d as getSentinel, h as isComplete, i as isSentinel, p as playAudioBuffer } from '../index-Ds4LDkmk.js';
|
|
2
|
+
import 'emittery';
|
|
3
|
+
import '../lib/client.js';
|
|
4
|
+
import '../types/index.js';
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/audio/utils.ts
|
|
31
|
+
var utils_exports = {};
|
|
32
|
+
__export(utils_exports, {
|
|
33
|
+
base64ToArray: () => base64ToArray,
|
|
34
|
+
createMessageHandlerForContextId: () => createMessageHandlerForContextId,
|
|
35
|
+
filterSentinel: () => filterSentinel,
|
|
36
|
+
getBufferDuration: () => getBufferDuration,
|
|
37
|
+
getSentinel: () => getSentinel,
|
|
38
|
+
isComplete: () => isComplete,
|
|
39
|
+
isSentinel: () => isSentinel,
|
|
40
|
+
playAudioBuffer: () => playAudioBuffer
|
|
41
|
+
});
|
|
42
|
+
module.exports = __toCommonJS(utils_exports);
|
|
43
|
+
var import_base64_js = __toESM(require("base64-js"));
|
|
44
|
+
|
|
45
|
+
// src/lib/constants.ts
|
|
46
|
+
var SAMPLE_RATE = 44100;
|
|
47
|
+
|
|
48
|
+
// src/audio/utils.ts
|
|
49
|
+
function getBufferDuration(b64) {
|
|
50
|
+
const floats = base64ToArray(b64);
|
|
51
|
+
return floats.length / SAMPLE_RATE;
|
|
52
|
+
}
|
|
53
|
+
function base64ToArray(b64) {
|
|
54
|
+
return filterSentinel(b64).reduce((acc, b) => {
|
|
55
|
+
const floats = new Float32Array(import_base64_js.default.toByteArray(b).buffer);
|
|
56
|
+
const newAcc = new Float32Array(acc.length + floats.length);
|
|
57
|
+
newAcc.set(acc, 0);
|
|
58
|
+
newAcc.set(floats, acc.length);
|
|
59
|
+
return newAcc;
|
|
60
|
+
}, new Float32Array(0));
|
|
61
|
+
}
|
|
62
|
+
function playAudioBuffer(b64, context, maybeStartAt = null, onEnded = null) {
|
|
63
|
+
const startAt = maybeStartAt != null ? maybeStartAt : context.currentTime;
|
|
64
|
+
const floats = base64ToArray(b64);
|
|
65
|
+
const source = context.createBufferSource();
|
|
66
|
+
const buffer = context.createBuffer(1, floats.length, SAMPLE_RATE);
|
|
67
|
+
buffer.getChannelData(0).set(floats);
|
|
68
|
+
source.buffer = buffer;
|
|
69
|
+
source.connect(context.destination);
|
|
70
|
+
source.start(startAt);
|
|
71
|
+
source.onended = onEnded;
|
|
72
|
+
return buffer.duration;
|
|
73
|
+
}
|
|
74
|
+
function createMessageHandlerForContextId(contextId, handler) {
|
|
75
|
+
return (event) => {
|
|
76
|
+
const message = JSON.parse(event.data);
|
|
77
|
+
if (message.context_id !== contextId) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
let chunk;
|
|
81
|
+
if (message.done) {
|
|
82
|
+
chunk = getSentinel();
|
|
83
|
+
} else {
|
|
84
|
+
chunk = message.data;
|
|
85
|
+
}
|
|
86
|
+
handler({ chunk, message });
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function getSentinel() {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
function isSentinel(x) {
|
|
93
|
+
return x === getSentinel();
|
|
94
|
+
}
|
|
95
|
+
function filterSentinel(collection) {
|
|
96
|
+
return collection.filter(
|
|
97
|
+
(x) => !isSentinel(x)
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
function isComplete(chunks) {
|
|
101
|
+
return isSentinel(chunks[chunks.length - 1]);
|
|
102
|
+
}
|
|
103
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
104
|
+
0 && (module.exports = {
|
|
105
|
+
base64ToArray,
|
|
106
|
+
createMessageHandlerForContextId,
|
|
107
|
+
filterSentinel,
|
|
108
|
+
getBufferDuration,
|
|
109
|
+
getSentinel,
|
|
110
|
+
isComplete,
|
|
111
|
+
isSentinel,
|
|
112
|
+
playAudioBuffer
|
|
113
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import {
|
|
2
|
+
base64ToArray,
|
|
3
|
+
createMessageHandlerForContextId,
|
|
4
|
+
filterSentinel,
|
|
5
|
+
getBufferDuration,
|
|
6
|
+
getSentinel,
|
|
7
|
+
isComplete,
|
|
8
|
+
isSentinel,
|
|
9
|
+
playAudioBuffer
|
|
10
|
+
} from "../chunk-BTFHUVNH.mjs";
|
|
11
|
+
import "../chunk-35HX6ML3.mjs";
|
|
12
|
+
export {
|
|
13
|
+
base64ToArray,
|
|
14
|
+
createMessageHandlerForContextId,
|
|
15
|
+
filterSentinel,
|
|
16
|
+
getBufferDuration,
|
|
17
|
+
getSentinel,
|
|
18
|
+
isComplete,
|
|
19
|
+
isSentinel,
|
|
20
|
+
playAudioBuffer
|
|
21
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
var __knownSymbol = (name, symbol) => {
|
|
2
|
+
return (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
|
|
3
|
+
};
|
|
4
|
+
var __async = (__this, __arguments, generator) => {
|
|
5
|
+
return new Promise((resolve, reject) => {
|
|
6
|
+
var fulfilled = (value) => {
|
|
7
|
+
try {
|
|
8
|
+
step(generator.next(value));
|
|
9
|
+
} catch (e) {
|
|
10
|
+
reject(e);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
var rejected = (value) => {
|
|
14
|
+
try {
|
|
15
|
+
step(generator.throw(value));
|
|
16
|
+
} catch (e) {
|
|
17
|
+
reject(e);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
21
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
var __forAwait = (obj, it, method) => (it = obj[__knownSymbol("asyncIterator")]) ? it.call(obj) : (obj = obj[__knownSymbol("iterator")](), it = {}, method = (key, fn) => (fn = obj[key]) && (it[key] = (arg) => new Promise((yes, no, done) => (arg = fn.call(obj, arg), done = arg.done, Promise.resolve(arg.value).then((value) => yes({ value, done }), no)))), method("next"), method("return"), it);
|
|
25
|
+
|
|
26
|
+
// src/lib/constants.ts
|
|
27
|
+
var BASE_URL = "https://api.cartesia.ai/v0";
|
|
28
|
+
var SAMPLE_RATE = 44100;
|
|
29
|
+
var constructWebsocketUrl = (baseUrl) => {
|
|
30
|
+
return new URL(`${baseUrl.replace(/^http/, "ws")}/ws`);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export {
|
|
34
|
+
__async,
|
|
35
|
+
__forAwait,
|
|
36
|
+
BASE_URL,
|
|
37
|
+
SAMPLE_RATE,
|
|
38
|
+
constructWebsocketUrl
|
|
39
|
+
};
|