Skip to content

audiojs/audio-generator

Repository files navigation

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.
audio-speaker — output audio stream to speaker, both in node/browser.
baudio, webaudio — alternative audio generators.

About

Generate audio with a function

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •