Skip to content

Commit

Permalink
Merge pull request #144 from tortis/main
Browse files Browse the repository at this point in the history
Add `outLength` to full API, tells caller how many samples were written
  • Loading branch information
aolsenjazz authored Aug 16, 2024
2 parents 6c4b1c2 + 13c0c80 commit bcb1764
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 22 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,14 @@ simple(dataIn) { ... }
*
* More (and better) info available at: http://www.mega-nerd.com/SRC/api_full.html
*
* @param {Float32Array} dataIn Float32Array containing mono|interleaved audio data where -1 < dataIn[i] < 1
* @param {Float32Array || null} dataOut Optionally, pass a Float32Array to avoid allocating an extra array for every esampling operation
* @return {Float32Array} The resampled data. If dataOut != null, dataOut is returned
* @param {Float32Array} dataIn Float32Array containing mono|interleaved audio data where -1 < dataIn[i] < 1
* @param {Float32Array|null} dataOut Optionally, pass a Float32Array to avoid allocating an extra array for every esampling operation
* @param {Object|null} outLength Amount of data written to dataOut
* @param {number} [outLength.frames] The number of frames in the output
* @return {Float32Array} The resampled data. If dataOut != null, dataOut is returned
*/
full(dataIn, dataOut=null) { ... }
full(dataIn, dataOut=null, outLength = null) { ... }
```

### `destroy`
Expand Down
2 changes: 1 addition & 1 deletion dist/libsamplerate.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/libsamplerate.worklet.js

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions dist/src.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ export declare class SRC {
*
* @param dataIn Float32Array containing mono|interleaved audio data where -1 < dataIn[i] < 1
* @param dataOut Optionally, pass a Float32Array to avoid allocating an extra array for every resampling operation
* @param outLength if resampleFunc === this.module.full, pass an optional object get get the number of frames written to dataOut
* @returns The resampled data. If dataOut != null, dataOut is returned
*/
full(dataIn: Float32Array, dataOut?: Float32Array | null): Float32Array;
full(dataIn: Float32Array, dataOut?: Float32Array | null, outLength?: {
frames: number;
} | null): Float32Array;
/**
* Cleans up WASM SRC resources. Once this is called on an instance, that instance must be
* reinitialized with src.init() before it can be used again.
Expand Down Expand Up @@ -77,7 +80,10 @@ export declare class SRC {
* @param resampleFunc this.module.simple || this.module.full
* @param dataIn Float32Array containing mono|interleaved audio data where -1 < dataIn[i] < 1
* @param dataOut if resampleFunc === this.module.full, pass an optional resuable buffer to avoid extra allocations
* @param outLength if resampleFunc === this.module.full, pass an optional object get get the number of frames written to dataOut
* @returns The resampled audio, if any
*/
_resample(resampleFunc: ModuleType['simple'] | ModuleType['full'], dataIn: Float32Array, dataOut?: Float32Array | null): Float32Array;
_resample(resampleFunc: ModuleType['simple'] | ModuleType['full'], dataIn: Float32Array, dataOut?: Float32Array | null, outLength?: {
frames: number;
} | null): Float32Array;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@alexanderolsen/libsamplerate-js",
"version": "2.1.1",
"version": "2.1.2",
"description": "Resample audio in node or browser using a webassembly port of libsamplerate.",
"keywords": [
"web assembly",
Expand Down
20 changes: 10 additions & 10 deletions src/glue.js

Large diffs are not rendered by default.

14 changes: 11 additions & 3 deletions src/src.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,15 @@ export class SRC {
*
* @param dataIn Float32Array containing mono|interleaved audio data where -1 < dataIn[i] < 1
* @param dataOut Optionally, pass a Float32Array to avoid allocating an extra array for every resampling operation
* @param outLength if resampleFunc === this.module.full, pass an optional object get get the number of frames written to dataOut
* @returns The resampled data. If dataOut != null, dataOut is returned
*/
full(
dataIn: Float32Array,
dataOut: Float32Array | null = null
dataOut: Float32Array | null = null,
outLength: {frames: number} | null = null,
): Float32Array {
return this._resample(this.module.full, dataIn, dataOut);
return this._resample(this.module.full, dataIn, dataOut, outLength);
}

/**
Expand Down Expand Up @@ -206,12 +208,14 @@ export class SRC {
* @param resampleFunc this.module.simple || this.module.full
* @param dataIn Float32Array containing mono|interleaved audio data where -1 < dataIn[i] < 1
* @param dataOut if resampleFunc === this.module.full, pass an optional resuable buffer to avoid extra allocations
* @param outLength if resampleFunc === this.module.full, pass an optional object get get the number of frames written to dataOut
* @returns The resampled audio, if any
*/
_resample(
resampleFunc: ModuleType['simple'] | ModuleType['full'],
dataIn: Float32Array,
dataOut: Float32Array | null = null
dataOut: Float32Array | null = null,
outLength: {frames: number} | null = null,
): Float32Array {
// if we don't actually need to resample, just copy values
if (this.inputSampleRate === this.outputSampleRate) return dataIn;
Expand All @@ -235,6 +239,10 @@ export class SRC {
this.outputSampleRate // ignored by module.full()
);

if (typeof outLength === 'object' && outLength !== null) {
outLength.frames = outputFrames;
}

return copyOrWriteArray(
outputFrames * this.nChannels,
this.targetArray,
Expand Down

0 comments on commit bcb1764

Please sign in to comment.