Skip to content

Commit bb80fa4

Browse files
committed
Move backupDownSampler into util and add tests
1 parent e799708 commit bb80fa4

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

src/containers/sound-editor.jsx

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {connect} from 'react-redux';
99
import {
1010
computeChunkedRMS,
1111
encodeAndAddSoundToVM,
12-
downsampleIfNeeded
12+
downsampleIfNeeded,
13+
backupDownSampler
1314
} from '../lib/audio/audio-util.js';
1415
import AudioEffects from '../lib/audio/audio-effects.js';
1516
import SoundEditorComponent from '../components/sound-editor/sound-editor.jsx';
@@ -24,7 +25,6 @@ class SoundEditor extends React.Component {
2425
constructor (props) {
2526
super(props);
2627
bindAll(this, [
27-
'backupDownSampler',
2828
'copy',
2929
'copyCurrentBuffer',
3030
'handleCopyToNew',
@@ -338,7 +338,7 @@ class SoundEditor extends React.Component {
338338
offlineContext = new window.webkitOfflineAudioContext(1, newLength, newRate);
339339
} catch {
340340
if (newRate === (buffer.sampleRate / 2)) {
341-
return resolve(this.backupDownSampler(buffer, newRate));
341+
return resolve(backupDownSampler(buffer, newRate));
342342
}
343343
return reject('Could not resample');
344344
}
@@ -360,18 +360,6 @@ class SoundEditor extends React.Component {
360360
};
361361
});
362362
}
363-
backupDownSampler (buffer, newRate) {
364-
log.warn(`Using backup down sampler for conversion from ${buffer.sampleRate} to ${newRate}`);
365-
const newLength = Math.floor(buffer.samples.length / 2);
366-
const newSamples = new Float32Array(newLength);
367-
for (let i = 0; i < newLength; i++) {
368-
newSamples[i] = buffer.samples[i * 2];
369-
}
370-
return {
371-
samples: newSamples,
372-
sampleRate: newRate
373-
};
374-
}
375363
paste () {
376364
// If there's no selection, paste at the end of the sound
377365
const {samples} = this.copyCurrentBuffer();

src/lib/audio/audio-util.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import WavEncoder from 'wav-encoder';
2+
import log from '../log.js';
23

34
const SOUND_BYTE_LIMIT = 10 * 1000 * 1000; // 10mb
45

@@ -75,9 +76,23 @@ const downsampleIfNeeded = (samples, sampleRate, resampler) => {
7576
return Promise.reject('Sound too large to save, refusing to edit');
7677
};
7778

79+
const backupDownSampler = (buffer, newRate) => {
80+
log.warn(`Using backup down sampler for conversion from ${buffer.sampleRate} to ${newRate}`);
81+
const newLength = Math.floor(buffer.samples.length / 2);
82+
const newSamples = new Float32Array(newLength);
83+
for (let i = 0; i < newLength; i++) {
84+
newSamples[i] = buffer.samples[i * 2];
85+
}
86+
return {
87+
samples: newSamples,
88+
sampleRate: newRate
89+
};
90+
};
91+
7892
export {
7993
computeRMS,
8094
computeChunkedRMS,
8195
encodeAndAddSoundToVM,
82-
downsampleIfNeeded
96+
downsampleIfNeeded,
97+
backupDownSampler
8398
};

test/unit/util/audio-util.test.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import {computeRMS, computeChunkedRMS, downsampleIfNeeded} from '../../../src/lib/audio/audio-util';
1+
import {
2+
computeRMS,
3+
computeChunkedRMS,
4+
downsampleIfNeeded,
5+
backupDownSampler
6+
} from '../../../src/lib/audio/audio-util';
27

38
describe('computeRMS', () => {
49
test('returns 0 when given no samples', () => {
@@ -75,3 +80,18 @@ describe('downsampleIfNeeded', () => {
7580
}
7681
});
7782
});
83+
84+
describe('backupDownSampler', () => {
85+
const buffer = {
86+
samples: [1, 0, 1, 0, 1, 0, 1],
87+
sampleRate: 2
88+
};
89+
test('result is half the length', () => {
90+
const {samples} = backupDownSampler(buffer, 1);
91+
expect(samples.length).toEqual(Math.floor(buffer.samples.length / 2));
92+
});
93+
test('result contains only even-index items', () => {
94+
const {samples} = backupDownSampler(buffer, 1);
95+
expect(samples.every(v => v === 1)).toBe(true);
96+
});
97+
});

0 commit comments

Comments
 (0)