@ikonai/sdk 0.0.42 → 1.0.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.
@@ -0,0 +1,1138 @@
1
+ function B(t, e, r, n, i) {
2
+ const s = i?.trackId ?? 0, a = i?.sequenceId ?? 0, c = i?.flags ?? 0, l = i?.targetIds ?? [], d = i?.payloadType ?? 8, w = 27 + l.length * 4, I = w + e.length, y = new Uint8Array(I), u = new DataView(y.buffer);
3
+ u.setUint32(0, I, !0), u.setUint32(4, t >>> 0, !0), u.setUint32(8, n >>> 0, !0), u.setUint32(12, s >>> 0, !0), u.setUint32(16, a >>> 0, !0), u.setUint32(20, l.length >>> 0, !0), u.setUint8(24, r & 255), u.setUint8(25, d & 255), u.setUint8(26, c & 255);
4
+ let v = 27;
5
+ for (let U = 0; U < l.length; U++)
6
+ u.setUint32(v, l[U] >>> 0, !0), v += 4;
7
+ return y.set(e, w), y;
8
+ }
9
+ var f = /* @__PURE__ */ ((t) => (t[t.Null = 1] = "Null", t[t.Bool = 2] = "Bool", t[t.Int32 = 3] = "Int32", t[t.Int64 = 4] = "Int64", t[t.UInt32 = 5] = "UInt32", t[t.UInt64 = 6] = "UInt64", t[t.Float32 = 7] = "Float32", t[t.Float64 = 8] = "Float64", t[t.Array = 9] = "Array", t[t.Dict = 10] = "Dict", t[t.Object = 11] = "Object", t[t.String = 12] = "String", t[t.Binary = 13] = "Binary", t[t.Guid = 14] = "Guid", t))(f || {});
10
+ const K = 161, $ = 162, b = new TextEncoder();
11
+ new TextDecoder("utf-8", { fatal: !0 });
12
+ class E {
13
+ constructor(e = 1) {
14
+ this.version = e, this.buffer.writeByte(K), this.buffer.writeVarUInt(e >>> 0);
15
+ }
16
+ buffer = new m();
17
+ closed = !1;
18
+ cached;
19
+ writeInt32Field(e, r) {
20
+ this.writeFixedField(e, 3, () => this.buffer.writeInt32(r | 0));
21
+ }
22
+ writeUInt32Field(e, r) {
23
+ this.writeFixedField(e, 5, () => this.buffer.writeUInt32(r >>> 0));
24
+ }
25
+ writeInt64Field(e, r) {
26
+ this.writeFixedField(e, 4, () => this.buffer.writeBigInt64(r));
27
+ }
28
+ writeUInt64Field(e, r) {
29
+ this.writeFixedField(e, 6, () => this.buffer.writeBigUInt64(r));
30
+ }
31
+ writeFloat32Field(e, r) {
32
+ this.writeFixedField(e, 7, () => this.buffer.writeFloat32(r));
33
+ }
34
+ writeFloat64Field(e, r) {
35
+ this.writeFixedField(e, 8, () => this.buffer.writeFloat64(r));
36
+ }
37
+ writeBoolField(e, r) {
38
+ this.writeFixedField(e, 2, () => this.buffer.writeByte(r ? 1 : 0));
39
+ }
40
+ writeGuidField(e, r) {
41
+ const n = r instanceof o ? r.asBytes() : r;
42
+ if (n.length !== 16)
43
+ throw new Error("Guid payload must be 16 bytes");
44
+ this.writeFixedField(e, 14, () => this.buffer.writeBytes(n));
45
+ }
46
+ writeStringField(e, r) {
47
+ const n = b.encode(r ?? "");
48
+ this.writeVariableField(e, 12, n);
49
+ }
50
+ writeBinaryField(e, r) {
51
+ this.writeVariableField(e, 13, r);
52
+ }
53
+ writeObjectField(e, r, n) {
54
+ const i = new E(r);
55
+ n(i);
56
+ const s = i.finish();
57
+ this.writeVariableField(e, 11, s);
58
+ }
59
+ writeArrayField(e, r, n) {
60
+ const i = new _(r);
61
+ n(i);
62
+ const s = i.finish();
63
+ this.writeVariableField(e, 9, s);
64
+ }
65
+ writeDictionaryField(e, r, n, i) {
66
+ const s = new O(r, n);
67
+ i(s);
68
+ const a = s.finish();
69
+ this.writeVariableField(e, 10, a);
70
+ }
71
+ finish() {
72
+ return this.closed || (this.buffer.writeByte($), this.closed = !0, this.cached = this.buffer.toUint8Array()), this.cached;
73
+ }
74
+ writeFixedField(e, r, n) {
75
+ this.writeFieldHeader(e, r, 0), n();
76
+ }
77
+ writeVariableField(e, r, n) {
78
+ this.writeFieldHeader(e, r, n.length), this.buffer.writeBytes(n);
79
+ }
80
+ writeFieldHeader(e, r, n) {
81
+ this.buffer.writeUInt32(e >>> 0), this.buffer.writeByte(F(r)), q(r) && this.buffer.writeVarUInt(n >>> 0);
82
+ }
83
+ }
84
+ class _ {
85
+ constructor(e) {
86
+ this.elementType = e;
87
+ }
88
+ payload = new m();
89
+ count = 0;
90
+ writeInt32(e) {
91
+ this.ensureElementType(
92
+ 3
93
+ /* Int32 */
94
+ ), this.count++, this.payload.writeInt32(e | 0);
95
+ }
96
+ writeUInt32(e) {
97
+ this.ensureElementType(
98
+ 5
99
+ /* UInt32 */
100
+ ), this.count++, this.payload.writeUInt32(e >>> 0);
101
+ }
102
+ writeInt64(e) {
103
+ this.ensureElementType(
104
+ 4
105
+ /* Int64 */
106
+ ), this.count++, this.payload.writeBigInt64(e);
107
+ }
108
+ writeUInt64(e) {
109
+ this.ensureElementType(
110
+ 6
111
+ /* UInt64 */
112
+ ), this.count++, this.payload.writeBigUInt64(e);
113
+ }
114
+ writeFloat32(e) {
115
+ this.ensureElementType(
116
+ 7
117
+ /* Float32 */
118
+ ), this.count++, this.payload.writeFloat32(e);
119
+ }
120
+ writeFloat64(e) {
121
+ this.ensureElementType(
122
+ 8
123
+ /* Float64 */
124
+ ), this.count++, this.payload.writeFloat64(e);
125
+ }
126
+ writeBool(e) {
127
+ this.ensureElementType(
128
+ 2
129
+ /* Bool */
130
+ ), this.count++, this.payload.writeByte(e ? 1 : 0);
131
+ }
132
+ writeGuid(e) {
133
+ this.ensureElementType(
134
+ 14
135
+ /* Guid */
136
+ ), this.count++;
137
+ const r = e instanceof o ? e.asBytes() : e;
138
+ if (r.length !== 16)
139
+ throw new Error("Guid payload must be 16 bytes");
140
+ this.payload.writeBytes(r);
141
+ }
142
+ writeString(e) {
143
+ this.ensureElementType(
144
+ 12
145
+ /* String */
146
+ ), this.count++;
147
+ const r = b.encode(e ?? "");
148
+ this.payload.writeVarUInt(r.length), this.payload.writeBytes(r);
149
+ }
150
+ writeBinary(e) {
151
+ this.ensureElementType(
152
+ 13
153
+ /* Binary */
154
+ ), this.count++, this.payload.writeVarUInt(e.length), this.payload.writeBytes(e);
155
+ }
156
+ writeObject(e, r) {
157
+ this.ensureElementType(
158
+ 11
159
+ /* Object */
160
+ ), this.count++;
161
+ const n = new E(e);
162
+ r(n);
163
+ const i = n.finish();
164
+ this.payload.writeVarUInt(i.length), this.payload.writeBytes(i);
165
+ }
166
+ writeArray(e, r) {
167
+ this.ensureElementType(
168
+ 9
169
+ /* Array */
170
+ ), this.count++;
171
+ const n = new _(e);
172
+ r(n);
173
+ const i = n.finish();
174
+ this.payload.writeBytes(i);
175
+ }
176
+ writeDictionary(e, r, n) {
177
+ this.ensureElementType(
178
+ 10
179
+ /* Dict */
180
+ ), this.count++;
181
+ const i = new O(e, r);
182
+ n(i);
183
+ const s = i.finish();
184
+ this.payload.writeBytes(s);
185
+ }
186
+ finish() {
187
+ const e = new m();
188
+ return e.writeByte(F(this.elementType)), e.writeVarUInt(this.count), e.writeBytes(this.payload.toUint8Array()), e.toUint8Array();
189
+ }
190
+ ensureElementType(e) {
191
+ if (this.elementType !== e)
192
+ throw new Error(`Array element type is ${f[this.elementType]}, expected ${f[e]}`);
193
+ }
194
+ }
195
+ class O {
196
+ constructor(e, r) {
197
+ this.keyType = e, this.valueType = r, G(e);
198
+ }
199
+ payload = new m();
200
+ count = 0;
201
+ entryOpen = !1;
202
+ beginEntry() {
203
+ if (this.entryOpen)
204
+ throw new Error("Previous dictionary entry not completed");
205
+ return this.count++, this.entryOpen = !0, new j(this.keyType, this.valueType, this.payload, () => {
206
+ this.entryOpen = !1;
207
+ });
208
+ }
209
+ finish() {
210
+ if (this.entryOpen)
211
+ throw new Error("Dictionary entry not completed");
212
+ const e = new m();
213
+ return e.writeByte(F(this.keyType)), e.writeByte(F(this.valueType)), e.writeVarUInt(this.count), e.writeBytes(this.payload.toUint8Array()), e.toUint8Array();
214
+ }
215
+ }
216
+ class j {
217
+ constructor(e, r, n, i) {
218
+ this.keyType = e, this.valueType = r, this.payload = n, this.onComplete = i;
219
+ }
220
+ keyWritten = !1;
221
+ valueWritten = !1;
222
+ completed = !1;
223
+ writeKeyInt32(e) {
224
+ this.ensureKeyType(
225
+ 3
226
+ /* Int32 */
227
+ ), this.payload.writeInt32(e | 0), this.keyWritten = !0;
228
+ }
229
+ writeKeyUInt32(e) {
230
+ this.ensureKeyType(
231
+ 5
232
+ /* UInt32 */
233
+ ), this.payload.writeUInt32(e >>> 0), this.keyWritten = !0;
234
+ }
235
+ writeKeyInt64(e) {
236
+ this.ensureKeyType(
237
+ 4
238
+ /* Int64 */
239
+ ), this.payload.writeBigInt64(e), this.keyWritten = !0;
240
+ }
241
+ writeKeyUInt64(e) {
242
+ this.ensureKeyType(
243
+ 6
244
+ /* UInt64 */
245
+ ), this.payload.writeBigUInt64(e), this.keyWritten = !0;
246
+ }
247
+ writeKeyFloat32(e) {
248
+ this.ensureKeyType(
249
+ 7
250
+ /* Float32 */
251
+ ), this.payload.writeFloat32(e), this.keyWritten = !0;
252
+ }
253
+ writeKeyFloat64(e) {
254
+ this.ensureKeyType(
255
+ 8
256
+ /* Float64 */
257
+ ), this.payload.writeFloat64(e), this.keyWritten = !0;
258
+ }
259
+ writeKeyBool(e) {
260
+ this.ensureKeyType(
261
+ 2
262
+ /* Bool */
263
+ ), this.payload.writeByte(e ? 1 : 0), this.keyWritten = !0;
264
+ }
265
+ writeKeyGuid(e) {
266
+ this.ensureKeyType(
267
+ 14
268
+ /* Guid */
269
+ );
270
+ const r = e instanceof o ? e.asBytes() : e;
271
+ if (r.length !== 16)
272
+ throw new Error("Guid payload must be 16 bytes");
273
+ this.payload.writeBytes(r), this.keyWritten = !0;
274
+ }
275
+ writeKeyString(e) {
276
+ this.ensureKeyType(
277
+ 12
278
+ /* String */
279
+ );
280
+ const r = b.encode(e ?? "");
281
+ this.payload.writeVarUInt(r.length), this.payload.writeBytes(r), this.keyWritten = !0;
282
+ }
283
+ writeKeyBinary(e) {
284
+ this.ensureKeyType(
285
+ 13
286
+ /* Binary */
287
+ ), this.payload.writeVarUInt(e.length), this.payload.writeBytes(e), this.keyWritten = !0;
288
+ }
289
+ writeValueInt32(e) {
290
+ this.ensureValueType(
291
+ 3
292
+ /* Int32 */
293
+ ), this.payload.writeInt32(e | 0), this.valueWritten = !0;
294
+ }
295
+ writeValueUInt32(e) {
296
+ this.ensureValueType(
297
+ 5
298
+ /* UInt32 */
299
+ ), this.payload.writeUInt32(e >>> 0), this.valueWritten = !0;
300
+ }
301
+ writeValueInt64(e) {
302
+ this.ensureValueType(
303
+ 4
304
+ /* Int64 */
305
+ ), this.payload.writeBigInt64(e), this.valueWritten = !0;
306
+ }
307
+ writeValueUInt64(e) {
308
+ this.ensureValueType(
309
+ 6
310
+ /* UInt64 */
311
+ ), this.payload.writeBigUInt64(e), this.valueWritten = !0;
312
+ }
313
+ writeValueFloat32(e) {
314
+ this.ensureValueType(
315
+ 7
316
+ /* Float32 */
317
+ ), this.payload.writeFloat32(e), this.valueWritten = !0;
318
+ }
319
+ writeValueFloat64(e) {
320
+ this.ensureValueType(
321
+ 8
322
+ /* Float64 */
323
+ ), this.payload.writeFloat64(e), this.valueWritten = !0;
324
+ }
325
+ writeValueBool(e) {
326
+ this.ensureValueType(
327
+ 2
328
+ /* Bool */
329
+ ), this.payload.writeByte(e ? 1 : 0), this.valueWritten = !0;
330
+ }
331
+ writeValueGuid(e) {
332
+ this.ensureValueType(
333
+ 14
334
+ /* Guid */
335
+ );
336
+ const r = e instanceof o ? e.asBytes() : e;
337
+ if (r.length !== 16)
338
+ throw new Error("Guid payload must be 16 bytes");
339
+ this.payload.writeBytes(r), this.valueWritten = !0;
340
+ }
341
+ writeNullValue() {
342
+ this.ensureValueType(
343
+ 1
344
+ /* Null */
345
+ ), this.valueWritten = !0;
346
+ }
347
+ writeValueBinary(e) {
348
+ this.ensureValueType(
349
+ 13
350
+ /* Binary */
351
+ ), this.payload.writeVarUInt(e.length), this.payload.writeBytes(e), this.valueWritten = !0;
352
+ }
353
+ writeValueString(e) {
354
+ this.ensureValueType(
355
+ 12
356
+ /* String */
357
+ );
358
+ const r = b.encode(e ?? "");
359
+ this.payload.writeVarUInt(r.length), this.payload.writeBytes(r), this.valueWritten = !0;
360
+ }
361
+ writeValueObject(e, r) {
362
+ this.ensureValueType(
363
+ 11
364
+ /* Object */
365
+ );
366
+ const n = new E(e);
367
+ r(n);
368
+ const i = n.finish();
369
+ this.payload.writeVarUInt(i.length), this.payload.writeBytes(i), this.valueWritten = !0;
370
+ }
371
+ writeValueArray(e, r) {
372
+ this.ensureValueType(
373
+ 9
374
+ /* Array */
375
+ );
376
+ const n = new _(e);
377
+ r(n);
378
+ const i = n.finish();
379
+ this.payload.writeBytes(i), this.valueWritten = !0;
380
+ }
381
+ writeValueDictionary(e, r, n) {
382
+ this.ensureValueType(
383
+ 10
384
+ /* Dict */
385
+ );
386
+ const i = new O(e, r);
387
+ n(i);
388
+ const s = i.finish();
389
+ this.payload.writeBytes(s), this.valueWritten = !0;
390
+ }
391
+ complete() {
392
+ if (!this.completed) {
393
+ if (!this.keyWritten || !this.valueWritten)
394
+ throw new Error("Dictionary entry must write both key and value");
395
+ this.completed = !0, this.onComplete();
396
+ }
397
+ }
398
+ ensureKeyType(e) {
399
+ if (this.keyType !== e)
400
+ throw new Error(`Dictionary key type is ${f[this.keyType]}, expected ${f[e]}`);
401
+ }
402
+ ensureValueType(e) {
403
+ if (this.valueType !== e)
404
+ throw new Error(`Dictionary value type is ${f[this.valueType]}, expected ${f[e]}`);
405
+ }
406
+ }
407
+ class o {
408
+ constructor(e) {
409
+ this.bytes = e;
410
+ }
411
+ static fromString(e) {
412
+ if (!e)
413
+ throw new Error("Guid string is empty");
414
+ const r = e.replace(/-/g, "");
415
+ if (r.length !== 32)
416
+ throw new Error("Guid string must be 32 hex characters");
417
+ const n = new Uint8Array(16), i = o.parseHexSlice(r, 0, 8), s = o.parseHexSlice(r, 8, 4), a = o.parseHexSlice(r, 12, 4);
418
+ o.writeUInt32LE(n, 0, i), o.writeUInt16LE(n, 4, s), o.writeUInt16LE(n, 6, a);
419
+ for (let c = 0; c < 8; c++)
420
+ n[8 + c] = o.parseHexSlice(r, 16 + c * 2, 2);
421
+ return new o(n);
422
+ }
423
+ static fromBytes(e) {
424
+ if (e.length !== 16)
425
+ throw new Error("Guid byte array must be 16 bytes");
426
+ return new o(Uint8Array.from(e));
427
+ }
428
+ static createZero() {
429
+ return new o(new Uint8Array(16));
430
+ }
431
+ static createRandom() {
432
+ const e = new Uint8Array(16), r = globalThis.crypto;
433
+ if (r?.getRandomValues)
434
+ r.getRandomValues(e);
435
+ else
436
+ for (let n = 0; n < e.length; n++)
437
+ e[n] = Math.floor(Math.random() * 256);
438
+ return e[6] = e[6] & 15 | 64, e[8] = e[8] & 63 | 128, new o(e);
439
+ }
440
+ toString() {
441
+ const e = this.bytes;
442
+ return [
443
+ o.toHex(o.readUInt32LE(e, 0), 8),
444
+ o.toHex(o.readUInt16LE(e, 4), 4),
445
+ o.toHex(o.readUInt16LE(e, 6), 4),
446
+ C(e.subarray(8, 10)),
447
+ C(e.subarray(10, 16))
448
+ ].join("-");
449
+ }
450
+ asBytes() {
451
+ return this.bytes.slice();
452
+ }
453
+ static parseHexSlice(e, r, n) {
454
+ const i = e.substr(r, n), s = Number.parseInt(i, 16);
455
+ if (Number.isNaN(s))
456
+ throw new Error("Guid string contains invalid characters");
457
+ return s >>> 0;
458
+ }
459
+ static writeUInt32LE(e, r, n) {
460
+ const i = n >>> 0;
461
+ e[r] = i & 255, e[r + 1] = i >>> 8 & 255, e[r + 2] = i >>> 16 & 255, e[r + 3] = i >>> 24 & 255;
462
+ }
463
+ static writeUInt16LE(e, r, n) {
464
+ const i = n & 65535;
465
+ e[r] = i & 255, e[r + 1] = i >>> 8 & 255;
466
+ }
467
+ static readUInt32LE(e, r) {
468
+ return (e[r] | e[r + 1] << 8 | e[r + 2] << 16 | e[r + 3] << 24) >>> 0;
469
+ }
470
+ static readUInt16LE(e, r) {
471
+ return (e[r] | e[r + 1] << 8) & 65535;
472
+ }
473
+ static toHex(e, r) {
474
+ return (e >>> 0).toString(16).padStart(r, "0");
475
+ }
476
+ }
477
+ class m {
478
+ static INITIAL_CAPACITY = 256;
479
+ buffer;
480
+ dataView;
481
+ length = 0;
482
+ constructor() {
483
+ this.buffer = new Uint8Array(m.INITIAL_CAPACITY), this.dataView = new DataView(this.buffer.buffer);
484
+ }
485
+ ensureCapacity(e) {
486
+ const r = this.length + e;
487
+ if (r <= this.buffer.length)
488
+ return;
489
+ let n = this.buffer.length;
490
+ for (; n < r; )
491
+ n *= 2;
492
+ const i = new Uint8Array(n);
493
+ i.set(this.buffer.subarray(0, this.length)), this.buffer = i, this.dataView = new DataView(this.buffer.buffer);
494
+ }
495
+ writeByte(e) {
496
+ this.ensureCapacity(1), this.buffer[this.length++] = e & 255;
497
+ }
498
+ writeBytes(e) {
499
+ this.ensureCapacity(e.length), this.buffer.set(e, this.length), this.length += e.length;
500
+ }
501
+ writeUInt32(e) {
502
+ this.ensureCapacity(4), this.dataView.setUint32(this.length, e >>> 0, !0), this.length += 4;
503
+ }
504
+ writeInt32(e) {
505
+ this.ensureCapacity(4), this.dataView.setInt32(this.length, e | 0, !0), this.length += 4;
506
+ }
507
+ writeFloat32(e) {
508
+ this.ensureCapacity(4), this.dataView.setFloat32(this.length, e, !0), this.length += 4;
509
+ }
510
+ writeFloat64(e) {
511
+ this.ensureCapacity(8), this.dataView.setFloat64(this.length, e, !0), this.length += 8;
512
+ }
513
+ writeBigInt64(e) {
514
+ this.ensureCapacity(8), this.dataView.setBigInt64(this.length, e, !0), this.length += 8;
515
+ }
516
+ writeBigUInt64(e) {
517
+ this.ensureCapacity(8), this.dataView.setBigUint64(this.length, e, !0), this.length += 8;
518
+ }
519
+ writeVarUInt(e) {
520
+ let r = e >>> 0;
521
+ for (; r >= 128; )
522
+ this.ensureCapacity(1), this.buffer[this.length++] = r & 127 | 128, r >>>= 7;
523
+ this.ensureCapacity(1), this.buffer[this.length++] = r & 127;
524
+ }
525
+ toUint8Array() {
526
+ return this.buffer.slice(0, this.length);
527
+ }
528
+ }
529
+ function F(t, e = 0) {
530
+ if ((e & 240) !== 0)
531
+ throw new Error("Teleport flags must fit into 4 bits");
532
+ return (t & 15) << 4 | e & 15;
533
+ }
534
+ function q(t) {
535
+ return t === 12 || t === 13 || t === 9 || t === 11 || t === 10;
536
+ }
537
+ function G(t) {
538
+ if (t === 9 || t === 11 || t === 10 || t === 1)
539
+ throw new Error("Dictionary keys must be primitive Teleport types");
540
+ }
541
+ function C(t) {
542
+ return Array.from(t).map((e) => e.toString(16).padStart(2, "0")).join("");
543
+ }
544
+ var R = /* @__PURE__ */ ((t) => (t[t.Unknown = 0] = "Unknown", t[t.Opus = 1] = "Opus", t[t.Mp3 = 2] = "Mp3", t[t.RawPcm16 = 3] = "RawPcm16", t))(R || {});
545
+ const N = 1, Y = 4194309, J = 205938238, Z = 717315017, Q = 1594075008, X = 2026534360, ee = 3409001585, te = 3466650495, re = 4007456593, ne = 4017217601, ie = 4065417427, se = 4086360332;
546
+ function ae(t) {
547
+ const e = {};
548
+ return oe(e), t && Object.assign(e, t), e;
549
+ }
550
+ function oe(t) {
551
+ return t.Samples = new Uint8Array(0), t.Epoch = 0, t.Sequence = 0, t.FrameSizeInInterleavedSamples = 0, t.TimeStampInInterleavedSamples = 0n, t.IsFirst = !1, t.IsLast = !1, t.AverageVolume = 0, t.AudioEventEstimatedDuration = 0, t.ShapeSetValues = void 0, t;
552
+ }
553
+ function ue(t) {
554
+ const e = new E(N);
555
+ return ce(t, e), e.finish();
556
+ }
557
+ function ce(t, e) {
558
+ e.writeFloat32Field(J, t.AverageVolume);
559
+ const r = t.ShapeSetValues;
560
+ r != null && e.writeArrayField(Z, f.Object, (n) => {
561
+ for (const i of r)
562
+ n.writeObject(he, (s) => {
563
+ pe(i, s);
564
+ });
565
+ }), e.writeBoolField(Q, t.IsFirst), e.writeUInt64Field(X, t.TimeStampInInterleavedSamples), e.writeBinaryField(ee, t.Samples), e.writeFloat32Field(te, t.AudioEventEstimatedDuration), e.writeUInt32Field(re, t.FrameSizeInInterleavedSamples >>> 0), e.writeBoolField(ne, t.IsLast), e.writeUInt32Field(ie, t.Sequence >>> 0), e.writeUInt32Field(se, t.Epoch >>> 0);
566
+ }
567
+ function le(t, e, r) {
568
+ const n = ue(t);
569
+ return B(Y, n, N, e, r);
570
+ }
571
+ const he = 1, de = 1154362099, fe = 3974819915;
572
+ function pe(t, e) {
573
+ e.writeUInt32Field(de, t.SetId >>> 0), e.writeArrayField(fe, f.Float32, (r) => {
574
+ for (const n of t.Values)
575
+ r.writeFloat32(n);
576
+ });
577
+ }
578
+ const k = 1, ye = 4194305, we = 265814330, Ie = 1368629611, me = 2431514951, Ee = 2914494629, ge = 3284746250, Se = 4101844078;
579
+ function Ae(t) {
580
+ const e = {};
581
+ return be(e), t && Object.assign(e, t), e;
582
+ }
583
+ function be(t) {
584
+ return t.Description = "", t.Codec = R.Unknown, t.SampleRate = 0, t.Channels = 0, t.BitDepth = 0, t.ShapeSets = void 0, t;
585
+ }
586
+ function Fe(t) {
587
+ const e = new E(k);
588
+ return _e(t, e), e.finish();
589
+ }
590
+ function _e(t, e) {
591
+ const r = t.ShapeSets;
592
+ r != null && e.writeArrayField(we, f.Object, (n) => {
593
+ for (const i of r)
594
+ n.writeObject(De, (s) => {
595
+ Me(i, s);
596
+ });
597
+ }), e.writeStringField(Ie, t.Description), e.writeInt32Field(me, t.Channels | 0), e.writeInt32Field(Ee, t.BitDepth | 0), e.writeInt32Field(ge, t.Codec), e.writeInt32Field(Se, t.SampleRate | 0);
598
+ }
599
+ function Ue(t, e, r) {
600
+ const n = Fe(t);
601
+ return B(ye, n, k, e, r);
602
+ }
603
+ const De = 1, Be = 1107713536, Oe = 1154362099, Re = 1185721362;
604
+ function Me(t, e) {
605
+ e.writeStringField(Be, t.Name), e.writeUInt32Field(Oe, t.SetId >>> 0), e.writeArrayField(Re, f.String, (r) => {
606
+ for (const n of t.ShapeNames)
607
+ r.writeString(n);
608
+ });
609
+ }
610
+ const x = 1, Ve = 4194306;
611
+ function Te(t) {
612
+ const e = {};
613
+ return t && Object.assign(e, t), e;
614
+ }
615
+ function Le(t) {
616
+ return new E(x).finish();
617
+ }
618
+ function Pe(t, e, r) {
619
+ const n = Le();
620
+ return B(Ve, n, x, e, r);
621
+ }
622
+ class We {
623
+ static getStorageForCapacity(e, r) {
624
+ const n = Int32Array.BYTES_PER_ELEMENT * 2, i = r.BYTES_PER_ELEMENT * e;
625
+ return new SharedArrayBuffer(n + i);
626
+ }
627
+ readWrite;
628
+ storage;
629
+ buf;
630
+ constructor(e, r) {
631
+ this.buf = e, this.readWrite = new Int32Array(e, 0, 2);
632
+ const n = Int32Array.BYTES_PER_ELEMENT * 2, i = (e.byteLength - n) / Float32Array.BYTES_PER_ELEMENT;
633
+ this.storage = new r(e, n, i);
634
+ }
635
+ get capacity() {
636
+ return this.storage.length;
637
+ }
638
+ flush() {
639
+ Atomics.store(this.readWrite, 0, 0), Atomics.store(this.readWrite, 1, 0);
640
+ }
641
+ availableRead() {
642
+ const e = Atomics.load(this.readWrite, 0), r = Atomics.load(this.readWrite, 1);
643
+ return r >= e ? r - e : this.capacity - (e - r);
644
+ }
645
+ availableWrite() {
646
+ return this.capacity - this.availableRead() - 1;
647
+ }
648
+ push(e) {
649
+ const r = Math.min(this.availableWrite(), e.length);
650
+ if (r <= 0)
651
+ return 0;
652
+ let n = Atomics.load(this.readWrite, 1);
653
+ const i = Math.min(r, this.capacity - n);
654
+ this.storage.set(e.subarray(0, i), n);
655
+ const s = r - i;
656
+ return s > 0 && this.storage.set(e.subarray(i, i + s), 0), n = (n + r) % this.capacity, Atomics.store(this.readWrite, 1, n), Atomics.notify(this.readWrite, 1), r;
657
+ }
658
+ pop(e) {
659
+ const r = Math.min(this.availableRead(), e.length);
660
+ if (r <= 0)
661
+ return 0;
662
+ let n = Atomics.load(this.readWrite, 0);
663
+ const i = Math.min(r, this.capacity - n);
664
+ e.set(this.storage.subarray(n, n + i), 0);
665
+ const s = r - i;
666
+ return s > 0 && e.set(this.storage.subarray(0, s), i), n = (n + r) % this.capacity, Atomics.store(this.readWrite, 0, n), r;
667
+ }
668
+ skip(e) {
669
+ const r = Math.min(this.availableRead(), e);
670
+ if (r <= 0)
671
+ return 0;
672
+ let n = Atomics.load(this.readWrite, 0);
673
+ return n = (n + r) % this.capacity, Atomics.store(this.readWrite, 0, n), r;
674
+ }
675
+ }
676
+ let ve = 1;
677
+ function Ce(t, e) {
678
+ return `[${t}] ${e}`;
679
+ }
680
+ function A(t, e, r, n) {
681
+ if ((/* @__PURE__ */ new Date()).toISOString(), ve <= t) {
682
+ const i = Ce(e, r);
683
+ switch (t) {
684
+ case 0:
685
+ console.debug(i, ...n);
686
+ break;
687
+ case 1:
688
+ console.info(i, ...n);
689
+ break;
690
+ case 2:
691
+ console.warn(i, ...n);
692
+ break;
693
+ case 3:
694
+ console.error(i, ...n);
695
+ break;
696
+ }
697
+ }
698
+ }
699
+ function Ne(t) {
700
+ return {
701
+ debug(e, ...r) {
702
+ A(0, t, e, r);
703
+ },
704
+ info(e, ...r) {
705
+ A(1, t, e, r);
706
+ },
707
+ warn(e, ...r) {
708
+ A(2, t, e, r);
709
+ },
710
+ error(e, ...r) {
711
+ A(3, t, e, r);
712
+ }
713
+ };
714
+ }
715
+ const ke = 20, M = 32e3, xe = 5, V = 8e3, D = Ne("AudioCaptureWorker");
716
+ class He {
717
+ encoder = null;
718
+ isReady = !1;
719
+ sampleRate;
720
+ channels;
721
+ bitrate;
722
+ frameSize;
723
+ onOutput;
724
+ onError;
725
+ timestampUs = 0;
726
+ frameDurationUs;
727
+ constructor(e) {
728
+ this.sampleRate = e.sampleRate, this.channels = e.channels, this.bitrate = e.bitrate, this.frameSize = e.frameSize, this.frameDurationUs = Math.floor(this.frameSize * 1e6 / this.sampleRate), this.onOutput = e.onOutput, this.onError = e.onError;
729
+ }
730
+ async initialize() {
731
+ try {
732
+ const { OpusEncoder: e, OPUS_APPLICATION_VOIP: r, getOpusModule: n } = await import("./index-D2GAzJKe.js");
733
+ await n(), this.encoder = new e({
734
+ sampleRate: this.sampleRate,
735
+ channels: this.channels,
736
+ bitrate: this.bitrate,
737
+ application: r,
738
+ complexity: xe
739
+ }), await this.encoder.ready, this.isReady = !0;
740
+ } catch (e) {
741
+ this.onError(e instanceof Error ? e : new Error(String(e)));
742
+ }
743
+ }
744
+ encode(e) {
745
+ if (!(!this.isReady || !this.encoder))
746
+ try {
747
+ let r;
748
+ if (e instanceof Float32Array)
749
+ r = e;
750
+ else {
751
+ const i = e.numberOfFrames, s = e.numberOfChannels;
752
+ r = new Float32Array(i * s), e.copyTo(r, { planeIndex: 0, format: "f32" }), e.close();
753
+ }
754
+ const n = this.encoder.encodeAndCopy(r);
755
+ this.onOutput(n, this.timestampUs, this.frameDurationUs), this.timestampUs += this.frameDurationUs;
756
+ } catch (r) {
757
+ this.onError(r instanceof Error ? r : new Error(String(r)));
758
+ }
759
+ }
760
+ async flush() {
761
+ }
762
+ close() {
763
+ try {
764
+ this.encoder?.destroy();
765
+ } catch {
766
+ }
767
+ this.encoder = null, this.isReady = !1;
768
+ }
769
+ getBitrate() {
770
+ return this.bitrate;
771
+ }
772
+ }
773
+ const T = (t) => self.postMessage(t);
774
+ let p = null;
775
+ function g(t, e) {
776
+ const r = t instanceof Error ? t : new Error(String(t));
777
+ T({ type: "error", captureId: e, error: { name: r.name, message: r.message, stack: r.stack } });
778
+ }
779
+ function ze(t) {
780
+ return t.byteOffset === 0 && t.byteLength === t.buffer.byteLength ? t.buffer : t.slice().buffer;
781
+ }
782
+ function L(t) {
783
+ if (!p)
784
+ return;
785
+ const e = ze(t);
786
+ p.postMessage({ type: "send", message: e }, [e]);
787
+ }
788
+ function Ke(t) {
789
+ if (t.length === 0)
790
+ return 0;
791
+ let e = 0;
792
+ for (let r = 0; r < t.length; r++)
793
+ e += Math.abs(t[r] ?? 0);
794
+ return e / t.length;
795
+ }
796
+ const h = /* @__PURE__ */ new Map();
797
+ let S = null;
798
+ function $e() {
799
+ S === null && (S = self.setInterval(() => {
800
+ for (const t of h.values())
801
+ if (!(t.transport !== "sab" || !t.ringBuffer || !t.ringTemp || t.stopped))
802
+ try {
803
+ const e = t.ringBuffer.pop(t.ringTemp);
804
+ if (e <= 0)
805
+ continue;
806
+ W(t, t.ringTemp.subarray(0, e));
807
+ } catch (e) {
808
+ g(e, t.captureId);
809
+ }
810
+ }, 10));
811
+ }
812
+ function je() {
813
+ if (S === null)
814
+ return;
815
+ let t = !1;
816
+ for (const e of h.values())
817
+ if (e.transport === "sab" && !e.stopped) {
818
+ t = !0;
819
+ break;
820
+ }
821
+ if (!t) {
822
+ try {
823
+ self.clearInterval(S);
824
+ } catch {
825
+ }
826
+ S = null;
827
+ }
828
+ }
829
+ function P(t) {
830
+ if (t.stopped)
831
+ return;
832
+ if (!t.inSegment) {
833
+ t.pendingReadOffset = 0, t.pendingWriteOffset = 0, t.pendingSamples = 0;
834
+ return;
835
+ }
836
+ const e = t.frameSize, r = t.frameBuffer, n = t.pendingBuffer.length;
837
+ for (; t.pendingSamples >= e; ) {
838
+ const i = t.pendingReadOffset;
839
+ if (i + e <= n)
840
+ r.set(t.pendingBuffer.subarray(i, i + e));
841
+ else {
842
+ const a = n - i;
843
+ r.set(t.pendingBuffer.subarray(i, n)), r.set(t.pendingBuffer.subarray(0, e - a), a);
844
+ }
845
+ t.pendingReadOffset = (i + e) % n, t.pendingSamples -= e;
846
+ const s = Ke(r);
847
+ t.lastVolume = s;
848
+ try {
849
+ if (t.useWasmEncoder)
850
+ t.encoder.encode(r);
851
+ else {
852
+ const a = new AudioData({
853
+ format: "f32",
854
+ sampleRate: t.sampleRate,
855
+ numberOfFrames: e,
856
+ numberOfChannels: t.channels,
857
+ timestamp: t.timestampUs,
858
+ data: r.buffer
859
+ });
860
+ t.encoder.encode(a), a.close();
861
+ }
862
+ } catch (a) {
863
+ g(a, t.captureId);
864
+ }
865
+ t.timestampUs += t.frameDurationUs;
866
+ }
867
+ }
868
+ function W(t, e) {
869
+ if (t.stopped)
870
+ return;
871
+ const r = e.length;
872
+ if (r === 0)
873
+ return;
874
+ const n = t.pendingSamples + r;
875
+ if (n > t.pendingBuffer.length) {
876
+ const a = Math.max(n, t.pendingBuffer.length * 2), c = new Float32Array(a);
877
+ if (t.pendingSamples > 0) {
878
+ const l = t.pendingBuffer.length;
879
+ if (t.pendingReadOffset + t.pendingSamples <= l)
880
+ c.set(t.pendingBuffer.subarray(t.pendingReadOffset, t.pendingReadOffset + t.pendingSamples));
881
+ else {
882
+ const d = l - t.pendingReadOffset;
883
+ c.set(t.pendingBuffer.subarray(t.pendingReadOffset, l)), c.set(t.pendingBuffer.subarray(0, t.pendingSamples - d), d);
884
+ }
885
+ }
886
+ t.pendingBuffer = c, t.pendingReadOffset = 0, t.pendingWriteOffset = t.pendingSamples;
887
+ }
888
+ const i = t.pendingBuffer.length, s = Math.min(r, i - t.pendingWriteOffset);
889
+ t.pendingBuffer.set(e.subarray(0, s), t.pendingWriteOffset), s < r && t.pendingBuffer.set(e.subarray(s), 0), t.pendingWriteOffset = (t.pendingWriteOffset + r) % i, t.pendingSamples += r, P(t);
890
+ }
891
+ function H(t, e, r, n, i = !0) {
892
+ const s = h.get(t.captureId);
893
+ if (!s || s.stopped || !s.inSegment && !s.segmentEndPending)
894
+ return;
895
+ const a = e.byteLength;
896
+ s.encoderOutputBuffer.length < a && (s.encoderOutputBuffer = new Uint8Array(Math.max(a, s.encoderOutputBuffer.length * 2))), s.encoderOutputBuffer.set(e);
897
+ const c = s.encoderOutputBuffer.subarray(0, a), l = s.segmentStartPending;
898
+ l && (s.segmentStartPending = !1, s.epoch++, s.sequence = 0);
899
+ const d = s.segmentEndPending;
900
+ d && (s.segmentEndPending = !1, s.inSegment = !1);
901
+ const w = s.frameSize * s.channels, I = BigInt(Math.floor(r / 1e6 * s.sampleRate * s.channels));
902
+ L(
903
+ le(
904
+ ae({
905
+ Samples: c,
906
+ Epoch: s.epoch,
907
+ Sequence: s.sequence,
908
+ FrameSizeInInterleavedSamples: w,
909
+ TimeStampInInterleavedSamples: I,
910
+ IsFirst: l,
911
+ IsLast: d,
912
+ AverageVolume: s.lastVolume,
913
+ AudioEventEstimatedDuration: 0
914
+ }),
915
+ s.senderId,
916
+ { trackId: s.trackId }
917
+ )
918
+ ), s.sequence++;
919
+ }
920
+ function qe(t) {
921
+ const e = Math.max(V, Math.floor(t.options?.bitrate ?? M)), r = new AudioEncoder({
922
+ output: (n) => {
923
+ const i = h.get(t.captureId);
924
+ if (!i)
925
+ return;
926
+ const s = n.byteLength;
927
+ i.encoderOutputBuffer.length < s && (i.encoderOutputBuffer = new Uint8Array(Math.max(s, i.encoderOutputBuffer.length * 2))), n.copyTo(i.encoderOutputBuffer), H(
928
+ i,
929
+ i.encoderOutputBuffer.subarray(0, s),
930
+ n.timestamp ?? i.timestampUs,
931
+ n.duration ?? i.frameDurationUs,
932
+ n.type === "key"
933
+ );
934
+ },
935
+ error: (n) => {
936
+ g(n, t.captureId);
937
+ }
938
+ });
939
+ return r.configure({
940
+ codec: "opus",
941
+ numberOfChannels: t.channels,
942
+ sampleRate: t.sampleRate,
943
+ bitrate: e,
944
+ bitrateMode: "constant",
945
+ opus: {
946
+ application: "lowdelay",
947
+ signal: "voice"
948
+ }
949
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
950
+ }), r;
951
+ }
952
+ function Ge(t) {
953
+ const e = Math.max(V, Math.floor(t.options?.bitrate ?? M));
954
+ return new He({
955
+ sampleRate: t.sampleRate,
956
+ channels: t.channels,
957
+ bitrate: e,
958
+ frameSize: t.frameSize,
959
+ onOutput: (r, n, i) => {
960
+ const s = h.get(t.captureId);
961
+ s && H(s, r, n, i, !0);
962
+ },
963
+ onError: (r) => {
964
+ g(r, t.captureId);
965
+ }
966
+ });
967
+ }
968
+ async function Ye() {
969
+ if (typeof AudioEncoder > "u")
970
+ return !1;
971
+ try {
972
+ return (await AudioEncoder.isConfigSupported({
973
+ codec: "opus",
974
+ numberOfChannels: 1,
975
+ sampleRate: 48e3
976
+ })).supported === !0;
977
+ } catch {
978
+ return !1;
979
+ }
980
+ }
981
+ async function Je(t) {
982
+ if (!p)
983
+ throw new Error("Audio capture worker missing send port");
984
+ if (h.has(t.captureId))
985
+ return;
986
+ const e = Math.max(8e3, Math.floor(t.sampleRate)), r = Math.max(1, Math.floor(t.channels)), n = Math.max(1, Math.floor(e * ke / 1e3)), i = Math.max(1, Math.floor(n * 1e6 / e)), s = n * 4, a = {
987
+ captureId: t.captureId,
988
+ senderId: t.senderId,
989
+ trackId: t.trackId,
990
+ sampleRate: e,
991
+ channels: r,
992
+ frameSize: n,
993
+ frameDurationUs: i,
994
+ timestampUs: 0,
995
+ sentFirst: !1,
996
+ stopped: !1,
997
+ transport: t.transport,
998
+ ringBuffer: null,
999
+ ringTemp: null,
1000
+ pendingBuffer: new Float32Array(s),
1001
+ pendingReadOffset: 0,
1002
+ pendingWriteOffset: 0,
1003
+ pendingSamples: 0,
1004
+ frameBuffer: new Float32Array(n),
1005
+ encoderOutputBuffer: new Uint8Array(1024),
1006
+ inSegment: !1,
1007
+ segmentStartPending: !1,
1008
+ segmentEndPending: !1,
1009
+ epoch: 0,
1010
+ sequence: 0,
1011
+ lastVolume: 0,
1012
+ options: t.options
1013
+ }, l = (t.options?.preferWebCodecs ?? !0) && await Ye();
1014
+ let d, w = !1;
1015
+ const I = Math.max(V, Math.floor(t.options?.bitrate ?? M));
1016
+ if (l)
1017
+ d = qe(a), D.info(
1018
+ `Audio capture started: encoder=WebCodecs, sampleRate=${e}Hz, channels=${r}, bitrate=${I}bps`
1019
+ );
1020
+ else {
1021
+ const u = Ge(a);
1022
+ await u.initialize(), d = u, w = !0, D.info(
1023
+ `Audio capture started: encoder=WASM Opus, sampleRate=${e}Hz, channels=${r}, bitrate=${I}bps`
1024
+ );
1025
+ }
1026
+ const y = {
1027
+ ...a,
1028
+ encoder: d,
1029
+ useWasmEncoder: w
1030
+ };
1031
+ if (t.transport === "sab") {
1032
+ if (!t.sharedArrayBuffer)
1033
+ throw new Error("Missing SharedArrayBuffer for sab transport");
1034
+ const u = new We(t.sharedArrayBuffer, Float32Array);
1035
+ y.ringBuffer = u, y.ringTemp = new Float32Array(Math.max(y.frameSize * 4, 4096)), $e();
1036
+ }
1037
+ h.set(t.captureId, y), L(
1038
+ Ue(
1039
+ Ae({
1040
+ Description: `mic:${t.captureId}`,
1041
+ Codec: R.Opus,
1042
+ SampleRate: e,
1043
+ Channels: r,
1044
+ BitDepth: 32
1045
+ }),
1046
+ t.senderId,
1047
+ { trackId: t.trackId }
1048
+ )
1049
+ ), T({ type: "started", captureId: t.captureId, sampleRate: e, channels: r });
1050
+ }
1051
+ async function z(t) {
1052
+ const e = h.get(t);
1053
+ if (e) {
1054
+ e.stopped = !0, h.delete(t);
1055
+ try {
1056
+ P(e);
1057
+ } catch {
1058
+ }
1059
+ try {
1060
+ await e.encoder.flush();
1061
+ } catch {
1062
+ }
1063
+ try {
1064
+ e.encoder.close();
1065
+ } catch {
1066
+ }
1067
+ L(Pe(Te({}), e.senderId, { trackId: e.trackId })), T({ type: "stopped", captureId: t }), je();
1068
+ }
1069
+ }
1070
+ function Ze(t) {
1071
+ const e = h.get(t);
1072
+ !e || e.stopped || (e.inSegment = !0, e.segmentStartPending = !0);
1073
+ }
1074
+ function Qe(t) {
1075
+ const e = h.get(t);
1076
+ if (!(!e || e.stopped || !e.inSegment) && (e.segmentEndPending = !0, P(e), e.segmentEndPending && !e.stopped)) {
1077
+ const r = e.frameSize - e.pendingSamples;
1078
+ if (r > 0) {
1079
+ const n = new Float32Array(r);
1080
+ W(e, n);
1081
+ }
1082
+ }
1083
+ }
1084
+ function Xe() {
1085
+ for (const t of Array.from(h.keys()))
1086
+ z(t);
1087
+ if (p) {
1088
+ try {
1089
+ p.close();
1090
+ } catch {
1091
+ }
1092
+ p = null;
1093
+ }
1094
+ }
1095
+ self.addEventListener("message", (t) => {
1096
+ const e = t.data;
1097
+ if (e.type === "attachSendPort") {
1098
+ if (p)
1099
+ try {
1100
+ p.close();
1101
+ } catch {
1102
+ }
1103
+ p = e.port;
1104
+ try {
1105
+ p.start?.();
1106
+ } catch {
1107
+ }
1108
+ return;
1109
+ }
1110
+ if (e.type === "start") {
1111
+ Je(e).catch((r) => {
1112
+ g(r, e.captureId);
1113
+ });
1114
+ return;
1115
+ }
1116
+ if (e.type === "pcm") {
1117
+ const r = h.get(e.captureId);
1118
+ if (!r || r.stopped)
1119
+ return;
1120
+ W(r, e.pcm);
1121
+ return;
1122
+ }
1123
+ if (e.type === "stop") {
1124
+ z(e.captureId).catch((r) => {
1125
+ D.warn(`Failed to stop audio capture: ${r}`), g(r, e.captureId);
1126
+ });
1127
+ return;
1128
+ }
1129
+ if (e.type === "startSegment") {
1130
+ Ze(e.captureId);
1131
+ return;
1132
+ }
1133
+ if (e.type === "endSegment") {
1134
+ Qe(e.captureId);
1135
+ return;
1136
+ }
1137
+ e.type === "dispose" && Xe();
1138
+ });