@cartesia/cartesia-js 0.0.3 → 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.
Files changed (57) hide show
  1. package/.turbo/turbo-build.log +44 -32
  2. package/CHANGELOG.md +6 -0
  3. package/dist/audio/index.cjs +404 -0
  4. package/dist/audio/index.d.cts +5 -0
  5. package/dist/audio/index.d.ts +1 -1
  6. package/dist/audio/index.js +9 -395
  7. package/dist/audio/utils.cjs +157 -0
  8. package/dist/audio/{utils.d.mts → utils.d.cts} +3 -3
  9. package/dist/audio/utils.d.ts +1 -1
  10. package/dist/audio/utils.js +16 -147
  11. package/dist/{chunk-3CYTAFLF.mjs → chunk-4MHF74A7.js} +15 -5
  12. package/dist/{chunk-FRIBQZPN.mjs → chunk-5TSWLYOW.js} +1 -1
  13. package/dist/{chunk-XSFPHPPG.mjs → chunk-MJIFZWHS.js} +1 -1
  14. package/dist/chunk-OVI3W3GG.js +12 -0
  15. package/dist/{chunk-HNLIBHEN.mjs → chunk-R4P7LWVZ.js} +1 -11
  16. package/dist/{lib/index.mjs → chunk-S6A27RQL.js} +3 -4
  17. package/dist/chunk-XPIMIAAE.js +17 -0
  18. package/dist/{index-DSBmfK9-.d.mts → index-C2_3XFxn.d.cts} +9 -4
  19. package/dist/{index-qwAyxV5I.d.ts → index-DgwnZezj.d.ts} +8 -3
  20. package/dist/index.cjs +419 -0
  21. package/dist/index.d.cts +6 -0
  22. package/dist/index.d.ts +6 -0
  23. package/dist/index.js +13 -0
  24. package/dist/lib/client.cjs +43 -0
  25. package/dist/lib/{client.d.mts → client.d.cts} +1 -1
  26. package/dist/lib/client.js +7 -42
  27. package/dist/lib/constants.cjs +38 -0
  28. package/dist/lib/constants.js +8 -35
  29. package/dist/lib/index.cjs +418 -0
  30. package/dist/lib/{index.d.mts → index.d.cts} +3 -3
  31. package/dist/lib/index.d.ts +1 -1
  32. package/dist/lib/index.js +10 -409
  33. package/dist/react/index.cjs +597 -0
  34. package/dist/react/{index.d.mts → index.d.cts} +4 -3
  35. package/dist/react/index.d.ts +2 -1
  36. package/dist/react/index.js +71 -455
  37. package/dist/react/utils.cjs +57 -0
  38. package/dist/react/utils.d.cts +7 -0
  39. package/dist/react/utils.d.ts +7 -0
  40. package/dist/react/utils.js +7 -0
  41. package/dist/types/index.cjs +18 -0
  42. package/dist/types/index.js +1 -18
  43. package/package.json +10 -7
  44. package/src/audio/index.ts +15 -0
  45. package/src/index.ts +3 -0
  46. package/src/react/index.ts +48 -10
  47. package/src/react/utils.ts +11 -0
  48. package/dist/audio/index.d.mts +0 -5
  49. package/dist/audio/index.mjs +0 -9
  50. package/dist/audio/utils.mjs +0 -25
  51. package/dist/lib/client.mjs +0 -7
  52. package/dist/lib/constants.mjs +0 -10
  53. package/dist/react/index.mjs +0 -130
  54. package/index.ts +0 -3
  55. /package/dist/{types/index.mjs → chunk-FXPGR372.js} +0 -0
  56. /package/dist/lib/{constants.d.mts → constants.d.cts} +0 -0
  57. /package/dist/types/{index.d.mts → index.d.cts} +0 -0
@@ -1,149 +1,4 @@
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
- bufferToWav: () => bufferToWav,
35
- createMessageHandlerForContextId: () => createMessageHandlerForContextId,
36
- filterSentinel: () => filterSentinel,
37
- getBufferDuration: () => getBufferDuration,
38
- getEmitteryCallbacks: () => getEmitteryCallbacks,
39
- getSentinel: () => getSentinel,
40
- isComplete: () => isComplete,
41
- isSentinel: () => isSentinel,
42
- playAudioBuffer: () => playAudioBuffer
43
- });
44
- module.exports = __toCommonJS(utils_exports);
45
- var import_base64_js = __toESM(require("base64-js"));
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
- function base64ToArray(b64) {
56
- return filterSentinel(b64).reduce((acc, b) => {
57
- const floats = new Float32Array(import_base64_js.default.toByteArray(b).buffer);
58
- const newAcc = new Float32Array(acc.length + floats.length);
59
- newAcc.set(acc, 0);
60
- newAcc.set(floats, acc.length);
61
- return newAcc;
62
- }, new Float32Array(0));
63
- }
64
- function playAudioBuffer(b64, context, maybeStartAt = null, onEnded = null) {
65
- const startAt = maybeStartAt != null ? maybeStartAt : context.currentTime;
66
- const floats = base64ToArray(b64);
67
- const source = context.createBufferSource();
68
- const buffer = context.createBuffer(1, floats.length, SAMPLE_RATE);
69
- buffer.getChannelData(0).set(floats);
70
- source.buffer = buffer;
71
- source.connect(context.destination);
72
- source.start(startAt);
73
- source.onended = onEnded;
74
- return buffer.duration;
75
- }
76
- function createMessageHandlerForContextId(contextId, handler) {
77
- return (event) => {
78
- const message = JSON.parse(event.data);
79
- if (message.context_id !== contextId) {
80
- return;
81
- }
82
- let chunk;
83
- if (message.done) {
84
- chunk = getSentinel();
85
- } else {
86
- chunk = message.data;
87
- }
88
- handler({ chunk, message });
89
- };
90
- }
91
- function getSentinel() {
92
- return null;
93
- }
94
- function isSentinel(x) {
95
- return x === getSentinel();
96
- }
97
- function filterSentinel(collection) {
98
- return collection.filter(
99
- (x) => !isSentinel(x)
100
- );
101
- }
102
- function isComplete(chunks) {
103
- return isSentinel(chunks[chunks.length - 1]);
104
- }
105
- function getEmitteryCallbacks(emitter) {
106
- return {
107
- on: emitter.on.bind(emitter),
108
- off: emitter.off.bind(emitter),
109
- once: emitter.once.bind(emitter),
110
- events: emitter.events.bind(emitter)
111
- };
112
- }
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
- // Annotate the CommonJS export names for ESM import in node:
146
- 0 && (module.exports = {
1
+ import {
147
2
  base64ToArray,
148
3
  bufferToWav,
149
4
  createMessageHandlerForContextId,
@@ -154,4 +9,18 @@ function bufferToWav(sampleRate, channelBuffers) {
154
9
  isComplete,
155
10
  isSentinel,
156
11
  playAudioBuffer
157
- });
12
+ } from "../chunk-5TSWLYOW.js";
13
+ import "../chunk-OVI3W3GG.js";
14
+ import "../chunk-R4P7LWVZ.js";
15
+ export {
16
+ base64ToArray,
17
+ bufferToWav,
18
+ createMessageHandlerForContextId,
19
+ filterSentinel,
20
+ getBufferDuration,
21
+ getEmitteryCallbacks,
22
+ getSentinel,
23
+ isComplete,
24
+ isSentinel,
25
+ playAudioBuffer
26
+ };
@@ -5,17 +5,19 @@ import {
5
5
  isComplete,
6
6
  isSentinel,
7
7
  playAudioBuffer
8
- } from "./chunk-FRIBQZPN.mjs";
8
+ } from "./chunk-5TSWLYOW.js";
9
9
  import {
10
10
  Client
11
- } from "./chunk-XSFPHPPG.mjs";
11
+ } from "./chunk-MJIFZWHS.js";
12
12
  import {
13
13
  SAMPLE_RATE,
14
+ constructWebsocketUrl
15
+ } from "./chunk-OVI3W3GG.js";
16
+ import {
14
17
  __async,
15
18
  __forAwait,
16
- __spreadValues,
17
- constructWebsocketUrl
18
- } from "./chunk-HNLIBHEN.mjs";
19
+ __spreadValues
20
+ } from "./chunk-R4P7LWVZ.js";
19
21
 
20
22
  // src/audio/index.ts
21
23
  import Emittery from "emittery";
@@ -129,10 +131,12 @@ var audio_default = class extends Client {
129
131
  const tryStart = (chunks2) => __async(this, null, function* () {
130
132
  startNextPlaybackAt = context.currentTime;
131
133
  if (isComplete(chunks2) || streamCompleteController.signal.aborted) {
134
+ emitter.emit("buffered");
132
135
  playChunks(chunks2);
133
136
  return true;
134
137
  }
135
138
  if (getBufferDuration(chunks2) > bufferDuration) {
139
+ emitter.emit("buffered");
136
140
  playChunks(chunks2);
137
141
  try {
138
142
  for (var iter2 = __forAwait(emitter.events("chunk")), more2, temp2, error2; more2 = !(temp2 = yield iter2.next()).done; more2 = false) {
@@ -153,6 +157,7 @@ var audio_default = class extends Client {
153
157
  }
154
158
  return true;
155
159
  }
160
+ emitter.emit("buffering");
156
161
  return false;
157
162
  });
158
163
  if (!(yield tryStart(chunks))) {
@@ -160,6 +165,8 @@ var audio_default = class extends Client {
160
165
  for (var iter = __forAwait(emitter.events("chunk")), more, temp, error; more = !(temp = yield iter.next()).done; more = false) {
161
166
  const { chunks: chunks2 } = temp.value;
162
167
  if (yield tryStart(chunks2)) {
168
+ const playbackEndsIn = Math.max(0, startNextPlaybackAt - context.currentTime) * 1e3;
169
+ emitter.emit("scheduled", { playbackEndsIn });
163
170
  break;
164
171
  }
165
172
  }
@@ -173,6 +180,9 @@ var audio_default = class extends Client {
173
180
  throw error[0];
174
181
  }
175
182
  }
183
+ } else {
184
+ const playbackEndsIn = Math.max(0, startNextPlaybackAt - context.currentTime) * 1e3;
185
+ emitter.emit("scheduled", { playbackEndsIn });
176
186
  }
177
187
  });
178
188
  return __spreadValues({
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  SAMPLE_RATE
3
- } from "./chunk-HNLIBHEN.mjs";
3
+ } from "./chunk-OVI3W3GG.js";
4
4
 
5
5
  // src/audio/utils.ts
6
6
  import base64 from "base64-js";
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  BASE_URL
3
- } from "./chunk-HNLIBHEN.mjs";
3
+ } from "./chunk-OVI3W3GG.js";
4
4
 
5
5
  // src/lib/client.ts
6
6
  var Client = class {
@@ -0,0 +1,12 @@
1
+ // src/lib/constants.ts
2
+ var BASE_URL = "https://api.cartesia.ai/v0";
3
+ var SAMPLE_RATE = 44100;
4
+ var constructWebsocketUrl = (baseUrl) => {
5
+ return new URL(`${baseUrl.replace(/^http/, "ws")}/audio/websocket`);
6
+ };
7
+
8
+ export {
9
+ BASE_URL,
10
+ SAMPLE_RATE,
11
+ constructWebsocketUrl
12
+ };
@@ -39,18 +39,8 @@ var __async = (__this, __arguments, generator) => {
39
39
  };
40
40
  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);
41
41
 
42
- // src/lib/constants.ts
43
- var BASE_URL = "https://api.cartesia.ai/v0";
44
- var SAMPLE_RATE = 44100;
45
- var constructWebsocketUrl = (baseUrl) => {
46
- return new URL(`${baseUrl.replace(/^http/, "ws")}/audio/websocket`);
47
- };
48
-
49
42
  export {
50
43
  __spreadValues,
51
44
  __async,
52
- __forAwait,
53
- BASE_URL,
54
- SAMPLE_RATE,
55
- constructWebsocketUrl
45
+ __forAwait
56
46
  };
@@ -1,11 +1,9 @@
1
1
  import {
2
2
  audio_default
3
- } from "../chunk-3CYTAFLF.mjs";
4
- import "../chunk-FRIBQZPN.mjs";
3
+ } from "./chunk-4MHF74A7.js";
5
4
  import {
6
5
  Client
7
- } from "../chunk-XSFPHPPG.mjs";
8
- import "../chunk-HNLIBHEN.mjs";
6
+ } from "./chunk-MJIFZWHS.js";
9
7
 
10
8
  // src/lib/index.ts
11
9
  var Cartesia = class extends Client {
@@ -14,6 +12,7 @@ var Cartesia = class extends Client {
14
12
  this.audio = new audio_default(options);
15
13
  }
16
14
  };
15
+
17
16
  export {
18
17
  Cartesia
19
18
  };
@@ -0,0 +1,17 @@
1
+ import {
2
+ __async
3
+ } from "./chunk-R4P7LWVZ.js";
4
+
5
+ // src/react/utils.ts
6
+ function pingServer(url) {
7
+ return __async(this, null, function* () {
8
+ const start = (/* @__PURE__ */ new Date()).getTime();
9
+ yield fetch(url);
10
+ const end = (/* @__PURE__ */ new Date()).getTime();
11
+ return end - start;
12
+ });
13
+ }
14
+
15
+ export {
16
+ pingServer
17
+ };
@@ -1,7 +1,7 @@
1
1
  import * as emittery from 'emittery';
2
2
  import emittery__default from 'emittery';
3
3
  import { WebSocket } from 'partysocket';
4
- import { Client } from './lib/client.mjs';
4
+ import { Client } from './lib/client.cjs';
5
5
 
6
6
  /**
7
7
  * Get the duration of base64-encoded audio buffer(s) in seconds.
@@ -98,6 +98,11 @@ type StreamEventData = {
98
98
  chunks: Chunk[];
99
99
  };
100
100
  message: unknown;
101
+ buffering: never;
102
+ buffered: never;
103
+ scheduled: {
104
+ playbackEndsIn: number;
105
+ };
101
106
  };
102
107
  type ConnectionEventData = {
103
108
  open: never;
@@ -125,9 +130,9 @@ declare class export_default extends Client {
125
130
  * starting playback.
126
131
  */
127
132
  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]>;
133
+ on: <Name extends keyof emittery.OmnipresentEventData | keyof StreamEventData>(eventName: Name | readonly Name[], listener: (eventData: (StreamEventData & emittery.OmnipresentEventData)[Name]) => void | Promise<void>) => emittery.UnsubscribeFunction;
134
+ off: <Name_1 extends keyof emittery.OmnipresentEventData | keyof StreamEventData>(eventName: Name_1 | readonly Name_1[], listener: (eventData: (StreamEventData & emittery.OmnipresentEventData)[Name_1]) => void | Promise<void>) => void;
135
+ once: <Name_2 extends keyof emittery.OmnipresentEventData | keyof StreamEventData>(eventName: Name_2 | readonly Name_2[]) => emittery.EmitteryOncePromise<(StreamEventData & emittery.OmnipresentEventData)[Name_2]>;
131
136
  events: <Name_3 extends keyof StreamEventData>(eventName: Name_3 | readonly Name_3[]) => AsyncIterableIterator<StreamEventData[Name_3]>;
132
137
  play: ({ bufferDuration }: {
133
138
  bufferDuration: number;
@@ -98,6 +98,11 @@ type StreamEventData = {
98
98
  chunks: Chunk[];
99
99
  };
100
100
  message: unknown;
101
+ buffering: never;
102
+ buffered: never;
103
+ scheduled: {
104
+ playbackEndsIn: number;
105
+ };
101
106
  };
102
107
  type ConnectionEventData = {
103
108
  open: never;
@@ -125,9 +130,9 @@ declare class export_default extends Client {
125
130
  * starting playback.
126
131
  */
127
132
  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]>;
133
+ on: <Name extends keyof emittery.OmnipresentEventData | keyof StreamEventData>(eventName: Name | readonly Name[], listener: (eventData: (StreamEventData & emittery.OmnipresentEventData)[Name]) => void | Promise<void>) => emittery.UnsubscribeFunction;
134
+ off: <Name_1 extends keyof emittery.OmnipresentEventData | keyof StreamEventData>(eventName: Name_1 | readonly Name_1[], listener: (eventData: (StreamEventData & emittery.OmnipresentEventData)[Name_1]) => void | Promise<void>) => void;
135
+ once: <Name_2 extends keyof emittery.OmnipresentEventData | keyof StreamEventData>(eventName: Name_2 | readonly Name_2[]) => emittery.EmitteryOncePromise<(StreamEventData & emittery.OmnipresentEventData)[Name_2]>;
131
136
  events: <Name_3 extends keyof StreamEventData>(eventName: Name_3 | readonly Name_3[]) => AsyncIterableIterator<StreamEventData[Name_3]>;
132
137
  play: ({ bufferDuration }: {
133
138
  bufferDuration: number;