JavaScript : How to split an audio blob into 1 second chunks and export to wav files using recorder. js?

标签 javascript audio chunks recorder web-mediarecorder

我想录制语音,将录制的语音(或音频 block )自动分割成1秒的 block ,将每个 block 导出到wav文件并发送到后端。这应该在用户说话时异步发生。

我目前使用以下 recorder.js 库来执行上述任务 https://cdn.rawgit.com/mattdiamond/Recorderjs/08e7abd9/dist/recorder.js

我的问题是,随着时间的推移,blob/wave 文件的大小会变得更大。我认为这是因为数据积累并使 block 大小变大。因此,随着时间的推移,我实际上并不是发送连续的 1 秒 block ,而是累积的 block 。

我无法弄清楚这个问题是在我的代码中的哪个位置引起的。这可能发生在 recorder.js 库内。如果有人使用 recorder js 或任何其他 JavaScript 方法来完成类似的任务,请仔细阅读这段代码并让我知道它在哪里出错,我们将不胜感激。

这是我的 JS 代码

var gumStream; // Stream from getUserMedia()
var rec; // Recorder.js object
var input; // MediaStreamAudioSourceNode we'll be recording
var recordingNotStopped; // User pressed record button and keep talking, still not stop button pressed
const trackLengthInMS = 1000; // Length of audio chunk in miliseconds
const maxNumOfSecs = 1000; // Number of mili seconds we support per recording (1 second)


// Shim for AudioContext when it's not available. 
var AudioContext = window.AudioContext || window.webkitAudioContext;
var audioContext //audio context to help us record

var recordButton = document.getElementById("recordButton");
var stopButton = document.getElementById("stopButton");

//Event handlers for above 2 buttons
recordButton.addEventListener("click", startRecording);
stopButton.addEventListener("click", stopRecording);

//Asynchronous function to stop the recoding in each second and export blob to a wav file
const sleep = time => new Promise(resolve => setTimeout(resolve, time));

const asyncFn = async() => {
  for (let i = 0; i < maxNumOfSecs; i++) {
    if (recordingNotStopped) {
      rec.record();
      await sleep(trackLengthInMS);
      rec.stop();

      //stop microphone access
      gumStream.getAudioTracks()[0].stop();

      //Create the wav blob and pass it on to createWaveBlob
      rec.exportWAV(createWaveBlob);
    }
  }
}

function startRecording() {
  console.log("recordButton clicked");
  recordingNotStopped = true;
  var constraints = {
    audio: true,
    video: false
  }

  recordButton.disabled = true;
  stopButton.disabled = false;

  //Using the standard promise based getUserMedia() 
  navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {

    //Create an audio context after getUserMedia is called
    audioContext = new AudioContext();

    // Assign to gumStream for later use 
    gumStream = stream;

    //Use the stream 
    input = audioContext.createMediaStreamSource(stream);

    //Create the Recorder object and configure to record mono sound (1 channel)
    rec = new Recorder(input, {
      numChannels: 1
    });

    //Call the asynchronous function to split and export audio
    asyncFn();
    console.log("Recording started");

  }).catch(function(err) {
    //Enable the record button if getUserMedia() fails
    recordButton.disabled = false;
    stopButton.disabled = true;
  });
}

function stopRecording() {
  console.log("stopButton clicked");
  recordingNotStopped = false;

  //disable the stop button and enable the record button to  allow for new recordings
  stopButton.disabled = true;
  recordButton.disabled = false;

  //Set the recorder to stop the recording
  rec.stop();

  //stop microphone access
  gumStream.getAudioTracks()[0].stop();
}

function createWaveBlob(blob) {
  var url = URL.createObjectURL(blob);

  //Convert the blob to a wav file and call the sendBlob function to send the wav file to the server
  var convertedfile = new File([blob], 'filename.wav');
  sendBlob(convertedfile);
}

最佳答案

Recorder.js 保留其录制的音频的记录缓冲区。当调用 exportWAV 时,记录缓冲区会被编码但不会被清除。您需要在再次调用 record 之前在录音机上调用 clear,以便从记录缓冲区中清除之前的音频 block 。

这就是上面代码中修复的方式。

//Extend the Recorder Class and add clear() method
Recorder.prototype.step = function () {
    this.clear();
};

//After calling the exportWAV(), call the clear() method
rec.exportWAV(createWaveBlob);
rec.step();

关于JavaScript : How to split an audio blob into 1 second chunks and export to wav files using recorder. js?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55224534/

相关文章:

python - 如何在ffmpeg中使用字节而不是文件路径?

c++ - SDL_Mixer 是否可以在自身上播放单个 block ?

php - 音频文件目录列表,需要音频播放器解决方案

java - 为什么我无法播放音频文件

javascript - 使用 JavaScript 按数字或字母字符将字符串拆分为 block

JavaScript 数组分块的特定方法

javascript - Angular 1 与 React

javascript - 我可以将 "new"匿名函数传递给 addEventListener

javascript - 覆盖 console.log();用于生产

javascript - d3 csv 解析器处理进度值