echogarden 2.7.0 → 2.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/data/lexicons/heteronyms.en.json +6 -2
- package/data/lexicons/words.en.json +379 -19
- package/dist/alignment/SemanticTextAlignment.js +6 -7
- package/dist/alignment/SemanticTextAlignment.js.map +1 -1
- package/dist/api/Alignment.js +2 -2
- package/dist/api/Alignment.js.map +1 -1
- package/dist/api/Recognition.js +2 -2
- package/dist/api/Recognition.js.map +1 -1
- package/dist/api/SpeechTranslation.js +2 -2
- package/dist/api/SpeechTranslation.js.map +1 -1
- package/dist/api/Synthesis.js +7 -7
- package/dist/api/Synthesis.js.map +1 -1
- package/dist/api/TimelineTranslationAlignment.js +2 -2
- package/dist/api/TimelineTranslationAlignment.js.map +1 -1
- package/dist/api/TranslationAlignment.js +2 -2
- package/dist/api/TranslationAlignment.js.map +1 -1
- package/dist/audio/AudioPlayer.js +2 -2
- package/dist/audio/AudioPlayer.js.map +1 -1
- package/dist/audio/AudioUtilities.d.ts +1 -1
- package/dist/cli/CLI.js +4 -4
- package/dist/cli/CLI.js.map +1 -1
- package/dist/denoising/RNNoise.js +5 -5
- package/dist/denoising/RNNoise.js.map +1 -1
- package/dist/dsp/FFT.js +13 -19
- package/dist/dsp/FFT.js.map +1 -1
- package/dist/dsp/Rubberband.js +5 -5
- package/dist/dsp/Rubberband.js.map +1 -1
- package/dist/dsp/Sonic.js +6 -6
- package/dist/dsp/Sonic.js.map +1 -1
- package/dist/dsp/SpeexResampler.js +11 -11
- package/dist/dsp/SpeexResampler.js.map +1 -1
- package/dist/nlp/EspeakPhonemizer.js +1 -1
- package/dist/nlp/EspeakPhonemizer.js.map +1 -1
- package/dist/nlp/Segmentation.d.ts +13 -10
- package/dist/nlp/Segmentation.js +129 -106
- package/dist/nlp/Segmentation.js.map +1 -1
- package/dist/nlp/TextNormalizer.js +74 -42
- package/dist/nlp/TextNormalizer.js.map +1 -1
- package/dist/recognition/AmazonTranscribeSTT.js +2 -2
- package/dist/recognition/AmazonTranscribeSTT.js.map +1 -1
- package/dist/recognition/SileroSTT.js +2 -2
- package/dist/recognition/SileroSTT.js.map +1 -1
- package/dist/recognition/WhisperSTT.js +4 -16
- package/dist/recognition/WhisperSTT.js.map +1 -1
- package/dist/subtitles/Subtitles.js +52 -7
- package/dist/subtitles/Subtitles.js.map +1 -1
- package/dist/synthesis/ElevenLabsTTS.js +11 -10
- package/dist/synthesis/ElevenLabsTTS.js.map +1 -1
- package/dist/synthesis/EspeakTTS.js +8 -8
- package/dist/synthesis/EspeakTTS.js.map +1 -1
- package/dist/synthesis/KokoroTTS.js +21 -10
- package/dist/synthesis/KokoroTTS.js.map +1 -1
- package/dist/synthesis/SvoxPicoTTS.js +24 -23
- package/dist/synthesis/SvoxPicoTTS.js.map +1 -1
- package/dist/text-translation/DeepLTextTranslation.js +4 -3
- package/dist/text-translation/DeepLTextTranslation.js.map +1 -1
- package/dist/text-translation/NLLBTextTranslation.js +7 -6
- package/dist/text-translation/NLLBTextTranslation.js.map +1 -1
- package/dist/utilities/StringUtilities.d.ts +1 -1
- package/dist/utilities/StringUtilities.js +5 -5
- package/dist/utilities/StringUtilities.js.map +1 -1
- package/dist/utilities/Timeline.d.ts +2 -3
- package/dist/utilities/Timeline.js +96 -147
- package/dist/utilities/Timeline.js.map +1 -1
- package/dist/utilities/WikipediaReader.js +3 -3
- package/dist/utilities/WikipediaReader.js.map +1 -1
- package/dist/voice-activity-detection/WebRtcVAD.js +4 -4
- package/dist/voice-activity-detection/WebRtcVAD.js.map +1 -1
- package/docs/Tasklist.md +1 -6
- package/package.json +13 -11
- package/src/alignment/SemanticTextAlignment.ts +6 -7
- package/src/api/Alignment.ts +2 -2
- package/src/api/Recognition.ts +2 -2
- package/src/api/SpeechTranslation.ts +2 -2
- package/src/api/Synthesis.ts +7 -7
- package/src/api/TimelineTranslationAlignment.ts +2 -2
- package/src/api/TranslationAlignment.ts +2 -2
- package/src/audio/AudioPlayer.ts +2 -2
- package/src/cli/CLI.ts +4 -4
- package/src/denoising/RNNoise.ts +5 -5
- package/src/dsp/FFT.ts +22 -19
- package/src/dsp/Rubberband.ts +5 -5
- package/src/dsp/Sonic.ts +6 -6
- package/src/dsp/SpeexResampler.ts +11 -11
- package/src/nlp/EspeakPhonemizer.ts +1 -1
- package/src/nlp/Segmentation.ts +146 -111
- package/src/nlp/TextNormalizer.ts +94 -52
- package/src/recognition/AmazonTranscribeSTT.ts +2 -2
- package/src/recognition/SileroSTT.ts +2 -2
- package/src/recognition/WhisperSTT.ts +4 -18
- package/src/subtitles/Subtitles.ts +69 -8
- package/src/synthesis/ElevenLabsTTS.ts +12 -11
- package/src/synthesis/EspeakTTS.ts +9 -8
- package/src/synthesis/KokoroTTS.ts +24 -12
- package/src/synthesis/SvoxPicoTTS.ts +26 -23
- package/src/text-translation/DeepLTextTranslation.ts +4 -3
- package/src/text-translation/NLLBTextTranslation.ts +8 -6
- package/src/typings/Fillers.d.ts +0 -16
- package/src/utilities/StringUtilities.ts +5 -6
- package/src/utilities/Timeline.ts +100 -170
- package/src/utilities/WikipediaReader.ts +3 -3
- package/src/voice-activity-detection/WebRtcVAD.ts +5 -4
- package/dist/nlp/CompromiseNLP.d.ts +0 -15
- package/dist/nlp/CompromiseNLP.js +0 -79
- package/dist/nlp/CompromiseNLP.js.map +0 -1
- package/dist/utilities/WasmMemoryManager.d.ts +0 -153
- package/dist/utilities/WasmMemoryManager.js +0 -440
- package/dist/utilities/WasmMemoryManager.js.map +0 -1
- package/src/nlp/CompromiseNLP.ts +0 -119
- package/src/utilities/WasmMemoryManager.ts +0 -566
|
@@ -1,566 +0,0 @@
|
|
|
1
|
-
import { decodeUtf8, encodeUtf8 } from "../encodings/Utf8.js"
|
|
2
|
-
import { TypedArray } from "../typings/TypedArray.js"
|
|
3
|
-
import { concatUint8Arrays } from "./Utilities.js"
|
|
4
|
-
|
|
5
|
-
export class WasmMemoryManager {
|
|
6
|
-
wasmModule: any
|
|
7
|
-
|
|
8
|
-
private wasmAlloc: WasmAllocMethod
|
|
9
|
-
private wasmFree: WasmFreeMethod
|
|
10
|
-
|
|
11
|
-
private allocatedReferences = new Set<WasmRef>()
|
|
12
|
-
|
|
13
|
-
constructor(wasmModule: any, options?: WasmMemoryManagerOptions) {
|
|
14
|
-
options = options ?? {}
|
|
15
|
-
|
|
16
|
-
this.wasmModule = wasmModule
|
|
17
|
-
|
|
18
|
-
if (options.wasmAlloc) {
|
|
19
|
-
this.wasmAlloc = options.wasmAlloc
|
|
20
|
-
} else {
|
|
21
|
-
if (!wasmModule._malloc) {
|
|
22
|
-
throw new Error(`Couldn't find a '_malloc' function in the module and no custom 'wasmAlloc' was provided in the options`)
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
this.wasmAlloc = wasmModule._malloc
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
if (options.wasmFree) {
|
|
29
|
-
this.wasmFree = options.wasmFree
|
|
30
|
-
} else {
|
|
31
|
-
if (!wasmModule._free) {
|
|
32
|
-
throw new Error(`Couldn't find a '_malloc' function in the module and no custom 'wasmFree' was provided in the options`)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
this.wasmFree = wasmModule._free
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
allocInt8() {
|
|
40
|
-
const address = this.alloc(1)
|
|
41
|
-
return this.wrapInt8(address).clear()
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
wrapInt8(address: number) {
|
|
45
|
-
const ref = new Int8Ref(address, this)
|
|
46
|
-
this.allocatedReferences.add(ref)
|
|
47
|
-
return ref
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
allocUint8() {
|
|
51
|
-
const address = this.alloc(1)
|
|
52
|
-
return this.wrapUint8(address).clear()
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
wrapUint8(address: number) {
|
|
56
|
-
const ref = new Uint8Ref(address, this)
|
|
57
|
-
this.allocatedReferences.add(ref)
|
|
58
|
-
return ref
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
allocInt16() {
|
|
62
|
-
const address = this.alloc(2)
|
|
63
|
-
return this.wrapInt16(address).clear()
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
wrapInt16(address: number) {
|
|
67
|
-
const ref = new Int16Ref(address, this)
|
|
68
|
-
this.allocatedReferences.add(ref)
|
|
69
|
-
return ref
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
allocUint16() {
|
|
73
|
-
const address = this.alloc(2)
|
|
74
|
-
return this.wrapUint16(address).clear()
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
wrapUint16(address: number) {
|
|
78
|
-
const ref = new Uint16Ref(address, this)
|
|
79
|
-
this.allocatedReferences.add(ref)
|
|
80
|
-
return ref
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
allocInt32() {
|
|
84
|
-
const address = this.alloc(4)
|
|
85
|
-
return this.wrapInt32(address).clear()
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
wrapInt32(address: number) {
|
|
89
|
-
const ref = new Int32Ref(address, this)
|
|
90
|
-
this.allocatedReferences.add(ref)
|
|
91
|
-
return ref
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
allocUint32() {
|
|
95
|
-
const address = this.alloc(4)
|
|
96
|
-
return this.wrapUint32(address).clear()
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
wrapUint32(address: number) {
|
|
100
|
-
const ref = new Uint32Ref(address, this)
|
|
101
|
-
this.allocatedReferences.add(ref)
|
|
102
|
-
return ref
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
allocPointer() {
|
|
106
|
-
const address = this.alloc(4)
|
|
107
|
-
return this.wrapPointer(address).clear()
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
wrapPointer(address: number) {
|
|
111
|
-
const ref = new PointerRef(address, this)
|
|
112
|
-
this.allocatedReferences.add(ref)
|
|
113
|
-
return ref
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
allocFloat32() {
|
|
117
|
-
const address = this.alloc(4)
|
|
118
|
-
return this.wrapFloat64(address).clear()
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
wrapFloat32(address: number) {
|
|
122
|
-
const ref = new Float32Ref(address, this)
|
|
123
|
-
this.allocatedReferences.add(ref)
|
|
124
|
-
return ref
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
allocFloat64() {
|
|
128
|
-
const address = this.alloc(8)
|
|
129
|
-
return this.wrapFloat64(address).clear()
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
wrapFloat64(address: number) {
|
|
133
|
-
const ref = new Float64Ref(address, this)
|
|
134
|
-
this.allocatedReferences.add(ref)
|
|
135
|
-
return ref
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// Allocate or wrap arrays
|
|
139
|
-
allocInt8Array(length: number) {
|
|
140
|
-
const address = this.alloc(length << 0)
|
|
141
|
-
return this.wrapInt8Array(address, length).clear()
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
wrapInt8Array(address: number, length: number) {
|
|
145
|
-
const ref = new Int8ArrayRef(address, length, this)
|
|
146
|
-
this.allocatedReferences.add(ref)
|
|
147
|
-
return ref
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
allocUint8Array(length: number) {
|
|
151
|
-
const address = this.alloc(length << 0)
|
|
152
|
-
return this.wrapUint8Array(address, length).clear()
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
wrapUint8Array(address: number, length: number) {
|
|
156
|
-
const ref = new Uint8ArrayRef(address, length, this)
|
|
157
|
-
this.allocatedReferences.add(ref)
|
|
158
|
-
return ref
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
allocInt16Array(length: number) {
|
|
162
|
-
const address = this.alloc(length << 1)
|
|
163
|
-
return this.wrapInt16Array(address, length).clear()
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
wrapInt16Array(address: number, length: number) {
|
|
167
|
-
const ref = new Int16ArrayRef(address, length, this)
|
|
168
|
-
this.allocatedReferences.add(ref)
|
|
169
|
-
return ref
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
allocUint16Array(length: number) {
|
|
173
|
-
const address = this.alloc(length << 1)
|
|
174
|
-
return this.wrapUint16Array(address, length).clear()
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
wrapUint16Array(address: number, length: number) {
|
|
178
|
-
const ref = new Uint16ArrayRef(address, length, this)
|
|
179
|
-
this.allocatedReferences.add(ref)
|
|
180
|
-
return ref
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
allocInt32Array(length: number) {
|
|
184
|
-
const address = this.alloc(length << 2)
|
|
185
|
-
return this.wrapInt32Array(address, length).clear()
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
wrapInt32Array(address: number, length: number) {
|
|
189
|
-
const ref = new Int32ArrayRef(address, length, this)
|
|
190
|
-
this.allocatedReferences.add(ref)
|
|
191
|
-
return ref
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
allocUint32Array(length: number) {
|
|
195
|
-
const address = this.alloc(length << 2)
|
|
196
|
-
return this.wrapUint32Array(address, length).clear()
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
wrapUint32Array(address: number, length: number) {
|
|
200
|
-
const ref = new Uint32ArrayRef(address, length, this)
|
|
201
|
-
this.allocatedReferences.add(ref)
|
|
202
|
-
return ref
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
allocFloat32Array(length: number) {
|
|
206
|
-
const address = this.alloc(length << 2)
|
|
207
|
-
return this.wrapFloat32Array(address, length).clear()
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
wrapFloat32Array(address: number, length: number) {
|
|
211
|
-
const ref = new Float32ArrayRef(address, length, this)
|
|
212
|
-
this.allocatedReferences.add(ref)
|
|
213
|
-
return ref
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
allocFloat64Array(length: number) {
|
|
217
|
-
const address = this.alloc(length << 3)
|
|
218
|
-
return this.wrapFloat64Array(address, length).clear()
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
wrapFloat64Array(address: number, length: number) {
|
|
222
|
-
const ref = new Float64ArrayRef(address, length, this)
|
|
223
|
-
this.allocatedReferences.add(ref)
|
|
224
|
-
return ref
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
allocNullTerminatedUtf8String(str: string) {
|
|
228
|
-
const strBuffer = concatUint8Arrays([encodeUtf8(str), new Uint8Array(1)])
|
|
229
|
-
const ref = this.allocUint8Array(strBuffer.length)
|
|
230
|
-
ref.view.set(strBuffer)
|
|
231
|
-
return ref
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
wrapNullTerminatedUtf8String(address: number) {
|
|
235
|
-
const ref = new NullTerminatedUtf8StringRef(address, this)
|
|
236
|
-
this.allocatedReferences.add(ref)
|
|
237
|
-
return ref
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
private alloc(size: number) {
|
|
241
|
-
const ptr = this.wasmAlloc(size)
|
|
242
|
-
|
|
243
|
-
return ptr
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
free(wasmReference: WasmRef) {
|
|
247
|
-
if (wasmReference.isFreed) {
|
|
248
|
-
return
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
this.wasmFree(wasmReference.address)
|
|
252
|
-
|
|
253
|
-
this.allocatedReferences.delete(wasmReference)
|
|
254
|
-
wasmReference.clearAddress()
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
freeAll() {
|
|
258
|
-
for (const wasmReference of this.allocatedReferences) {
|
|
259
|
-
this.free(wasmReference)
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
detach<T extends WasmRef>(wasmReference: T) {
|
|
264
|
-
this.allocatedReferences.delete(wasmReference)
|
|
265
|
-
|
|
266
|
-
return wasmReference
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
abstract class ValueRef<T extends number | string> {
|
|
271
|
-
protected ptr: number
|
|
272
|
-
private readonly manager: WasmMemoryManager
|
|
273
|
-
|
|
274
|
-
protected get module() { return this.manager.wasmModule }
|
|
275
|
-
|
|
276
|
-
constructor(ptr: number, manager: WasmMemoryManager) {
|
|
277
|
-
this.ptr = ptr
|
|
278
|
-
this.manager = manager
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
get value(): T {
|
|
282
|
-
this.assertNotFreed()
|
|
283
|
-
return this.getValue()
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
set value(newValue: T) {
|
|
287
|
-
this.assertNotFreed()
|
|
288
|
-
this.setValue(newValue)
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
abstract getValue(): T
|
|
292
|
-
abstract setValue(newValue: T): void
|
|
293
|
-
|
|
294
|
-
get address() {
|
|
295
|
-
this.assertNotFreed()
|
|
296
|
-
return this.ptr
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
clear() {
|
|
300
|
-
this.assertNotFreed()
|
|
301
|
-
|
|
302
|
-
if (typeof this.value == 'number') {
|
|
303
|
-
this.value = 0 as any
|
|
304
|
-
} else if (typeof this.value == 'string') {
|
|
305
|
-
throw new Error('Unimplemented')
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
return this
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
free() {
|
|
312
|
-
this.manager.free(this as any)
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
detach() {
|
|
316
|
-
return this.manager.detach(this as any) as this
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
clearAddress() {
|
|
320
|
-
this.ptr = 0
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
get isFreed() { return this.ptr == 0 }
|
|
324
|
-
|
|
325
|
-
protected assertNotFreed() {
|
|
326
|
-
if (this.isFreed) {
|
|
327
|
-
throw new Error('Attempt to read a freed WASM value reference.')
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
export class Int8Ref extends ValueRef<number> {
|
|
333
|
-
getValue() {
|
|
334
|
-
return this.module.HEAP8[this.ptr >>> 0] as number
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
setValue(newValue: number) {
|
|
338
|
-
this.module.HEAP8[this.ptr >>> 0] = newValue
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
export class Uint8Ref extends ValueRef<number> {
|
|
343
|
-
getValue() {
|
|
344
|
-
return this.module.HEAPU8[this.ptr >>> 0] as number
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
setValue(newValue: number) {
|
|
348
|
-
this.module.HEAPU8[this.ptr >>> 0] = newValue
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
export class Int16Ref extends ValueRef<number> {
|
|
353
|
-
getValue() {
|
|
354
|
-
return this.module.HEAP16[this.ptr >>> 1] as number
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
setValue(newValue: number) {
|
|
358
|
-
this.module.HEAP16[this.ptr >>> 1] = newValue
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
export class Uint16Ref extends ValueRef<number> {
|
|
363
|
-
getValue() {
|
|
364
|
-
return this.module.HEAPU16[this.ptr >>> 1] as number
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
setValue(newValue: number) {
|
|
368
|
-
this.module.HEAPU16[this.ptr >>> 1] = newValue
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
export class Int32Ref extends ValueRef<number> {
|
|
373
|
-
getValue() {
|
|
374
|
-
return this.module.HEAP32[this.ptr >>> 2] as number
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
setValue(newValue: number) {
|
|
378
|
-
this.module.HEAP32[this.ptr >>> 2] = newValue
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
export class Uint32Ref extends ValueRef<number> {
|
|
383
|
-
getValue() {
|
|
384
|
-
return this.module.HEAPU32[this.ptr >>> 2] as number
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
setValue(newValue: number) {
|
|
388
|
-
this.module.HEAPU32[this.ptr >>> 2] = newValue
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
export class PointerRef extends Uint32Ref { }
|
|
393
|
-
|
|
394
|
-
export class Float32Ref extends ValueRef<number> {
|
|
395
|
-
getValue() {
|
|
396
|
-
return this.module.HEAPF32[this.ptr >>> 2] as number
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
setValue(newValue: number) {
|
|
400
|
-
this.module.HEAPF32[this.ptr >>> 2] = newValue
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
export class Float64Ref extends ValueRef<number> {
|
|
405
|
-
getValue() {
|
|
406
|
-
return this.module.HEAPF64[this.ptr >>> 3] as number
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
setValue(newValue: number) {
|
|
410
|
-
this.module.HEAPF64[this.ptr >>> 3] = newValue
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
export class NullTerminatedUtf8StringRef extends ValueRef<string> {
|
|
415
|
-
getValue() {
|
|
416
|
-
const ptr = this.ptr >>> 0
|
|
417
|
-
|
|
418
|
-
const heapU8 = this.module.HEAPU8
|
|
419
|
-
|
|
420
|
-
const endByteOffset = heapU8.subarray(ptr).indexOf(0)
|
|
421
|
-
|
|
422
|
-
const strBytes = heapU8.subarray(ptr, ptr + endByteOffset)
|
|
423
|
-
|
|
424
|
-
const str = decodeUtf8(strBytes)
|
|
425
|
-
|
|
426
|
-
return str
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
setValue(newValue: string) {
|
|
430
|
-
throw new Error('Unimplemented')
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
abstract class TypedArrayRef<T extends TypedArray> {
|
|
435
|
-
protected ptr: number
|
|
436
|
-
readonly length: number
|
|
437
|
-
private readonly manager: WasmMemoryManager
|
|
438
|
-
|
|
439
|
-
get module() { return this.manager.wasmModule }
|
|
440
|
-
|
|
441
|
-
constructor(ptr: number, length: number, manager: WasmMemoryManager) {
|
|
442
|
-
this.ptr = ptr
|
|
443
|
-
this.length = length
|
|
444
|
-
this.manager = manager
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
get view() {
|
|
448
|
-
this.assertNotFreed()
|
|
449
|
-
return this.getView()
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
protected abstract getView(): T
|
|
453
|
-
|
|
454
|
-
slice(start?: number, end?: number) {
|
|
455
|
-
return this.view.slice(start, end)
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
get address() {
|
|
459
|
-
this.assertNotFreed()
|
|
460
|
-
return this.ptr
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
clear() {
|
|
464
|
-
this.view.fill(0)
|
|
465
|
-
return this
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
free() {
|
|
469
|
-
this.manager.free(this)
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
clearAddress() {
|
|
473
|
-
this.ptr = 0
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
detach() {
|
|
477
|
-
return this.manager.detach(this)
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
get isFreed() { return this.ptr == 0 }
|
|
481
|
-
|
|
482
|
-
protected assertNotFreed() {
|
|
483
|
-
if (this.isFreed) {
|
|
484
|
-
throw new Error('Attempt to read a freed WASM typed array reference.')
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
export class Int8ArrayRef extends TypedArrayRef<Int8Array> {
|
|
490
|
-
getView() {
|
|
491
|
-
const startIndex = this.ptr >>> 0
|
|
492
|
-
return this.module.HEAP8.subarray(startIndex, startIndex + this.length) as Int8Array
|
|
493
|
-
}
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
export class Uint8ArrayRef extends TypedArrayRef<Uint8Array> {
|
|
497
|
-
getView() {
|
|
498
|
-
const startIndex = this.ptr >>> 0
|
|
499
|
-
return this.module.HEAPU8.subarray(startIndex, startIndex + this.length) as Uint8Array
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
readAsNullTerminatedUtf8String(): string {
|
|
503
|
-
let strBytes = this.view
|
|
504
|
-
|
|
505
|
-
const indexOfFirstZero = strBytes.indexOf(0)
|
|
506
|
-
|
|
507
|
-
if (indexOfFirstZero >= 0) {
|
|
508
|
-
strBytes = strBytes.subarray(0, indexOfFirstZero)
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
const str = decodeUtf8(strBytes)
|
|
512
|
-
|
|
513
|
-
return str
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
export class Int16ArrayRef extends TypedArrayRef<Int16Array> {
|
|
518
|
-
getView() {
|
|
519
|
-
const startIndex = this.ptr >>> 1
|
|
520
|
-
return this.module.HEAP16.subarray(startIndex, startIndex + this.length) as Int16Array
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
export class Uint16ArrayRef extends TypedArrayRef<Uint16Array> {
|
|
525
|
-
getView() {
|
|
526
|
-
const startIndex = this.ptr >>> 1
|
|
527
|
-
return this.module.HEAPU16.subarray(startIndex, startIndex + this.length) as Uint16Array
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
export class Int32ArrayRef extends TypedArrayRef<Int32Array> {
|
|
532
|
-
getView() {
|
|
533
|
-
const startIndex = this.ptr >>> 2
|
|
534
|
-
return this.module.HEAP32.subarray(startIndex, startIndex + this.length) as Int32Array
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
export class Uint32ArrayRef extends TypedArrayRef<Uint32Array> {
|
|
539
|
-
getView() {
|
|
540
|
-
const startIndex = this.ptr >>> 2
|
|
541
|
-
return this.module.HEAPU32.subarray(startIndex, startIndex + this.length) as Uint32Array
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
export class Float32ArrayRef extends TypedArrayRef<Float32Array> {
|
|
546
|
-
getView() {
|
|
547
|
-
const startIndex = this.ptr >>> 2
|
|
548
|
-
return this.module.HEAPF32.subarray(startIndex, startIndex + this.length) as Float32Array
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
export class Float64ArrayRef extends TypedArrayRef<Float64Array> {
|
|
553
|
-
getView() {
|
|
554
|
-
const startIndex = this.ptr >>> 3
|
|
555
|
-
return this.module.HEAPF64.subarray(startIndex, startIndex + this.length) as Float64Array
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
export type WasmRef = ValueRef<number> | ValueRef<string> | TypedArrayRef<TypedArray>
|
|
559
|
-
|
|
560
|
-
export interface WasmMemoryManagerOptions {
|
|
561
|
-
wasmAlloc?: WasmAllocMethod
|
|
562
|
-
wasmFree?: WasmFreeMethod
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
export type WasmAllocMethod = (size: number) => number
|
|
566
|
-
export type WasmFreeMethod = (address: number) => void
|