Trim an audio file using javascript (first 3 seconds)

孤街浪徒 提交于 2021-02-07 08:36:01

问题


I have a question that can i trim my audio file that is recorded via javascript. I want to trim the first 3 seconds of it can you help me please? I recorded the audio file using p5.j and merged the recorded file and a real karaoke file with AudioContext() and I have to trim it because of unpleasant sound at starting that makes merging wrong.


回答1:


You will probably need to read the audio into an AudioBuffer using something like AudioContext.decodeAudioData(), plug the AudioBuffer into a AudioBufferSourceNode. Then you can skip the first 3 seconds using the offset parameter of AudioBufferSourceNode.start() and record the resulting output stream.

Example code:

var source = audioCtx.createBufferSource();
var dest = audioCtx.createMediaStreamDestination();
var mediaRecorder = new MediaRecorder(dest.stream);

var request = new XMLHttpRequest();
request.open('GET', 'your.ogg', true);
request.responseType = 'arraybuffer';

request.onload = function() {
  var audioData = request.response;
  audioCtx.decodeAudioData(
    audioData,
    function(buffer) {
      source.buffer = buffer;
      source.connect(dest);
      mediaRecorder.start();
      source.start(audioCtx.currentTime, 3);
      // etc...
    },
    function(e){ 
      console.log("Error with decoding audio data" + e.err);
    }
  );

}

request.send();


来源:https://stackoverflow.com/questions/54303632/trim-an-audio-file-using-javascript-first-3-seconds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!