Is there a way to use the Web Audio API to sample audio faster than real-time?

前端 未结 2 1160
春和景丽
春和景丽 2020-11-30 03:47

I\'m playing around with the Web Audio API & trying to find a way to import an mp3 (so therefore this is only in Chrome), and generate a waveform of it on a canvas. I c

2条回答
  •  悲&欢浪女
    2020-11-30 03:51

    There is a really amazing 'offline' mode of the Web Audio API that allows you to pre-process an entire file through an audio context and then do something with the result:

    var context = new webkitOfflineAudioContext();
    
    var source = context.createBufferSource();
    source.buffer = buffer;
    source.connect(context.destination);
    source.noteOn(0);
    
    context.oncomplete = function(e) {
      var audioBuffer = e.renderedBuffer;
    };
    
    context.startRendering();
    

    So the setup looks exactly the same as the real-time processing mode, except you set up the oncomplete callback and the call to startRendering(). What you get back in e.redneredBuffer is an AudioBuffer.

提交回复
热议问题