Giter Site home page Giter Site logo

sink.js's Introduction

sink.js

sink.js is a javascript library dedicated for audio output. Features include buffer fill callbacks, synchronous write, asynchronous write, ring buffers and recording. Currently supported platforms are Firefox 4+ and Chrome with Web Audio API enabled in about:flags. Additional platforms, such as a flash fallback can be added as plugins, but are currently not featured, nor will ever be enabled by default. For a platform to be added as enabled by default, it needs to be reliable in terms of latency and stability. Flash fallbacks โ€“ for example โ€“ cannot offer this kind of reliability and the sound will always be flaky at best.

Basic usage

To create a sink, the following function is available:


var sink = Sink([callback=null], [channelCount=2], [preBufferSize=4096], [sampleRate=44100])

Currently, it is recommended to check that you are actually running at the sample rate that you specified, so when you create a sink, get the samplerate reference always from its .sampleRate property.

The callback is fed with two arguments, the buffer to fill and the channel count of the buffer. For example, to play back stereo noise, you can do the following:

var sink = Sink(function(buffer, channelCount){
	var i;
	for (i=0; i<buffer.length; i++){
		buffer[i] = Math.random() - 0.5;
	}
});

Note that buffer is interleaved and therefore the samples should be written as :

[
    channel1, channel2, ... channelN,	// frame 1
    channel1, channel2, ... channelN,	// frame 2
    ...	                                // ...
]

For example, to play back a 440Hz sine wave in stereo, you can do :

var k, v, n = 0;
var sink = Sink(function(buffer, channelCount){
	for (var j=0; j<buffer.length; j+=2, n++) {
        	v = Math.sin(k*n);
        	buffer[j] = v;
        	buffer[j+1] = v;
    	}
}, 2); // 2 channels ... which is already the default

k = 2*Math.PI*440/sink.sampleRate;

Note also that the length of the buffer can vary, it will however always be less than preBufferSize. If you need control over buffer's length, check-out [proxies][proxy].

To bring the sink to a force stop, you can use the .kill() method. But so far, this doesn't work in Chrome, and the Chrome's AudioContext can't be taken down. This will be fixed as soon as possible. Also, beware of creating multiple sinks to avoid unexpected results.

Buffer writing API

As an alternative to the callback API, you can directly write sound to the buffers with .write(buffer, [delay=undefined]) method. The default writing mode is "async" where the specified buffer will be mixed with existing data. If a delay is not specified, the system will automatically create a delay to try to compensate the latency. The delay is specified as number of samples.

Another mode of writing is "sync". You can set this by changing your sink's .writeMode property to "sync". In the "sync" write mode, the delay is disregarded and instead all written buffers will be appended right after the previous one has been exhausted. To get the current sample offset in the "sync" write mode, you can use the .getSyncWriteOffset() method.

Beware of writing zero-length buffers, as they will induce NaNs to your buffers. This can be a bad thing especially if written in the "sync" mode and combined with some effects processing in a callback.

Proxy

[proxy]:

Proxy is a thin-wrapper around the basic sink, allowing you to control the size of the buffer received by the callback.

To create a proxy, use the following function:


var proxy = Sink.createProxy([bufferSize=4096])

For example, to play back a 440Hz sine wave in stereo, you can do :

var bufferSize = 4096, sink = Sink();
var proxy = sink.createProxy(bufferSize);
proxy.on('audioprocess', function(buffer, channelCount){
	for (var j=0; j<bufferSize; j+=2, n++) {
        	v = Math.sin(k*n);
        	buffer[j] = v;
        	buffer[j+1] = v;
    	}
});

Recording

Recording can be done by creating an instance of the recording, like this:

var recording = sink.record();

// And when you want to stop recording:

recording.stop();

// To join the recording into a single buffer:

var buffer = recording.join();

Ring buffer

Here's an example how to use the ring buffer:

// Enable & create the ring buffer

sink.ringBuffer = new Float32Array(sink.sampleRate * sink.channelCount);

// Get or modify the current offset of the ring buffer.

console.log(sink.ringOffset);

That would create a ring buffer of the length of a single second, and you can manipulate that anyway you want, and use it together with callbacks and / or writing.

Projects using sink.js

  • aurora.js A JS-powered video and audio playback framework.
  • Audiolet A comphrehensive, graph based audio synthesis / composition library for JS.
  • audiolib.js A full-fletched audio synthesis / processing framework.

License

sink.js is released under MIT license.

sink.js's People

Contributors

jussi-kalliokoski avatar sebpiq avatar davidgovea avatar ehsan avatar

Watchers

JT5D 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.