Skip to content

Commit

Permalink
feat: add sliceAudio method (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
27medkamal committed Oct 10, 2023
1 parent 62d23f3 commit 1e3af49
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ test/*.bundle.js
!.yarn/releases
!.yarn/sdks
!.yarn/versions

.idea
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ Concatenate two or more audio buffers in the order specified.\
Pad the audio with silence, at the beginning, the end, or any specified points through the audio.\
**Returns:** a single `AudioBuffer` object.

## crunker.sliceAudio(buffer, start, end, fadeIn, fadeOut);

Slice the audio to the specified range, removing any content outside the range. Optionally add a fade-in at the start and a fade-out at the end to avoid audible clicks.

- **buffer:** The audio buffer to be trimmed.
- **start:** The starting second from where the audio should begin.
- **end:** The ending second where the audio should be trimmed.
- **fadeIn:** (Optional) Number of seconds for the fade-in effect at the beginning. Default is `0`.
- **fadeOut:** (Optional) Number of seconds for the fade-out effect at the end. Default is `0`.

**Returns:** a single `AudioBuffer` object.

## crunker.export(buffer, type);

Export an audio buffers with MIME type option.\
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
"author": "Jack Edgson",
"license": "MIT",
"files": [
"LICENSE"
"LICENSE",
"README.md",
"dist",
"dist"
],
"bugs": {
"url": "https://github.com/jaggad/crunker/issues"
Expand Down
38 changes: 38 additions & 0 deletions src/crunker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,44 @@ export default class Crunker {
return updatedBuffer;
}

/**
* Slices an AudioBuffer from the specified start time to the end time, with optional fade in and out.
*
* @param buffer AudioBuffer to slice
* @param start Start time (in seconds)
* @param end End time (in seconds)
* @param fadeIn Fade in duration (in seconds, default is 0)
* @param fadeOut Fade out duration (in seconds, default is 0)
*/
sliceAudio(buffer: AudioBuffer, start: number, end: number, fadeIn: number = 0, fadeOut: number = 0): AudioBuffer {
if (start >= end) throw new Error('Crunker: "start" time should be less than "end" time in sliceAudio method');

const length = Math.round((end - start) * this._sampleRate);
const offset = Math.round(start * this._sampleRate);
const newBuffer = this._context.createBuffer(buffer.numberOfChannels, length, this._sampleRate);

for (let channel = 0; channel < buffer.numberOfChannels; channel++) {
const inputData = buffer.getChannelData(channel);
const outputData = newBuffer.getChannelData(channel);

for (let i = 0; i < length; i++) {
outputData[i] = inputData[offset + i];

// Apply fade in
if (i < fadeIn * this._sampleRate) {
outputData[i] *= i / (fadeIn * this._sampleRate);
}

// Apply fade out
if (i > length - fadeOut * this._sampleRate) {
outputData[i] *= (length - i) / (fadeOut * this._sampleRate);
}
}
}

return newBuffer;
}

/**
* Plays the provided AudioBuffer in an AudioBufferSourceNode.
*/
Expand Down
4 changes: 4 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ describe('Crunker', () => {
expect(audio.concatAudio(buffers).duration.toFixed(2)).to.equal('16.30');
});

it('slices a buffer correctly', () => {
expect(audio.sliceAudio(buffers[0], 0, 1, 0.1, 0.1).duration.toFixed(2)).to.equal('1.00');
});

it('exports an object', () => {
const output = audio.export(buffers[0]);
expect(output).to.not.equal(null);
Expand Down

0 comments on commit 1e3af49

Please sign in to comment.