[英]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,通过仅考虑一个通道来进行单通道记录)
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;
}
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之间。
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.