diff --git a/src/lib/audio/audio-effects.js b/src/lib/audio/audio-effects.js index b62f55788e8bab0b6ef0abb48e14759c51387183..8dd551f3801cc4fc04832a46220f8f30a91ccd45 100644 --- a/src/lib/audio/audio-effects.js +++ b/src/lib/audio/audio-effects.js @@ -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 { diff --git a/test/unit/util/audio-effects.test.js b/test/unit/util/audio-effects.test.js index ac34f286fad209cd975428bd92d60bc040d67547..f3d52152a65cd53f1d9056bfcdeb005bf42fa9c1 100644 --- a/test/unit/util/audio-effects.test.js +++ b/test/unit/util/audio-effects.test.js @@ -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', () => {