echogarden 0.11.12 → 0.11.13

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 (122) hide show
  1. package/data/schemas/options.json +16 -0
  2. package/dist/api/Alignment.js +2 -2
  3. package/dist/api/Alignment.js.map +1 -1
  4. package/dist/api/Recognition.js +2 -2
  5. package/dist/api/Recognition.js.map +1 -1
  6. package/dist/api/Synthesis.js +5 -4
  7. package/dist/api/Synthesis.js.map +1 -1
  8. package/dist/api/Translation.js +2 -2
  9. package/dist/api/Translation.js.map +1 -1
  10. package/dist/audio/AudioUtilities.d.ts +1 -0
  11. package/dist/audio/AudioUtilities.js +25 -7
  12. package/dist/audio/AudioUtilities.js.map +1 -1
  13. package/dist/cli/CLI.js +2 -2
  14. package/dist/cli/CLI.js.map +1 -1
  15. package/dist/recognition/WhisperSTT.js +2 -2
  16. package/dist/recognition/WhisperSTT.js.map +1 -1
  17. package/dist/subtitles/Subtitles.d.ts +10 -7
  18. package/dist/subtitles/Subtitles.js +268 -207
  19. package/dist/subtitles/Subtitles.js.map +1 -1
  20. package/docs/Options.md +4 -2
  21. package/package.json +7 -6
  22. package/src/alignment/DTWMfccSequenceAlignment.ts +43 -0
  23. package/src/alignment/DTWSequenceAlignment.ts +121 -0
  24. package/src/alignment/DTWSequenceAlignmentWindowed.ts +210 -0
  25. package/src/alignment/LevenshteinSequenceAlignment.ts +126 -0
  26. package/src/alignment/SpeechAlignment.ts +488 -0
  27. package/src/api/API.ts +12 -0
  28. package/src/api/APIOptions.ts +15 -0
  29. package/src/api/Alignment.ts +329 -0
  30. package/src/api/Common.ts +16 -0
  31. package/src/api/Denoising.ts +120 -0
  32. package/src/api/LanguageDetection.ts +286 -0
  33. package/src/api/Recognition.ts +344 -0
  34. package/src/api/Synthesis.ts +1735 -0
  35. package/src/api/Translation.ts +143 -0
  36. package/src/api/Vad.ts +172 -0
  37. package/src/audio/AudioBufferConversion.ts +248 -0
  38. package/src/audio/AudioPlayer.ts +358 -0
  39. package/src/audio/AudioRecorder.ts +91 -0
  40. package/src/audio/AudioUtilities.ts +392 -0
  41. package/src/audio/SoxPath.ts +24 -0
  42. package/src/cli/CLI.ts +1360 -0
  43. package/src/cli/CLIConfigFile.ts +91 -0
  44. package/src/cli/CLILauncher.ts +26 -0
  45. package/src/cli/CLIOptionsSchema.ts +54 -0
  46. package/src/cli/CLIParser.ts +41 -0
  47. package/src/cli/CLIStarter.ts +40 -0
  48. package/src/codecs/FFMpegTranscoder.ts +214 -0
  49. package/src/codecs/TIMITCodec.ts +17 -0
  50. package/src/codecs/WaveCodec.ts +260 -0
  51. package/src/denoising/RNNoise.ts +95 -0
  52. package/src/dsp/BiquadFilter.ts +488 -0
  53. package/src/dsp/FFT.ts +187 -0
  54. package/src/dsp/MFCC.ts +227 -0
  55. package/src/dsp/MelSpectogram.ts +145 -0
  56. package/src/dsp/Rubberband.ts +249 -0
  57. package/src/dsp/Sonic.ts +59 -0
  58. package/src/dsp/SpeexResampler.ts +79 -0
  59. package/src/math/VectorMath.ts +812 -0
  60. package/src/nlp/ChineseSegmentation.ts +68 -0
  61. package/src/nlp/CompromiseNLP.ts +113 -0
  62. package/src/nlp/EspeakPhonemizer.ts +168 -0
  63. package/src/nlp/IPA.ts +139 -0
  64. package/src/nlp/JapaneseSegmentation.ts +53 -0
  65. package/src/nlp/Lexicon.ts +119 -0
  66. package/src/nlp/PhoneConversion.ts +508 -0
  67. package/src/nlp/Segmentation.ts +237 -0
  68. package/src/nlp/TextNormalizer.ts +160 -0
  69. package/src/recognition/AmazonTranscribeSTT.ts +112 -0
  70. package/src/recognition/AzureCognitiveServicesSTT.ts +76 -0
  71. package/src/recognition/GoogleCloudSTT.ts +92 -0
  72. package/src/recognition/SileroSTT.ts +173 -0
  73. package/src/recognition/VoskSTT.ts +112 -0
  74. package/src/recognition/WhisperSTT.ts +1518 -0
  75. package/src/server/Client.ts +297 -0
  76. package/src/server/Server.ts +178 -0
  77. package/src/server/ServerStarter.ts +12 -0
  78. package/src/server/Worker.ts +400 -0
  79. package/src/server/WorkerStarter.ts +38 -0
  80. package/src/speech-language-detection/SileroLanguageDetection.ts +105 -0
  81. package/src/subtitles/Subtitles.ts +478 -0
  82. package/src/synthesis/AwsPollyTTS.ts +78 -0
  83. package/src/synthesis/AzureCognitiveServicesTTS.ts +146 -0
  84. package/src/synthesis/CoquiServerTTS.ts +29 -0
  85. package/src/synthesis/ElevenLabsTTS.ts +104 -0
  86. package/src/synthesis/EspeakTTS.ts +552 -0
  87. package/src/synthesis/FliteTTS.ts +387 -0
  88. package/src/synthesis/GoogleCloudTTS.ts +112 -0
  89. package/src/synthesis/GoogleTranslateTTS.ts +210 -0
  90. package/src/synthesis/MicrosoftEdgeTTS.ts +298 -0
  91. package/src/synthesis/SamTTS.ts +30 -0
  92. package/src/synthesis/SapiTTS.ts +222 -0
  93. package/src/synthesis/StreamlabsPollyTTS.ts +114 -0
  94. package/src/synthesis/SvoxPicoTTS.ts +318 -0
  95. package/src/synthesis/VitsTTS.ts +734 -0
  96. package/src/tests/Test.ts +24 -0
  97. package/src/text-language-detection/FastTextLanguageDetection.ts +53 -0
  98. package/src/text-language-detection/TinyLDLanguageDetection.ts +16 -0
  99. package/src/typings/Fillers.d.ts +41 -0
  100. package/src/utilities/BinaryArrayConversion.ts +159 -0
  101. package/src/utilities/Compression.ts +91 -0
  102. package/src/utilities/FileDownloader.ts +201 -0
  103. package/src/utilities/FileSystem.ts +265 -0
  104. package/src/utilities/Hashing.ts +230 -0
  105. package/src/utilities/Locale.ts +119 -0
  106. package/src/utilities/Logger.ts +72 -0
  107. package/src/utilities/NdArrayUtilities.ts +31 -0
  108. package/src/utilities/ObjectUtilities.ts +169 -0
  109. package/src/utilities/OpenPromise.ts +13 -0
  110. package/src/utilities/PackageManager.ts +97 -0
  111. package/src/utilities/Queue.ts +17 -0
  112. package/src/utilities/RandomGenerator.ts +237 -0
  113. package/src/utilities/SignalChannel.ts +22 -0
  114. package/src/utilities/TarballMaker.ts +68 -0
  115. package/src/utilities/Timeline.ts +231 -0
  116. package/src/utilities/Timer.ts +93 -0
  117. package/src/utilities/Utilities.ts +574 -0
  118. package/src/utilities/WasmMemoryManager.ts +516 -0
  119. package/src/utilities/WebReader.ts +55 -0
  120. package/src/utilities/WikipediaReader.ts +41 -0
  121. package/src/voice-activity-detection/SileroVAD.ts +86 -0
  122. package/src/voice-activity-detection/WebRtcVAD.ts +76 -0
@@ -0,0 +1,516 @@
1
+ export class WasmMemoryManager {
2
+ wasmModule: any
3
+
4
+ private allocatedReferences = new Set<WasmRef>()
5
+
6
+ constructor(wasmModule: any) {
7
+ this.wasmModule = wasmModule
8
+ }
9
+
10
+ allocInt8() {
11
+ const address = this.alloc(1)
12
+ return this.wrapInt8(address).clear()
13
+ }
14
+
15
+ wrapInt8(address: number) {
16
+ const ref = new Int8Ref(address, this)
17
+ this.allocatedReferences.add(ref)
18
+ return ref
19
+ }
20
+
21
+ allocUint8() {
22
+ const address = this.alloc(1)
23
+ return this.wrapUint8(address).clear()
24
+ }
25
+
26
+ wrapUint8(address: number) {
27
+ const ref = new Uint8Ref(address, this)
28
+ this.allocatedReferences.add(ref)
29
+ return ref
30
+ }
31
+
32
+ allocInt16() {
33
+ const address = this.alloc(2)
34
+ return this.wrapInt16(address).clear()
35
+ }
36
+
37
+ wrapInt16(address: number) {
38
+ const ref = new Int16Ref(address, this)
39
+ this.allocatedReferences.add(ref)
40
+ return ref
41
+ }
42
+
43
+ allocUint16() {
44
+ const address = this.alloc(2)
45
+ return this.wrapUint16(address).clear()
46
+ }
47
+
48
+ wrapUint16(address: number) {
49
+ const ref = new Uint16Ref(address, this)
50
+ this.allocatedReferences.add(ref)
51
+ return ref
52
+ }
53
+
54
+ allocInt32() {
55
+ const address = this.alloc(4)
56
+ return this.wrapInt32(address).clear()
57
+ }
58
+
59
+ wrapInt32(address: number) {
60
+ const ref = new Int32Ref(address, this)
61
+ this.allocatedReferences.add(ref)
62
+ return ref
63
+ }
64
+
65
+ allocUint32() {
66
+ const address = this.alloc(4)
67
+ return this.wrapUint32(address).clear()
68
+ }
69
+
70
+ wrapUint32(address: number) {
71
+ const ref = new Uint32Ref(address, this)
72
+ this.allocatedReferences.add(ref)
73
+ return ref
74
+ }
75
+
76
+ allocPointer() {
77
+ const address = this.alloc(4)
78
+ return this.wrapPointer(address).clear()
79
+ }
80
+
81
+ wrapPointer(address: number) {
82
+ const ref = new PointerRef(address, this)
83
+ this.allocatedReferences.add(ref)
84
+ return ref
85
+ }
86
+
87
+ allocFloat32() {
88
+ const address = this.alloc(4)
89
+ return this.wrapFloat64(address).clear()
90
+ }
91
+
92
+ wrapFloat32(address: number) {
93
+ const ref = new Float32Ref(address, this)
94
+ this.allocatedReferences.add(ref)
95
+ return ref
96
+ }
97
+
98
+ allocFloat64() {
99
+ const address = this.alloc(8)
100
+ return this.wrapFloat64(address).clear()
101
+ }
102
+
103
+ wrapFloat64(address: number) {
104
+ const ref = new Float64Ref(address, this)
105
+ this.allocatedReferences.add(ref)
106
+ return ref
107
+ }
108
+
109
+ // Allocate or wrap arrays
110
+ allocInt8Array(length: number) {
111
+ const address = this.alloc(length << 0)
112
+ return this.wrapInt8Array(address, length).clear()
113
+ }
114
+
115
+ wrapInt8Array(address: number, length: number) {
116
+ const ref = new Int8ArrayRef(address, length, this)
117
+ this.allocatedReferences.add(ref)
118
+ return ref
119
+ }
120
+
121
+ allocUint8Array(length: number) {
122
+ const address = this.alloc(length << 0)
123
+ return this.wrapUint8Array(address, length).clear()
124
+ }
125
+
126
+ wrapUint8Array(address: number, length: number) {
127
+ const ref = new Uint8ArrayRef(address, length, this)
128
+ this.allocatedReferences.add(ref)
129
+ return ref
130
+ }
131
+
132
+ allocInt16Array(length: number) {
133
+ const address = this.alloc(length << 1)
134
+ return this.wrapInt16Array(address, length).clear()
135
+ }
136
+
137
+ wrapInt16Array(address: number, length: number) {
138
+ const ref = new Int16ArrayRef(address, length, this)
139
+ this.allocatedReferences.add(ref)
140
+ return ref
141
+ }
142
+
143
+ allocUint16Array(length: number) {
144
+ const address = this.alloc(length << 1)
145
+ return this.wrapUint16Array(address, length).clear()
146
+ }
147
+
148
+ wrapUint16Array(address: number, length: number) {
149
+ const ref = new Uint16ArrayRef(address, length, this)
150
+ this.allocatedReferences.add(ref)
151
+ return ref
152
+ }
153
+
154
+ allocInt32Array(length: number) {
155
+ const address = this.alloc(length << 2)
156
+ return this.wrapInt32Array(address, length).clear()
157
+ }
158
+
159
+ wrapInt32Array(address: number, length: number) {
160
+ const ref = new Int32ArrayRef(address, length, this)
161
+ this.allocatedReferences.add(ref)
162
+ return ref
163
+ }
164
+
165
+ allocUint32Array(length: number) {
166
+ const address = this.alloc(length << 2)
167
+ return this.wrapUint32Array(address, length).clear()
168
+ }
169
+
170
+ wrapUint32Array(address: number, length: number) {
171
+ const ref = new Uint32ArrayRef(address, length, this)
172
+ this.allocatedReferences.add(ref)
173
+ return ref
174
+ }
175
+
176
+ allocFloat32Array(length: number) {
177
+ const address = this.alloc(length << 2)
178
+ return this.wrapFloat32Array(address, length).clear()
179
+ }
180
+
181
+ wrapFloat32Array(address: number, length: number) {
182
+ const ref = new Float32ArrayRef(address, length, this)
183
+ this.allocatedReferences.add(ref)
184
+ return ref
185
+ }
186
+
187
+ allocFloat64Array(length: number) {
188
+ const address = this.alloc(length << 3)
189
+ return this.wrapFloat64Array(address, length).clear()
190
+ }
191
+
192
+ wrapFloat64Array(address: number, length: number) {
193
+ const ref = new Float64ArrayRef(address, length, this)
194
+ this.allocatedReferences.add(ref)
195
+ return ref
196
+ }
197
+
198
+ allocNullTerminatedUtf8String(str: string) {
199
+ const strBuffer = Buffer.concat([Buffer.from(str, "utf8"), Buffer.alloc(1)])
200
+ const ref = this.allocUint8Array(strBuffer.length)
201
+ ref.view.set(strBuffer)
202
+ return ref
203
+ }
204
+
205
+ wrapNullTerminatedUtf8String(address: number) {
206
+ const ref = new NullTerminatedUtf8StringRef(address, this)
207
+ this.allocatedReferences.add(ref)
208
+ return ref
209
+ }
210
+
211
+ private alloc(size: number) {
212
+ const ptr = this.wasmModule._malloc(size)
213
+ return ptr as number
214
+ }
215
+
216
+ free(wasmReference: WasmRef) {
217
+ if (wasmReference.isFreed) {
218
+ return
219
+ }
220
+
221
+ this.wasmModule._free(wasmReference.address)
222
+
223
+ this.allocatedReferences.delete(wasmReference)
224
+ wasmReference.clearAddress()
225
+ }
226
+
227
+ freeAll() {
228
+ for (const wasmReference of this.allocatedReferences) {
229
+ this.free(wasmReference)
230
+ }
231
+ }
232
+ }
233
+
234
+ abstract class ValueRef<T extends number | string> {
235
+ protected ptr: number
236
+ private readonly manager: WasmMemoryManager
237
+
238
+ protected get module() { return this.manager.wasmModule }
239
+
240
+ constructor(ptr: number, manager: WasmMemoryManager) {
241
+ this.ptr = ptr
242
+ this.manager = manager
243
+ }
244
+
245
+ get value(): T {
246
+ this.assertNotFreed()
247
+ return this.getValue()
248
+ }
249
+
250
+ set value(newValue: T) {
251
+ this.assertNotFreed()
252
+ this.setValue(newValue)
253
+ }
254
+
255
+ abstract getValue(): T
256
+ abstract setValue(newValue: T): void
257
+
258
+ get address() {
259
+ this.assertNotFreed()
260
+ return this.ptr
261
+ }
262
+
263
+ clear() {
264
+ this.assertNotFreed()
265
+
266
+ if (typeof this.value == 'number') {
267
+ this.value = 0 as any
268
+ } else if (typeof this.value == 'string') {
269
+ throw new Error("Unimplemented")
270
+ }
271
+
272
+ return this
273
+ }
274
+
275
+ free() {
276
+ this.manager.free(this as any)
277
+ }
278
+
279
+ clearAddress() {
280
+ this.ptr = 0
281
+ }
282
+
283
+ get isFreed() { return this.ptr == 0 }
284
+
285
+ protected assertNotFreed() {
286
+ if (this.isFreed) {
287
+ throw new Error("Attempt to read a freed WASM value reference.")
288
+ }
289
+ }
290
+ }
291
+
292
+ export class Int8Ref extends ValueRef<number> {
293
+ getValue() {
294
+ return this.module.HEAP8[this.ptr >>> 0] as number
295
+ }
296
+
297
+ setValue(newValue: number) {
298
+ this.module.HEAP8[this.ptr >>> 0] = newValue
299
+ }
300
+ }
301
+
302
+ export class Uint8Ref extends ValueRef<number> {
303
+ getValue() {
304
+ return this.module.HEAPU8[this.ptr >>> 0] as number
305
+ }
306
+
307
+ setValue(newValue: number) {
308
+ this.module.HEAPU8[this.ptr >>> 0] = newValue
309
+ }
310
+ }
311
+
312
+ export class Int16Ref extends ValueRef<number> {
313
+ getValue() {
314
+ return this.module.HEAP16[this.ptr >>> 1] as number
315
+ }
316
+
317
+ setValue(newValue: number) {
318
+ this.module.HEAP16[this.ptr >>> 1] = newValue
319
+ }
320
+ }
321
+
322
+ export class Uint16Ref extends ValueRef<number> {
323
+ getValue() {
324
+ return this.module.HEAPU16[this.ptr >>> 1] as number
325
+ }
326
+
327
+ setValue(newValue: number) {
328
+ this.module.HEAPU16[this.ptr >>> 1] = newValue
329
+ }
330
+ }
331
+
332
+ export class Int32Ref extends ValueRef<number> {
333
+ getValue() {
334
+ return this.module.HEAP32[this.ptr >>> 2] as number
335
+ }
336
+
337
+ setValue(newValue: number) {
338
+ this.module.HEAP32[this.ptr >>> 2] = newValue
339
+ }
340
+ }
341
+
342
+ export class Uint32Ref extends ValueRef<number> {
343
+ getValue() {
344
+ return this.module.HEAPU32[this.ptr >>> 2] as number
345
+ }
346
+
347
+ setValue(newValue: number) {
348
+ this.module.HEAPU32[this.ptr >>> 2] = newValue
349
+ }
350
+ }
351
+
352
+ export class PointerRef extends Uint32Ref { }
353
+
354
+ export class Float32Ref extends ValueRef<number> {
355
+ getValue() {
356
+ return this.module.HEAPF32[this.ptr >>> 2] as number
357
+ }
358
+
359
+ setValue(newValue: number) {
360
+ this.module.HEAPF32[this.ptr >>> 2] = newValue
361
+ }
362
+ }
363
+
364
+ export class Float64Ref extends ValueRef<number> {
365
+ getValue() {
366
+ return this.module.HEAPF64[this.ptr >>> 3] as number
367
+ }
368
+
369
+ setValue(newValue: number) {
370
+ this.module.HEAPF64[this.ptr >>> 3] = newValue
371
+ }
372
+ }
373
+
374
+ export class NullTerminatedUtf8StringRef extends ValueRef<string> {
375
+ getValue() {
376
+ const ptr = this.ptr >>> 0
377
+
378
+ const heapU8 = this.module.HEAPU8
379
+
380
+ const endByteOffset = heapU8.subarray(ptr).indexOf(0)
381
+
382
+ const strBytes = heapU8.subarray(ptr, ptr + endByteOffset)
383
+
384
+ const str = Buffer.from(strBytes).toString("utf8")
385
+
386
+ return str
387
+ }
388
+
389
+ setValue(newValue: string) {
390
+ throw new Error("Unimplemented")
391
+ }
392
+ }
393
+
394
+ abstract class TypedArrayRef<T extends TypedArray> {
395
+ protected ptr: number
396
+ readonly length: number
397
+ private readonly manager: WasmMemoryManager
398
+
399
+ get module() { return this.manager.wasmModule }
400
+
401
+ constructor(ptr: number, length: number, manager: WasmMemoryManager) {
402
+ this.ptr = ptr
403
+ this.length = length
404
+ this.manager = manager
405
+ }
406
+
407
+ get view() {
408
+ this.assertNotFreed()
409
+ return this.getView()
410
+ }
411
+
412
+ protected abstract getView(): T
413
+
414
+ slice(start?: number, end?: number) {
415
+ return this.view.slice(start, end)
416
+ }
417
+
418
+ get address() {
419
+ this.assertNotFreed()
420
+ return this.ptr
421
+ }
422
+
423
+ clear() {
424
+ this.view.fill(0)
425
+ return this
426
+ }
427
+
428
+ free() {
429
+ this.manager.free(this)
430
+ }
431
+
432
+ clearAddress() {
433
+ this.ptr = 0
434
+ }
435
+
436
+ get isFreed() { return this.ptr == 0 }
437
+
438
+ protected assertNotFreed() {
439
+ if (this.isFreed) {
440
+ throw new Error("Attempt to read a freed WASM typed array reference.")
441
+ }
442
+ }
443
+ }
444
+
445
+ export class Int8ArrayRef extends TypedArrayRef<Int8Array> {
446
+ getView() {
447
+ const startIndex = this.ptr >>> 0
448
+ return this.module.HEAP8.subarray(startIndex, startIndex + this.length) as Int8Array
449
+ }
450
+ }
451
+
452
+ export class Uint8ArrayRef extends TypedArrayRef<Uint8Array> {
453
+ getView() {
454
+ const startIndex = this.ptr >>> 0
455
+ return this.module.HEAPU8.subarray(startIndex, startIndex + this.length) as Uint8Array
456
+ }
457
+
458
+ readAsNullTerminatedUtf8String(): string {
459
+ let strBytes = this.view
460
+
461
+ const indexOfFirstZero = strBytes.indexOf(0)
462
+
463
+ if (indexOfFirstZero >= 0) {
464
+ strBytes = strBytes.subarray(0, indexOfFirstZero)
465
+ }
466
+
467
+ const str = Buffer.from(strBytes).toString("utf8")
468
+
469
+ return str
470
+ }
471
+ }
472
+
473
+ export class Int16ArrayRef extends TypedArrayRef<Int16Array> {
474
+ getView() {
475
+ const startIndex = this.ptr >>> 1
476
+ return this.module.HEAP16.subarray(startIndex, startIndex + this.length) as Int16Array
477
+ }
478
+ }
479
+
480
+ export class Uint16ArrayRef extends TypedArrayRef<Uint16Array> {
481
+ getView() {
482
+ const startIndex = this.ptr >>> 1
483
+ return this.module.HEAPU16.subarray(startIndex, startIndex + this.length) as Uint16Array
484
+ }
485
+ }
486
+
487
+ export class Int32ArrayRef extends TypedArrayRef<Int32Array> {
488
+ getView() {
489
+ const startIndex = this.ptr >>> 2
490
+ return this.module.HEAP32.subarray(startIndex, startIndex + this.length) as Int32Array
491
+ }
492
+ }
493
+
494
+ export class Uint32ArrayRef extends TypedArrayRef<Uint32Array> {
495
+ getView() {
496
+ const startIndex = this.ptr >>> 2
497
+ return this.module.HEAPU32.subarray(startIndex, startIndex + this.length) as Uint32Array
498
+ }
499
+ }
500
+
501
+ export class Float32ArrayRef extends TypedArrayRef<Float32Array> {
502
+ getView() {
503
+ const startIndex = this.ptr >>> 2
504
+ return this.module.HEAPF32.subarray(startIndex, startIndex + this.length) as Float32Array
505
+ }
506
+ }
507
+
508
+ export class Float64ArrayRef extends TypedArrayRef<Float64Array> {
509
+ getView() {
510
+ const startIndex = this.ptr >>> 3
511
+ return this.module.HEAPF64.subarray(startIndex, startIndex + this.length) as Float64Array
512
+ }
513
+ }
514
+
515
+ export type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array
516
+ export type WasmRef = ValueRef<number> | ValueRef<string> | TypedArrayRef<TypedArray>
@@ -0,0 +1,55 @@
1
+ import { Readability } from '@mozilla/readability'
2
+ import { JSDOM, VirtualConsole } from 'jsdom'
3
+ import { request } from 'gaxios'
4
+ import { Logger } from './Logger.js'
5
+ import { convertHtmlToText } from './Utilities.js'
6
+
7
+ export async function fetchDocumentText(url: string) {
8
+ const progressLogger = new Logger()
9
+ progressLogger.start(`Fetching ${url}`)
10
+
11
+ const response = await request<string>({
12
+ url,
13
+ responseType: "text",
14
+ headers: {
15
+ "sec-ch-ua": `".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"`,
16
+ "x-same-domain": "1",
17
+ "dnt": "1",
18
+ "content-type": "application/x-www-form-urlencoded;charset=UTF-8",
19
+ "sec-ch-ua-mobile": "?0",
20
+ "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36",
21
+ "sec-ch-ua-arch": "x86",
22
+ "sec-ch-ua-full-version": "103.0.5060.114",
23
+ "sec-ch-ua-platform-version": "10.0.0",
24
+ "sec-ch-ua-full-version-list": `".Not/A)Brand";v="99.0.0.0", "Google Chrome";v="103.0.5060.114", "Chromium";v="103.0.5060.114"`,
25
+ "sec-ch-ua-bitness": "64",
26
+ "sec-ch-ua-model": "",
27
+ "sec-ch-ua-platform": "Windows",
28
+ "accept": "*/*",
29
+ //"origin": "https://www.google.com/",
30
+ "sec-fetch-site": "same-origin",
31
+ "sec-fetch-mode": "cors",
32
+ "sec-fetch-dest": "empty",
33
+ "referer": "https://www.google.com/",
34
+ "accept-encoding": "gzip, deflate, br",
35
+ "accept-language": "en-US,en;q=0.9",
36
+ },
37
+ })
38
+
39
+ progressLogger.start(`Parsing document body`)
40
+
41
+ const doc = new JSDOM(response.data, {
42
+ url,
43
+ virtualConsole: new VirtualConsole()
44
+ })
45
+
46
+ const reader = new Readability(doc.window.document)
47
+
48
+ const article = reader.parse()
49
+
50
+ const text: string = await convertHtmlToText(article?.content || "")
51
+
52
+ progressLogger.end()
53
+
54
+ return text
55
+ }
@@ -0,0 +1,41 @@
1
+ import { splitToParagraphs, wordCharacterPattern } from '../nlp/Segmentation.js'
2
+ import { Logger } from './Logger.js'
3
+
4
+ export async function parseWikipediaArticle(articleName: string, language: string) {
5
+ const logger = new Logger()
6
+
7
+ logger.startAsync("Fetching Wikipedia article")
8
+
9
+ const { default: wtf } = await import('wtf_wikipedia')
10
+
11
+ const document = await wtf.fetch(articleName, language)
12
+
13
+ if (!document) {
14
+ throw new Error("Error fetching Wikipedia article")
15
+ }
16
+
17
+ const sections = document.sections()
18
+ const sectionsText: string[] = []
19
+
20
+ for (const section of sections) {
21
+ const sectionTitle = section.title()
22
+
23
+ if (wordCharacterPattern.test(sectionTitle)) {
24
+ sectionsText.push(sectionTitle)
25
+ }
26
+
27
+ const sectionParagraphs = splitToParagraphs(section.text(), 'single', 'preserve')
28
+
29
+ for (const paragraph of sectionParagraphs) {
30
+ const paragraphText = paragraph
31
+
32
+ if (wordCharacterPattern.test(paragraphText)) {
33
+ sectionsText.push(paragraphText)
34
+ }
35
+ }
36
+ }
37
+
38
+ logger.end()
39
+
40
+ return sectionsText
41
+ }
@@ -0,0 +1,86 @@
1
+ import Onnx from 'onnxruntime-node'
2
+
3
+ import { concatFloat32Arrays } from '../utilities/Utilities.js'
4
+ import { RawAudio } from '../audio/AudioUtilities.js'
5
+
6
+ let sileroVad: SileroVAD
7
+
8
+ export async function detectVoiceActivity(rawAudio: RawAudio, modelPath: string, frameDuration: 30 | 60 | 90 = 30) {
9
+ if (rawAudio.sampleRate != 16000) {
10
+ throw new Error("Audio sample rate must be 16KHz")
11
+ }
12
+
13
+ const audioSamples = rawAudio.audioChannels[0]
14
+
15
+ const frameLength = Math.floor(16000 * (frameDuration / 1000))
16
+
17
+ if (!sileroVad) {
18
+ sileroVad = new SileroVAD(modelPath)
19
+ }
20
+
21
+ const frameProbabilities: number[] = []
22
+
23
+ for (let position = 0; position < audioSamples.length; position += frameLength) {
24
+ let chunk = audioSamples.subarray(position, position + frameLength)
25
+
26
+ if (chunk.length < frameLength) {
27
+ chunk = concatFloat32Arrays([chunk, new Float32Array(frameLength - chunk.length)])
28
+ }
29
+
30
+ const probability = await sileroVad.predictAudioFrame(chunk)
31
+
32
+ frameProbabilities.push(probability)
33
+ }
34
+
35
+ return frameProbabilities
36
+ }
37
+
38
+ export class SileroVAD {
39
+ session?: Onnx.InferenceSession
40
+
41
+ modelStateH: Onnx.Tensor
42
+ modelStateC: Onnx.Tensor
43
+ modelSampleRate: Onnx.Tensor
44
+
45
+ modelPath: string
46
+
47
+ constructor(modelPath: string) {
48
+ this.modelSampleRate = new Onnx.Tensor('int64', new BigInt64Array([BigInt(16000)]), [])
49
+
50
+ const h = new Float32Array(2 * 1 * 64)
51
+ const c = new Float32Array(2 * 1 * 64)
52
+
53
+ this.modelStateH = new Onnx.Tensor('float32', h, [2, 1, 64])
54
+ this.modelStateC = new Onnx.Tensor('float32', c, [2, 1, 64])
55
+
56
+ this.modelPath = modelPath
57
+ }
58
+
59
+ async predictAudioFrame(frame: Float32Array) {
60
+ if (!this.session) {
61
+ await this.initializeSession(this.modelPath)
62
+ }
63
+
64
+ const inputTensor = new Onnx.Tensor('float32', frame, [1, frame.length])
65
+
66
+ //const inputs = { input: inputTensor, h0: this.modelStateH, c0: this.modelStateC }
67
+ const inputs = { input: inputTensor, sr: this.modelSampleRate, h: this.modelStateH, c: this.modelStateC }
68
+
69
+ const results = await this.session!.run(inputs)
70
+
71
+ const probability = results["output"].data[0] as number
72
+
73
+ this.modelStateH = results["hn"]
74
+ this.modelStateC = results["cn"]
75
+
76
+ return probability
77
+ }
78
+
79
+ private async initializeSession(modelPath: string) {
80
+ const onnxOptions: Onnx.InferenceSession.SessionOptions = {
81
+ logSeverityLevel: 3
82
+ }
83
+
84
+ this.session = await Onnx.InferenceSession.create(modelPath, onnxOptions)
85
+ }
86
+ }