Giter Club home page Giter Club logo

pcm-mixer's Introduction

PCM Mixer

Library that allows you to mix live PCM streams by adding a correct amount of silence in between incoming stream data. Useful for when you need to combine audio streams from multiple people that come in at different times and aren't synced.

Todo

  • When input data is requested, but not enough data is available yet, the input returns only silent output. This is noticeable as periods of silence in between streams.
  • Add support for float PCM streams
  • Verify functionality with different bitdepths and channel count
  • Verify timing

Installation

$ npm install pcm-mixer --save

Usage

const Mixer = require('pcm-mixer').default
const Speaker = require('speaker') // If you want to output to your speaker

let mixer = new Mixer({
    bitDepth: 32
})

let firstInput = mixer.input({ volume: 0.2 })
let secondInput = mixer.input() // Default volume is 1.0

somehowRetreiveStream().pipe(firstInput)
somehowRetreiveAnotherStream().pipe(secondInput)

let speaker = new Speaker({ bitDepth: 32 })
mixer.start().pipe(speaker)

Once a stream that is piped to an input emits an unpipe event, that input is automatically removed from the mixer. Keep in mind that the mixer always has a silent input attached and as such doesn't emit an end event itself.

Also have a look at the test directory for some examples.

Mixer options

The Mixer constructor optionally accepts two parameters: audioOptions and mixingFunction.

audioOptions

Option Description Default
channels The number of channels in the output 2
bitDepth The bit depth of the audio stream 16
samplingRate The sampling rate to be used for timing purposes 44100 (44.1 kHz)
float Determines if the output should be a float PCM stream false
signed Determines if the output should be a signed PCM stream true

These audio options are also used as default values for inputs, although you can override these for each input individually.

Currently it is not supported to have inputs with different options than the mixer, as there is not yet a normalization of the input values (e.g. conversion of 32-bit samples to 16-bit samples).

mixingFunction

A function that takes in an array with entries of type [Buffer, number] for each currently attached input as its first parameter. The number corresponds to the volume of the input.

An example mixing function could look like this:

function mixingFunction (inputs, length) {
    let buffer = Buffer.allocUnsafe(length)
    for (let i = 0; i < length; i++) {
        let value = inputs.reduce((previousValue, input) => {
            return previousValue + (input[0][i] * input[1])
        }, 0)

        buffer[i] = Math.min(Math.max(value, -32768), 32767)
    }

    return buffer
}

pcm-mixer's People

Contributors

padarom avatar artenuvielle avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.