Giter Site home page Giter Site logo

hackur / node-core-audio Goto Github PK

View Code? Open in Web Editor NEW

This project forked from audionet/node-core-audio

0.0 2.0 0.0 19.52 MB

Bindings for PortAudio giving JavaScript access to sound card samples (mostly unmaintained)

License: MIT License

C++ 35.44% Objective-C 0.43% C 49.68% Python 2.27% JavaScript 12.17%

node-core-audio's Introduction

Node Core Audio

alt tag

A C++ extension for node.js that gives javascript access to audio buffers and basic audio processing functionality

Right now, it's basically a node.js binding for PortAudio.

NOTE: Looking for help maintaining this repository!

Active contributors:

Installation

npm install node-core-audio

Basic Usage

Below is the most basic use of the audio engine. We create a new instance of node-core-audio, and then give it our processing function. The audio engine will call the audio callback whenever it needs an output buffer to send to the sound card.

// Create a new instance of node-core-audio
var coreAudio = require("node-core-audio");

// Create a new audio engine
var engine = coreAudio.createNewAudioEngine();

// Add an audio processing callback
// This function accepts an input buffer coming from the sound card,
// and returns an ourput buffer to be sent to your speakers.
//
// Note: This function must return an output buffer
function processAudio( inputBuffer ) {
	console.log( "%d channels", inputBuffer.length );
	console.log( "Channel 0 has %d samples", inputBuffer[0].length );

	return inputBuffer;
}

engine.addAudioCallback( processAudio );

// Alternatively, you can read/write samples to the sound card manually

var engine = coreAudio.createNewAudioEngine();

// Grab a buffer
var buffer = engine.read();

// Silence the 0th channel
for( var iSample=0; iSample<inputBuffer[0].length; ++iSample )
	buffer[0][iSample] = 0.0;

// Send the buffer back to the sound card
engine.write( buffer );

Important! Processing Thread

When you are writing code inside of your audio callback, you are operating on the processing thread of the application. This high priority environment means you should try to think about performance as much as possible. Allocations and other complex operations are possible, but dangerous.

IF YOU TAKE TOO LONG TO RETURN A BUFFER TO THE SOUND CARD, YOU WILL HAVE AUDIO DROPOUTS

The basic principle is that you should have everything ready to go before you enter the processing function. Buffers, objects, and functions should be created in a constructor or static function outside of the audio callback whenever possible. The examples in this readme are not necessarily good practice as far as performance is concerned.

The callback is only called if all buffers has been processed by the soundcard.

Audio Engine Options

  • sampleRate [default 44100]
    • Sample rate - number of samples per second in the audio stream
  • sampleFormat [default sampleFormatFloat32]
    • Bit depth - Number of bits used to represent sample values
    • formats are sampleFormatFloat32, sampleFormatInt32, sampleFormatInt24, sampleFormatInt16, sampleFormatInt8, sampleFormatUInt8.
  • framesPerBuffer [default 256]
    • Buffer length - Number of samples per buffer
  • interleaved [default false]
    • Interleaved / Deinterleaved - determines whether samples are given to you as a two dimensional array (buffer[channel][sample]) (deinterleaved) or one buffer with samples from alternating channels (interleaved).
  • inputChannels [default 2]
    • Input channels - number of input channels
  • outputChannels [default 2]
    • Output channels - number of output channels
  • inputDevice [default to Pa_GetDefaultInputDevice]
    • Input device - id of the input device
  • outputDevice [default to Pa_GetDefaultOutputDevice]
    • Output device - id of the output device

API

First things first

var coreAudio = require("node-core-audio");

Create and audio processing function

function processAudio( inputBuffer ) {
    // Just print the value of the first sample on the left channel
    console.log( inputBuffer[0][0] );
}

Initialize the audio engine and setup the processing loop

var engine = coreAudio.createNewAudioEngine();

engine.addAudioCallback( processAudio );

General functionality

// Returns whether the audio engine is active
bool engine.isActive();

// Updates the parameters and restarts the engine. All keys from getOptions() are available.
engine.setOptions({
	inputChannels: 2
});

// Returns all parameters
array engine.getOptions();

// Reads buffer of the input of the soundcard and returns as array.
// Note: this is a blocking call, don't take too long!
array engine.read();

// Writes the buffer to the output of the soundcard. Returns false if underflowed.
// notic: blocking i/o
bool engine.write(array input);

// Returns the name of a given device
string engine.getDeviceName( int inputDeviceIndex );

// Returns the total number of audio devices
int engine.getNumDevices();

Known Issues / TODO

  • Add FFTW to C++ extension, so you can get fast FFT's from javascript, and also register for the FFT of incoming audio, rather than the audio itself
  • Add support for streaming audio over sockets

License

MIT - See LICENSE file.

Copyright Mike Vegeto, 2013

node-core-audio's People

Contributors

zectbynmo avatar vsukhomlinov avatar fishrock123 avatar chuatl avatar mrose17 avatar anprogrammer avatar swyphcosmo avatar xseignard avatar tambien avatar

Watchers

Jeremy Sarda avatar James Cloos 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.