Skip to content

Commit b699e76

Browse files
committed
Use backup down sampler when 22kHz not supported
1 parent 914ba49 commit b699e76

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/containers/sound-editor.jsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class SoundEditor extends React.Component {
2424
constructor (props) {
2525
super(props);
2626
bindAll(this, [
27+
'backupDownSampler',
2728
'copy',
2829
'copyCurrentBuffer',
2930
'handleCopyToNew',
@@ -43,7 +44,8 @@ class SoundEditor extends React.Component {
4344
'paste',
4445
'handleKeyPress',
4546
'handleContainerClick',
46-
'setRef'
47+
'setRef',
48+
'resampleBufferToRate'
4749
]);
4850
this.state = {
4951
copyBuffer: null,
@@ -332,7 +334,14 @@ class SoundEditor extends React.Component {
332334
if (window.OfflineAudioContext) {
333335
offlineContext = new window.OfflineAudioContext(1, newLength, newRate);
334336
} else if (window.webkitOfflineAudioContext) {
335-
offlineContext = new window.webkitOfflineAudioContext(1, newLength, newRate);
337+
try {
338+
offlineContext = new window.webkitOfflineAudioContext(1, newLength, newRate);
339+
} catch {
340+
if (newRate === (buffer.sampleRate / 2)) {
341+
return resolve(this.backupDownSampler(buffer, newRate));
342+
}
343+
return reject('Could not resample');
344+
}
336345
} else {
337346
return reject('No offline audio context');
338347
}
@@ -351,6 +360,16 @@ class SoundEditor extends React.Component {
351360
};
352361
});
353362
}
363+
backupDownSampler (buffer, newRate) {
364+
log.warn(`Using backup down sampler for conversion from ${buffer.sampleRate} to ${newRate}`);
365+
const newSamples = buffer.samples.filter((element, index) =>
366+
index % 2 === 0
367+
);
368+
return {
369+
samples: newSamples,
370+
sampleRate: newRate
371+
};
372+
}
354373
paste () {
355374
// If there's no selection, paste at the end of the sound
356375
const {samples} = this.copyCurrentBuffer();

0 commit comments

Comments
 (0)