【问题标题】:Stream live audio to Node.js server将实时音频流式传输到 Node.js 服务器
【发布时间】:2018-06-27 20:51:09
【问题描述】:

我正在处理一个项目,我需要将音频流发送到 Node.js 服务器。我可以用这个功能捕捉麦克风的声音:

function micCapture(){
    'use strict';

    navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;

    var constraints = {
        audio: true,
        video: false
    };

    var video = document.querySelector('video');

    function successCallback(stream) {
        window.stream = stream; // stream available to console
        if (window.URL) {
            video.src = window.webkitURL.createObjectURL(stream);
        } else {
            video.src = stream;
        }
        //Send audio stream
        //server.send(stream);
    }

    function errorCallback(error) {
        console.log('navigator.getUserMedia error: ', error);
    }

    navigator.getUserMedia(constraints, successCallback, errorCallback);
}

如您所见,我能够捕捉音频并在网站上播放。

现在我想将该音频流发送到 Node.js 服务器,然后将其发送回其他客户端。就像语音聊天一样,但我不想使用 WebRTC,因为我需要服务器中的流。我怎样才能做到这一点?我可以使用 socket.io-stream 来做到这一点吗?在我看到的示例中,他们录制了音频并发送了一个文件,但我需要“实时”音频。

【问题讨论】:

  • 是的,您当然可以使用 WebSockets 将音频从客户端流式传输到服务器...我建议您编写一些代码,然后返回特定的可回答的编程问题...您所说的上述代码不是特定于您的套接字问题
  • 感谢您的建议。我会编写一些代码并更新问题。
  • 你在这方面有什么好的进展吗?
  • 我最终尝试了WebRTC,但我最终放弃了这个项目

标签: javascript node.js audio


【解决方案1】:

我最近使用 socket.io 从浏览器到服务器进行了实时音频上传。我会在这里回答,以防其他人需要。

    var stream;
    var socket = io();
    var bufferSize = 1024 * 16;
    var audioContext = new AudioContext();
    // createScriptProcessor is deprecated. Let me know if anyone find alternative
    var processor = audioContext.createScriptProcessor(bufferSize, 1, 1);
    processor.connect(audioContext.destination);

    navigator.mediaDevices.getUserMedia({ video: false, audio: true }).then(handleMicStream).catch(err => {
      console.log('error from getUserMedia', err);
    });

handleMicStream将在用户接受使用麦克风的权限时运行。

  function handleMicStream(streamObj) {
    // keep the context in a global variable
    stream = streamObj;

    input = audioContext.createMediaStreamSource(stream);

    input.connect(processor);

    processor.onaudioprocess = e => {
      microphoneProcess(e); // receives data from microphone
    };
  }


  function microphoneProcess(e) {
    const left = e.inputBuffer.getChannelData(0); // get only one audio channel
    const left16 = convertFloat32ToInt16(left); // skip if you don't need this
    socket.emit('micBinaryStream', left16); // send to server via web socket
  }

// Converts data to BINARY16
function convertFloat32ToInt16(buffer) {
    let l = buffer.length;
    const buf = new Int16Array(l / 3);

    while (l--) {
      if (l % 3 === 0) {
        buf[l / 3] = buffer[l] * 0xFFFF;
      }
    }
    return buf.buffer;
  }



让你的 socket.io 服务器监听micBinaryStream,你应该会得到数据。我需要将数据作为 google api 的 BINARY16 格式,如果您不需要,可以跳过对 convertFloat32ToInt16() 的函数调用。

重要

当您需要停止收听时,您必须断开处理器并结束流。运行下面的函数closeAll()

function closeAll() {
    const tracks = stream ? stream.getTracks() : null;
    const track = tracks ? tracks[0] : null;

    if (track) {
      track.stop();
    }

    if (processor) {
      if (input) {
        try {
          input.disconnect(processor);
        } catch (error) {
          console.warn('Attempt to disconnect input failed.');
        }
      }
      processor.disconnect(audioContext.destination);
    }

    if (audioContext) {
      audioContext.close().then(() => {
        input = null;
        processor = null;
        audioContext = null;
      });
    }
  }

【讨论】:

  • 您将这段代码用于 Google Speech to Text 服务?
  • 是的,我想我所做的是将麦克风上传到谷歌 API 并获取文本。我的 github 存储库中也有代码。如果需要,我会在此处链接。
  • 感谢您的帮助,上面的代码在 EJS 上的工作就像一个魅力,但在我使用 Angular 时出现了一些问题。如果可能,请添加 repo 链接,它可能会帮助其他用户。直到我要解决 Angular 问题。
【解决方案2】:

这是一个古老的问题,我明白了。我也在做同样的事情(除了我的服务器不运行 node.js 并且是用 C# 编写的)并且偶然发现了这一点。

不知道是否有人仍然感兴趣,但我已经详细说明了一点。当前已弃用的 createScriptProcessor 的替代方案是 AudioWorklet 接口。

发件人:https://webaudio.github.io/web-audio-api/#audioworklet

1.32.1。概念

AudioWorklet 对象允许开发人员提供脚本(例如 JavaScript 或 >WebAssembly 代码)以在渲染线程上处理音频,支持自定义 >AudioNodes。这种处理机制确保脚本>代码与音频图中的其他内置 AudioNode 同步执行。

据我所知,您无法在 Javascript 中实现接口,但您可以扩展派生自它的类。

而我们需要的是:https://developer.mozilla.org/en-US/docs/Web/API/AudioWorkletProcessor

所以我确实编写了一个处理器,它只用输入值镜像输出并显示它们。

class CustomAudioProcessor extends AudioWorkletProcessor {
    process (inputs, outputs, parameters) {
        const input = inputs[0];
        const output = output[0];
        for (let channel = 0; channel < input.length; ++channel) {   
            for (let i = 0; i < input[channel].length; ++i) {
            // Just copying all the data from input to output
            output[channel][i] = input[channel][i];
            // The next one will make the app crash but yeah, the values are there
            // console.log(output[channel][i]);
            }
        }
    }
}

然后必须将处理器放入音频管道中,在麦克风之后和扬声器之前。

function record() {

constraints = { audio: true };
navigator.mediaDevices.getUserMedia(constraints)
.then(function(stream) {
   audioCtx = new AudioContext();
    var source = audioCtx.createMediaStreamSource(stream);
    audioCtx.audioWorklet.addModule("custom-audio-processor.js").then(() => {
        customAudioProcessor = new AudioWorkletNode(audioCtx, "custom-audio-processor");
        source.connect(customAudioProcessor);
        customAudioProcessor.connect(audioCtx.destination);
    }) 

    audioCtx.destination.play();

有效!祝你好运! :)

【讨论】:

    猜你喜欢
    • 2013-02-27
    • 2012-01-24
    • 2011-04-26
    • 2013-05-11
    • 2010-09-27
    • 1970-01-01
    • 2014-05-29
    • 2021-01-28
    • 2014-01-19
    相关资源
    最近更新 更多