Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change ScriptProcessorNode::onaudioprocess API - use owned buffer value #497

Merged
merged 4 commits into from
Apr 20, 2024

Conversation

orottier
Copy link
Owner

This is easier for the node bindings. We hook into the Drop call of the event to ship the output buffer to the render thread.

TODO: I found a gnarly bug where the ScriptProcessorRenderer would be cleared from the audio graph erroneously. AudioContextRegistration should not be allowed to implement Clone because it has a very meaningful Drop implementation. That's why I now resort to an Arc hack to prevent the Drop method to run.

@b-ma could you check if this solves your issues? I will look into the clone/drop bug of AudioContextRegistration in the meantime

This is easier for the node bindings. We hook into the Drop call of the
event to ship the output buffer to the render thread.

TODO: I found a gnarly bug where the ScriptProcessorRenderer would be
cleared from the audio graph erroneously. AudioContextRegistration
should not be allowed to implement Clone because it has a very
meaningful Drop implementation. That's why I now resort to an `Arc` hack
to prevent the Drop method to run.
@b-ma
Copy link
Collaborator

b-ma commented Apr 18, 2024

Unfortunately, not completely...

I'm now able to propagate the event to JS thread safe fnuction, but now the problem is propagated to the AudioBuffer behaviour:

  • I need to clone the AudioBuffer which is owned by the rust event to create the JS AudioBuffer
  • but then when I write to the output buffer channel data, the inner values are logically cloned...

@b-ma
Copy link
Collaborator

b-ma commented Apr 18, 2024

Ok, I managed to have something work! cf. https://github.com/ircam-ismm/node-web-audio-api/pull/114/files#diff-f2a3e90afd7d940e52b634347f9a1e12ee21402dafa586f92c960ad13fa052fcR150

Used the std::mem::replace trick to steal the I/O buffers from the event and give them back once the JS function is executed.

The only drawback is that I had to add an alternative implementation of ThreadSafeFunction to the source code which allows to execute rust code before and after the execution of the JS callback, but it seems a far better design than the one provided by napi-rs so it is ok I think

Thanks for the help!

@b-ma
Copy link
Collaborator

b-ma commented Apr 18, 2024

Just thinking, maybe we could add a AudioBuffer::tumbstone constructor to the public API? This pattern seems really helpful in some situation

Rewrite the AudioProcessingEvent a bit to circumvent this limitation
@orottier orottier marked this pull request as ready for review April 20, 2024 10:11
@orottier orottier merged commit 88c1c04 into main Apr 20, 2024
3 checks passed
@orottier orottier deleted the feature/audio_process_event_owned branch April 20, 2024 10:20
@orottier
Copy link
Owner Author

Just thinking, maybe we could add a AudioBuffer::tumbstone constructor to the public API? This pattern seems really helpful in some situation

We could, but maybe we should mark it [doc(hidden)] because we will probably run into all sorts of panics and runtime errors when you actually use a zero-channel buffer (in the AudioBufferSourceNode for example)

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.

2 participants