Skip to content

Conversation

@northwind33
Copy link

@northwind33 northwind33 commented Dec 21, 2025

Closes #171

Description

This PR adds bindings for opus_decode_float and opus_encode_float to support floating-point operations.

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

Copilot AI review requested due to automatic review settings December 21, 2025 17:02
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds support for floating-point Opus encoding and decoding operations by introducing opus_encode_float and opus_decode_float bindings to the Node.js wrapper.

  • Adds encodeFloat and decodeFloat methods to OpusEncoder class
  • Allocates a new float* buffer for float operations
  • Updates documentation with usage examples for float32 operations

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/node-opus.h Adds outFloat member variable and declares EncodeFloat and DecodeFloat methods
src/node-opus.cc Implements float encoding/decoding methods, initializes outFloat buffer in constructor, and registers new methods
README.md Documents the new float32 encoding/decoding API with usage examples

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 72 to +84
if (this->outPcm) delete this->outPcm;
if (this->outFloat) delete this->outFloat;
Copy link

Copilot AI Dec 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using delete on an array allocated with new[] is undefined behavior and can cause memory leaks or crashes. Since outFloat is allocated with new float[channels * MAX_FRAME_SIZE] on line 66, it should be deallocated with delete[] instead of delete. The same issue exists with outPcm on line 76.

Suggested change
if (this->outPcm) delete this->outPcm;
if (this->outFloat) delete this->outFloat;
if (this->outPcm) delete[] this->outPcm;
if (this->outFloat) delete[] this->outFloat;

Copilot uses AI. Check for mistakes.
int frameSize = buf.Length() / 4 / this->channels;

int compressedLength = opus_encode_float(this->encoder, pcm, frameSize, &(this->outOpus[0]), MAX_PACKET_SIZE);

Copy link

Copilot AI Dec 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing error handling for the return value of opus_encode_float. According to the Opus API documentation, opus_encode_float can return negative values on error (same error codes as opus_decode_float). This function should check if compressedLength < 0 and throw an appropriate error, similar to how DecodeFloat checks for negative return values on line 243. Without this check, negative error codes could be passed to Buffer::Copy causing undefined behavior.

Suggested change
if (compressedLength < 0) {
Napi::TypeError::New(env, getDecodeError(compressedLength)).ThrowAsJavaScriptException();
return env.Null();
}

Copilot uses AI. Check for mistakes.
@northwind33 northwind33 changed the title feat(OpusEncoder): add opus_decode/encode_float bindingq feat(OpusEncoder): add opus_decode/encode_float binding Dec 21, 2025
@northwind33
Copy link
Author

I see there are conflicts due to the recent code updates. I'll rebase and fix them shortly.

@northwind33
Copy link
Author

Sorry, but I just realized it's 3:00 AM here. I'll fix it later.

@northwind33
Copy link
Author

Regarding the remaining points raised by copilot:

  • delete vs delete[]: Yes, arrays must be freed with delete[]. However, I realized I simply mirrored the existing logic for outPcm which implies the original code also has this bug as well.
  • Error Handling: This also seems correct. I'll add the proper check for them.

I'm ready to push fixes for them. Before I begin, I have some questions; For the two points mentioned above, should I fix the existing bug in this PR while I'm at it? Or would you prefer I only fix my new code and open a separate issue/PR for the existing one?

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