Class: WebAudioL16Stream

WebAudioL16Stream

new WebAudioL16Stream(options)

Transforms Buffers or AudioBuffers into a binary stream of l16 (raw wav) audio, downsampling in the process.

The watson speech-to-text service works on 1600khz and internally downsamples audio received at higher samplerates. WebAudio is usually 48000khz, so downsampling here reduces bandwidth usage by 2/3.

Format event + stream can be combined with https://www.npmjs.com/package/wav to generate a wav file with a proper header

Todo: support multi-channel audio (for use with

Parameters:
Name Type Description
options Object
Source:

Methods

downsample(bufferNewSamples) → {Float32Array}

Downsamples WebAudio to 16 kHz.

Browsers can downsample WebAudio natively with OfflineAudioContext's but it was designed for non-streaming use and requires a new context for each AudioBuffer. Firefox can handle this, but chrome (v47) crashes after a few minutes. So, we'll do it in JS for now.

This really belongs in it's own stream, but there's no way to create new AudioBuffer instances from JS, so its fairly coupled to the wav conversion code.

Parameters:
Name Type Description
bufferNewSamples AudioBuffer

Microphone/MediaElement audio chunk

Source:
Returns:

'audio/l16' chunk

Type
Float32Array

floatTo16BitPCM(input) → {Buffer}

Accepts a Float32Array of audio data and converts it to a Buffer of l16 audio data (raw wav)

Explanation for the math: The raw values captured from the Web Audio API are in 32-bit Floating Point, between -1 and 1 (per the specification). The values for 16-bit PCM range between -32768 and +32767 (16-bit signed integer). Filter & combine samples to reduce frequency, then multiply to by 0x7FFF (32767) to convert. Store in little endian.

Parameters:
Name Type Description
input Float32Array
Source:
Returns:
Type
Buffer

handleFirstAudioBuffer(audioBuffer, encoding, next)

Does some one-time setup to grab sampleRate and emit format, then sets _transform to the actual audio buffer handler and calls it.

Parameters:
Name Type Description
audioBuffer AudioBuffer
encoding String
next function
Source:

transformAudioBuffer(audioBuffer, encoding, next)

Accepts an AudioBuffer (for objectMode), then downsamples to 16000 and converts to a 16-bit pcm

Parameters:
Name Type Description
audioBuffer AudioBuffer
encoding String
next function
Source:

transformBuffer(nodebuffer, encoding, next)

Accepts a Buffer (for binary mode), then downsamples to 16000 and converts to a 16-bit pcm

Parameters:
Name Type Description
nodebuffer Buffer
encoding String
next function
Source: