audio-generator
v2.1.2
Published
Generate audio stream based on function
Downloads
208
Readme
audio-generator
Generate audio stream with a function.
Usage
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.