Skip to content

feat(OpusEncoder): add encodeFloat and decodeFloat - #232

Open
officialasishkumar wants to merge 1 commit into
discordjs:mainfrom
officialasishkumar:feat/opus-encoder-float
Open

officialasishkumar wants to merge 1 commit into
discordjs:mainfrom
officialasishkumar:feat/opus-encoder-float

Conversation

@officialasishkumar

Copy link
Copy Markdown

Description

Adds encodeFloat() and decodeFloat() to OpusEncoder, exposing opus_encode_float and opus_decode_float so audio can be processed as 32-bit floating point PCM instead of 16-bit integers.

  • encodeFloat(buf) takes a Buffer or Float32Array of interleaved samples and returns the Opus packet. The samples are copied into an aligned scratch vector before encoding, because Buffers (e.g. pooled ones) aren't guaranteed to be 4-byte aligned, and inputs longer than the maximum Opus frame are rejected before copying.
  • decodeFloat(buf) returns a Buffer of little-endian float32 samples. The output buffer is allocated lazily on first use, so encoders that only use the 16-bit API don't pay for the extra memory.
  • Input validation and error reporting match encode() / decode().
  • The existing encode() / decode() behaviour is unchanged.

Tests, typings and a README example are included. The tests check that the float output matches the 16-bit output to within one 16-bit step, that Float32Array and unaligned Buffer inputs work, and that the error paths throw.

Fixes #171

  • Code changes have been tested, or there are no code changes

Expose opus_encode_float and opus_decode_float so audio can be handled as
32-bit floating point PCM instead of signed 16-bit integers.

- encodeFloat() accepts a Buffer or Float32Array of interleaved samples.
  The samples are copied into an aligned scratch vector first, since
  Buffers (e.g. pooled ones) aren't guaranteed to be 4-byte aligned, and
  oversized inputs are rejected before copying.
- decodeFloat() decodes into a lazily allocated float buffer, so encoders
  that never use it don't pay for the extra memory, and returns a Buffer
  of little-endian float32 samples.
- Both methods validate their input and report libopus errors the same
  way as encode() and decode().

Adds tests, typings and a README example.

close discordjs#171
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support decoding as float32

1 participant