From 914ba4910b111929f404a82aea5eb59022aaeb11 Mon Sep 17 00:00:00 2001 From: Paul Kaplan <pkaplan@media.mit.edu> Date: Thu, 12 Dec 2019 15:57:25 -0500 Subject: [PATCH] Use vendor prefixed offline audio context for safari --- src/containers/sound-editor.jsx | 39 +++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/src/containers/sound-editor.jsx b/src/containers/sound-editor.jsx index 0ce73b2f6..c9826ab4d 100644 --- a/src/containers/sound-editor.jsx +++ b/src/containers/sound-editor.jsx @@ -325,25 +325,30 @@ class SoundEditor extends React.Component { }); } resampleBufferToRate (buffer, newRate) { - return new Promise(resolve => { + return new Promise((resolve, reject) => { + const sampleRateRatio = newRate / buffer.sampleRate; + const newLength = sampleRateRatio * buffer.samples.length; + let offlineContext; if (window.OfflineAudioContext) { - const sampleRateRatio = newRate / buffer.sampleRate; - const newLength = sampleRateRatio * buffer.samples.length; - const offlineContext = new window.OfflineAudioContext(1, newLength, newRate); - const source = offlineContext.createBufferSource(); - const audioBuffer = offlineContext.createBuffer(1, buffer.samples.length, buffer.sampleRate); - audioBuffer.getChannelData(0).set(buffer.samples); - source.buffer = audioBuffer; - source.connect(offlineContext.destination); - source.start(); - offlineContext.startRendering(); - offlineContext.oncomplete = ({renderedBuffer}) => { - resolve({ - samples: renderedBuffer.getChannelData(0), - sampleRate: newRate - }); - }; + offlineContext = new window.OfflineAudioContext(1, newLength, newRate); + } else if (window.webkitOfflineAudioContext) { + offlineContext = new window.webkitOfflineAudioContext(1, newLength, newRate); + } else { + return reject('No offline audio context'); } + const source = offlineContext.createBufferSource(); + const audioBuffer = offlineContext.createBuffer(1, buffer.samples.length, buffer.sampleRate); + audioBuffer.getChannelData(0).set(buffer.samples); + source.buffer = audioBuffer; + source.connect(offlineContext.destination); + source.start(); + offlineContext.startRendering(); + offlineContext.oncomplete = ({renderedBuffer}) => { + resolve({ + samples: renderedBuffer.getChannelData(0), + sampleRate: newRate + }); + }; }); } paste () { -- GitLab