Javascript 通过网络传输麦克风音频输入(原始PCM)

Javascript 通过网络传输麦克风音频输入(原始PCM),javascript,html,networking,audio,html5-audio,Javascript,Html,Networking,Audio,Html5 Audio,仅使用javascript录制原始pcm音频的最佳方式是什么?我将通过一个网络连接进行数据流传输,然后我需要在另一端重新组装数据包。网络部分已经完成,但我似乎不知道如何录制,然后将原始pcm数据包放入播放机,然后让我在扬声器上听到 谢谢你的帮助。没有经过测试,但我会这样做的 请求允许使用麦克风 使用以流作为输入的回调,并将其连接到ScriptProcessor ScriptProcessor为您提供一组示例 下面是一些可能会有所帮助的代码片段 // ask for permission navi

仅使用javascript录制原始pcm音频的最佳方式是什么?我将通过一个网络连接进行数据流传输,然后我需要在另一端重新组装数据包。网络部分已经完成,但我似乎不知道如何录制,然后将原始pcm数据包放入播放机,然后让我在扬声器上听到


谢谢你的帮助。

没有经过测试,但我会这样做的

  • 请求允许使用麦克风
  • 使用以流作为输入的回调,并将其连接到ScriptProcessor
  • ScriptProcessor为您提供一组示例
  • 下面是一些可能会有所帮助的代码片段

    // ask for permission
    navigator.getUserMedia( {audio:true}, successCB, errorCB );
    
    回调示例

    function successCB(stream) {
        const ctx = new AudioContext();
        const mediaStreamSource = ctx.createMediaStreamSource(stream);
    
        const scriptNode = ctx.createScriptProcessor(4096, 1, 1);
        scriptNode.onaudioprocess(audioProcessingEvent => {
            const inputBuffer = audioProcessingEvent.inputBuffer;
            const inputData = inputBuffer.getChannelData(0 /*channel*/);
    
            for (var sample = 0; sample < inputBuffer.length; sample++) {
                const THE_SAMPLE = inputData[sample];
                // save it in an array ?
            }
        });
        
        // connect the mic to the script
        mediaStreamSource.connect(scriptNode);
    
        // if you need to hear yourself (be careful with feedback or use headphones)
        mediaStreamSource.connect( ctx.destination );
    }
    
    函数成功cb(流){
    const ctx=新的AudioContext();
    const mediaStreamSource=ctx.createMediaStreamSource(流);
    const scriptNode=ctx.createScriptProcessor(4096,1,1);
    scriptNode.onaudioprocess(audioProcessingEvent=>{
    const inputBuffer=audioProcessingEvent.inputBuffer;
    const inputData=inputBuffer.getChannelData(0/*channel*/);
    对于(var sample=0;sample

    仅供参考,对于AudioWorkletNode,ScriptProcessor已被弃用,我还没有尝试过。

    这一切都很好,但一旦我将数据传输到另一端,我将如何播放它?所以,当一个新的音频包进来时,我有一个回调,我应该把它放进一个缓冲区或其他什么地方。你能告诉我怎么做吗?相同的代码,但使用audioProcessingEvent.outputBuffer获取指向输出缓冲区的指针,然后outputData[sample]=你的_数据,并将ScriptProcessor连接到ctx.destination