简体   繁体   English

如何在javascript中使用audioWorklet和AudioWorkletProcessor录制音频?

[英]How to record audio using audioWorklet and AudioWorkletProcessor in javascript?

I have below logic to record (set samplerate = 16000 in AudioContext, mono channel recording by considering only one channel) 我有以下逻辑进行记录(在AudioContext中设置采样率= 16000,通过仅考虑一个通道来进行单通道记录)

  1. I set a parameter shouldRecord from AudioWorklet, and depending on that AudioWorkletProcessor will start putting data into buffer as below 我从AudioWorklet设置了一个参数shouldRecord,然后根据该AudioWorkletProcessor将开始将数据放入缓冲区,如下所示
  process(inputs, outputs, parameters) {
    const isRecordingValues = parameters.isRecording;
    //taking first input
    var input0 = inputs[0];
    var inputChannel = input0[0];
    if (isRecordingValues.length ===1){
      const shouldRecord = isRecordingValues[0] === 1;
      if (!shouldRecord && !this._isBufferEmpty()) {

        this._flush();
        this._recordingStopped();
      }

      if (shouldRecord) {
            this._appendToBuffer(inputChannel);
      }

    }
    return true;
  }

}

_appendToBuffer is as below: _appendToBuffer如下:

  _appendToBuffer(value) {
    if (this._isBufferFull()) {
      this._flush();
    }

    // Here _buffer is of type Float32Array 
    this._buffer.set(value, this._bytesWritten);
    this._bytesWritten += value.length;
  }

  1. In _flush method i am sending the contents of _buffer to AudioWorklet as below: 在_flush方法中,我将_buffer的内容发送到AudioWorklet,如下所示:
    var blob = this._exportWAV(buffer, this._bytesWritten);
    this.port.postMessage({
      eventType: 'data',
      audioBuffer: blob 
    });

Here buffer contains values between -1.0 to 1.0 . 这里的buffer的值在-1.0到1.0之间。

  1. I receive the data in AudioWorklet as ArrayBuffer object and i download it as Wave file. 我在AudioWorklet中以ArrayBuffer对象的形式接收数据,并以Wave文件的形式下载。 Irrespective of the size of the file i can open the file in Windows Media Player without error but it lasts less than a second and playback ends. 无论文件大小如何,我都可以在Windows Media Player中打开文件而不会出现错误,但是持续时间不到一秒钟,并且播放结束。

I believe i am doing something wrong in the process method and the data recorded in buffer is not in correct format. 我相信我在process方法上做错了,缓冲区中记录的数据格式不正确。

What am i doing wrong here? 我在这里做错了什么?

I changed the _flush method as follows: 我将_flush方法更改如下:

_flush() {

  let buffer = this._buffer;
  if (this._bytesWritten < this._bufferSize) {
    buffer = buffer.slice(0, this._bytesWritten);
  }
  this.port.postMessage({
    eventType: 'data',
    audioBuffer: buffer
  });

  this._initBuffer();}

so i am directly sending my buffer to AudioWorklet. 所以我直接将缓冲区发送到AudioWorklet。 when i receive this buffer in AudioWorklet I am sending it as Blob to Flask app as below 当我在AudioWorklet中收到此缓冲区时,我将其作为Blob发送到Flask应用程序,如下所示

const audioData = e.data.audioBuffer.buffer;
socket.emit( 'my event', {
   blob : new Blob([audioData], { type: 'audio/wav' })
});

this gives me plain simple floating point numbers between -1.0 to 1.0 to work with in Flask app. 这使我可以在Flask应用程序中使用-1.0到1.0之间的简单浮点数。 I am then converting these floating points with below function 然后我用下面的函数转换这些浮点数

def convert(raw_floats):
   data = raw_floats
   floats = array.array('f', data)
   samples = [int(sample * 32767)
           for sample in floats]
   raw_ints = struct.pack("<%dh" % len(samples), *samples)
   return raw_ints

I am saving these raw_ints to WAVE file, which is playable in Windows Media Player. 我将这些raw_ints保存到WAVE文件,该文件可以在Windows Media Player中播放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 AudioWorkletProcessor 进行低延迟音频调度? - Using AudioWorkletProcessor for low-latency audio scheduling? 如何使用 Audioworklet 编码振荡器? - How to code an oscillator using Audioworklet? 如何使用javascript录制音频元素的音频 - How to record audio from Audio Element using javascript 如何使用 AudioWorklet 获取麦克风音量 - How to get microphone volume using AudioWorklet 如何使用JAVASCRIPT录制音视频? (反应 JS) - How to record audio and video using JAVASCRIPT ? (REACT JS) Web音频API中的音频工作程序和AudioWorklet - Audio worker and AudioWorklet in web audio API 使用Web Audio Api AudioWorklet捕获比特并从音频流中进行BPM检测 - Using Web Audio Api AudioWorklet to grab the bits and do BPM detection from audiostream 在 JavaScript 中使用带有音频的 getDisplayMedia 录制屏幕 - Record screen using getDisplayMedia with Audio in JavaScript 如何在 JavaScript 中录制麦克风音频并提交给 DialogFlow? - How to record microphone audio in JavaScript and submit to DialogFlow? 任何人都可以建议如何使用html5和javascript在网站上录制麦克风的音频 - Can anyone suggest how to record audio from microphone on a website using html5 and javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM