Skip to content

Commit

Permalink
Add outLength to full API, tells caller how many samples were written
Browse files Browse the repository at this point in the history
Includes both number of frames and number of samples for convenience and
to avoid confusion.

Real-world example usage:

```typescript
const outLength = {frames: 0};
resampler.full(res.samples, sampleBuffer, outLength);
res.samples = sampleBuffer.slice(0, outLength.samples);
```
  • Loading branch information
tortis committed Aug 16, 2024
1 parent b01693d commit a8fc67c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 21 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;
}
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 a8fc67c

Please sign in to comment.