Skip to content
Snippets Groups Projects
Commit 40dc8487 authored by Eric Rosenbaum's avatar Eric Rosenbaum
Browse files

Fix off-by-one error in reverse effect, add a test

parent bbe72e34
No related branches found
No related tags found
No related merge requests found
......@@ -83,7 +83,7 @@ class AudioEffects {
const endSamples = Math.floor(this.trimEndSeconds * buffer.sampleRate);
let counter = 0;
for (let i = 0; i < bufferLength; i++) {
if (i > startSamples && i < endSamples) {
if (i >= startSamples && i < endSamples) {
newBufferData[i] = originalBufferData[endSamples - counter - 1];
counter++;
} else {
......
......@@ -31,6 +31,25 @@ describe('Audio Effects manager', () => {
test.skip('process starts the offline rendering context and returns a promise', () => {
// @todo haven't been able to get web audio test api to actually run render
});
test('reverse effect strictly reverses the samples', () => {
const fakeSound = [1, 2, 3, 4, 5, 6, 7, 8];
const fakeBuffer = audioContext.createBuffer(1, 8, 44100);
const bufferData = fakeBuffer.getChannelData(0);
fakeSound.forEach((sample, index) => {
bufferData[index] = sample;
});
// Reverse the entire sound
const reverseAll = new AudioEffects(fakeBuffer, 'reverse', 0, 1);
expect(Array.from(reverseAll.buffer.getChannelData(0))).toEqual(fakeSound.reverse());
// Reverse part of the sound
const reverseSelection = new AudioEffects(fakeBuffer, 'reverse', 0.25, 0.75);
const selectionReversed = [1, 2, 6, 5, 4, 3, 7, 8];
expect(Array.from(reverseSelection.buffer.getChannelData(0))).toEqual(selectionReversed);
});
});
describe('Effects', () => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment