@huggingface/transformers 3.0.0-alpha.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/LICENSE +202 -0
- package/README.md +376 -0
- package/dist/ort-wasm-simd-threaded.jsep.wasm +0 -0
- package/dist/transformers.cjs +30741 -0
- package/dist/transformers.cjs.map +1 -0
- package/dist/transformers.js +33858 -0
- package/dist/transformers.js.map +1 -0
- package/dist/transformers.min.cjs +173 -0
- package/dist/transformers.min.cjs.map +1 -0
- package/dist/transformers.min.js +231 -0
- package/dist/transformers.min.js.map +1 -0
- package/package.json +92 -0
- package/src/backends/onnx.js +151 -0
- package/src/configs.js +360 -0
- package/src/env.js +152 -0
- package/src/generation/configuration_utils.js +381 -0
- package/src/generation/logits_process.js +716 -0
- package/src/generation/logits_sampler.js +204 -0
- package/src/generation/parameters.js +35 -0
- package/src/generation/stopping_criteria.js +156 -0
- package/src/generation/streamers.js +212 -0
- package/src/models/whisper/common_whisper.js +151 -0
- package/src/models/whisper/generation_whisper.js +89 -0
- package/src/models.js +7028 -0
- package/src/ops/registry.js +92 -0
- package/src/pipelines.js +3341 -0
- package/src/processors.js +2614 -0
- package/src/tokenizers.js +4395 -0
- package/src/transformers.js +28 -0
- package/src/utils/audio.js +704 -0
- package/src/utils/constants.js +2 -0
- package/src/utils/core.js +149 -0
- package/src/utils/data-structures.js +445 -0
- package/src/utils/devices.js +11 -0
- package/src/utils/dtypes.js +62 -0
- package/src/utils/generic.js +35 -0
- package/src/utils/hub.js +671 -0
- package/src/utils/image.js +745 -0
- package/src/utils/maths.js +1050 -0
- package/src/utils/tensor.js +1378 -0
- package/types/backends/onnx.d.ts +26 -0
- package/types/backends/onnx.d.ts.map +1 -0
- package/types/configs.d.ts +59 -0
- package/types/configs.d.ts.map +1 -0
- package/types/env.d.ts +106 -0
- package/types/env.d.ts.map +1 -0
- package/types/generation/configuration_utils.d.ts +320 -0
- package/types/generation/configuration_utils.d.ts.map +1 -0
- package/types/generation/logits_process.d.ts +354 -0
- package/types/generation/logits_process.d.ts.map +1 -0
- package/types/generation/logits_sampler.d.ts +51 -0
- package/types/generation/logits_sampler.d.ts.map +1 -0
- package/types/generation/parameters.d.ts +47 -0
- package/types/generation/parameters.d.ts.map +1 -0
- package/types/generation/stopping_criteria.d.ts +81 -0
- package/types/generation/stopping_criteria.d.ts.map +1 -0
- package/types/generation/streamers.d.ts +81 -0
- package/types/generation/streamers.d.ts.map +1 -0
- package/types/models/whisper/common_whisper.d.ts +8 -0
- package/types/models/whisper/common_whisper.d.ts.map +1 -0
- package/types/models/whisper/generation_whisper.d.ts +76 -0
- package/types/models/whisper/generation_whisper.d.ts.map +1 -0
- package/types/models.d.ts +3845 -0
- package/types/models.d.ts.map +1 -0
- package/types/ops/registry.d.ts +11 -0
- package/types/ops/registry.d.ts.map +1 -0
- package/types/pipelines.d.ts +2403 -0
- package/types/pipelines.d.ts.map +1 -0
- package/types/processors.d.ts +917 -0
- package/types/processors.d.ts.map +1 -0
- package/types/tokenizers.d.ts +999 -0
- package/types/tokenizers.d.ts.map +1 -0
- package/types/transformers.d.ts +13 -0
- package/types/transformers.d.ts.map +1 -0
- package/types/utils/audio.d.ts +130 -0
- package/types/utils/audio.d.ts.map +1 -0
- package/types/utils/constants.d.ts +2 -0
- package/types/utils/constants.d.ts.map +1 -0
- package/types/utils/core.d.ts +91 -0
- package/types/utils/core.d.ts.map +1 -0
- package/types/utils/data-structures.d.ts +236 -0
- package/types/utils/data-structures.d.ts.map +1 -0
- package/types/utils/devices.d.ts +8 -0
- package/types/utils/devices.d.ts.map +1 -0
- package/types/utils/dtypes.d.ts +22 -0
- package/types/utils/dtypes.d.ts.map +1 -0
- package/types/utils/generic.d.ts +11 -0
- package/types/utils/generic.d.ts.map +1 -0
- package/types/utils/hub.d.ts +191 -0
- package/types/utils/hub.d.ts.map +1 -0
- package/types/utils/image.d.ts +119 -0
- package/types/utils/image.d.ts.map +1 -0
- package/types/utils/maths.d.ts +280 -0
- package/types/utils/maths.d.ts.map +1 -0
- package/types/utils/tensor.d.ts +392 -0
- package/types/utils/tensor.d.ts.map +1 -0
|
@@ -0,0 +1,704 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Helper module for audio processing.
|
|
3
|
+
*
|
|
4
|
+
* These functions and classes are only used internally,
|
|
5
|
+
* meaning an end-user shouldn't need to access anything here.
|
|
6
|
+
*
|
|
7
|
+
* @module utils/audio
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
getFile,
|
|
12
|
+
} from './hub.js';
|
|
13
|
+
import { FFT, max } from './maths.js';
|
|
14
|
+
import {
|
|
15
|
+
calculateReflectOffset,
|
|
16
|
+
} from './core.js';
|
|
17
|
+
import { Tensor, matmul } from './tensor.js';
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Helper function to read audio from a path/URL.
|
|
22
|
+
* @param {string|URL} url The path/URL to load the audio from.
|
|
23
|
+
* @param {number} sampling_rate The sampling rate to use when decoding the audio.
|
|
24
|
+
* @returns {Promise<Float32Array>} The decoded audio as a `Float32Array`.
|
|
25
|
+
*/
|
|
26
|
+
export async function read_audio(url, sampling_rate) {
|
|
27
|
+
if (typeof AudioContext === 'undefined') {
|
|
28
|
+
// Running in node or an environment without AudioContext
|
|
29
|
+
throw Error(
|
|
30
|
+
"Unable to load audio from path/URL since `AudioContext` is not available in your environment. " +
|
|
31
|
+
"Instead, audio data should be passed directly to the pipeline/processor. " +
|
|
32
|
+
"For more information and some example code, see https://huggingface.co/docs/transformers.js/guides/node-audio-processing."
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const response = await (await getFile(url)).arrayBuffer();
|
|
37
|
+
const audioCTX = new AudioContext({ sampleRate: sampling_rate });
|
|
38
|
+
if (typeof sampling_rate === 'undefined') {
|
|
39
|
+
console.warn(`No sampling rate provided, using default of ${audioCTX.sampleRate}Hz.`)
|
|
40
|
+
}
|
|
41
|
+
const decoded = await audioCTX.decodeAudioData(response);
|
|
42
|
+
|
|
43
|
+
/** @type {Float32Array} */
|
|
44
|
+
let audio;
|
|
45
|
+
|
|
46
|
+
// We now replicate HuggingFace's `ffmpeg_read` method:
|
|
47
|
+
if (decoded.numberOfChannels === 2) {
|
|
48
|
+
// When downmixing a stereo audio file to mono using the -ac 1 option in FFmpeg,
|
|
49
|
+
// the audio signal is summed across both channels to create a single mono channel.
|
|
50
|
+
// However, if the audio is at full scale (i.e. the highest possible volume level),
|
|
51
|
+
// the summing of the two channels can cause the audio signal to clip or distort.
|
|
52
|
+
|
|
53
|
+
// To prevent this clipping, FFmpeg applies a scaling factor of 1/sqrt(2) (~ 0.707)
|
|
54
|
+
// to the audio signal before summing the two channels. This scaling factor ensures
|
|
55
|
+
// that the combined audio signal will not exceed the maximum possible level, even
|
|
56
|
+
// if both channels are at full scale.
|
|
57
|
+
|
|
58
|
+
// After applying this scaling factor, the audio signal from both channels is summed
|
|
59
|
+
// to create a single mono channel. It's worth noting that this scaling factor is
|
|
60
|
+
// only applied when downmixing stereo audio to mono using the -ac 1 option in FFmpeg.
|
|
61
|
+
// If you're using a different downmixing method, or if you're not downmixing the
|
|
62
|
+
// audio at all, this scaling factor may not be needed.
|
|
63
|
+
const SCALING_FACTOR = Math.sqrt(2);
|
|
64
|
+
|
|
65
|
+
const left = decoded.getChannelData(0);
|
|
66
|
+
const right = decoded.getChannelData(1);
|
|
67
|
+
|
|
68
|
+
audio = new Float32Array(left.length);
|
|
69
|
+
for (let i = 0; i < decoded.length; ++i) {
|
|
70
|
+
audio[i] = SCALING_FACTOR * (left[i] + right[i]) / 2;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
} else {
|
|
74
|
+
// If the audio is not stereo, we can just use the first channel:
|
|
75
|
+
audio = decoded.getChannelData(0);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return audio;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Helper function to generate windows that are special cases of the generalized cosine window.
|
|
83
|
+
* See https://www.mathworks.com/help/signal/ug/generalized-cosine-windows.html for more information.
|
|
84
|
+
* @param {number} M Number of points in the output window. If zero or less, an empty array is returned.
|
|
85
|
+
* @param {number} a_0 Offset for the generalized cosine window.
|
|
86
|
+
* @returns {Float64Array} The generated window.
|
|
87
|
+
*/
|
|
88
|
+
function generalized_cosine_window(M, a_0) {
|
|
89
|
+
if (M < 1) {
|
|
90
|
+
return new Float64Array();
|
|
91
|
+
}
|
|
92
|
+
if (M === 1) {
|
|
93
|
+
return new Float64Array([1]);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const a_1 = 1 - a_0;
|
|
97
|
+
const factor = 2 * Math.PI / (M - 1);
|
|
98
|
+
|
|
99
|
+
const cos_vals = new Float64Array(M);
|
|
100
|
+
for (let i = 0; i < M; ++i) {
|
|
101
|
+
cos_vals[i] = a_0 - a_1 * Math.cos(i * factor);
|
|
102
|
+
}
|
|
103
|
+
return cos_vals;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Generates a Hanning window of length M.
|
|
108
|
+
* See https://numpy.org/doc/stable/reference/generated/numpy.hanning.html for more information.
|
|
109
|
+
*
|
|
110
|
+
* @param {number} M The length of the Hanning window to generate.
|
|
111
|
+
* @returns {Float64Array} The generated Hanning window.
|
|
112
|
+
*/
|
|
113
|
+
export function hanning(M) {
|
|
114
|
+
return generalized_cosine_window(M, 0.5);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Generates a Hamming window of length M.
|
|
120
|
+
* See https://numpy.org/doc/stable/reference/generated/numpy.hamming.html for more information.
|
|
121
|
+
*
|
|
122
|
+
* @param {number} M The length of the Hamming window to generate.
|
|
123
|
+
* @returns {Float64Array} The generated Hamming window.
|
|
124
|
+
*/
|
|
125
|
+
export function hamming(M) {
|
|
126
|
+
return generalized_cosine_window(M, 0.54);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
const HERTZ_TO_MEL_MAPPING = {
|
|
131
|
+
"htk": (/** @type {number} */ freq) => 2595.0 * Math.log10(1.0 + (freq / 700.0)),
|
|
132
|
+
"kaldi": (/** @type {number} */ freq) => 1127.0 * Math.log(1.0 + (freq / 700.0)),
|
|
133
|
+
"slaney": (/** @type {number} */ freq, min_log_hertz = 1000.0, min_log_mel = 15.0, logstep = 27.0 / Math.log(6.4)) =>
|
|
134
|
+
freq >= min_log_hertz
|
|
135
|
+
? min_log_mel + Math.log(freq / min_log_hertz) * logstep
|
|
136
|
+
: 3.0 * freq / 200.0,
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* @template {Float32Array|Float64Array|number} T
|
|
141
|
+
* @param {T} freq
|
|
142
|
+
* @param {string} [mel_scale]
|
|
143
|
+
* @returns {T}
|
|
144
|
+
*/
|
|
145
|
+
function hertz_to_mel(freq, mel_scale = "htk") {
|
|
146
|
+
const fn = HERTZ_TO_MEL_MAPPING[mel_scale];
|
|
147
|
+
if (!fn) {
|
|
148
|
+
throw new Error('mel_scale should be one of "htk", "slaney" or "kaldi".');
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return typeof freq === 'number' ? fn(freq) : freq.map(x => fn(x));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const MEL_TO_HERTZ_MAPPING = {
|
|
155
|
+
"htk": (/** @type {number} */ mels) => 700.0 * (10.0 ** (mels / 2595.0) - 1.0),
|
|
156
|
+
"kaldi": (/** @type {number} */ mels) => 700.0 * (Math.exp(mels / 1127.0) - 1.0),
|
|
157
|
+
"slaney": (/** @type {number} */ mels, min_log_hertz = 1000.0, min_log_mel = 15.0, logstep = Math.log(6.4) / 27.0) => mels >= min_log_mel
|
|
158
|
+
? min_log_hertz * Math.exp(logstep * (mels - min_log_mel))
|
|
159
|
+
: 200.0 * mels / 3.0,
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* @template {Float32Array|Float64Array|number} T
|
|
164
|
+
* @param {T} mels
|
|
165
|
+
* @param {string} [mel_scale]
|
|
166
|
+
* @returns {T}
|
|
167
|
+
*/
|
|
168
|
+
function mel_to_hertz(mels, mel_scale = "htk") {
|
|
169
|
+
const fn = MEL_TO_HERTZ_MAPPING[mel_scale];
|
|
170
|
+
if (!fn) {
|
|
171
|
+
throw new Error('mel_scale should be one of "htk", "slaney" or "kaldi".');
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return typeof mels === 'number' ? fn(mels) : mels.map(x => fn(x));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Creates a triangular filter bank.
|
|
179
|
+
*
|
|
180
|
+
* Adapted from torchaudio and librosa.
|
|
181
|
+
*
|
|
182
|
+
* @param {Float64Array} fft_freqs Discrete frequencies of the FFT bins in Hz, of shape `(num_frequency_bins,)`.
|
|
183
|
+
* @param {Float64Array} filter_freqs Center frequencies of the triangular filters to create, in Hz, of shape `(num_mel_filters,)`.
|
|
184
|
+
* @returns {number[][]} of shape `(num_frequency_bins, num_mel_filters)`.
|
|
185
|
+
*/
|
|
186
|
+
function _create_triangular_filter_bank(fft_freqs, filter_freqs) {
|
|
187
|
+
const filter_diff = Float64Array.from(
|
|
188
|
+
{ length: filter_freqs.length - 1 },
|
|
189
|
+
(_, i) => filter_freqs[i + 1] - filter_freqs[i]
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
const slopes = Array.from({
|
|
193
|
+
length: fft_freqs.length
|
|
194
|
+
}, () => new Array(filter_freqs.length));
|
|
195
|
+
|
|
196
|
+
for (let j = 0; j < fft_freqs.length; ++j) {
|
|
197
|
+
const slope = slopes[j];
|
|
198
|
+
for (let i = 0; i < filter_freqs.length; ++i) {
|
|
199
|
+
slope[i] = filter_freqs[i] - fft_freqs[j];
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const numFreqs = filter_freqs.length - 2;
|
|
204
|
+
const ret = Array.from({ length: numFreqs }, () => new Array(fft_freqs.length));
|
|
205
|
+
|
|
206
|
+
for (let j = 0; j < fft_freqs.length; ++j) { // 201
|
|
207
|
+
const slope = slopes[j];
|
|
208
|
+
for (let i = 0; i < numFreqs; ++i) { // 80
|
|
209
|
+
const down = -slope[i] / filter_diff[i];
|
|
210
|
+
const up = slope[i + 2] / filter_diff[i + 1];
|
|
211
|
+
ret[i][j] = Math.max(0, Math.min(down, up));
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return ret;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Return evenly spaced numbers over a specified interval.
|
|
219
|
+
* @param {number} start The starting value of the sequence.
|
|
220
|
+
* @param {number} end The end value of the sequence.
|
|
221
|
+
* @param {number} num Number of samples to generate.
|
|
222
|
+
* @returns `num` evenly spaced samples, calculated over the interval `[start, stop]`.
|
|
223
|
+
*/
|
|
224
|
+
function linspace(start, end, num) {
|
|
225
|
+
const step = (end - start) / (num - 1);
|
|
226
|
+
return Float64Array.from({ length: num }, (_, i) => start + step * i);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Creates a frequency bin conversion matrix used to obtain a mel spectrogram. This is called a *mel filter bank*, and
|
|
231
|
+
* various implementation exist, which differ in the number of filters, the shape of the filters, the way the filters
|
|
232
|
+
* are spaced, the bandwidth of the filters, and the manner in which the spectrum is warped. The goal of these
|
|
233
|
+
* features is to approximate the non-linear human perception of the variation in pitch with respect to the frequency.
|
|
234
|
+
* @param {number} num_frequency_bins Number of frequencies used to compute the spectrogram (should be the same as in `stft`).
|
|
235
|
+
* @param {number} num_mel_filters Number of mel filters to generate.
|
|
236
|
+
* @param {number} min_frequency Lowest frequency of interest in Hz.
|
|
237
|
+
* @param {number} max_frequency Highest frequency of interest in Hz. This should not exceed `sampling_rate / 2`.
|
|
238
|
+
* @param {number} sampling_rate Sample rate of the audio waveform.
|
|
239
|
+
* @param {string} [norm] If `"slaney"`, divide the triangular mel weights by the width of the mel band (area normalization).
|
|
240
|
+
* @param {string} [mel_scale] The mel frequency scale to use, `"htk"` or `"slaney"`.
|
|
241
|
+
* @param {boolean} [triangularize_in_mel_space] If this option is enabled, the triangular filter is applied in mel space rather than frequency space.
|
|
242
|
+
* This should be set to `true` in order to get the same results as `torchaudio` when computing mel filters.
|
|
243
|
+
* @returns {number[][]} Triangular filter bank matrix, which is a 2D array of shape (`num_frequency_bins`, `num_mel_filters`).
|
|
244
|
+
* This is a projection matrix to go from a spectrogram to a mel spectrogram.
|
|
245
|
+
*/
|
|
246
|
+
export function mel_filter_bank(
|
|
247
|
+
num_frequency_bins,
|
|
248
|
+
num_mel_filters,
|
|
249
|
+
min_frequency,
|
|
250
|
+
max_frequency,
|
|
251
|
+
sampling_rate,
|
|
252
|
+
norm = null,
|
|
253
|
+
mel_scale = "htk",
|
|
254
|
+
triangularize_in_mel_space = false,
|
|
255
|
+
) {
|
|
256
|
+
if (norm !== null && norm !== "slaney") {
|
|
257
|
+
throw new Error('norm must be one of null or "slaney"');
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const mel_min = hertz_to_mel(min_frequency, mel_scale);
|
|
261
|
+
const mel_max = hertz_to_mel(max_frequency, mel_scale);
|
|
262
|
+
const mel_freqs = linspace(mel_min, mel_max, num_mel_filters + 2);
|
|
263
|
+
|
|
264
|
+
let filter_freqs = mel_to_hertz(mel_freqs, mel_scale);
|
|
265
|
+
let fft_freqs; // frequencies of FFT bins in Hz
|
|
266
|
+
|
|
267
|
+
if (triangularize_in_mel_space) {
|
|
268
|
+
const fft_bin_width = sampling_rate / (num_frequency_bins * 2);
|
|
269
|
+
fft_freqs = hertz_to_mel(Float64Array.from({ length: num_frequency_bins }, (_, i) => i * fft_bin_width), mel_scale);
|
|
270
|
+
filter_freqs = mel_freqs;
|
|
271
|
+
} else {
|
|
272
|
+
fft_freqs = linspace(0, Math.floor(sampling_rate / 2), num_frequency_bins);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const mel_filters = _create_triangular_filter_bank(fft_freqs, filter_freqs);
|
|
276
|
+
|
|
277
|
+
if (norm !== null && norm === "slaney") {
|
|
278
|
+
// Slaney-style mel is scaled to be approx constant energy per channel
|
|
279
|
+
for (let i = 0; i < num_mel_filters; ++i) {
|
|
280
|
+
const filter = mel_filters[i];
|
|
281
|
+
const enorm = 2.0 / (filter_freqs[i + 2] - filter_freqs[i]);
|
|
282
|
+
for (let j = 0; j < num_frequency_bins; ++j) {
|
|
283
|
+
// Apply this enorm to all frequency bins
|
|
284
|
+
filter[j] *= enorm;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// TODO warn if there is a zero row
|
|
290
|
+
|
|
291
|
+
return mel_filters;
|
|
292
|
+
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* @template {Float32Array|Float64Array} T
|
|
297
|
+
* Pads an array with a reflected version of itself on both ends.
|
|
298
|
+
* @param {T} array The array to pad.
|
|
299
|
+
* @param {number} left The amount of padding to add to the left.
|
|
300
|
+
* @param {number} right The amount of padding to add to the right.
|
|
301
|
+
* @returns {T} The padded array.
|
|
302
|
+
*/
|
|
303
|
+
function padReflect(array, left, right) {
|
|
304
|
+
// @ts-ignore
|
|
305
|
+
const padded = new array.constructor(array.length + left + right);
|
|
306
|
+
const w = array.length - 1;
|
|
307
|
+
|
|
308
|
+
for (let i = 0; i < array.length; ++i) {
|
|
309
|
+
padded[left + i] = array[i];
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
for (let i = 1; i <= left; ++i) {
|
|
313
|
+
padded[left - i] = array[calculateReflectOffset(i, w)];
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
for (let i = 1; i <= right; ++i) {
|
|
317
|
+
padded[w + left + i] = array[calculateReflectOffset(w - i, w)];
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return padded;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Helper function to compute `amplitude_to_db` and `power_to_db`.
|
|
325
|
+
* @template {Float32Array|Float64Array} T
|
|
326
|
+
* @param {T} spectrogram
|
|
327
|
+
* @param {number} factor
|
|
328
|
+
* @param {number} reference
|
|
329
|
+
* @param {number} min_value
|
|
330
|
+
* @param {number} db_range
|
|
331
|
+
* @returns {T}
|
|
332
|
+
*/
|
|
333
|
+
function _db_conversion_helper(spectrogram, factor, reference, min_value, db_range) {
|
|
334
|
+
if (reference <= 0) {
|
|
335
|
+
throw new Error('reference must be greater than zero');
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
if (min_value <= 0) {
|
|
339
|
+
throw new Error('min_value must be greater than zero');
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
reference = Math.max(min_value, reference);
|
|
343
|
+
|
|
344
|
+
const logReference = Math.log10(reference);
|
|
345
|
+
for (let i = 0; i < spectrogram.length; ++i) {
|
|
346
|
+
spectrogram[i] = factor * Math.log10(Math.max(min_value, spectrogram[i]) - logReference)
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
if (db_range !== null) {
|
|
350
|
+
if (db_range <= 0) {
|
|
351
|
+
throw new Error('db_range must be greater than zero');
|
|
352
|
+
}
|
|
353
|
+
const maxValue = max(spectrogram)[0] - db_range;
|
|
354
|
+
for (let i = 0; i < spectrogram.length; ++i) {
|
|
355
|
+
spectrogram[i] = Math.max(spectrogram[i], maxValue);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
return spectrogram;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Converts an amplitude spectrogram to the decibel scale. This computes `20 * log10(spectrogram / reference)`,
|
|
364
|
+
* using basic logarithm properties for numerical stability. NOTE: Operates in-place.
|
|
365
|
+
*
|
|
366
|
+
* The motivation behind applying the log function on the (mel) spectrogram is that humans do not hear loudness on a
|
|
367
|
+
* linear scale. Generally to double the perceived volume of a sound we need to put 8 times as much energy into it.
|
|
368
|
+
* This means that large variations in energy may not sound all that different if the sound is loud to begin with.
|
|
369
|
+
* This compression operation makes the (mel) spectrogram features match more closely what humans actually hear.
|
|
370
|
+
*
|
|
371
|
+
* @template {Float32Array|Float64Array} T
|
|
372
|
+
* @param {T} spectrogram The input amplitude (mel) spectrogram.
|
|
373
|
+
* @param {number} [reference=1.0] Sets the input spectrogram value that corresponds to 0 dB.
|
|
374
|
+
* For example, use `np.max(spectrogram)` to set the loudest part to 0 dB. Must be greater than zero.
|
|
375
|
+
* @param {number} [min_value=1e-5] The spectrogram will be clipped to this minimum value before conversion to decibels,
|
|
376
|
+
* to avoid taking `log(0)`. The default of `1e-5` corresponds to a minimum of -100 dB. Must be greater than zero.
|
|
377
|
+
* @param {number} [db_range=null] Sets the maximum dynamic range in decibels. For example, if `db_range = 80`, the
|
|
378
|
+
* difference between the peak value and the smallest value will never be more than 80 dB. Must be greater than zero.
|
|
379
|
+
* @returns {T} The modified spectrogram in decibels.
|
|
380
|
+
*/
|
|
381
|
+
function amplitude_to_db(spectrogram, reference = 1.0, min_value = 1e-5, db_range = null) {
|
|
382
|
+
return _db_conversion_helper(spectrogram, 20.0, reference, min_value, db_range);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Converts a power spectrogram to the decibel scale. This computes `10 * log10(spectrogram / reference)`,
|
|
387
|
+
* using basic logarithm properties for numerical stability. NOTE: Operates in-place.
|
|
388
|
+
*
|
|
389
|
+
* The motivation behind applying the log function on the (mel) spectrogram is that humans do not hear loudness on a
|
|
390
|
+
* linear scale. Generally to double the perceived volume of a sound we need to put 8 times as much energy into it.
|
|
391
|
+
* This means that large variations in energy may not sound all that different if the sound is loud to begin with.
|
|
392
|
+
* This compression operation makes the (mel) spectrogram features match more closely what humans actually hear.
|
|
393
|
+
*
|
|
394
|
+
* Based on the implementation of `librosa.power_to_db`.
|
|
395
|
+
*
|
|
396
|
+
* @template {Float32Array|Float64Array} T
|
|
397
|
+
* @param {T} spectrogram The input power (mel) spectrogram. Note that a power spectrogram has the amplitudes squared!
|
|
398
|
+
* @param {number} [reference=1.0] Sets the input spectrogram value that corresponds to 0 dB.
|
|
399
|
+
* For example, use `np.max(spectrogram)` to set the loudest part to 0 dB. Must be greater than zero.
|
|
400
|
+
* @param {number} [min_value=1e-10] The spectrogram will be clipped to this minimum value before conversion to decibels,
|
|
401
|
+
* to avoid taking `log(0)`. The default of `1e-10` corresponds to a minimum of -100 dB. Must be greater than zero.
|
|
402
|
+
* @param {number} [db_range=null] Sets the maximum dynamic range in decibels. For example, if `db_range = 80`, the
|
|
403
|
+
* difference between the peak value and the smallest value will never be more than 80 dB. Must be greater than zero.
|
|
404
|
+
* @returns {T} The modified spectrogram in decibels.
|
|
405
|
+
*/
|
|
406
|
+
function power_to_db(spectrogram, reference = 1.0, min_value = 1e-10, db_range = null) {
|
|
407
|
+
return _db_conversion_helper(spectrogram, 10.0, reference, min_value, db_range);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Calculates a spectrogram over one waveform using the Short-Time Fourier Transform.
|
|
412
|
+
*
|
|
413
|
+
* This function can create the following kinds of spectrograms:
|
|
414
|
+
* - amplitude spectrogram (`power = 1.0`)
|
|
415
|
+
* - power spectrogram (`power = 2.0`)
|
|
416
|
+
* - complex-valued spectrogram (`power = None`)
|
|
417
|
+
* - log spectrogram (use `log_mel` argument)
|
|
418
|
+
* - mel spectrogram (provide `mel_filters`)
|
|
419
|
+
* - log-mel spectrogram (provide `mel_filters` and `log_mel`)
|
|
420
|
+
*
|
|
421
|
+
* In this implementation, the window is assumed to be zero-padded to have the same size as the analysis frame.
|
|
422
|
+
* A padded window can be obtained from `window_function()`. The FFT input buffer may be larger than the analysis frame,
|
|
423
|
+
* typically the next power of two.
|
|
424
|
+
*
|
|
425
|
+
* @param {Float32Array|Float64Array} waveform The input waveform of shape `(length,)`. This must be a single real-valued, mono waveform.
|
|
426
|
+
* @param {Float32Array|Float64Array} window The windowing function to apply of shape `(frame_length,)`, including zero-padding if necessary. The actual window length may be
|
|
427
|
+
* shorter than `frame_length`, but we're assuming the array has already been zero-padded.
|
|
428
|
+
* @param {number} frame_length The length of the analysis frames in samples (a.k.a., `fft_length`).
|
|
429
|
+
* @param {number} hop_length The stride between successive analysis frames in samples.
|
|
430
|
+
* @param {Object} options
|
|
431
|
+
* @param {number} [options.fft_length=null] The size of the FFT buffer in samples. This determines how many frequency bins the spectrogram will have.
|
|
432
|
+
* For optimal speed, this should be a power of two. If `null`, uses `frame_length`.
|
|
433
|
+
* @param {number} [options.power=1.0] If 1.0, returns the amplitude spectrogram. If 2.0, returns the power spectrogram. If `null`, returns complex numbers.
|
|
434
|
+
* @param {boolean} [options.center=true] Whether to pad the waveform so that frame `t` is centered around time `t * hop_length`. If `false`, frame
|
|
435
|
+
* `t` will start at time `t * hop_length`.
|
|
436
|
+
* @param {string} [options.pad_mode="reflect"] Padding mode used when `center` is `true`. Possible values are: `"constant"` (pad with zeros),
|
|
437
|
+
* `"edge"` (pad with edge values), `"reflect"` (pads with mirrored values).
|
|
438
|
+
* @param {boolean} [options.onesided=true] If `true`, only computes the positive frequencies and returns a spectrogram containing `fft_length // 2 + 1`
|
|
439
|
+
* frequency bins. If `false`, also computes the negative frequencies and returns `fft_length` frequency bins.
|
|
440
|
+
* @param {number} [options.preemphasis=null] Coefficient for a low-pass filter that applies pre-emphasis before the DFT.
|
|
441
|
+
* @param {number[][]} [options.mel_filters=null] The mel filter bank of shape `(num_freq_bins, num_mel_filters)`.
|
|
442
|
+
* If supplied, applies this filter bank to create a mel spectrogram.
|
|
443
|
+
* @param {number} [options.mel_floor=1e-10] Minimum value of mel frequency banks.
|
|
444
|
+
* @param {string} [options.log_mel=null] How to convert the spectrogram to log scale. Possible options are:
|
|
445
|
+
* `null` (don't convert), `"log"` (take the natural logarithm) `"log10"` (take the base-10 logarithm), `"dB"` (convert to decibels).
|
|
446
|
+
* Can only be used when `power` is not `null`.
|
|
447
|
+
* @param {number} [options.reference=1.0] Sets the input spectrogram value that corresponds to 0 dB. For example, use `max(spectrogram)[0]` to set
|
|
448
|
+
* the loudest part to 0 dB. Must be greater than zero.
|
|
449
|
+
* @param {number} [options.min_value=1e-10] The spectrogram will be clipped to this minimum value before conversion to decibels, to avoid taking `log(0)`.
|
|
450
|
+
* For a power spectrogram, the default of `1e-10` corresponds to a minimum of -100 dB. For an amplitude spectrogram, the value `1e-5` corresponds to -100 dB.
|
|
451
|
+
* Must be greater than zero.
|
|
452
|
+
* @param {number} [options.db_range=null] Sets the maximum dynamic range in decibels. For example, if `db_range = 80`, the difference between the
|
|
453
|
+
* peak value and the smallest value will never be more than 80 dB. Must be greater than zero.
|
|
454
|
+
* @param {boolean} [options.remove_dc_offset=null] Subtract mean from waveform on each frame, applied before pre-emphasis. This should be set to `true` in
|
|
455
|
+
* order to get the same results as `torchaudio.compliance.kaldi.fbank` when computing mel filters.
|
|
456
|
+
* @param {number} [options.max_num_frames=null] If provided, limits the number of frames to compute to this value.
|
|
457
|
+
* @param {number} [options.min_num_frames=null] If provided, ensures the number of frames to compute is at least this value.
|
|
458
|
+
* @param {boolean} [options.do_pad=true] If `true`, pads the output spectrogram to have `max_num_frames` frames.
|
|
459
|
+
* @param {boolean} [options.transpose=false] If `true`, the returned spectrogram will have shape `(num_frames, num_frequency_bins/num_mel_filters)`. If `false`, the returned spectrogram will have shape `(num_frequency_bins/num_mel_filters, num_frames)`.
|
|
460
|
+
* @returns {Promise<Tensor>} Spectrogram of shape `(num_frequency_bins, length)` (regular spectrogram) or shape `(num_mel_filters, length)` (mel spectrogram).
|
|
461
|
+
*/
|
|
462
|
+
export async function spectrogram(
|
|
463
|
+
waveform,
|
|
464
|
+
window,
|
|
465
|
+
frame_length,
|
|
466
|
+
hop_length,
|
|
467
|
+
{
|
|
468
|
+
fft_length = null,
|
|
469
|
+
power = 1.0,
|
|
470
|
+
center = true,
|
|
471
|
+
pad_mode = "reflect",
|
|
472
|
+
onesided = true,
|
|
473
|
+
preemphasis = null,
|
|
474
|
+
mel_filters = null,
|
|
475
|
+
mel_floor = 1e-10,
|
|
476
|
+
log_mel = null,
|
|
477
|
+
reference = 1.0,
|
|
478
|
+
min_value = 1e-10,
|
|
479
|
+
db_range = null,
|
|
480
|
+
remove_dc_offset = null,
|
|
481
|
+
|
|
482
|
+
// Custom parameters for efficiency reasons
|
|
483
|
+
min_num_frames = null,
|
|
484
|
+
max_num_frames = null,
|
|
485
|
+
do_pad = true,
|
|
486
|
+
transpose = false,
|
|
487
|
+
} = {}
|
|
488
|
+
) {
|
|
489
|
+
const window_length = window.length;
|
|
490
|
+
if (fft_length === null) {
|
|
491
|
+
fft_length = frame_length;
|
|
492
|
+
}
|
|
493
|
+
if (frame_length > fft_length) {
|
|
494
|
+
throw Error(`frame_length (${frame_length}) may not be larger than fft_length (${fft_length})`)
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
if (window_length !== frame_length) {
|
|
498
|
+
throw new Error(`Length of the window (${window_length}) must equal frame_length (${frame_length})`);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
if (hop_length <= 0) {
|
|
502
|
+
throw new Error("hop_length must be greater than zero");
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
if (power === null && mel_filters !== null) {
|
|
506
|
+
throw new Error(
|
|
507
|
+
"You have provided `mel_filters` but `power` is `None`. Mel spectrogram computation is not yet supported for complex-valued spectrogram. " +
|
|
508
|
+
"Specify `power` to fix this issue."
|
|
509
|
+
);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
if (center) {
|
|
513
|
+
if (pad_mode !== 'reflect') {
|
|
514
|
+
throw new Error(`pad_mode="${pad_mode}" not implemented yet.`)
|
|
515
|
+
}
|
|
516
|
+
const half_window = Math.floor((fft_length - 1) / 2) + 1;
|
|
517
|
+
waveform = padReflect(waveform, half_window, half_window);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// split waveform into frames of frame_length size
|
|
521
|
+
let num_frames = Math.floor(1 + Math.floor((waveform.length - frame_length) / hop_length))
|
|
522
|
+
if (min_num_frames !== null && num_frames < min_num_frames) {
|
|
523
|
+
num_frames = min_num_frames
|
|
524
|
+
}
|
|
525
|
+
const num_frequency_bins = onesided ? Math.floor(fft_length / 2) + 1 : fft_length
|
|
526
|
+
|
|
527
|
+
let d1 = num_frames;
|
|
528
|
+
let d1Max = num_frames;
|
|
529
|
+
|
|
530
|
+
// If maximum number of frames is provided, we must either pad or truncate
|
|
531
|
+
if (max_num_frames !== null) {
|
|
532
|
+
if (max_num_frames > num_frames) { // input is too short, so we pad
|
|
533
|
+
if (do_pad) {
|
|
534
|
+
d1Max = max_num_frames;
|
|
535
|
+
}
|
|
536
|
+
} else { // input is too long, so we truncate
|
|
537
|
+
d1Max = d1 = max_num_frames;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
// Preallocate arrays to store output.
|
|
542
|
+
const fft = new FFT(fft_length);
|
|
543
|
+
const inputBuffer = new Float64Array(fft_length);
|
|
544
|
+
const outputBuffer = new Float64Array(fft.outputBufferSize);
|
|
545
|
+
const transposedMagnitudeData = new Float32Array(num_frequency_bins * d1Max);
|
|
546
|
+
|
|
547
|
+
for (let i = 0; i < d1; ++i) {
|
|
548
|
+
// Populate buffer with waveform data
|
|
549
|
+
const offset = i * hop_length;
|
|
550
|
+
const buffer_size = Math.min(waveform.length - offset, frame_length);
|
|
551
|
+
if (buffer_size !== frame_length) {
|
|
552
|
+
// The full buffer is not needed, so we need to reset it (avoid overflow from previous iterations)
|
|
553
|
+
// NOTE: We don't need to reset the buffer if it's full since we overwrite the first
|
|
554
|
+
// `frame_length` values and the rest (`fft_length - frame_length`) remains zero.
|
|
555
|
+
inputBuffer.fill(0, 0, frame_length);
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
for (let j = 0; j < buffer_size; ++j) {
|
|
559
|
+
inputBuffer[j] = waveform[offset + j];
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
if (remove_dc_offset) {
|
|
563
|
+
let sum = 0;
|
|
564
|
+
for (let j = 0; j < buffer_size; ++j) {
|
|
565
|
+
sum += inputBuffer[j];
|
|
566
|
+
}
|
|
567
|
+
const mean = sum / buffer_size;
|
|
568
|
+
for (let j = 0; j < buffer_size; ++j) {
|
|
569
|
+
inputBuffer[j] -= mean;
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
if (preemphasis !== null) {
|
|
574
|
+
// Done in reverse to avoid copies and distructive modification
|
|
575
|
+
for (let j = buffer_size - 1; j >= 1; --j) {
|
|
576
|
+
inputBuffer[j] -= preemphasis * inputBuffer[j - 1];
|
|
577
|
+
}
|
|
578
|
+
inputBuffer[0] *= 1 - preemphasis;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
// Apply window function
|
|
582
|
+
for (let j = 0; j < window.length; ++j) {
|
|
583
|
+
inputBuffer[j] *= window[j];
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
fft.realTransform(outputBuffer, inputBuffer);
|
|
587
|
+
|
|
588
|
+
// compute magnitudes
|
|
589
|
+
for (let j = 0; j < num_frequency_bins; ++j) {
|
|
590
|
+
const j2 = j << 1;
|
|
591
|
+
|
|
592
|
+
// NOTE: We transpose the data here to avoid doing it later
|
|
593
|
+
transposedMagnitudeData[j * d1Max + i] = outputBuffer[j2] ** 2 + outputBuffer[j2 + 1] ** 2;
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
if (power !== null && power !== 2) {
|
|
598
|
+
// slight optimization to not sqrt
|
|
599
|
+
const pow = 2 / power; // we use 2 since we already squared
|
|
600
|
+
for (let i = 0; i < transposedMagnitudeData.length; ++i) {
|
|
601
|
+
transposedMagnitudeData[i] **= pow;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
// TODO: What if `mel_filters` is null?
|
|
606
|
+
const num_mel_filters = mel_filters.length;
|
|
607
|
+
|
|
608
|
+
// Perform matrix muliplication:
|
|
609
|
+
// mel_spec = mel_filters @ magnitudes.T
|
|
610
|
+
// - mel_filters.shape=(80, 201)
|
|
611
|
+
// - magnitudes.shape=(3000, 201) => magnitudes.T.shape=(201, 3000)
|
|
612
|
+
// - mel_spec.shape=(80, 3000)
|
|
613
|
+
let mel_spec = await matmul(
|
|
614
|
+
// TODO: Make `mel_filters` a Tensor during initialization
|
|
615
|
+
new Tensor('float32', mel_filters.flat(), [num_mel_filters, num_frequency_bins]),
|
|
616
|
+
new Tensor('float32', transposedMagnitudeData, [num_frequency_bins, d1Max]),
|
|
617
|
+
);
|
|
618
|
+
if (transpose) {
|
|
619
|
+
mel_spec = mel_spec.transpose(1, 0);
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
const mel_spec_data = /** @type {Float32Array} */(mel_spec.data);
|
|
623
|
+
for (let i = 0; i < mel_spec_data.length; ++i) {
|
|
624
|
+
mel_spec_data[i] = Math.max(mel_floor, mel_spec_data[i]);
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
if (power !== null && log_mel !== null) {
|
|
628
|
+
const o = Math.min(mel_spec_data.length, d1 * num_mel_filters);
|
|
629
|
+
// NOTE: operates in-place
|
|
630
|
+
switch (log_mel) {
|
|
631
|
+
case 'log':
|
|
632
|
+
for (let i = 0; i < o; ++i) {
|
|
633
|
+
mel_spec_data[i] = Math.log(mel_spec_data[i]);
|
|
634
|
+
}
|
|
635
|
+
break;
|
|
636
|
+
case 'log10':
|
|
637
|
+
for (let i = 0; i < o; ++i) {
|
|
638
|
+
mel_spec_data[i] = Math.log10(mel_spec_data[i]);
|
|
639
|
+
}
|
|
640
|
+
break;
|
|
641
|
+
case 'dB':
|
|
642
|
+
if (power === 1.0) {
|
|
643
|
+
amplitude_to_db(mel_spec_data, reference, min_value, db_range);
|
|
644
|
+
} else if (power === 2.0) {
|
|
645
|
+
power_to_db(mel_spec_data, reference, min_value, db_range);
|
|
646
|
+
} else {
|
|
647
|
+
throw new Error(`Cannot use log_mel option '${log_mel}' with power ${power}`)
|
|
648
|
+
}
|
|
649
|
+
break;
|
|
650
|
+
default:
|
|
651
|
+
throw new Error(`log_mel must be one of null, 'log', 'log10' or 'dB'. Got '${log_mel}'`);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
return mel_spec;
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
/**
|
|
659
|
+
* Returns an array containing the specified window.
|
|
660
|
+
* @param {number} window_length The length of the window in samples.
|
|
661
|
+
* @param {string} name The name of the window function.
|
|
662
|
+
* @param {Object} options Additional options.
|
|
663
|
+
* @param {boolean} [options.periodic=true] Whether the window is periodic or symmetric.
|
|
664
|
+
* @param {number} [options.frame_length=null] The length of the analysis frames in samples.
|
|
665
|
+
* Provide a value for `frame_length` if the window is smaller than the frame length, so that it will be zero-padded.
|
|
666
|
+
* @param {boolean} [options.center=true] Whether to center the window inside the FFT buffer. Only used when `frame_length` is provided.
|
|
667
|
+
* @returns {Float64Array} The window of shape `(window_length,)` or `(frame_length,)`.
|
|
668
|
+
*/
|
|
669
|
+
export function window_function(window_length, name, {
|
|
670
|
+
periodic = true,
|
|
671
|
+
frame_length = null,
|
|
672
|
+
center = true,
|
|
673
|
+
} = {}) {
|
|
674
|
+
const length = periodic ? window_length + 1 : window_length;
|
|
675
|
+
let window;
|
|
676
|
+
switch (name) {
|
|
677
|
+
case 'boxcar':
|
|
678
|
+
window = new Float64Array(length).fill(1.0);
|
|
679
|
+
break;
|
|
680
|
+
case 'hann':
|
|
681
|
+
case 'hann_window':
|
|
682
|
+
window = hanning(length);
|
|
683
|
+
break;
|
|
684
|
+
case 'hamming':
|
|
685
|
+
window = hamming(length);
|
|
686
|
+
break;
|
|
687
|
+
case 'povey':
|
|
688
|
+
window = hanning(length).map(x => Math.pow(x, 0.85));
|
|
689
|
+
break;
|
|
690
|
+
default:
|
|
691
|
+
throw new Error(`Unknown window type ${name}.`);
|
|
692
|
+
}
|
|
693
|
+
if (periodic) {
|
|
694
|
+
window = window.subarray(0, window_length);
|
|
695
|
+
}
|
|
696
|
+
if (frame_length === null) {
|
|
697
|
+
return window;
|
|
698
|
+
}
|
|
699
|
+
if (window_length > frame_length) {
|
|
700
|
+
throw new Error(`Length of the window (${window_length}) may not be larger than frame_length (${frame_length})`);
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
return window;
|
|
704
|
+
}
|