lanczos-resampler 0.1.0 → 0.2.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/README.md CHANGED
@@ -116,11 +116,11 @@ assert.equal(input.length, numProcessed);
116
116
  #### Resampling the whole audio track
117
117
 
118
118
  ```javascript
119
- import { WholeResampler, outputLength } as lanczos from 'lanczos-resampler';
119
+ import { WholeResampler, numOutputFrames } as lanczos from 'lanczos-resampler';
120
120
 
121
121
  const input = new Float32Array(1024);
122
122
  input.fill(0.1);
123
- const outputLen = outputLength(1024, 44100, 48000);
123
+ const outputLen = numOutputFrames(1024, 44100, 48000);
124
124
  const output = new Float32Array(outputLen);
125
125
  const resampler = new WholeResampler();
126
126
  const numProcessed = resampler.resampleInto(input, output);
@@ -131,9 +131,9 @@ console.log(output)
131
131
 
132
132
  ## Documentation
133
133
 
134
- Rust: <https://docs.rs/lanczos-resampler>
134
+ Rust: <https://docs.rs/lanczos-resampler/latest/lanczos_resampler/>
135
135
 
136
- JS: <https://igankevich.github.com/lanczos-resampler>
136
+ JS: <https://igankevich.github.io/lanczos-resampler>
137
137
 
138
138
 
139
139
  ## No-std support
@@ -7,7 +7,7 @@ export class ChunkedInterleavedResampler {
7
7
  /**
8
8
  * Get maximum output chunk length given the input chunk length.
9
9
  *
10
- * Returns the same value as {@link outputLength} plus one.
10
+ * Returns the same value as {@link numOutputFrames} plus one.
11
11
  * This additional sample is used to compensate for unevenly divisible sample rates.
12
12
  *
13
13
  * You should consider updating buffer size every time you change output sample rate via
@@ -34,7 +34,8 @@ export class ChunkedInterleavedResampler {
34
34
  * Resamples input signal chunk from the source to the target sample rate and appends the
35
35
  * resulting signal to the output.
36
36
  *
37
- * Returns the number of processed input samples. The output is clamped to _[-1; 1]_.
37
+ * Returns the number of processed input samples and the number of produced output samples.
38
+ * The output is clamped to _[-1; 1]_.
38
39
  *
39
40
  * For each {@link ChunkedInterleavedResampler.inputSampleRate} input samples this method produces exactly
40
41
  * {@link ChunkedInterleavedResampler.outputSampleRate} output samples even if it is called multiple times with a smaller
@@ -52,7 +53,7 @@ export class ChunkedInterleavedResampler {
52
53
  * isn't an interpolation function, but a filter. To minimize such discrepancies chunk size should
53
54
  * be much larger than _2⋅A + 1_.
54
55
  */
55
- resample(chunk: Float32Array, output: Float32Array): number;
56
+ resample(chunk: Float32Array, output: Float32Array): ResampleOutcome;
56
57
  /**
57
58
  * Get the number of channels.
58
59
  */
@@ -76,7 +77,7 @@ export class ChunkedResampler {
76
77
  /**
77
78
  * Get maximum output chunk length given the input chunk length.
78
79
  *
79
- * Returns the same value as {@link outputLength} plus one.
80
+ * Returns the same value as {@link numOutputFrames} plus one.
80
81
  * This additional sample is used to compensate for unevenly divisible sample rates.
81
82
  *
82
83
  * You should consider updating buffer size every time you change output sample rate via
@@ -101,7 +102,8 @@ export class ChunkedResampler {
101
102
  * Resamples input signal chunk from the source to the target sample rate and appends the
102
103
  * resulting signal to the output.
103
104
  *
104
- * Returns the number of processed input samples. The output is clamped to _[-1; 1]_.
105
+ * Returns the number of processed input samples and the number of produced output samples.
106
+ * The output is clamped to _[-1; 1]_.
105
107
  *
106
108
  * For each {@link ChunkedResampler.inputSampleRate} input samples this method produces exactly
107
109
  * {@link ChunkedResampler.outputSampleRate} output samples even if it is called multiple times with a smaller
@@ -119,7 +121,7 @@ export class ChunkedResampler {
119
121
  * isn't an interpolation function, but a filter. To minimize such discrepancies chunk size should
120
122
  * be much larger than _2⋅A + 1_.
121
123
  */
122
- resample(chunk: Float32Array, output: Float32Array): number;
124
+ resample(chunk: Float32Array, output: Float32Array): ResampleOutcome;
123
125
  /**
124
126
  * Get input sample rate in Hz.
125
127
  */
@@ -133,6 +135,20 @@ export class ChunkedResampler {
133
135
  outputSampleRate: number;
134
136
  }
135
137
 
138
+ export class ResampleOutcome {
139
+ private constructor();
140
+ free(): void;
141
+ [Symbol.dispose](): void;
142
+ /**
143
+ * How many samples were read from the input.
144
+ */
145
+ numRead: number;
146
+ /**
147
+ * How many samples were wrtten to the output.
148
+ */
149
+ numWritten: number;
150
+ }
151
+
136
152
  export class WholeResampler {
137
153
  free(): void;
138
154
  [Symbol.dispose](): void;
@@ -149,7 +165,7 @@ export class WholeResampler {
149
165
  * #### Panics
150
166
  *
151
167
  * Panics when the output isn't large enough to hold all the resampled points.
152
- * Use {@link outputLength} to ensure that the buffer size is sufficient.
168
+ * Use {@link numOutputFrames} to ensure that the buffer size is sufficient.
153
169
  */
154
170
  resampleInto(input: Float32Array, output: Float32Array): number;
155
171
  /**
@@ -162,7 +178,7 @@ export class WholeResampler {
162
178
  * #### Panics
163
179
  *
164
180
  * - Panics when the output isn't large enough to hold all the resampled points.
165
- * Use {@link outputLength} to ensure that the buffer size is sufficient.
181
+ * Use {@link numOutputFrames} to ensure that the buffer size is sufficient.
166
182
  * - Panics when either the input or the output length isn't evenly divisible by the number of
167
183
  * channels.
168
184
  * @param input - input frames
@@ -91,6 +91,10 @@ const ChunkedResamplerFinalization = (typeof FinalizationRegistry === 'undefined
91
91
  ? { register: () => {}, unregister: () => {} }
92
92
  : new FinalizationRegistry(ptr => wasm.__wbg_chunkedresampler_free(ptr >>> 0, 1));
93
93
 
94
+ const ResampleOutcomeFinalization = (typeof FinalizationRegistry === 'undefined')
95
+ ? { register: () => {}, unregister: () => {} }
96
+ : new FinalizationRegistry(ptr => wasm.__wbg_resampleoutcome_free(ptr >>> 0, 1));
97
+
94
98
  const WholeResamplerFinalization = (typeof FinalizationRegistry === 'undefined')
95
99
  ? { register: () => {}, unregister: () => {} }
96
100
  : new FinalizationRegistry(ptr => wasm.__wbg_wholeresampler_free(ptr >>> 0, 1));
@@ -150,7 +154,7 @@ export class ChunkedInterleavedResampler {
150
154
  /**
151
155
  * Get maximum output chunk length given the input chunk length.
152
156
  *
153
- * Returns the same value as {@link outputLength} plus one.
157
+ * Returns the same value as {@link numOutputFrames} plus one.
154
158
  * This additional sample is used to compensate for unevenly divisible sample rates.
155
159
  *
156
160
  * You should consider updating buffer size every time you change output sample rate via
@@ -195,7 +199,8 @@ export class ChunkedInterleavedResampler {
195
199
  * Resamples input signal chunk from the source to the target sample rate and appends the
196
200
  * resulting signal to the output.
197
201
  *
198
- * Returns the number of processed input samples. The output is clamped to _[-1; 1]_.
202
+ * Returns the number of processed input samples and the number of produced output samples.
203
+ * The output is clamped to _[-1; 1]_.
199
204
  *
200
205
  * For each {@link ChunkedInterleavedResampler.inputSampleRate} input samples this method produces exactly
201
206
  * {@link ChunkedInterleavedResampler.outputSampleRate} output samples even if it is called multiple times with a smaller
@@ -214,13 +219,13 @@ export class ChunkedInterleavedResampler {
214
219
  * be much larger than _2⋅A + 1_.
215
220
  * @param {Float32Array} chunk
216
221
  * @param {Float32Array} output
217
- * @returns {number}
222
+ * @returns {ResampleOutcome}
218
223
  */
219
224
  resample(chunk, output) {
220
225
  const ptr0 = passArrayF32ToWasm0(chunk, wasm.__wbindgen_export);
221
226
  const len0 = WASM_VECTOR_LEN;
222
227
  const ret = wasm.chunkedinterleavedresampler_resample(this.__wbg_ptr, ptr0, len0, addHeapObject(output));
223
- return ret >>> 0;
228
+ return ResampleOutcome.__wrap(ret);
224
229
  }
225
230
  }
226
231
  if (Symbol.dispose) ChunkedInterleavedResampler.prototype[Symbol.dispose] = ChunkedInterleavedResampler.prototype.free;
@@ -272,7 +277,7 @@ export class ChunkedResampler {
272
277
  /**
273
278
  * Get maximum output chunk length given the input chunk length.
274
279
  *
275
- * Returns the same value as {@link outputLength} plus one.
280
+ * Returns the same value as {@link numOutputFrames} plus one.
276
281
  * This additional sample is used to compensate for unevenly divisible sample rates.
277
282
  *
278
283
  * You should consider updating buffer size every time you change output sample rate via
@@ -315,7 +320,8 @@ export class ChunkedResampler {
315
320
  * Resamples input signal chunk from the source to the target sample rate and appends the
316
321
  * resulting signal to the output.
317
322
  *
318
- * Returns the number of processed input samples. The output is clamped to _[-1; 1]_.
323
+ * Returns the number of processed input samples and the number of produced output samples.
324
+ * The output is clamped to _[-1; 1]_.
319
325
  *
320
326
  * For each {@link ChunkedResampler.inputSampleRate} input samples this method produces exactly
321
327
  * {@link ChunkedResampler.outputSampleRate} output samples even if it is called multiple times with a smaller
@@ -334,17 +340,71 @@ export class ChunkedResampler {
334
340
  * be much larger than _2⋅A + 1_.
335
341
  * @param {Float32Array} chunk
336
342
  * @param {Float32Array} output
337
- * @returns {number}
343
+ * @returns {ResampleOutcome}
338
344
  */
339
345
  resample(chunk, output) {
340
346
  const ptr0 = passArrayF32ToWasm0(chunk, wasm.__wbindgen_export);
341
347
  const len0 = WASM_VECTOR_LEN;
342
348
  const ret = wasm.chunkedresampler_resample(this.__wbg_ptr, ptr0, len0, addHeapObject(output));
343
- return ret >>> 0;
349
+ return ResampleOutcome.__wrap(ret);
344
350
  }
345
351
  }
346
352
  if (Symbol.dispose) ChunkedResampler.prototype[Symbol.dispose] = ChunkedResampler.prototype.free;
347
353
 
354
+ /**
355
+ * Resampling outcome.
356
+ */
357
+ export class ResampleOutcome {
358
+ static __wrap(ptr) {
359
+ ptr = ptr >>> 0;
360
+ const obj = Object.create(ResampleOutcome.prototype);
361
+ obj.__wbg_ptr = ptr;
362
+ ResampleOutcomeFinalization.register(obj, obj.__wbg_ptr, obj);
363
+ return obj;
364
+ }
365
+ __destroy_into_raw() {
366
+ const ptr = this.__wbg_ptr;
367
+ this.__wbg_ptr = 0;
368
+ ResampleOutcomeFinalization.unregister(this);
369
+ return ptr;
370
+ }
371
+ free() {
372
+ const ptr = this.__destroy_into_raw();
373
+ wasm.__wbg_resampleoutcome_free(ptr, 0);
374
+ }
375
+ /**
376
+ * How many samples were read from the input.
377
+ * @returns {number}
378
+ */
379
+ get numRead() {
380
+ const ret = wasm.__wbg_get_resampleoutcome_numRead(this.__wbg_ptr);
381
+ return ret >>> 0;
382
+ }
383
+ /**
384
+ * How many samples were read from the input.
385
+ * @param {number} arg0
386
+ */
387
+ set numRead(arg0) {
388
+ wasm.__wbg_set_resampleoutcome_numRead(this.__wbg_ptr, arg0);
389
+ }
390
+ /**
391
+ * How many samples were wrtten to the output.
392
+ * @returns {number}
393
+ */
394
+ get numWritten() {
395
+ const ret = wasm.__wbg_get_resampleoutcome_numWritten(this.__wbg_ptr);
396
+ return ret >>> 0;
397
+ }
398
+ /**
399
+ * How many samples were wrtten to the output.
400
+ * @param {number} arg0
401
+ */
402
+ set numWritten(arg0) {
403
+ wasm.__wbg_set_resampleoutcome_numWritten(this.__wbg_ptr, arg0);
404
+ }
405
+ }
406
+ if (Symbol.dispose) ResampleOutcome.prototype[Symbol.dispose] = ResampleOutcome.prototype.free;
407
+
348
408
  /**
349
409
  * A resampler that processes audio input as a whole.
350
410
  *
@@ -382,7 +442,7 @@ export class WholeResampler {
382
442
  * #### Panics
383
443
  *
384
444
  * Panics when the output isn't large enough to hold all the resampled points.
385
- * Use {@link outputLength} to ensure that the buffer size is sufficient.
445
+ * Use {@link numOutputFrames} to ensure that the buffer size is sufficient.
386
446
  * @param {Float32Array} input
387
447
  * @param {Float32Array} output
388
448
  * @returns {number}
@@ -407,7 +467,7 @@ export class WholeResampler {
407
467
  * #### Panics
408
468
  *
409
469
  * - Panics when the output isn't large enough to hold all the resampled points.
410
- * Use {@link outputLength} to ensure that the buffer size is sufficient.
470
+ * Use {@link numOutputFrames} to ensure that the buffer size is sufficient.
411
471
  * - Panics when either the input or the output length isn't evenly divisible by the number of
412
472
  * channels.
413
473
  * @param {Float32Array} input - input frames
Binary file
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "lanczos-resampler",
3
3
  "type": "module",
4
4
  "description": "Audio resampler for Rust/JS that uses Lanczos filter.",
5
- "version": "0.1.0",
5
+ "version": "0.2.0",
6
6
  "license": "MIT",
7
7
  "repository": {
8
8
  "type": "git",
@@ -15,7 +15,7 @@
15
15
  "lanczos_resampler.d.ts"
16
16
  ],
17
17
  "main": "lanczos_resampler.js",
18
- "homepage": "https://github.com/igankevich/lanczos-resampler",
18
+ "homepage": "https://igankevich.github.io/lanczos-resampler/",
19
19
  "types": "lanczos_resampler.d.ts",
20
20
  "sideEffects": [
21
21
  "./lanczos_resampler.js",