@fugood/node-whisper-wasm 1.2.0-rc.1 → 1.2.0-rc.3
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/index.d.ts +91 -0
- package/index.js +132 -7
- package/package.json +1 -1
- package/wasm/whisper-node.js +1 -1
- package/wasm/whisper-node.threads.js +1 -1
- package/wasm/whisper-node.threads.wasm +0 -0
- package/wasm/whisper-node.wasm +0 -0
- package/worker.js +44 -0
package/index.d.ts
CHANGED
|
@@ -1,8 +1,31 @@
|
|
|
1
|
+
/** Alignment heads for DTW token timestamps: the model the context loads, or `n-top-most` */
|
|
2
|
+
export type DtwAheadsPreset =
|
|
3
|
+
| 'tiny.en'
|
|
4
|
+
| 'tiny'
|
|
5
|
+
| 'base.en'
|
|
6
|
+
| 'base'
|
|
7
|
+
| 'small.en'
|
|
8
|
+
| 'small'
|
|
9
|
+
| 'medium.en'
|
|
10
|
+
| 'medium'
|
|
11
|
+
| 'large-v1'
|
|
12
|
+
| 'large-v2'
|
|
13
|
+
| 'large-v3'
|
|
14
|
+
| 'large-v3-turbo'
|
|
15
|
+
| 'n-top-most'
|
|
16
|
+
|
|
1
17
|
export interface NativeContextOptions {
|
|
2
18
|
filePath: string
|
|
3
19
|
modelUrl?: string
|
|
4
20
|
useFlashAttn?: boolean
|
|
5
21
|
useGpu?: boolean
|
|
22
|
+
/**
|
|
23
|
+
* Compute DTW-aligned token timestamps (`tokens[].tDtw` with `includeTokens`) with the
|
|
24
|
+
* alignment heads of this model. Not available with flash attention: see `WhisperContext.dtw`.
|
|
25
|
+
*/
|
|
26
|
+
dtwAheadsPreset?: DtwAheadsPreset
|
|
27
|
+
/** Text layers used by the `n-top-most` preset (Default: all) */
|
|
28
|
+
dtwNTop?: number
|
|
6
29
|
maxModelBytes?: number
|
|
7
30
|
cacheModel?: boolean
|
|
8
31
|
modelCacheName?: string
|
|
@@ -34,9 +57,14 @@ export interface NativeParakeetContextOptions {
|
|
|
34
57
|
}
|
|
35
58
|
|
|
36
59
|
export interface TranscribeOptions {
|
|
60
|
+
/** Spoken language code, or 'auto' to detect it (Default: 'en') */
|
|
37
61
|
language?: string
|
|
38
62
|
translate?: boolean
|
|
39
63
|
maxThreads?: number
|
|
64
|
+
/** Accepted for compatibility with the native builds, ignored in the browser */
|
|
65
|
+
cpu_mask?: string
|
|
66
|
+
/** Accepted for compatibility with the native builds, ignored in the browser */
|
|
67
|
+
cpu_strict?: boolean
|
|
40
68
|
maxContext?: number
|
|
41
69
|
maxLen?: number
|
|
42
70
|
tokenTimestamps?: boolean
|
|
@@ -44,12 +72,28 @@ export interface TranscribeOptions {
|
|
|
44
72
|
wordThold?: number
|
|
45
73
|
offset?: number
|
|
46
74
|
duration?: number
|
|
75
|
+
/**
|
|
76
|
+
* Encoder audio context size: a non-negative integer (0 = model default, at most the
|
|
77
|
+
* model's audio context, 1500 for stock models), or 'auto' to fit it to the input length
|
|
78
|
+
*/
|
|
79
|
+
audioCtx?: number | 'auto'
|
|
47
80
|
temperature?: number
|
|
48
81
|
temperatureInc?: number
|
|
49
82
|
beamSize?: number
|
|
50
83
|
bestOf?: number
|
|
84
|
+
/** Maximum number of text tokens per segment, 0 for no limit (Default: 0) */
|
|
85
|
+
maxTokens?: number
|
|
86
|
+
/** One segment per 30-second decoding window (Default: false) */
|
|
87
|
+
singleSegment?: boolean
|
|
88
|
+
noSpeechThold?: number
|
|
89
|
+
logprobThold?: number
|
|
90
|
+
entropyThold?: number
|
|
91
|
+
suppressBlank?: boolean
|
|
92
|
+
suppressNst?: boolean
|
|
51
93
|
prompt?: string
|
|
52
94
|
nProcessors?: number
|
|
95
|
+
/** Add each segment's text tokens (`segments[].tokens`) to the result (Default: false) */
|
|
96
|
+
includeTokens?: boolean
|
|
53
97
|
onProgress?: (progress: number) => void
|
|
54
98
|
onNewSegments?: (result: TranscribeNewSegmentsResult) => void
|
|
55
99
|
}
|
|
@@ -61,13 +105,36 @@ export interface TranscribeNewSegmentsResult {
|
|
|
61
105
|
segments: TranscribeResult['segments']
|
|
62
106
|
}
|
|
63
107
|
|
|
108
|
+
/** A text token of a Whisper result (special and timestamp tokens are left out) */
|
|
109
|
+
export interface TranscribeToken {
|
|
110
|
+
id: number
|
|
111
|
+
/** A character split across tokens comes with the token that completes it */
|
|
112
|
+
text: string
|
|
113
|
+
p: number
|
|
114
|
+
plog: number
|
|
115
|
+
/** Start / end in ms with `tokenTimestamps`, else -1 */
|
|
116
|
+
t0: number
|
|
117
|
+
t1: number
|
|
118
|
+
/** DTW-aligned time in ms when the context uses DTW (`dtwAheadsPreset`), else -1 */
|
|
119
|
+
tDtw: number
|
|
120
|
+
}
|
|
121
|
+
|
|
64
122
|
export interface TranscribeResult {
|
|
65
123
|
language?: string
|
|
124
|
+
/** Whisper: probability of the language when it was auto-detected (`language: 'auto'`) */
|
|
125
|
+
languageProb?: number
|
|
66
126
|
result: string
|
|
127
|
+
/** Segments, t0 / t1 in ms */
|
|
67
128
|
segments: Array<{
|
|
68
129
|
text: string
|
|
69
130
|
t0: number
|
|
70
131
|
t1: number
|
|
132
|
+
/** Whisper: probability that the segment's audio window has no speech */
|
|
133
|
+
noSpeechProb?: number
|
|
134
|
+
/** Whisper: mean log probability of the segment's text tokens */
|
|
135
|
+
avgLogprob?: number
|
|
136
|
+
/** Whisper with `includeTokens` */
|
|
137
|
+
tokens?: TranscribeToken[]
|
|
71
138
|
}>
|
|
72
139
|
isAborted: boolean
|
|
73
140
|
}
|
|
@@ -80,19 +147,37 @@ export interface ParakeetTranscribeOptions {
|
|
|
80
147
|
}
|
|
81
148
|
|
|
82
149
|
export interface VadOptions {
|
|
150
|
+
/** Probability threshold to consider as speech (Default: 0.5) */
|
|
83
151
|
threshold?: number
|
|
152
|
+
/** Silence threshold while in speech (Default: max(threshold - 0.15, 0.01)) */
|
|
153
|
+
negThreshold?: number
|
|
84
154
|
minSpeechDurationMs?: number
|
|
85
155
|
minSilenceDurationMs?: number
|
|
156
|
+
/** Default: no limit */
|
|
86
157
|
maxSpeechDurationS?: number
|
|
87
158
|
speechPadMs?: number
|
|
159
|
+
/** @deprecated Ignored since VAD runs on vad.cpp */
|
|
88
160
|
samplesOverlap?: number
|
|
89
161
|
}
|
|
90
162
|
|
|
163
|
+
/** Speech segment, times in 10 ms units (centiseconds) */
|
|
91
164
|
export interface VadSegment {
|
|
92
165
|
t0: number
|
|
93
166
|
t1: number
|
|
94
167
|
}
|
|
95
168
|
|
|
169
|
+
/** Stateful VAD over live audio, from `WhisperVadContext.createStream()` */
|
|
170
|
+
export interface WhisperVadStream {
|
|
171
|
+
readonly sampleRate: number
|
|
172
|
+
readonly hopSamples: number
|
|
173
|
+
/** Feed 16 kHz mono PCM16 (or Float32Array) audio; resolves with the new speech probabilities */
|
|
174
|
+
push(audio: ArrayBuffer | ArrayBufferView | Float32Array): Promise<number[]>
|
|
175
|
+
/** End of audio: resolves with the probabilities still pending */
|
|
176
|
+
flush(): Promise<number[]>
|
|
177
|
+
reset(): Promise<void>
|
|
178
|
+
release(): Promise<void>
|
|
179
|
+
}
|
|
180
|
+
|
|
96
181
|
export interface BenchResult {
|
|
97
182
|
config: string
|
|
98
183
|
nThreads: number
|
|
@@ -103,6 +188,8 @@ export interface BenchResult {
|
|
|
103
188
|
}
|
|
104
189
|
|
|
105
190
|
export interface WhisperContext {
|
|
191
|
+
/** Whether DTW token timestamps are computed (`dtwAheadsPreset`, off with flash attention) */
|
|
192
|
+
readonly dtw: boolean
|
|
106
193
|
transcribe(
|
|
107
194
|
filePath: string,
|
|
108
195
|
options?: TranscribeOptions,
|
|
@@ -156,6 +243,10 @@ export interface ParakeetContext {
|
|
|
156
243
|
}
|
|
157
244
|
|
|
158
245
|
export interface WhisperVadContext {
|
|
246
|
+
readonly sampleRate: number
|
|
247
|
+
readonly hopSamples: number
|
|
248
|
+
readonly arch: string
|
|
249
|
+
createStream(): Promise<WhisperVadStream>
|
|
159
250
|
detectSpeech(filePath: string, options?: VadOptions): Promise<VadSegment[]>
|
|
160
251
|
detectSpeechFile(filePath: string, options?: VadOptions): Promise<VadSegment[]>
|
|
161
252
|
detectSpeechData(
|
package/index.js
CHANGED
|
@@ -1123,6 +1123,7 @@ const createWhisperNodeApi = function (root) {
|
|
|
1123
1123
|
this._id = id
|
|
1124
1124
|
this._meta = meta
|
|
1125
1125
|
this._released = false
|
|
1126
|
+
this.dtw = meta.dtw === true
|
|
1126
1127
|
}
|
|
1127
1128
|
|
|
1128
1129
|
WorkerWhisperContextInstance.prototype._assertValid = function () {
|
|
@@ -1209,6 +1210,10 @@ const createWhisperNodeApi = function (root) {
|
|
|
1209
1210
|
|
|
1210
1211
|
var model = await ensureModel(runtime, modelSource, 'whisper', options)
|
|
1211
1212
|
var useFlashAttn = useGpu && options.useFlashAttn === true
|
|
1213
|
+
// DTW token timestamps: alignment heads preset ('' = off), text layers of 'n-top-most'
|
|
1214
|
+
var dtwAheadsPreset =
|
|
1215
|
+
typeof options.dtwAheadsPreset === 'string' ? options.dtwAheadsPreset : ''
|
|
1216
|
+
var dtwNTop = Number.isFinite(options.dtwNTop) ? Math.floor(options.dtwNTop) : -1
|
|
1212
1217
|
var init
|
|
1213
1218
|
try {
|
|
1214
1219
|
init = unwrapWasmResult(
|
|
@@ -1216,6 +1221,8 @@ const createWhisperNodeApi = function (root) {
|
|
|
1216
1221
|
model.virtualPath,
|
|
1217
1222
|
useGpu,
|
|
1218
1223
|
useFlashAttn,
|
|
1224
|
+
dtwAheadsPreset,
|
|
1225
|
+
dtwNTop,
|
|
1219
1226
|
),
|
|
1220
1227
|
)
|
|
1221
1228
|
} catch (error) {
|
|
@@ -1226,7 +1233,13 @@ const createWhisperNodeApi = function (root) {
|
|
|
1226
1233
|
useGpu = false
|
|
1227
1234
|
useFlashAttn = false
|
|
1228
1235
|
init = unwrapWasmResult(
|
|
1229
|
-
await runtime.__wasm_init_whisper(
|
|
1236
|
+
await runtime.__wasm_init_whisper(
|
|
1237
|
+
model.virtualPath,
|
|
1238
|
+
false,
|
|
1239
|
+
false,
|
|
1240
|
+
dtwAheadsPreset,
|
|
1241
|
+
dtwNTop,
|
|
1242
|
+
),
|
|
1230
1243
|
)
|
|
1231
1244
|
}
|
|
1232
1245
|
|
|
@@ -1235,6 +1248,7 @@ const createWhisperNodeApi = function (root) {
|
|
|
1235
1248
|
wasmFilePath: model.virtualPath,
|
|
1236
1249
|
useGpu: useGpu,
|
|
1237
1250
|
useFlashAttn: useFlashAttn,
|
|
1251
|
+
dtw: init.dtw === true,
|
|
1238
1252
|
bytes: model.bytes,
|
|
1239
1253
|
cacheHit: model.cacheHit,
|
|
1240
1254
|
cacheStored: model.cacheStored,
|
|
@@ -1249,6 +1263,7 @@ const createWhisperNodeApi = function (root) {
|
|
|
1249
1263
|
this._id = id
|
|
1250
1264
|
this._meta = meta
|
|
1251
1265
|
this._released = false
|
|
1266
|
+
this.dtw = meta.dtw === true
|
|
1252
1267
|
}
|
|
1253
1268
|
|
|
1254
1269
|
WhisperContextInstance.prototype._assertValid = function () {
|
|
@@ -1609,6 +1624,46 @@ const createWhisperNodeApi = function (root) {
|
|
|
1609
1624
|
this._id = id
|
|
1610
1625
|
this._meta = meta
|
|
1611
1626
|
this._released = false
|
|
1627
|
+
this.sampleRate = meta.sampleRate || 16000
|
|
1628
|
+
this.hopSamples = meta.hopSamples || 512
|
|
1629
|
+
this.arch = meta.arch || ''
|
|
1630
|
+
}
|
|
1631
|
+
|
|
1632
|
+
WorkerWhisperVadContextInstance.prototype.createStream = async function () {
|
|
1633
|
+
this._assertValid()
|
|
1634
|
+
var created = await this._proxy.request('vadStreamCreate', [this._id])
|
|
1635
|
+
return new WorkerWhisperVadStreamInstance(
|
|
1636
|
+
this._proxy,
|
|
1637
|
+
created.id,
|
|
1638
|
+
this.sampleRate,
|
|
1639
|
+
this.hopSamples,
|
|
1640
|
+
)
|
|
1641
|
+
}
|
|
1642
|
+
|
|
1643
|
+
// Requests reach the worker in call order and the worker-side stream
|
|
1644
|
+
// serializes them, so no queue is needed here.
|
|
1645
|
+
function WorkerWhisperVadStreamInstance(proxy, id, sampleRate, hopSamples) {
|
|
1646
|
+
this._proxy = proxy
|
|
1647
|
+
this._id = id
|
|
1648
|
+
this.sampleRate = sampleRate
|
|
1649
|
+
this.hopSamples = hopSamples
|
|
1650
|
+
}
|
|
1651
|
+
|
|
1652
|
+
WorkerWhisperVadStreamInstance.prototype.push = function (audio) {
|
|
1653
|
+
var data = copyFloat32Audio(audio)
|
|
1654
|
+
return this._proxy.request('vadStreamPush', [this._id, data], [data.buffer])
|
|
1655
|
+
}
|
|
1656
|
+
|
|
1657
|
+
WorkerWhisperVadStreamInstance.prototype.flush = function () {
|
|
1658
|
+
return this._proxy.request('vadStreamFlush', [this._id])
|
|
1659
|
+
}
|
|
1660
|
+
|
|
1661
|
+
WorkerWhisperVadStreamInstance.prototype.reset = function () {
|
|
1662
|
+
return this._proxy.request('vadStreamReset', [this._id]).then(function () {})
|
|
1663
|
+
}
|
|
1664
|
+
|
|
1665
|
+
WorkerWhisperVadStreamInstance.prototype.release = function () {
|
|
1666
|
+
return this._proxy.request('vadStreamRelease', [this._id]).then(function () {})
|
|
1612
1667
|
}
|
|
1613
1668
|
|
|
1614
1669
|
WorkerWhisperVadContextInstance.prototype._assertValid = function () {
|
|
@@ -1663,12 +1718,7 @@ const createWhisperNodeApi = function (root) {
|
|
|
1663
1718
|
var useGpu = false
|
|
1664
1719
|
var nThreads = normalizeMaxThreads(options.nThreads || 1, runtime) || 1
|
|
1665
1720
|
|
|
1666
|
-
|
|
1667
|
-
emitLog(
|
|
1668
|
-
'WARN',
|
|
1669
|
-
'WASM VAD currently falls back to CPU because ggml-webgpu does not support the VAD graph safely yet.',
|
|
1670
|
-
)
|
|
1671
|
-
}
|
|
1721
|
+
// useGpu is ignored: VAD always runs on the CPU (vad.cpp's own kernels)
|
|
1672
1722
|
|
|
1673
1723
|
var model = await ensureModel(runtime, modelSource, 'vad', options)
|
|
1674
1724
|
var init = unwrapWasmResult(
|
|
@@ -1680,6 +1730,9 @@ const createWhisperNodeApi = function (root) {
|
|
|
1680
1730
|
wasmFilePath: model.virtualPath,
|
|
1681
1731
|
useGpu: useGpu,
|
|
1682
1732
|
nThreads: init.nThreads,
|
|
1733
|
+
sampleRate: init.sampleRate,
|
|
1734
|
+
hopSamples: init.hopSamples,
|
|
1735
|
+
arch: init.arch,
|
|
1683
1736
|
bytes: model.bytes,
|
|
1684
1737
|
cacheHit: model.cacheHit,
|
|
1685
1738
|
cacheStored: model.cacheStored,
|
|
@@ -1694,6 +1747,78 @@ const createWhisperNodeApi = function (root) {
|
|
|
1694
1747
|
this._id = id
|
|
1695
1748
|
this._meta = meta
|
|
1696
1749
|
this._released = false
|
|
1750
|
+
this.sampleRate = meta.sampleRate || 16000
|
|
1751
|
+
this.hopSamples = meta.hopSamples || 512
|
|
1752
|
+
this.arch = meta.arch || ''
|
|
1753
|
+
}
|
|
1754
|
+
|
|
1755
|
+
WhisperVadContextInstance.prototype.createStream = async function () {
|
|
1756
|
+
this._assertValid()
|
|
1757
|
+
var created = unwrapWasmResult(
|
|
1758
|
+
await this._runtime.__wasm_vad_stream_create(this._id),
|
|
1759
|
+
)
|
|
1760
|
+
return new WhisperVadStreamInstance(
|
|
1761
|
+
this._runtime,
|
|
1762
|
+
this._id,
|
|
1763
|
+
created.streamId,
|
|
1764
|
+
this.sampleRate,
|
|
1765
|
+
this.hopSamples,
|
|
1766
|
+
)
|
|
1767
|
+
}
|
|
1768
|
+
|
|
1769
|
+
// Stateful VAD stream: the model state carries over between pushes. Calls
|
|
1770
|
+
// run in call order, each after the previous one settles.
|
|
1771
|
+
function WhisperVadStreamInstance(runtime, contextId, streamId, sampleRate, hopSamples) {
|
|
1772
|
+
this._runtime = runtime
|
|
1773
|
+
this._contextId = contextId
|
|
1774
|
+
this._streamId = streamId
|
|
1775
|
+
this._queue = Promise.resolve()
|
|
1776
|
+
this.sampleRate = sampleRate
|
|
1777
|
+
this.hopSamples = hopSamples
|
|
1778
|
+
}
|
|
1779
|
+
|
|
1780
|
+
WhisperVadStreamInstance.prototype._enqueue = function (op) {
|
|
1781
|
+
var run = this._queue.then(op, op)
|
|
1782
|
+
this._queue = run.catch(function () {})
|
|
1783
|
+
return run
|
|
1784
|
+
}
|
|
1785
|
+
|
|
1786
|
+
WhisperVadStreamInstance.prototype.push = function (audio) {
|
|
1787
|
+
var self = this
|
|
1788
|
+
// Copy now: the caller may reuse its buffer before this call runs
|
|
1789
|
+
var data = copyFloat32Audio(audio)
|
|
1790
|
+
return this._enqueue(async function () {
|
|
1791
|
+
var result = unwrapWasmResult(
|
|
1792
|
+
await self._runtime.__wasm_vad_stream_push(self._contextId, self._streamId, data),
|
|
1793
|
+
)
|
|
1794
|
+
return result.probs || []
|
|
1795
|
+
})
|
|
1796
|
+
}
|
|
1797
|
+
|
|
1798
|
+
WhisperVadStreamInstance.prototype.flush = function () {
|
|
1799
|
+
var self = this
|
|
1800
|
+
return this._enqueue(async function () {
|
|
1801
|
+
var result = unwrapWasmResult(
|
|
1802
|
+
await self._runtime.__wasm_vad_stream_flush(self._contextId, self._streamId),
|
|
1803
|
+
)
|
|
1804
|
+
return result.probs || []
|
|
1805
|
+
})
|
|
1806
|
+
}
|
|
1807
|
+
|
|
1808
|
+
WhisperVadStreamInstance.prototype.reset = function () {
|
|
1809
|
+
var self = this
|
|
1810
|
+
return this._enqueue(async function () {
|
|
1811
|
+
unwrapWasmResult(
|
|
1812
|
+
await self._runtime.__wasm_vad_stream_reset(self._contextId, self._streamId),
|
|
1813
|
+
)
|
|
1814
|
+
})
|
|
1815
|
+
}
|
|
1816
|
+
|
|
1817
|
+
WhisperVadStreamInstance.prototype.release = function () {
|
|
1818
|
+
var self = this
|
|
1819
|
+
return this._enqueue(async function () {
|
|
1820
|
+
await self._runtime.__wasm_vad_stream_release(self._contextId, self._streamId)
|
|
1821
|
+
})
|
|
1697
1822
|
}
|
|
1698
1823
|
|
|
1699
1824
|
WhisperVadContextInstance.prototype._assertValid = function () {
|