@@ -24,6 +24,7 @@ class SoundEditor extends React.Component {
24
24
constructor ( props ) {
25
25
super ( props ) ;
26
26
bindAll ( this , [
27
+ 'backupDownSampler' ,
27
28
'copy' ,
28
29
'copyCurrentBuffer' ,
29
30
'handleCopyToNew' ,
@@ -43,7 +44,8 @@ class SoundEditor extends React.Component {
43
44
'paste' ,
44
45
'handleKeyPress' ,
45
46
'handleContainerClick' ,
46
- 'setRef'
47
+ 'setRef' ,
48
+ 'resampleBufferToRate'
47
49
] ) ;
48
50
this . state = {
49
51
copyBuffer : null ,
@@ -332,7 +334,14 @@ class SoundEditor extends React.Component {
332
334
if ( window . OfflineAudioContext ) {
333
335
offlineContext = new window . OfflineAudioContext ( 1 , newLength , newRate ) ;
334
336
} 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
+ }
336
345
} else {
337
346
return reject ( 'No offline audio context' ) ;
338
347
}
@@ -351,6 +360,16 @@ class SoundEditor extends React.Component {
351
360
} ;
352
361
} ) ;
353
362
}
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
+ }
354
373
paste ( ) {
355
374
// If there's no selection, paste at the end of the sound
356
375
const { samples} = this . copyCurrentBuffer ( ) ;
0 commit comments