使用WebAudio API从getChannelData方法获取麦克风PCM数据不起作用

问题描述 投票:0回答:1

我正在尝试使用Web Audio API从麦克风获取原始PCM样本。经过研究,看来我应该得到"a Float32Array containing the PCM data associated with the channel"。该流程对我有用,但是当我尝试将PCM样本放入WAV文件时,我得到的是随机噪声,而不是麦克风在录音时实际上在麦克风中讲话的声音。这是一个使用AudioBuffer.getChannelData()获取PCM数据的代码段,您可以将其直接粘贴到浏览器控制台中:

let recLength = 0;
let recBuffers = [];

let bufferLen = 4096;
let numChannels = 1;
let mimeType = 'audio/wav';

window.AudioContext = window.AudioContext || window.webkitAudioContext;
let audio_context = new AudioContext();

let audio_stream;

navigator.mediaDevices.getUserMedia({audio: true}).then(function (stream)
{
    audio_stream = stream;
});

// wait a sec or two

let source = audio_context.createMediaStreamSource(audio_stream);
let context = source.context;
let sampleRate = context.sampleRate; // you will need to know this number for creating a WAV file.

let node = (context.createScriptProcessor || context.createJavaScriptNode).call(context, bufferLen, numChannels, numChannels);

node.onaudioprocess = function(e)
{
    const inputBuffer = e.inputBuffer.getChannelData(0);

    recBuffers.push(inputBuffer);
    recLength += inputBuffer.length;
};

source.connect(node);
node.connect(context.destination);

// wait 10 seconds or so, while speaking into microphone

node.disconnect();

function mergeBuffers()
{
    let result = new Float32Array(recLength);
    let offset = 0;
    for (let i = 0; i < recBuffers.length; i++) {
        result.set(recBuffers[i], offset);
        offset += recBuffers[i].length;
    }
    return result;
}

let mergedBuffer = mergeBuffers();

let normalBuffer = [];
for (let i = 0; i < mergedBuffer.length; i++)
{
    normalBuffer.push(mergedBuffer[i]);
}

JSON.stringify(normalBuffer);

现在,如果我复制最后一行的字符串输出,并将其传递到生成WAV文件的任何库中,则输出WAV文件只是随机噪声。如果您想自己复制此文件,我正在使用this NodeJS library编写WAV文件:

let arr = [1,2,3]; // replace this array with the string output of normalBuffer from above

samples = [[]];
for (let i = 0; i < arr.length; i++)
{
    samples[0].push(arr[i]);
}

const fs = require("fs");
const WaveFile = require("wavefile");

let wav = new WaveFile();
wav.fromScratch(1, 44100, "32f", samples); // the sampling rate (second parameter) could be different on your machine, but you can print it out in the code snippet above to find out
fs.writeFileSync("test.wav", wav.toBuffer());

我还尝试将样本转换为无符号的16位Ints,但是我仍然遇到同样的问题,并且我尝试将样本乘以某个常数,以防录音量太小,而且无济于事。

javascript web-audio-api sampling microphone getusermedia
1个回答
0
投票

问题(至少在Chrome中是这样,问题是ScriptProcessorNode不断重复使用相同的基础音频缓冲区。这意味着Float32Array数组中的每个recBuffers都指向相同的内存。您可以通过复制数据来避免这种情况。

行...

const inputBuffer = e.inputBuffer.getChannelData(0);

...变成...

const inputBuffer = new Float32Array(bufferLen);

e.inputBuffer.copyFromChannel(inputBuffer, 0);

[请记住,由于Safari还没有copyFromChannel方法,因此无法在Safari中使用。缓冲区将需要手动复制。

如果只想录制wav文件,则可能更容易重用extendable-media-recorder之类的现有库。但是我想您只是创建了一个wav文件来调试问题。

© www.soinside.com 2019 - 2024. All rights reserved.