Skip to content

Using the StreamEncoder

Melchor Garau Madrigal edited this page Feb 19, 2017 · 1 revision

The StreamEncoder is just a stream.Transform object that transforms interleaved PCM data into FLAC data. If you didn't already read about stream.Transform, you must do it now before continue reading this guide.

First, you must create the encoder with the options you want. An example of that:

let enc = new flac.StreamEncoder({
    channels: 2,
    bitsPerSample: 24,
    samplerate: 48000,
    totalSamplesEstimate: 1234567890, //Realise that is just an example :)
});

What is interleaved PCM audio?

You can call write() method (from stream.Writable) to write PCM data:

enc.write(pcmData);

Or you can pipe a stream.Readable object to the encoder:

aReadablePcmStream.pipe(enc);

To read the FLAC data, you can use the the method read() from stream.Readable:

let buffer = enc.read();

Also you can use the data event (from stream.Readable) to read the data:

enc.on('data', (buffer) => {
    ...
});

Or, you can pipe the output of the encoder to a stream.Writable object:

enc.pipe(aWritableObject);

Remember to close the StreamEncoder to flush all data an delete the encoder. If you piped the input, the close is usually done automatically.

Clone this wiki locally