diff --git a/src/containers/sound-editor.jsx b/src/containers/sound-editor.jsx index c9826ab4d4f3db31d47537487209c7247c476547..c68c30d05b0ad4d31949056a521ff7813e9f67ec 100644 --- a/src/containers/sound-editor.jsx +++ b/src/containers/sound-editor.jsx @@ -24,6 +24,7 @@ class SoundEditor extends React.Component { constructor (props) { super(props); bindAll(this, [ + 'backupDownSampler', 'copy', 'copyCurrentBuffer', 'handleCopyToNew', @@ -43,7 +44,8 @@ class SoundEditor extends React.Component { 'paste', 'handleKeyPress', 'handleContainerClick', - 'setRef' + 'setRef', + 'resampleBufferToRate' ]); this.state = { copyBuffer: null, @@ -332,7 +334,14 @@ class SoundEditor extends React.Component { if (window.OfflineAudioContext) { offlineContext = new window.OfflineAudioContext(1, newLength, newRate); } else if (window.webkitOfflineAudioContext) { - offlineContext = new window.webkitOfflineAudioContext(1, newLength, newRate); + try { + offlineContext = new window.webkitOfflineAudioContext(1, newLength, newRate); + } catch { + if (newRate === (buffer.sampleRate / 2)) { + return resolve(this.backupDownSampler(buffer, newRate)); + } + return reject('Could not resample'); + } } else { return reject('No offline audio context'); } @@ -351,6 +360,16 @@ class SoundEditor extends React.Component { }; }); } + backupDownSampler (buffer, newRate) { + log.warn(`Using backup down sampler for conversion from ${buffer.sampleRate} to ${newRate}`); + const newSamples = buffer.samples.filter((element, index) => + index % 2 === 0 + ); + return { + samples: newSamples, + sampleRate: newRate + }; + } paste () { // If there's no selection, paste at the end of the sound const {samples} = this.copyCurrentBuffer();