Home

Awesome

audio-generator Build Status unstable

Generate audio stream with a function.

Usage

npm install audio-generator

Direct

const generator = require('audio-generator');
const speaker = require('audio-speaker');

// panned sine generator
let generate = generator(time => [
		Math.sin(Math.PI * 2 * time * 439), //channel 1
		Math.sin(Math.PI * 2 * time * 441), //channel 2
	], { duration: 2 });
let write = speaker();

// hook up generator → speaker loop
(function loop () {
	write(generate(), loop);
})();

Pull stream

var generator = require('audio-generator/pull');
var speaker = require('audio-speaker/pull');
var pull = require('pull-stream/pull');

pull(
	generator(Math.random, { duration: 2 }),
	speaker()
);

Node stream

var Generator = require('audio-generator/stream');
var Speaker = require('audio-speaker/stream');

Generator(
	//Generator function, returns sample values -1..1 for channels
	function (time) {
		return [
			Math.sin(Math.PI * 2 * time * 439), //channel 1
			Math.sin(Math.PI * 2 * time * 441), //channel 2
		]
	},

	{
		//Duration of generated stream, in seconds, after which stream will end.
		duration: Infinity,

		//Periodicity of the time.
		period: Infinity
})
.on('error', function (e) {
	//error happened during generation the frame
})
.pipe(Speaker());

Related

audio-through — universal audio processing stream.<br/> audio-speaker — output audio stream to speaker, both in node/browser.<br/> baudio, webaudio — alternative audio generators.