@@ -325,25 +325,30 @@ class SoundEditor extends React.Component {
325
325
} ) ;
326
326
}
327
327
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 ;
329
332
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' ) ;
346
338
}
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
+ } ;
347
352
} ) ;
348
353
}
349
354
paste ( ) {
0 commit comments