web-audio-stream
Interface between Web Audio API and streams. Send AudioBuffer/Buffer/ArrayBuffer/FloatArray data to Web Audio API (writable mode) or connect any AudioNode to stream (readable mode). There are three types of connection available: as plain funcitons, as streams or pull-streams.
Usage
const context = const Generator = const Readable Writable = let oscillator = contextoscillatortype = 'sawtooth'oscillatorfrequencyvalue = 440oscillatorstart //pipe oscillator audio data to stream //pipe generator stream to audio destinationGenerator Math
API
const {Read, Write} = require('web-audio-stream')
Get Web-Audio-API reader or writer constructors. They can be required separately:
const createReader = const createWriter =
let write = Write(destNode, options?)
Create function writing to web-audio-API AudioNode with the following signature: write(audioBuffer, (err) => {})
. To end stream properly, call write(null)
.
options
parameter is optional and may provide the following:
mode
− 0 or 1, defines buffer or script mode of feeding data, may affect performance insignificantly.context
− audio context, defaults to audio-context module.samplesPerFrame
andchannels
define audio buffer params.
const Writer = const Generate = const util = let write = let generate = //add stopperlet isStopped = 0 { if err throw err if isStopped return //generate new audio buffer let aBuf = //send audio buffer to audio node }
Writer is smart enough to recognize any type of data placed into it: AudioBuffer, AudioBufferList, ArrayBuffer, FloatArray, Buffer, Array. Make sure only that passed buffer format complies with passed options, ie. samplerPerFrame
, channels
etc.
Note on performance. Internally writer uses audio-buffer-list to manage memory efficiently, providing pretty low latency.
let read = Read(sourceNode, options?)
Create reader from web AudioNode with signature read((err, audioBuffer) => {})
, returning audio frames data. To end reading, pass read(null)
.
const Reader = let oscNode = contextoscNodetype = 'sawtooth'oscNodefrequencyvalue = 440oscNodestart let read = let count = 0
Stream API
const {Readable, Writable} = require('web-audio-stream/stream')
Get readable or writable stream to pipe data from stream to web-audio directly.
//web-audio → streamconst Readable = //stream → web-audioconst Writable =
let writable = Writable(destNode, options?)
Create writer to web audio node, possibly based on options. Pipe any stream to writable, or write data directly to it, basically it implements Writable stream class.
const Writable = const context = //options or single properties are optionallet writable = const Generator = let src = Generator { return Math}src //or simply send data to web-audiolet chunk = 1024for let i = 0; i < 1024; i++ chunki = Mathwritable
let readable = Readable(audioNode, options?)
Readable stream to read data from web-audio-API.
const Readable = let readable = readable
Pull-stream API
Pull-stream interfaces internally use plain reader and writer and can be used in the same fashion.
const {sink, source} = require('web-audio-stream/pull')
const Sink = const Source =
let sink = Sink(destNode, options?)
let source = Source(srcNode, options?)
Related
- audio-speaker — node/browser speaker stream.
- audio-through — universal audio stream class.