@cartesia/cartesia-js 1.0.0 → 1.0.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.
Files changed (62) hide show
  1. package/.turbo/turbo-build.log +50 -50
  2. package/CHANGELOG.md +12 -0
  3. package/LICENSE.md +21 -0
  4. package/README.md +92 -19
  5. package/dist/{chunk-PQ6CIPFW.js → chunk-6YQ6KDIQ.js} +44 -5
  6. package/dist/{chunk-RO7TY474.js → chunk-BHY7MNGT.js} +11 -6
  7. package/dist/{chunk-F4QWVJY3.js → chunk-EDAAHENY.js} +2 -2
  8. package/dist/{chunk-WIFMLPT5.js → chunk-GHY2WEOK.js} +13 -0
  9. package/dist/{chunk-FN7BK4PS.js → chunk-IZBPLCGW.js} +97 -75
  10. package/dist/{chunk-JYLAM6VU.js → chunk-LZO6K34D.js} +2 -2
  11. package/dist/{chunk-3FL2SNIR.js → chunk-NQVZNVOU.js} +1 -1
  12. package/dist/{chunk-IEN4NCER.js → chunk-NVOCUUOF.js} +3 -3
  13. package/dist/chunk-PISCPZK4.js +40 -0
  14. package/dist/{chunk-SGXUEFII.js → chunk-UCYL2SOX.js} +18 -15
  15. package/dist/index.cjs +186 -103
  16. package/dist/index.d.cts +4 -4
  17. package/dist/index.d.ts +4 -4
  18. package/dist/index.js +15 -9
  19. package/dist/lib/client.cjs +35 -10
  20. package/dist/lib/client.d.cts +2 -2
  21. package/dist/lib/client.d.ts +2 -2
  22. package/dist/lib/client.js +2 -2
  23. package/dist/lib/constants.js +1 -1
  24. package/dist/lib/index.cjs +181 -102
  25. package/dist/lib/index.js +8 -8
  26. package/dist/react/index.cjs +286 -158
  27. package/dist/react/index.d.cts +5 -4
  28. package/dist/react/index.d.ts +5 -4
  29. package/dist/react/index.js +115 -66
  30. package/dist/react/utils.js +2 -2
  31. package/dist/tts/index.cjs +165 -89
  32. package/dist/tts/index.js +6 -6
  33. package/dist/tts/player.cjs +5 -0
  34. package/dist/tts/player.js +4 -3
  35. package/dist/tts/source.cjs +50 -4
  36. package/dist/tts/source.d.cts +16 -6
  37. package/dist/tts/source.d.ts +16 -6
  38. package/dist/tts/source.js +4 -2
  39. package/dist/tts/utils.cjs +18 -6
  40. package/dist/tts/utils.d.cts +7 -5
  41. package/dist/tts/utils.d.ts +7 -5
  42. package/dist/tts/utils.js +3 -2
  43. package/dist/tts/websocket.cjs +165 -89
  44. package/dist/tts/websocket.d.cts +12 -8
  45. package/dist/tts/websocket.d.ts +12 -8
  46. package/dist/tts/websocket.js +5 -5
  47. package/dist/types/index.d.cts +65 -5
  48. package/dist/types/index.d.ts +65 -5
  49. package/dist/voices/index.cjs +31 -23
  50. package/dist/voices/index.d.cts +2 -1
  51. package/dist/voices/index.d.ts +2 -1
  52. package/dist/voices/index.js +3 -3
  53. package/package.json +1 -1
  54. package/src/index.ts +2 -0
  55. package/src/lib/client.ts +10 -10
  56. package/src/react/index.ts +115 -64
  57. package/src/tts/source.ts +53 -7
  58. package/src/tts/utils.ts +26 -12
  59. package/src/tts/websocket.ts +42 -23
  60. package/src/types/index.ts +89 -4
  61. package/src/voices/index.ts +22 -15
  62. package/dist/chunk-PQ5EVEEH.js +0 -34
@@ -22,6 +22,18 @@ var __spreadValues = (a, b) => {
22
22
  return a;
23
23
  };
24
24
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
25
+ var __objRest = (source, exclude) => {
26
+ var target = {};
27
+ for (var prop in source)
28
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
29
+ target[prop] = source[prop];
30
+ if (source != null && __getOwnPropSymbols)
31
+ for (var prop of __getOwnPropSymbols(source)) {
32
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
33
+ target[prop] = source[prop];
34
+ }
35
+ return target;
36
+ };
25
37
  var __export = (target, all) => {
26
38
  for (var name in all)
27
39
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -110,20 +122,25 @@ var constructApiUrl = (baseUrl, path, { websocket = false } = {}) => {
110
122
  // src/lib/client.ts
111
123
  var Client = class {
112
124
  constructor(options = {}) {
113
- if (!(options.apiKey || process.env.CARTESIA_API_KEY)) {
125
+ const apiKey = options.apiKey || process.env.CARTESIA_API_KEY;
126
+ if (!apiKey) {
114
127
  throw new Error("Missing Cartesia API key.");
115
128
  }
116
- this.apiKey = options.apiKey || process.env.CARTESIA_API_KEY;
129
+ this.apiKey = typeof apiKey === "function" ? apiKey : () => __async(this, null, function* () {
130
+ return apiKey;
131
+ });
117
132
  this.baseUrl = options.baseUrl || BASE_URL;
118
133
  }
119
- fetch(path, options = {}) {
120
- const url = constructApiUrl(this.baseUrl, path);
121
- return (0, import_cross_fetch.default)(url.toString(), __spreadProps(__spreadValues({}, options), {
122
- headers: __spreadValues({
123
- "X-API-Key": this.apiKey,
124
- "Cartesia-Version": CARTESIA_VERSION
125
- }, options.headers)
126
- }));
134
+ _fetch(_0) {
135
+ return __async(this, arguments, function* (path, options = {}) {
136
+ const url = constructApiUrl(this.baseUrl, path);
137
+ const headers = new Headers(options.headers);
138
+ headers.set("X-API-Key", yield this.apiKey());
139
+ headers.set("Cartesia-Version", CARTESIA_VERSION);
140
+ return (0, import_cross_fetch.default)(url.toString(), __spreadProps(__spreadValues({}, options), {
141
+ headers
142
+ }));
143
+ });
127
144
  }
128
145
  };
129
146
 
@@ -134,7 +151,13 @@ var import_partysocket = require("partysocket");
134
151
 
135
152
  // src/tts/source.ts
136
153
  var import_emittery = __toESM(require("emittery"), 1);
137
- var _emitter, _buffer, _readIndex, _writeIndex, _closed, _sampleRate;
154
+ var ENCODING_MAP = {
155
+ pcm_f32le: { arrayType: Float32Array, bytesPerElement: 4 },
156
+ pcm_s16le: { arrayType: Int16Array, bytesPerElement: 2 },
157
+ pcm_alaw: { arrayType: Uint8Array, bytesPerElement: 1 },
158
+ pcm_mulaw: { arrayType: Uint8Array, bytesPerElement: 1 }
159
+ };
160
+ var _emitter, _buffer, _readIndex, _writeIndex, _closed, _sampleRate, _encoding, _container, _createBuffer, createBuffer_fn;
138
161
  var Source = class {
139
162
  /**
140
163
  * Create a new Source.
@@ -142,23 +165,44 @@ var Source = class {
142
165
  * @param options - Options for the Source.
143
166
  * @param options.sampleRate - The sample rate of the audio.
144
167
  */
145
- constructor({ sampleRate }) {
168
+ constructor({
169
+ sampleRate,
170
+ encoding,
171
+ container
172
+ }) {
173
+ /**
174
+ * Create a new buffer for the source.
175
+ *
176
+ * @param size - The size of the buffer to create.
177
+ * @returns The new buffer as a TypedArray based on the encoding.
178
+ */
179
+ __privateAdd(this, _createBuffer);
146
180
  __privateAdd(this, _emitter, new import_emittery.default());
147
181
  __privateAdd(this, _buffer, void 0);
148
182
  __privateAdd(this, _readIndex, 0);
149
183
  __privateAdd(this, _writeIndex, 0);
150
184
  __privateAdd(this, _closed, false);
151
185
  __privateAdd(this, _sampleRate, void 0);
186
+ __privateAdd(this, _encoding, void 0);
187
+ __privateAdd(this, _container, void 0);
152
188
  this.on = __privateGet(this, _emitter).on.bind(__privateGet(this, _emitter));
153
189
  this.once = __privateGet(this, _emitter).once.bind(__privateGet(this, _emitter));
154
190
  this.events = __privateGet(this, _emitter).events.bind(__privateGet(this, _emitter));
155
191
  this.off = __privateGet(this, _emitter).off.bind(__privateGet(this, _emitter));
156
192
  __privateSet(this, _sampleRate, sampleRate);
157
- __privateSet(this, _buffer, new Float32Array(1024));
193
+ __privateSet(this, _encoding, encoding);
194
+ __privateSet(this, _container, container);
195
+ __privateSet(this, _buffer, __privateMethod(this, _createBuffer, createBuffer_fn).call(this, 1024));
158
196
  }
159
197
  get sampleRate() {
160
198
  return __privateGet(this, _sampleRate);
161
199
  }
200
+ get encoding() {
201
+ return __privateGet(this, _encoding);
202
+ }
203
+ get container() {
204
+ return __privateGet(this, _container);
205
+ }
162
206
  /**
163
207
  * Append audio to the buffer.
164
208
  *
@@ -172,7 +216,7 @@ var Source = class {
172
216
  while (newCapacity < requiredCapacity) {
173
217
  newCapacity *= 2;
174
218
  }
175
- const newBuffer = new Float32Array(newCapacity);
219
+ const newBuffer = __privateMethod(this, _createBuffer, createBuffer_fn).call(this, newCapacity);
176
220
  newBuffer.set(__privateGet(this, _buffer));
177
221
  __privateSet(this, _buffer, newBuffer);
178
222
  }
@@ -220,6 +264,9 @@ var Source = class {
220
264
  get readIndex() {
221
265
  return __privateGet(this, _readIndex);
222
266
  }
267
+ get writeIndex() {
268
+ return __privateGet(this, _writeIndex);
269
+ }
223
270
  /**
224
271
  * Close the source. This signals that no more audio will be enqueued.
225
272
  *
@@ -241,19 +288,27 @@ _readIndex = new WeakMap();
241
288
  _writeIndex = new WeakMap();
242
289
  _closed = new WeakMap();
243
290
  _sampleRate = new WeakMap();
291
+ _encoding = new WeakMap();
292
+ _container = new WeakMap();
293
+ _createBuffer = new WeakSet();
294
+ createBuffer_fn = function(size) {
295
+ const { arrayType: ArrayType } = ENCODING_MAP[__privateGet(this, _encoding)];
296
+ return new ArrayType(size);
297
+ };
244
298
 
245
299
  // src/tts/utils.ts
246
300
  var import_base64_js = __toESM(require("base64-js"), 1);
247
- function base64ToArray(b64) {
301
+ function base64ToArray(b64, encoding) {
248
302
  const byteArrays = filterSentinel(b64).map((b) => import_base64_js.default.toByteArray(b));
303
+ const { arrayType: ArrayType, bytesPerElement } = ENCODING_MAP[encoding];
249
304
  const totalLength = byteArrays.reduce(
250
- (acc, arr) => acc + arr.length / Float32Array.BYTES_PER_ELEMENT,
305
+ (acc, arr) => acc + arr.length / bytesPerElement,
251
306
  0
252
307
  );
253
- const result = new Float32Array(totalLength);
308
+ const result = new ArrayType(totalLength);
254
309
  let offset = 0;
255
310
  for (const arr of byteArrays) {
256
- const floats = new Float32Array(arr.buffer);
311
+ const floats = new ArrayType(arr.buffer);
257
312
  result.set(floats, offset);
258
313
  offset += floats.length;
259
314
  }
@@ -271,10 +326,10 @@ function createMessageHandlerForContextId(contextId, handler) {
271
326
  let chunk;
272
327
  if (message.done) {
273
328
  chunk = getSentinel();
274
- } else {
329
+ } else if (message.type === "chunk") {
275
330
  chunk = message.data;
276
331
  }
277
- handler({ chunk, message: event.data });
332
+ handler({ chunk, message: event.data, data: message });
278
333
  };
279
334
  }
280
335
  function getSentinel() {
@@ -298,14 +353,14 @@ function getEmitteryCallbacks(emitter) {
298
353
  }
299
354
 
300
355
  // src/tts/websocket.ts
301
- var _isConnected, _sampleRate2, _generateId, generateId_fn;
356
+ var _isConnected, _sampleRate2, _container2, _encoding2, _generateId, generateId_fn;
302
357
  var WebSocket = class extends Client {
303
358
  /**
304
359
  * Create a new WebSocket client.
305
360
  *
306
361
  * @param args - Arguments to pass to the Client constructor.
307
362
  */
308
- constructor({ sampleRate }, ...args) {
363
+ constructor({ sampleRate, container, encoding }, ...args) {
309
364
  super(...args);
310
365
  /**
311
366
  * Generate a unique ID suitable for a streaming context.
@@ -318,12 +373,16 @@ var WebSocket = class extends Client {
318
373
  __privateAdd(this, _generateId);
319
374
  __privateAdd(this, _isConnected, false);
320
375
  __privateAdd(this, _sampleRate2, void 0);
376
+ __privateAdd(this, _container2, void 0);
377
+ __privateAdd(this, _encoding2, void 0);
321
378
  __privateSet(this, _sampleRate2, sampleRate);
379
+ __privateSet(this, _container2, container != null ? container : "raw");
380
+ __privateSet(this, _encoding2, encoding != null ? encoding : "pcm_f32le");
322
381
  }
323
382
  /**
324
383
  * Send a message over the WebSocket to start a stream.
325
384
  *
326
- * @param inputs - Stream options.
385
+ * @param inputs - Stream options. Defined in the StreamRequest type.
327
386
  * @param options - Options for the stream.
328
387
  * @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
329
388
  * If set to `0`, the stream will not time out.
@@ -331,26 +390,30 @@ var WebSocket = class extends Client {
331
390
  * @returns An Emittery instance that emits messages from the WebSocket.
332
391
  * @returns An abort function that can be called to cancel the stream.
333
392
  */
334
- send(inputs, { timeout = 0 } = {}) {
335
- var _a, _b, _c, _d;
393
+ send(_a, { timeout = 0 } = {}) {
394
+ var inputs = __objRest(_a, []);
395
+ var _a2, _b, _c, _d;
336
396
  if (!__privateGet(this, _isConnected)) {
337
397
  throw new Error("Not connected to WebSocket. Call .connect() first.");
338
398
  }
339
- const contextId = __privateMethod(this, _generateId, generateId_fn).call(this);
340
- (_a = this.socket) == null ? void 0 : _a.send(
341
- JSON.stringify(__spreadProps(__spreadValues({
342
- context_id: contextId
343
- }, inputs), {
344
- output_format: {
345
- container: "raw",
346
- encoding: "pcm_f32le",
347
- sample_rate: __privateGet(this, _sampleRate2)
348
- }
349
- }))
399
+ if (!inputs.context_id) {
400
+ inputs.context_id = __privateMethod(this, _generateId, generateId_fn).call(this);
401
+ }
402
+ if (!inputs.output_format) {
403
+ inputs.output_format = {
404
+ container: __privateGet(this, _container2),
405
+ encoding: __privateGet(this, _encoding2),
406
+ sample_rate: __privateGet(this, _sampleRate2)
407
+ };
408
+ }
409
+ (_a2 = this.socket) == null ? void 0 : _a2.send(
410
+ JSON.stringify(__spreadValues({}, inputs))
350
411
  );
351
412
  const emitter = new import_emittery2.default();
352
413
  const source = new Source({
353
- sampleRate: __privateGet(this, _sampleRate2)
414
+ sampleRate: __privateGet(this, _sampleRate2),
415
+ encoding: __privateGet(this, _encoding2),
416
+ container: __privateGet(this, _container2)
354
417
  });
355
418
  const streamCompleteController = new AbortController();
356
419
  let timeoutId = null;
@@ -358,19 +421,26 @@ var WebSocket = class extends Client {
358
421
  timeoutId = setTimeout(streamCompleteController.abort, timeout);
359
422
  }
360
423
  const handleMessage = createMessageHandlerForContextId(
361
- contextId,
362
- (_0) => __async(this, [_0], function* ({ chunk, message }) {
424
+ inputs.context_id,
425
+ (_0) => __async(this, [_0], function* ({ chunk, message, data }) {
363
426
  emitter.emit("message", message);
427
+ if (data.type === "timestamps") {
428
+ emitter.emit("timestamps", data.word_timestamps);
429
+ return;
430
+ }
364
431
  if (isSentinel(chunk)) {
365
432
  yield source.close();
366
433
  streamCompleteController.abort();
367
434
  return;
368
435
  }
369
- yield source.enqueue(base64ToArray([chunk]));
370
436
  if (timeoutId) {
371
437
  clearTimeout(timeoutId);
372
438
  timeoutId = setTimeout(streamCompleteController.abort, timeout);
373
439
  }
440
+ if (!chunk) {
441
+ return;
442
+ }
443
+ yield source.enqueue(base64ToArray([chunk], __privateGet(this, _encoding2)));
374
444
  })
375
445
  );
376
446
  (_b = this.socket) == null ? void 0 : _b.addEventListener("message", handleMessage, {
@@ -413,56 +483,60 @@ var WebSocket = class extends Client {
413
483
  * @throws {Error} If the WebSocket fails to connect.
414
484
  */
415
485
  connect() {
416
- const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
417
- websocket: true
486
+ return __async(this, null, function* () {
487
+ const emitter = new import_emittery2.default();
488
+ this.socket = new import_partysocket.WebSocket(() => __async(this, null, function* () {
489
+ const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
490
+ websocket: true
491
+ });
492
+ url.searchParams.set("api_key", yield this.apiKey());
493
+ url.searchParams.set("cartesia_version", CARTESIA_VERSION);
494
+ return url.toString();
495
+ }));
496
+ this.socket.onopen = () => {
497
+ __privateSet(this, _isConnected, true);
498
+ emitter.emit("open");
499
+ };
500
+ this.socket.onclose = () => {
501
+ __privateSet(this, _isConnected, false);
502
+ emitter.emit("close");
503
+ };
504
+ return new Promise(
505
+ (resolve, reject) => {
506
+ var _a, _b, _c;
507
+ (_a = this.socket) == null ? void 0 : _a.addEventListener(
508
+ "open",
509
+ () => {
510
+ resolve(getEmitteryCallbacks(emitter));
511
+ },
512
+ {
513
+ once: true
514
+ }
515
+ );
516
+ const aborter = new AbortController();
517
+ (_b = this.socket) == null ? void 0 : _b.addEventListener(
518
+ "error",
519
+ () => {
520
+ aborter.abort();
521
+ reject(new Error("WebSocket failed to connect."));
522
+ },
523
+ {
524
+ signal: aborter.signal
525
+ }
526
+ );
527
+ (_c = this.socket) == null ? void 0 : _c.addEventListener(
528
+ "close",
529
+ () => {
530
+ aborter.abort();
531
+ reject(new Error("WebSocket closed before it could connect."));
532
+ },
533
+ {
534
+ signal: aborter.signal
535
+ }
536
+ );
537
+ }
538
+ );
418
539
  });
419
- url.searchParams.set("api_key", this.apiKey);
420
- url.searchParams.set("cartesia_version", CARTESIA_VERSION);
421
- const emitter = new import_emittery2.default();
422
- this.socket = new import_partysocket.WebSocket(url.toString());
423
- this.socket.onopen = () => {
424
- __privateSet(this, _isConnected, true);
425
- emitter.emit("open");
426
- };
427
- this.socket.onclose = () => {
428
- __privateSet(this, _isConnected, false);
429
- emitter.emit("close");
430
- };
431
- return new Promise(
432
- (resolve, reject) => {
433
- var _a, _b, _c;
434
- (_a = this.socket) == null ? void 0 : _a.addEventListener(
435
- "open",
436
- () => {
437
- resolve(getEmitteryCallbacks(emitter));
438
- },
439
- {
440
- once: true
441
- }
442
- );
443
- const aborter = new AbortController();
444
- (_b = this.socket) == null ? void 0 : _b.addEventListener(
445
- "error",
446
- () => {
447
- aborter.abort();
448
- reject(new Error("WebSocket failed to connect."));
449
- },
450
- {
451
- signal: aborter.signal
452
- }
453
- );
454
- (_c = this.socket) == null ? void 0 : _c.addEventListener(
455
- "close",
456
- () => {
457
- aborter.abort();
458
- reject(new Error("WebSocket closed before it could connect."));
459
- },
460
- {
461
- signal: aborter.signal
462
- }
463
- );
464
- }
465
- );
466
540
  }
467
541
  /**
468
542
  * Disconnect from the Cartesia streaming WebSocket.
@@ -474,6 +548,8 @@ var WebSocket = class extends Client {
474
548
  };
475
549
  _isConnected = new WeakMap();
476
550
  _sampleRate2 = new WeakMap();
551
+ _container2 = new WeakMap();
552
+ _encoding2 = new WeakMap();
477
553
  _generateId = new WeakSet();
478
554
  generateId_fn = function() {
479
555
  return (0, import_human_id.humanId)({
package/dist/tts/index.js CHANGED
@@ -1,12 +1,12 @@
1
1
  import {
2
2
  TTS
3
- } from "../chunk-F4QWVJY3.js";
4
- import "../chunk-FN7BK4PS.js";
5
- import "../chunk-PQ6CIPFW.js";
6
- import "../chunk-PQ5EVEEH.js";
3
+ } from "../chunk-EDAAHENY.js";
4
+ import "../chunk-IZBPLCGW.js";
5
+ import "../chunk-PISCPZK4.js";
7
6
  import "../chunk-2BFEKY3F.js";
8
- import "../chunk-RO7TY474.js";
9
- import "../chunk-WIFMLPT5.js";
7
+ import "../chunk-BHY7MNGT.js";
8
+ import "../chunk-6YQ6KDIQ.js";
9
+ import "../chunk-GHY2WEOK.js";
10
10
  export {
11
11
  TTS as default
12
12
  };
@@ -78,6 +78,11 @@ module.exports = __toCommonJS(player_exports);
78
78
 
79
79
  // src/tts/utils.ts
80
80
  var import_base64_js = __toESM(require("base64-js"), 1);
81
+
82
+ // src/tts/source.ts
83
+ var import_emittery = __toESM(require("emittery"), 1);
84
+
85
+ // src/tts/utils.ts
81
86
  function playAudioBuffer(floats, context, startAt, sampleRate) {
82
87
  const source = context.createBufferSource();
83
88
  const buffer = context.createBuffer(1, floats.length, sampleRate);
@@ -1,8 +1,9 @@
1
1
  import {
2
2
  Player
3
- } from "../chunk-JYLAM6VU.js";
4
- import "../chunk-RO7TY474.js";
5
- import "../chunk-WIFMLPT5.js";
3
+ } from "../chunk-LZO6K34D.js";
4
+ import "../chunk-BHY7MNGT.js";
5
+ import "../chunk-6YQ6KDIQ.js";
6
+ import "../chunk-GHY2WEOK.js";
6
7
  export {
7
8
  Player as default
8
9
  };
@@ -44,6 +44,10 @@ var __privateSet = (obj, member, value, setter) => {
44
44
  setter ? setter.call(obj, value) : member.set(obj, value);
45
45
  return value;
46
46
  };
47
+ var __privateMethod = (obj, member, method) => {
48
+ __accessCheck(obj, member, "access private method");
49
+ return method;
50
+ };
47
51
  var __async = (__this, __arguments, generator) => {
48
52
  return new Promise((resolve, reject) => {
49
53
  var fulfilled = (value) => {
@@ -68,11 +72,18 @@ var __async = (__this, __arguments, generator) => {
68
72
  // src/tts/source.ts
69
73
  var source_exports = {};
70
74
  __export(source_exports, {
75
+ ENCODING_MAP: () => ENCODING_MAP,
71
76
  default: () => Source
72
77
  });
73
78
  module.exports = __toCommonJS(source_exports);
74
79
  var import_emittery = __toESM(require("emittery"), 1);
75
- var _emitter, _buffer, _readIndex, _writeIndex, _closed, _sampleRate;
80
+ var ENCODING_MAP = {
81
+ pcm_f32le: { arrayType: Float32Array, bytesPerElement: 4 },
82
+ pcm_s16le: { arrayType: Int16Array, bytesPerElement: 2 },
83
+ pcm_alaw: { arrayType: Uint8Array, bytesPerElement: 1 },
84
+ pcm_mulaw: { arrayType: Uint8Array, bytesPerElement: 1 }
85
+ };
86
+ var _emitter, _buffer, _readIndex, _writeIndex, _closed, _sampleRate, _encoding, _container, _createBuffer, createBuffer_fn;
76
87
  var Source = class {
77
88
  /**
78
89
  * Create a new Source.
@@ -80,23 +91,44 @@ var Source = class {
80
91
  * @param options - Options for the Source.
81
92
  * @param options.sampleRate - The sample rate of the audio.
82
93
  */
83
- constructor({ sampleRate }) {
94
+ constructor({
95
+ sampleRate,
96
+ encoding,
97
+ container
98
+ }) {
99
+ /**
100
+ * Create a new buffer for the source.
101
+ *
102
+ * @param size - The size of the buffer to create.
103
+ * @returns The new buffer as a TypedArray based on the encoding.
104
+ */
105
+ __privateAdd(this, _createBuffer);
84
106
  __privateAdd(this, _emitter, new import_emittery.default());
85
107
  __privateAdd(this, _buffer, void 0);
86
108
  __privateAdd(this, _readIndex, 0);
87
109
  __privateAdd(this, _writeIndex, 0);
88
110
  __privateAdd(this, _closed, false);
89
111
  __privateAdd(this, _sampleRate, void 0);
112
+ __privateAdd(this, _encoding, void 0);
113
+ __privateAdd(this, _container, void 0);
90
114
  this.on = __privateGet(this, _emitter).on.bind(__privateGet(this, _emitter));
91
115
  this.once = __privateGet(this, _emitter).once.bind(__privateGet(this, _emitter));
92
116
  this.events = __privateGet(this, _emitter).events.bind(__privateGet(this, _emitter));
93
117
  this.off = __privateGet(this, _emitter).off.bind(__privateGet(this, _emitter));
94
118
  __privateSet(this, _sampleRate, sampleRate);
95
- __privateSet(this, _buffer, new Float32Array(1024));
119
+ __privateSet(this, _encoding, encoding);
120
+ __privateSet(this, _container, container);
121
+ __privateSet(this, _buffer, __privateMethod(this, _createBuffer, createBuffer_fn).call(this, 1024));
96
122
  }
97
123
  get sampleRate() {
98
124
  return __privateGet(this, _sampleRate);
99
125
  }
126
+ get encoding() {
127
+ return __privateGet(this, _encoding);
128
+ }
129
+ get container() {
130
+ return __privateGet(this, _container);
131
+ }
100
132
  /**
101
133
  * Append audio to the buffer.
102
134
  *
@@ -110,7 +142,7 @@ var Source = class {
110
142
  while (newCapacity < requiredCapacity) {
111
143
  newCapacity *= 2;
112
144
  }
113
- const newBuffer = new Float32Array(newCapacity);
145
+ const newBuffer = __privateMethod(this, _createBuffer, createBuffer_fn).call(this, newCapacity);
114
146
  newBuffer.set(__privateGet(this, _buffer));
115
147
  __privateSet(this, _buffer, newBuffer);
116
148
  }
@@ -158,6 +190,9 @@ var Source = class {
158
190
  get readIndex() {
159
191
  return __privateGet(this, _readIndex);
160
192
  }
193
+ get writeIndex() {
194
+ return __privateGet(this, _writeIndex);
195
+ }
161
196
  /**
162
197
  * Close the source. This signals that no more audio will be enqueued.
163
198
  *
@@ -179,3 +214,14 @@ _readIndex = new WeakMap();
179
214
  _writeIndex = new WeakMap();
180
215
  _closed = new WeakMap();
181
216
  _sampleRate = new WeakMap();
217
+ _encoding = new WeakMap();
218
+ _container = new WeakMap();
219
+ _createBuffer = new WeakSet();
220
+ createBuffer_fn = function(size) {
221
+ const { arrayType: ArrayType } = ENCODING_MAP[__privateGet(this, _encoding)];
222
+ return new ArrayType(size);
223
+ };
224
+ // Annotate the CommonJS export names for ESM import in node:
225
+ 0 && (module.exports = {
226
+ ENCODING_MAP
227
+ });
@@ -1,6 +1,11 @@
1
1
  import * as emittery from 'emittery';
2
- import { SourceEventData } from '../types/index.cjs';
2
+ import { Encoding, SourceEventData, TypedArray } from '../types/index.cjs';
3
3
 
4
+ type EncodingInfo = {
5
+ arrayType: Float32ArrayConstructor | Int16ArrayConstructor | Uint8ArrayConstructor;
6
+ bytesPerElement: number;
7
+ };
8
+ declare const ENCODING_MAP: Record<Encoding, EncodingInfo>;
4
9
  declare class Source {
5
10
  #private;
6
11
  on: <Name extends keyof SourceEventData | keyof emittery.OmnipresentEventData>(eventName: Name | readonly Name[], listener: (eventData: (SourceEventData & emittery.OmnipresentEventData)[Name]) => void | Promise<void>) => emittery.UnsubscribeFunction;
@@ -13,16 +18,20 @@ declare class Source {
13
18
  * @param options - Options for the Source.
14
19
  * @param options.sampleRate - The sample rate of the audio.
15
20
  */
16
- constructor({ sampleRate }: {
21
+ constructor({ sampleRate, encoding, container, }: {
17
22
  sampleRate: number;
23
+ encoding: string;
24
+ container: string;
18
25
  });
19
26
  get sampleRate(): number;
27
+ get encoding(): Encoding;
28
+ get container(): string;
20
29
  /**
21
30
  * Append audio to the buffer.
22
31
  *
23
32
  * @param src The audio to append.
24
33
  */
25
- enqueue(src: Float32Array): Promise<void>;
34
+ enqueue(src: TypedArray): Promise<void>;
26
35
  /**
27
36
  * Read audio from the buffer.
28
37
  *
@@ -30,7 +39,7 @@ declare class Source {
30
39
  * @returns The number of samples read. If the source is closed, this will be
31
40
  * less than the length of the provided buffer.
32
41
  */
33
- read(dst: Float32Array): Promise<number>;
42
+ read(dst: TypedArray): Promise<number>;
34
43
  /**
35
44
  * Get the number of samples in a given duration.
36
45
  *
@@ -38,8 +47,9 @@ declare class Source {
38
47
  * @returns The number of samples.
39
48
  */
40
49
  durationToSampleCount(durationSecs: number): number;
41
- get buffer(): Float32Array;
50
+ get buffer(): TypedArray;
42
51
  get readIndex(): number;
52
+ get writeIndex(): number;
43
53
  /**
44
54
  * Close the source. This signals that no more audio will be enqueued.
45
55
  *
@@ -50,4 +60,4 @@ declare class Source {
50
60
  close(): Promise<void>;
51
61
  }
52
62
 
53
- export { Source as default };
63
+ export { ENCODING_MAP, Source as default };
@@ -1,6 +1,11 @@
1
1
  import * as emittery from 'emittery';
2
- import { SourceEventData } from '../types/index.js';
2
+ import { Encoding, SourceEventData, TypedArray } from '../types/index.js';
3
3
 
4
+ type EncodingInfo = {
5
+ arrayType: Float32ArrayConstructor | Int16ArrayConstructor | Uint8ArrayConstructor;
6
+ bytesPerElement: number;
7
+ };
8
+ declare const ENCODING_MAP: Record<Encoding, EncodingInfo>;
4
9
  declare class Source {
5
10
  #private;
6
11
  on: <Name extends keyof SourceEventData | keyof emittery.OmnipresentEventData>(eventName: Name | readonly Name[], listener: (eventData: (SourceEventData & emittery.OmnipresentEventData)[Name]) => void | Promise<void>) => emittery.UnsubscribeFunction;
@@ -13,16 +18,20 @@ declare class Source {
13
18
  * @param options - Options for the Source.
14
19
  * @param options.sampleRate - The sample rate of the audio.
15
20
  */
16
- constructor({ sampleRate }: {
21
+ constructor({ sampleRate, encoding, container, }: {
17
22
  sampleRate: number;
23
+ encoding: string;
24
+ container: string;
18
25
  });
19
26
  get sampleRate(): number;
27
+ get encoding(): Encoding;
28
+ get container(): string;
20
29
  /**
21
30
  * Append audio to the buffer.
22
31
  *
23
32
  * @param src The audio to append.
24
33
  */
25
- enqueue(src: Float32Array): Promise<void>;
34
+ enqueue(src: TypedArray): Promise<void>;
26
35
  /**
27
36
  * Read audio from the buffer.
28
37
  *
@@ -30,7 +39,7 @@ declare class Source {
30
39
  * @returns The number of samples read. If the source is closed, this will be
31
40
  * less than the length of the provided buffer.
32
41
  */
33
- read(dst: Float32Array): Promise<number>;
42
+ read(dst: TypedArray): Promise<number>;
34
43
  /**
35
44
  * Get the number of samples in a given duration.
36
45
  *
@@ -38,8 +47,9 @@ declare class Source {
38
47
  * @returns The number of samples.
39
48
  */
40
49
  durationToSampleCount(durationSecs: number): number;
41
- get buffer(): Float32Array;
50
+ get buffer(): TypedArray;
42
51
  get readIndex(): number;
52
+ get writeIndex(): number;
43
53
  /**
44
54
  * Close the source. This signals that no more audio will be enqueued.
45
55
  *
@@ -50,4 +60,4 @@ declare class Source {
50
60
  close(): Promise<void>;
51
61
  }
52
62
 
53
- export { Source as default };
63
+ export { ENCODING_MAP, Source as default };