简体   繁体   中英

Stream PCM audio with node.js

I want to generate a specific sound pattern on a nodejs server and then stream the audio to an http client using express.js.

With this implementation of the WebAudio API https://github.com/mohayonao/web-audio-engine/ I was able to play some generated audio directly on the server with the speaker module as in the example ( https://github.com/mohayonao/web-audio-engine#example )

const Speaker = require("speaker");
const AudioContext = require("web-audio-engine").StreamAudioContext;

function audio(){
  const context = new AudioContext();

  const osc = context.createOscillator();
  osc.type = "sine";
  osc.frequency.setValueAtTime(220, 0);
  osc.frequency.setValueAtTime(440, 1);
  osc.start(0);
  osc.stop(2);
  osc.connect(context.destination);
  osc.onended = () => {
    console.log("finished")
    delete context
  };

  context.pipe(new Speaker());       
  context.resume();
}

audio()

Now I tried to pipe the audio stream to an http response of express:

const express = require("express")
const app = express();

app.get('/', function (req, res) {
  res.set('Content-Type', 'audio/wav');
  audio((context)=>{
    context.pipe(res)
  })
});

app.listen(3000)


const Speaker = require("speaker");
const AudioContext = require("web-audio-engine").StreamAudioContext;

function audio(callback){
  const context = new AudioContext();

  const osc = context.createOscillator();
  osc.type = "sine";
  osc.frequency.setValueAtTime(220, 0);
  osc.frequency.setValueAtTime(440, 1);
  osc.start(0);
  osc.stop(2);
  osc.connect(context.destination);
  osc.onended = () => {
    console.log("finished")
    delete context
  };

  callback(context);
  context.resume();
}

When I open localhost:3000 in the browser now there is an audio player, so the content-type was recognized, but it's not playing anything, just showing the loading animation. After 2 seconds, when the audio stream is finished, it displays a 'broken-file'-icon.
How can you stream the generated PCM audio the right way (in the wav format?)

Have you tried using lame to decode before sending it to the speaker ? Example:

const decoder = require("lame").Decoder;
const Speaker = require("speaker");

context.pipe(decoder()).pipe(new Speaker());

Link to Lame

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM