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

Move backupDownSampler into util and add tests

parent e7997087
No related branches found
No related tags found
No related merge requests found
...@@ -9,7 +9,8 @@ import {connect} from 'react-redux'; ...@@ -9,7 +9,8 @@ import {connect} from 'react-redux';
import { import {
computeChunkedRMS, computeChunkedRMS,
encodeAndAddSoundToVM, encodeAndAddSoundToVM,
downsampleIfNeeded downsampleIfNeeded,
backupDownSampler
} from '../lib/audio/audio-util.js'; } from '../lib/audio/audio-util.js';
import AudioEffects from '../lib/audio/audio-effects.js'; import AudioEffects from '../lib/audio/audio-effects.js';
import SoundEditorComponent from '../components/sound-editor/sound-editor.jsx'; import SoundEditorComponent from '../components/sound-editor/sound-editor.jsx';
...@@ -24,7 +25,6 @@ class SoundEditor extends React.Component { ...@@ -24,7 +25,6 @@ class SoundEditor extends React.Component {
constructor (props) { constructor (props) {
super(props); super(props);
bindAll(this, [ bindAll(this, [
'backupDownSampler',
'copy', 'copy',
'copyCurrentBuffer', 'copyCurrentBuffer',
'handleCopyToNew', 'handleCopyToNew',
...@@ -338,7 +338,7 @@ class SoundEditor extends React.Component { ...@@ -338,7 +338,7 @@ class SoundEditor extends React.Component {
offlineContext = new window.webkitOfflineAudioContext(1, newLength, newRate); offlineContext = new window.webkitOfflineAudioContext(1, newLength, newRate);
} catch { } catch {
if (newRate === (buffer.sampleRate / 2)) { if (newRate === (buffer.sampleRate / 2)) {
return resolve(this.backupDownSampler(buffer, newRate)); return resolve(backupDownSampler(buffer, newRate));
} }
return reject('Could not resample'); return reject('Could not resample');
} }
...@@ -360,18 +360,6 @@ class SoundEditor extends React.Component { ...@@ -360,18 +360,6 @@ class SoundEditor extends React.Component {
}; };
}); });
} }
backupDownSampler (buffer, newRate) {
log.warn(`Using backup down sampler for conversion from ${buffer.sampleRate} to ${newRate}`);
const newLength = Math.floor(buffer.samples.length / 2);
const newSamples = new Float32Array(newLength);
for (let i = 0; i < newLength; i++) {
newSamples[i] = buffer.samples[i * 2];
}
return {
samples: newSamples,
sampleRate: newRate
};
}
paste () { paste () {
// If there's no selection, paste at the end of the sound // If there's no selection, paste at the end of the sound
const {samples} = this.copyCurrentBuffer(); const {samples} = this.copyCurrentBuffer();
......
import WavEncoder from 'wav-encoder'; import WavEncoder from 'wav-encoder';
import log from '../log.js';
const SOUND_BYTE_LIMIT = 10 * 1000 * 1000; // 10mb const SOUND_BYTE_LIMIT = 10 * 1000 * 1000; // 10mb
...@@ -75,9 +76,23 @@ const downsampleIfNeeded = (samples, sampleRate, resampler) => { ...@@ -75,9 +76,23 @@ const downsampleIfNeeded = (samples, sampleRate, resampler) => {
return Promise.reject('Sound too large to save, refusing to edit'); return Promise.reject('Sound too large to save, refusing to edit');
}; };
const backupDownSampler = (buffer, newRate) => {
log.warn(`Using backup down sampler for conversion from ${buffer.sampleRate} to ${newRate}`);
const newLength = Math.floor(buffer.samples.length / 2);
const newSamples = new Float32Array(newLength);
for (let i = 0; i < newLength; i++) {
newSamples[i] = buffer.samples[i * 2];
}
return {
samples: newSamples,
sampleRate: newRate
};
};
export { export {
computeRMS, computeRMS,
computeChunkedRMS, computeChunkedRMS,
encodeAndAddSoundToVM, encodeAndAddSoundToVM,
downsampleIfNeeded downsampleIfNeeded,
backupDownSampler
}; };
import {computeRMS, computeChunkedRMS, downsampleIfNeeded} from '../../../src/lib/audio/audio-util'; import {
computeRMS,
computeChunkedRMS,
downsampleIfNeeded,
backupDownSampler
} from '../../../src/lib/audio/audio-util';
describe('computeRMS', () => { describe('computeRMS', () => {
test('returns 0 when given no samples', () => { test('returns 0 when given no samples', () => {
...@@ -75,3 +80,18 @@ describe('downsampleIfNeeded', () => { ...@@ -75,3 +80,18 @@ describe('downsampleIfNeeded', () => {
} }
}); });
}); });
describe('backupDownSampler', () => {
const buffer = {
samples: [1, 0, 1, 0, 1, 0, 1],
sampleRate: 2
};
test('result is half the length', () => {
const {samples} = backupDownSampler(buffer, 1);
expect(samples.length).toEqual(Math.floor(buffer.samples.length / 2));
});
test('result contains only even-index items', () => {
const {samples} = backupDownSampler(buffer, 1);
expect(samples.every(v => v === 1)).toBe(true);
});
});
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