@cartesia/cartesia-js 0.0.2 → 0.0.4-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +45 -33
- package/CHANGELOG.md +12 -0
- package/dist/audio/index.cjs +404 -0
- package/dist/audio/index.d.cts +5 -0
- package/dist/audio/index.d.ts +1 -1
- package/dist/audio/index.js +9 -395
- package/dist/audio/utils.cjs +157 -0
- package/dist/audio/{utils.d.mts → utils.d.cts} +3 -3
- package/dist/audio/utils.d.ts +1 -1
- package/dist/audio/utils.js +16 -147
- package/dist/{chunk-3CYTAFLF.mjs → chunk-4MHF74A7.js} +15 -5
- package/dist/{chunk-FRIBQZPN.mjs → chunk-5TSWLYOW.js} +1 -1
- package/dist/{chunk-XSFPHPPG.mjs → chunk-MJIFZWHS.js} +1 -1
- package/dist/chunk-OVI3W3GG.js +12 -0
- package/dist/{chunk-HNLIBHEN.mjs → chunk-R4P7LWVZ.js} +1 -11
- package/dist/{lib/index.mjs → chunk-S6A27RQL.js} +3 -4
- package/dist/chunk-XPIMIAAE.js +17 -0
- package/dist/{index-DSBmfK9-.d.mts → index-C2_3XFxn.d.cts} +9 -4
- package/dist/{index-qwAyxV5I.d.ts → index-DgwnZezj.d.ts} +8 -3
- package/dist/index.cjs +419 -0
- package/dist/index.d.cts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +13 -0
- package/dist/lib/client.cjs +43 -0
- package/dist/lib/{client.d.mts → client.d.cts} +1 -1
- package/dist/lib/client.js +7 -42
- package/dist/lib/constants.cjs +38 -0
- package/dist/lib/constants.js +8 -35
- package/dist/lib/index.cjs +418 -0
- package/dist/lib/{index.d.mts → index.d.cts} +3 -3
- package/dist/lib/index.d.ts +1 -1
- package/dist/lib/index.js +10 -409
- package/dist/react/index.cjs +597 -0
- package/dist/react/{index.d.mts → index.d.cts} +4 -3
- package/dist/react/index.d.ts +2 -1
- package/dist/react/index.js +71 -455
- package/dist/react/utils.cjs +57 -0
- package/dist/react/utils.d.cts +7 -0
- package/dist/react/utils.d.ts +7 -0
- package/dist/react/utils.js +7 -0
- package/dist/types/index.cjs +18 -0
- package/dist/types/index.js +1 -18
- package/package.json +10 -7
- package/src/audio/index.ts +15 -0
- package/src/index.ts +3 -0
- package/src/react/index.ts +48 -10
- package/src/react/utils.ts +11 -0
- package/dist/audio/index.d.mts +0 -5
- package/dist/audio/index.mjs +0 -9
- package/dist/audio/utils.mjs +0 -25
- package/dist/lib/client.mjs +0 -7
- package/dist/lib/constants.mjs +0 -10
- package/dist/react/index.mjs +0 -130
- package/index.ts +0 -3
- /package/dist/{types/index.mjs → chunk-FXPGR372.js} +0 -0
- /package/dist/lib/{constants.d.mts → constants.d.cts} +0 -0
- /package/dist/types/{index.d.mts → index.d.cts} +0 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,45 +1,57 @@
|
|
|
1
1
|
$ tsup src/ --format cjs,esm --dts
|
|
2
|
-
[34mCLI[39m Building entry: src/audio/index.ts, src/audio/utils.ts, src/lib/client.ts, src/lib/constants.ts, src/lib/index.ts, src/react/index.ts, src/types/index.ts
|
|
2
|
+
[34mCLI[39m Building entry: src/index.ts, src/audio/index.ts, src/audio/utils.ts, src/lib/client.ts, src/lib/constants.ts, src/lib/index.ts, src/react/index.ts, src/react/utils.ts, src/types/index.ts
|
|
3
3
|
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
4
4
|
[34mCLI[39m tsup v8.0.2
|
|
5
5
|
[34mCLI[39m Target: es6
|
|
6
6
|
[34mCJS[39m Build start
|
|
7
7
|
[34mESM[39m Build start
|
|
8
|
-
[32mCJS[39m [1mdist/
|
|
9
|
-
[32mCJS[39m [1mdist/
|
|
10
|
-
[32mCJS[39m [1mdist/
|
|
11
|
-
[32mCJS[39m [1mdist/
|
|
12
|
-
[32mCJS[39m [1mdist/
|
|
13
|
-
[32mCJS[39m [1mdist/lib/constants.
|
|
14
|
-
[32mCJS[39m [1mdist/lib/index.
|
|
15
|
-
[32mCJS[39m
|
|
16
|
-
[
|
|
17
|
-
[
|
|
18
|
-
[32mESM[39m [1mdist/
|
|
19
|
-
[32mESM[39m [1mdist/
|
|
20
|
-
[32mESM[39m [1mdist/
|
|
21
|
-
[32mESM[39m [1mdist/lib/
|
|
22
|
-
[32mESM[39m [1mdist/react/index.
|
|
23
|
-
[32mESM[39m [1mdist/
|
|
24
|
-
[32mESM[39m [1mdist/
|
|
25
|
-
[32mESM[39m [1mdist/
|
|
26
|
-
[32mESM[39m [1mdist/chunk-
|
|
27
|
-
[32mESM[39m
|
|
8
|
+
[32mCJS[39m [1mdist/types/index.cjs [22m[32m764.00 B[39m
|
|
9
|
+
[32mCJS[39m [1mdist/index.cjs [22m[32m13.34 KB[39m
|
|
10
|
+
[32mCJS[39m [1mdist/audio/index.cjs [22m[32m13.05 KB[39m
|
|
11
|
+
[32mCJS[39m [1mdist/audio/utils.cjs [22m[32m5.24 KB[39m
|
|
12
|
+
[32mCJS[39m [1mdist/lib/client.cjs [22m[32m1.40 KB[39m
|
|
13
|
+
[32mCJS[39m [1mdist/lib/constants.cjs [22m[32m1.35 KB[39m
|
|
14
|
+
[32mCJS[39m [1mdist/lib/index.cjs [22m[32m13.32 KB[39m
|
|
15
|
+
[32mCJS[39m [1mdist/react/index.cjs [22m[32m18.78 KB[39m
|
|
16
|
+
[32mCJS[39m [1mdist/react/utils.cjs [22m[32m1.80 KB[39m
|
|
17
|
+
[32mCJS[39m ⚡️ Build success in 76ms
|
|
18
|
+
[32mESM[39m [1mdist/index.js [22m[32m276.00 B[39m
|
|
19
|
+
[32mESM[39m [1mdist/audio/index.js [22m[32m219.00 B[39m
|
|
20
|
+
[32mESM[39m [1mdist/chunk-FXPGR372.js [22m[32m0 B[39m
|
|
21
|
+
[32mESM[39m [1mdist/lib/client.js [22m[32m132.00 B[39m
|
|
22
|
+
[32mESM[39m [1mdist/react/index.js [22m[32m4.31 KB[39m
|
|
23
|
+
[32mESM[39m [1mdist/audio/utils.js [22m[32m498.00 B[39m
|
|
24
|
+
[32mESM[39m [1mdist/lib/constants.js [22m[32m185.00 B[39m
|
|
25
|
+
[32mESM[39m [1mdist/lib/index.js [22m[32m229.00 B[39m
|
|
26
|
+
[32mESM[39m [1mdist/chunk-S6A27RQL.js [22m[32m288.00 B[39m
|
|
27
|
+
[32mESM[39m [1mdist/chunk-4MHF74A7.js [22m[32m7.88 KB[39m
|
|
28
|
+
[32mESM[39m [1mdist/chunk-5TSWLYOW.js [22m[32m3.22 KB[39m
|
|
29
|
+
[32mESM[39m [1mdist/chunk-MJIFZWHS.js [22m[32m385.00 B[39m
|
|
30
|
+
[32mESM[39m [1mdist/chunk-OVI3W3GG.js [22m[32m275.00 B[39m
|
|
31
|
+
[32mESM[39m [1mdist/react/utils.js [22m[32m109.00 B[39m
|
|
32
|
+
[32mESM[39m [1mdist/chunk-XPIMIAAE.js [22m[32m337.00 B[39m
|
|
33
|
+
[32mESM[39m [1mdist/chunk-R4P7LWVZ.js [22m[32m1.76 KB[39m
|
|
34
|
+
[32mESM[39m [1mdist/types/index.js [22m[32m31.00 B[39m
|
|
35
|
+
[32mESM[39m ⚡️ Build success in 78ms
|
|
28
36
|
[34mDTS[39m Build start
|
|
29
|
-
[32mDTS[39m ⚡️ Build success in
|
|
37
|
+
[32mDTS[39m ⚡️ Build success in 7157ms
|
|
38
|
+
[32mDTS[39m [1mdist/lib/constants.d.cts [22m[32m211.00 B[39m
|
|
39
|
+
[32mDTS[39m [1mdist/react/index.d.cts [22m[32m749.00 B[39m
|
|
40
|
+
[32mDTS[39m [1mdist/lib/client.d.cts [22m[32m181.00 B[39m
|
|
41
|
+
[32mDTS[39m [1mdist/react/utils.d.cts [22m[32m240.00 B[39m
|
|
42
|
+
[32mDTS[39m [1mdist/types/index.d.cts [22m[32m103.00 B[39m
|
|
43
|
+
[32mDTS[39m [1mdist/lib/index.d.cts [22m[32m327.00 B[39m
|
|
44
|
+
[32mDTS[39m [1mdist/audio/utils.d.cts [22m[32m400.00 B[39m
|
|
45
|
+
[32mDTS[39m [1mdist/audio/index.d.cts [22m[32m232.00 B[39m
|
|
46
|
+
[32mDTS[39m [1mdist/index.d.cts [22m[32m215.00 B[39m
|
|
47
|
+
[32mDTS[39m [1mdist/index-C2_3XFxn.d.cts [22m[32m6.59 KB[39m
|
|
30
48
|
[32mDTS[39m [1mdist/lib/constants.d.ts [22m[32m211.00 B[39m
|
|
31
|
-
[32mDTS[39m [1mdist/
|
|
32
|
-
[32mDTS[39m [1mdist/react/index.d.ts [22m[32m720.00 B[39m
|
|
49
|
+
[32mDTS[39m [1mdist/react/index.d.ts [22m[32m746.00 B[39m
|
|
33
50
|
[32mDTS[39m [1mdist/lib/client.d.ts [22m[32m180.00 B[39m
|
|
51
|
+
[32mDTS[39m [1mdist/react/utils.d.ts [22m[32m240.00 B[39m
|
|
34
52
|
[32mDTS[39m [1mdist/types/index.d.ts [22m[32m103.00 B[39m
|
|
53
|
+
[32mDTS[39m [1mdist/lib/index.d.ts [22m[32m324.00 B[39m
|
|
35
54
|
[32mDTS[39m [1mdist/audio/utils.d.ts [22m[32m397.00 B[39m
|
|
36
55
|
[32mDTS[39m [1mdist/audio/index.d.ts [22m[32m229.00 B[39m
|
|
37
|
-
[32mDTS[39m [1mdist/index
|
|
38
|
-
[32mDTS[39m [1mdist/
|
|
39
|
-
[32mDTS[39m [1mdist/lib/index.d.mts [22m[32m327.00 B[39m
|
|
40
|
-
[32mDTS[39m [1mdist/react/index.d.mts [22m[32m723.00 B[39m
|
|
41
|
-
[32mDTS[39m [1mdist/lib/client.d.mts [22m[32m181.00 B[39m
|
|
42
|
-
[32mDTS[39m [1mdist/types/index.d.mts [22m[32m103.00 B[39m
|
|
43
|
-
[32mDTS[39m [1mdist/audio/utils.d.mts [22m[32m400.00 B[39m
|
|
44
|
-
[32mDTS[39m [1mdist/audio/index.d.mts [22m[32m232.00 B[39m
|
|
45
|
-
[32mDTS[39m [1mdist/index-DSBmfK9-.d.mts [22m[32m6.49 KB[39m
|
|
56
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m211.00 B[39m
|
|
57
|
+
[32mDTS[39m [1mdist/index-DgwnZezj.d.ts [22m[32m6.59 KB[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -0,0 +1,404 @@
|
|
|
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 __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
10
|
+
var __knownSymbol = (name, symbol) => {
|
|
11
|
+
return (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
|
|
12
|
+
};
|
|
13
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
14
|
+
var __spreadValues = (a, b) => {
|
|
15
|
+
for (var prop in b || (b = {}))
|
|
16
|
+
if (__hasOwnProp.call(b, prop))
|
|
17
|
+
__defNormalProp(a, prop, b[prop]);
|
|
18
|
+
if (__getOwnPropSymbols)
|
|
19
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
20
|
+
if (__propIsEnum.call(b, prop))
|
|
21
|
+
__defNormalProp(a, prop, b[prop]);
|
|
22
|
+
}
|
|
23
|
+
return a;
|
|
24
|
+
};
|
|
25
|
+
var __export = (target, all) => {
|
|
26
|
+
for (var name in all)
|
|
27
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
28
|
+
};
|
|
29
|
+
var __copyProps = (to, from, except, desc) => {
|
|
30
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
31
|
+
for (let key of __getOwnPropNames(from))
|
|
32
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
33
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
34
|
+
}
|
|
35
|
+
return to;
|
|
36
|
+
};
|
|
37
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
38
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
39
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
40
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
41
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
42
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
43
|
+
mod
|
|
44
|
+
));
|
|
45
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
46
|
+
var __async = (__this, __arguments, generator) => {
|
|
47
|
+
return new Promise((resolve, reject) => {
|
|
48
|
+
var fulfilled = (value) => {
|
|
49
|
+
try {
|
|
50
|
+
step(generator.next(value));
|
|
51
|
+
} catch (e) {
|
|
52
|
+
reject(e);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
var rejected = (value) => {
|
|
56
|
+
try {
|
|
57
|
+
step(generator.throw(value));
|
|
58
|
+
} catch (e) {
|
|
59
|
+
reject(e);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
63
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
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);
|
|
67
|
+
|
|
68
|
+
// src/audio/index.ts
|
|
69
|
+
var audio_exports = {};
|
|
70
|
+
__export(audio_exports, {
|
|
71
|
+
default: () => audio_default
|
|
72
|
+
});
|
|
73
|
+
module.exports = __toCommonJS(audio_exports);
|
|
74
|
+
var import_emittery = __toESM(require("emittery"), 1);
|
|
75
|
+
var import_human_id = require("human-id");
|
|
76
|
+
var import_partysocket = require("partysocket");
|
|
77
|
+
|
|
78
|
+
// src/lib/constants.ts
|
|
79
|
+
var BASE_URL = "https://api.cartesia.ai/v0";
|
|
80
|
+
var SAMPLE_RATE = 44100;
|
|
81
|
+
var constructWebsocketUrl = (baseUrl) => {
|
|
82
|
+
return new URL(`${baseUrl.replace(/^http/, "ws")}/audio/websocket`);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// src/lib/client.ts
|
|
86
|
+
var Client = class {
|
|
87
|
+
constructor(options = {}) {
|
|
88
|
+
if (!(options.apiKey || process.env.CARTESIA_API_KEY)) {
|
|
89
|
+
throw new Error("Missing Cartesia API key.");
|
|
90
|
+
}
|
|
91
|
+
this.apiKey = options.apiKey || process.env.CARTESIA_API_KEY;
|
|
92
|
+
this.baseUrl = options.baseUrl || BASE_URL;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// src/audio/utils.ts
|
|
97
|
+
var import_base64_js = __toESM(require("base64-js"), 1);
|
|
98
|
+
function getBufferDuration(b64) {
|
|
99
|
+
const floats = base64ToArray(b64);
|
|
100
|
+
return floats.length / SAMPLE_RATE;
|
|
101
|
+
}
|
|
102
|
+
function base64ToArray(b64) {
|
|
103
|
+
return filterSentinel(b64).reduce((acc, b) => {
|
|
104
|
+
const floats = new Float32Array(import_base64_js.default.toByteArray(b).buffer);
|
|
105
|
+
const newAcc = new Float32Array(acc.length + floats.length);
|
|
106
|
+
newAcc.set(acc, 0);
|
|
107
|
+
newAcc.set(floats, acc.length);
|
|
108
|
+
return newAcc;
|
|
109
|
+
}, new Float32Array(0));
|
|
110
|
+
}
|
|
111
|
+
function playAudioBuffer(b64, context, maybeStartAt = null, onEnded = null) {
|
|
112
|
+
const startAt = maybeStartAt != null ? maybeStartAt : context.currentTime;
|
|
113
|
+
const floats = base64ToArray(b64);
|
|
114
|
+
const source = context.createBufferSource();
|
|
115
|
+
const buffer = context.createBuffer(1, floats.length, SAMPLE_RATE);
|
|
116
|
+
buffer.getChannelData(0).set(floats);
|
|
117
|
+
source.buffer = buffer;
|
|
118
|
+
source.connect(context.destination);
|
|
119
|
+
source.start(startAt);
|
|
120
|
+
source.onended = onEnded;
|
|
121
|
+
return buffer.duration;
|
|
122
|
+
}
|
|
123
|
+
function createMessageHandlerForContextId(contextId, handler) {
|
|
124
|
+
return (event) => {
|
|
125
|
+
const message = JSON.parse(event.data);
|
|
126
|
+
if (message.context_id !== contextId) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
let chunk;
|
|
130
|
+
if (message.done) {
|
|
131
|
+
chunk = getSentinel();
|
|
132
|
+
} else {
|
|
133
|
+
chunk = message.data;
|
|
134
|
+
}
|
|
135
|
+
handler({ chunk, message });
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
function getSentinel() {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
function isSentinel(x) {
|
|
142
|
+
return x === getSentinel();
|
|
143
|
+
}
|
|
144
|
+
function filterSentinel(collection) {
|
|
145
|
+
return collection.filter(
|
|
146
|
+
(x) => !isSentinel(x)
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
function isComplete(chunks) {
|
|
150
|
+
return isSentinel(chunks[chunks.length - 1]);
|
|
151
|
+
}
|
|
152
|
+
function getEmitteryCallbacks(emitter) {
|
|
153
|
+
return {
|
|
154
|
+
on: emitter.on.bind(emitter),
|
|
155
|
+
off: emitter.off.bind(emitter),
|
|
156
|
+
once: emitter.once.bind(emitter),
|
|
157
|
+
events: emitter.events.bind(emitter)
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// src/audio/index.ts
|
|
162
|
+
var audio_default = class extends Client {
|
|
163
|
+
constructor() {
|
|
164
|
+
super(...arguments);
|
|
165
|
+
this.isConnected = false;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Stream audio from a model.
|
|
169
|
+
*
|
|
170
|
+
* @param inputs - Stream options. Includes a `model` key and some `parameters`, which
|
|
171
|
+
* are model-specific and can be found in the model's documentation.
|
|
172
|
+
* @param options - Options for the stream.
|
|
173
|
+
* @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
|
|
174
|
+
* If `0`, the stream will not time out.
|
|
175
|
+
* @returns An object with a method `play` of type `(bufferDuration: number) => Promise<void>`
|
|
176
|
+
* that plays the audio as it arrives, with `bufferDuration` seconds of audio buffered before
|
|
177
|
+
* starting playback.
|
|
178
|
+
*/
|
|
179
|
+
stream(inputs, { timeout = 0 } = {}) {
|
|
180
|
+
var _a, _b, _c, _d;
|
|
181
|
+
if (!this.isConnected) {
|
|
182
|
+
throw new Error("Not connected to WebSocket. Call .connect() first.");
|
|
183
|
+
}
|
|
184
|
+
const contextId = this.generateId();
|
|
185
|
+
(_a = this.socket) == null ? void 0 : _a.send(
|
|
186
|
+
JSON.stringify({
|
|
187
|
+
data: inputs,
|
|
188
|
+
context_id: contextId
|
|
189
|
+
})
|
|
190
|
+
);
|
|
191
|
+
const streamCompleteController = new AbortController();
|
|
192
|
+
let timeoutId = null;
|
|
193
|
+
if (timeout > 0) {
|
|
194
|
+
timeoutId = setTimeout(streamCompleteController.abort, timeout);
|
|
195
|
+
}
|
|
196
|
+
const chunks = [];
|
|
197
|
+
const emitter = new import_emittery.default();
|
|
198
|
+
const handleMessage = createMessageHandlerForContextId(
|
|
199
|
+
contextId,
|
|
200
|
+
(_0) => __async(this, [_0], function* ({ chunk, message }) {
|
|
201
|
+
chunks.push(chunk);
|
|
202
|
+
yield emitter.emit("chunk", {
|
|
203
|
+
chunk,
|
|
204
|
+
chunks
|
|
205
|
+
});
|
|
206
|
+
yield emitter.emit("message", message);
|
|
207
|
+
if (isSentinel(chunk)) {
|
|
208
|
+
yield emitter.emit("streamed", {
|
|
209
|
+
chunks
|
|
210
|
+
});
|
|
211
|
+
streamCompleteController.abort();
|
|
212
|
+
} else if (timeoutId) {
|
|
213
|
+
clearTimeout(timeoutId);
|
|
214
|
+
timeoutId = setTimeout(streamCompleteController.abort, timeout);
|
|
215
|
+
}
|
|
216
|
+
})
|
|
217
|
+
);
|
|
218
|
+
(_b = this.socket) == null ? void 0 : _b.addEventListener("message", handleMessage, {
|
|
219
|
+
signal: streamCompleteController.signal
|
|
220
|
+
});
|
|
221
|
+
(_c = this.socket) == null ? void 0 : _c.addEventListener(
|
|
222
|
+
"close",
|
|
223
|
+
() => {
|
|
224
|
+
streamCompleteController.abort();
|
|
225
|
+
},
|
|
226
|
+
{
|
|
227
|
+
once: true
|
|
228
|
+
}
|
|
229
|
+
);
|
|
230
|
+
(_d = this.socket) == null ? void 0 : _d.addEventListener(
|
|
231
|
+
"error",
|
|
232
|
+
() => {
|
|
233
|
+
streamCompleteController.abort();
|
|
234
|
+
},
|
|
235
|
+
{
|
|
236
|
+
once: true
|
|
237
|
+
}
|
|
238
|
+
);
|
|
239
|
+
streamCompleteController.signal.addEventListener("abort", () => {
|
|
240
|
+
if (timeoutId) {
|
|
241
|
+
clearTimeout(timeoutId);
|
|
242
|
+
}
|
|
243
|
+
emitter.clearListeners();
|
|
244
|
+
});
|
|
245
|
+
const play = (_0) => __async(this, [_0], function* ({ bufferDuration }) {
|
|
246
|
+
const context = new AudioContext({
|
|
247
|
+
sampleRate: SAMPLE_RATE
|
|
248
|
+
});
|
|
249
|
+
let startNextPlaybackAt = 0;
|
|
250
|
+
const playLatestChunk = (chunk) => {
|
|
251
|
+
if (isSentinel(chunk)) {
|
|
252
|
+
return true;
|
|
253
|
+
}
|
|
254
|
+
startNextPlaybackAt = playAudioBuffer([chunk], context, startNextPlaybackAt) + Math.max(context.currentTime, startNextPlaybackAt);
|
|
255
|
+
return false;
|
|
256
|
+
};
|
|
257
|
+
const playChunks = (chunks2) => {
|
|
258
|
+
startNextPlaybackAt += playAudioBuffer(
|
|
259
|
+
chunks2,
|
|
260
|
+
context,
|
|
261
|
+
startNextPlaybackAt
|
|
262
|
+
);
|
|
263
|
+
if (isComplete(chunks2)) {
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
const tryStart = (chunks2) => __async(this, null, function* () {
|
|
268
|
+
startNextPlaybackAt = context.currentTime;
|
|
269
|
+
if (isComplete(chunks2) || streamCompleteController.signal.aborted) {
|
|
270
|
+
emitter.emit("buffered");
|
|
271
|
+
playChunks(chunks2);
|
|
272
|
+
return true;
|
|
273
|
+
}
|
|
274
|
+
if (getBufferDuration(chunks2) > bufferDuration) {
|
|
275
|
+
emitter.emit("buffered");
|
|
276
|
+
playChunks(chunks2);
|
|
277
|
+
try {
|
|
278
|
+
for (var iter2 = __forAwait(emitter.events("chunk")), more2, temp2, error2; more2 = !(temp2 = yield iter2.next()).done; more2 = false) {
|
|
279
|
+
const { chunk } = temp2.value;
|
|
280
|
+
if (playLatestChunk(chunk)) {
|
|
281
|
+
break;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
} catch (temp2) {
|
|
285
|
+
error2 = [temp2];
|
|
286
|
+
} finally {
|
|
287
|
+
try {
|
|
288
|
+
more2 && (temp2 = iter2.return) && (yield temp2.call(iter2));
|
|
289
|
+
} finally {
|
|
290
|
+
if (error2)
|
|
291
|
+
throw error2[0];
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return true;
|
|
295
|
+
}
|
|
296
|
+
emitter.emit("buffering");
|
|
297
|
+
return false;
|
|
298
|
+
});
|
|
299
|
+
if (!(yield tryStart(chunks))) {
|
|
300
|
+
try {
|
|
301
|
+
for (var iter = __forAwait(emitter.events("chunk")), more, temp, error; more = !(temp = yield iter.next()).done; more = false) {
|
|
302
|
+
const { chunks: chunks2 } = temp.value;
|
|
303
|
+
if (yield tryStart(chunks2)) {
|
|
304
|
+
const playbackEndsIn = Math.max(0, startNextPlaybackAt - context.currentTime) * 1e3;
|
|
305
|
+
emitter.emit("scheduled", { playbackEndsIn });
|
|
306
|
+
break;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
} catch (temp) {
|
|
310
|
+
error = [temp];
|
|
311
|
+
} finally {
|
|
312
|
+
try {
|
|
313
|
+
more && (temp = iter.return) && (yield temp.call(iter));
|
|
314
|
+
} finally {
|
|
315
|
+
if (error)
|
|
316
|
+
throw error[0];
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
} else {
|
|
320
|
+
const playbackEndsIn = Math.max(0, startNextPlaybackAt - context.currentTime) * 1e3;
|
|
321
|
+
emitter.emit("scheduled", { playbackEndsIn });
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
return __spreadValues({
|
|
325
|
+
play
|
|
326
|
+
}, getEmitteryCallbacks(emitter));
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Generate a unique ID suitable for a streaming context.
|
|
330
|
+
*
|
|
331
|
+
* Not suitable for security purposes or as a primary key, since
|
|
332
|
+
* it lacks the amount of entropy required for those use cases.
|
|
333
|
+
*
|
|
334
|
+
* @returns A unique ID.
|
|
335
|
+
*/
|
|
336
|
+
generateId() {
|
|
337
|
+
return (0, import_human_id.humanId)({
|
|
338
|
+
separator: "-",
|
|
339
|
+
capitalize: false
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Authenticate and connect to a Cartesia streaming WebSocket.
|
|
344
|
+
*
|
|
345
|
+
* @returns A promise that resolves when the WebSocket is connected.
|
|
346
|
+
* @throws {Error} If the WebSocket fails to connect.
|
|
347
|
+
*/
|
|
348
|
+
connect() {
|
|
349
|
+
const url = constructWebsocketUrl(this.baseUrl);
|
|
350
|
+
url.searchParams.set("api_key", this.apiKey);
|
|
351
|
+
const emitter = new import_emittery.default();
|
|
352
|
+
this.socket = new import_partysocket.WebSocket(url.toString());
|
|
353
|
+
this.socket.onopen = () => {
|
|
354
|
+
this.isConnected = true;
|
|
355
|
+
emitter.emit("open");
|
|
356
|
+
};
|
|
357
|
+
this.socket.onclose = () => {
|
|
358
|
+
this.isConnected = false;
|
|
359
|
+
emitter.emit("close");
|
|
360
|
+
};
|
|
361
|
+
return new Promise(
|
|
362
|
+
(resolve, reject) => {
|
|
363
|
+
var _a, _b, _c;
|
|
364
|
+
(_a = this.socket) == null ? void 0 : _a.addEventListener(
|
|
365
|
+
"open",
|
|
366
|
+
() => {
|
|
367
|
+
resolve(getEmitteryCallbacks(emitter));
|
|
368
|
+
},
|
|
369
|
+
{
|
|
370
|
+
once: true
|
|
371
|
+
}
|
|
372
|
+
);
|
|
373
|
+
const aborter = new AbortController();
|
|
374
|
+
(_b = this.socket) == null ? void 0 : _b.addEventListener(
|
|
375
|
+
"error",
|
|
376
|
+
() => {
|
|
377
|
+
aborter.abort();
|
|
378
|
+
reject(new Error("WebSocket failed to connect."));
|
|
379
|
+
},
|
|
380
|
+
{
|
|
381
|
+
signal: aborter.signal
|
|
382
|
+
}
|
|
383
|
+
);
|
|
384
|
+
(_c = this.socket) == null ? void 0 : _c.addEventListener(
|
|
385
|
+
"close",
|
|
386
|
+
() => {
|
|
387
|
+
aborter.abort();
|
|
388
|
+
reject(new Error("WebSocket closed before it could connect."));
|
|
389
|
+
},
|
|
390
|
+
{
|
|
391
|
+
signal: aborter.signal
|
|
392
|
+
}
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Disconnect from the Cartesia streaming WebSocket.
|
|
399
|
+
*/
|
|
400
|
+
disconnect() {
|
|
401
|
+
var _a;
|
|
402
|
+
(_a = this.socket) == null ? void 0 : _a.close();
|
|
403
|
+
}
|
|
404
|
+
};
|
package/dist/audio/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import 'emittery';
|
|
2
2
|
import 'partysocket';
|
|
3
3
|
import '../lib/client.js';
|
|
4
|
-
export { C as Chunk, l as ConnectionEventData, S as StreamEventData, m as StreamRequest, e as default } from '../index-
|
|
4
|
+
export { C as Chunk, l as ConnectionEventData, S as StreamEventData, m as StreamRequest, e as default } from '../index-DgwnZezj.js';
|
|
5
5
|
import '../types/index.js';
|