Skip to content

Commit 914ba49

Browse files
committed
Use vendor prefixed offline audio context for safari
1 parent 748f855 commit 914ba49

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

src/containers/sound-editor.jsx

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -325,25 +325,30 @@ class SoundEditor extends React.Component {
325325
});
326326
}
327327
resampleBufferToRate (buffer, newRate) {
328-
return new Promise(resolve => {
328+
return new Promise((resolve, reject) => {
329+
const sampleRateRatio = newRate / buffer.sampleRate;
330+
const newLength = sampleRateRatio * buffer.samples.length;
331+
let offlineContext;
329332
if (window.OfflineAudioContext) {
330-
const sampleRateRatio = newRate / buffer.sampleRate;
331-
const newLength = sampleRateRatio * buffer.samples.length;
332-
const offlineContext = new window.OfflineAudioContext(1, newLength, newRate);
333-
const source = offlineContext.createBufferSource();
334-
const audioBuffer = offlineContext.createBuffer(1, buffer.samples.length, buffer.sampleRate);
335-
audioBuffer.getChannelData(0).set(buffer.samples);
336-
source.buffer = audioBuffer;
337-
source.connect(offlineContext.destination);
338-
source.start();
339-
offlineContext.startRendering();
340-
offlineContext.oncomplete = ({renderedBuffer}) => {
341-
resolve({
342-
samples: renderedBuffer.getChannelData(0),
343-
sampleRate: newRate
344-
});
345-
};
333+
offlineContext = new window.OfflineAudioContext(1, newLength, newRate);
334+
} else if (window.webkitOfflineAudioContext) {
335+
offlineContext = new window.webkitOfflineAudioContext(1, newLength, newRate);
336+
} else {
337+
return reject('No offline audio context');
346338
}
339+
const source = offlineContext.createBufferSource();
340+
const audioBuffer = offlineContext.createBuffer(1, buffer.samples.length, buffer.sampleRate);
341+
audioBuffer.getChannelData(0).set(buffer.samples);
342+
source.buffer = audioBuffer;
343+
source.connect(offlineContext.destination);
344+
source.start();
345+
offlineContext.startRendering();
346+
offlineContext.oncomplete = ({renderedBuffer}) => {
347+
resolve({
348+
samples: renderedBuffer.getChannelData(0),
349+
sampleRate: newRate
350+
});
351+
};
347352
});
348353
}
349354
paste () {

0 commit comments

Comments
 (0)