TooTallNate / node-speaker

Output PCM audio data to the speakers
648 stars 145 forks source link

Two streams parallel #87

Open ChristianFranke opened 7 years ago

ChristianFranke commented 7 years ago

Hi! Is it possible to play two pcm-streams parallel? Like..

var Speaker = require("speaker");
var lame = require("lame");
var fs = require("fs");
var volume = require("pcm-volume");

var readable = fs.createReadStream("coming-out.mp3");

// see node-lame documentation for more information
var decoder1 = new lame.Decoder({
    channels: 2,
    bitDepth: 16,
    sampleRate: 44100,
    bitRate: 128,
    outSampleRate: 22050,
    mode: lame.STEREO
});

var masterSpeaker = new Speaker();

// Create a volume instance
var v1 = new volume();

v1.pipe(masterSpeaker); // pipe volume to speaker
decoder1.pipe(v1); // pipe PCM data to volume
readable.pipe(decoder1); // pipe file input to decoder

setTimeout(function() {
    // Create a volume instance
    // see node-lame documentation for more information
    var decoder2 = new lame.Decoder({
        channels: 2,
        bitDepth: 16,
        sampleRate: 44100,
        bitRate: 128,
        outSampleRate: 22050,
        mode: lame.STEREO
    });

    var v2 = new volume();
    v2.pipe(masterSpeaker); // pipe volume to speaker
    decoder2.pipe(v2); // pipe PCM data to volume
    readable.pipe(decoder2); // pipe file input to decoder
}, 2000);
Flowr-es commented 7 years ago

So it is possible to have multiple speaker streams in parallel. I think it will not work to pipe into one speaker two pcm streams.

It should work like this (untested):

var Speaker = require("speaker");
var lame = require("lame");
var fs = require("fs");
var volume = require("pcm-volume");

var readable = fs.createReadStream("coming-out.mp3");

// see node-lame documentation for more information
var decoder1 = new lame.Decoder({
    channels: 2,
    bitDepth: 16,
    sampleRate: 44100,
    bitRate: 128,
    outSampleRate: 22050,
    mode: lame.STEREO
});

var masterSpeaker = new Speaker();
var masterSpeaker2 = new Speaker();

// Create a volume instance
var v1 = new volume();

v1.pipe(masterSpeaker); // pipe volume to speaker
decoder1.pipe(v1); // pipe PCM data to volume
readable.pipe(decoder1); // pipe file input to decoder

setTimeout(function() {
    // Create a volume instance
    // see node-lame documentation for more information
    var decoder2 = new lame.Decoder({
        channels: 2,
        bitDepth: 16,
        sampleRate: 44100,
        bitRate: 128,
        outSampleRate: 22050,
        mode: lame.STEREO
    });

    var v2 = new volume();
    v2.pipe(masterSpeaker2); // pipe volume to speaker
    decoder2.pipe(v2); // pipe PCM data to volume
    readable.pipe(decoder2); // pipe file input to decoder
}, 2000);
remscli commented 7 years ago

Hi, I'm trying to play two streams simultaneously but when I pipe the second speaker it stops the first one. I tried with your code @MexXxo and I still have this issue.

Did it worked for you @ChristianFranke ?

Flowr-es commented 7 years ago

Hi @remscli , I just found out, that it works with my Raspi when I'm using the Jack Output. If I'm switchting to a USB Soundcard, it is also not possible for me to have two streams in parallel.

It is maybe possible to combine two streams as one, as possible workaround.

ylh888 commented 7 years ago

On a Mac, I have trouble playing two streams at the same time as well. The program does not return.

See example here https://gist.github.com/ylh888/51cda38e2b5cfbaa0fa009ed8ef17d53

The code works well as long as the different 'notes' don't overlap in time. When they do, the code does not return.

Any help to work around, or at least to avoid infinite loop, would be appreciated.