From b699e769728518e58c79937875559a540275a185 Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum <eric.rosenbaum@gmail.com> Date: Mon, 16 Dec 2019 13:49:39 -0500 Subject: [PATCH] Use backup down sampler when 22kHz not supported --- src/containers/sound-editor.jsx | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/containers/sound-editor.jsx b/src/containers/sound-editor.jsx index c9826ab4d..c68c30d05 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(); -- GitLab