RTNeural icon indicating copy to clipboard operation
RTNeural copied to clipboard

Bindings for other languages?

Open mishushakov opened this issue 3 years ago • 6 comments

hey, yesterday, i had an idea of using vst-rs, which is a Rust implementation of VST

in order to generate Rust bindings, i tried using bindgen, but it failed, because the code in RTNeural has some template-related code

i'm wondering whether you'd be interested in making RTNeural more compatible with other languages in the future?

mishushakov avatar Jul 19 '21 17:07 mishushakov

Yes, I would definitely like to support Rust, though from doing a bit of research, it seems like it may be a somewhat daunting task!

For the dynamically implemented layers in RTNeural, the only template parameter is the data type (i.e. float or double), so we could probably work out a way to feed code into bindgen that would work. Unfortunately, for the more optimized statically implemented layers, the template parameters also cover things like the layer sizes, so until bindgen can handle templates, I don't think it would be possible to automatically generate bindings for those classes. Further, the library backends for RTNeural (Eigen and xsimd) don't have Rust bindings yet either (at least to my knowledge), likely due to the same limitations of templates.

Would it be possible to share the output of bindgen that was failing for you? It may be possible use macros to remove some of the problematic code when generating the Rust bindings.

In general though, I think this issue may require some minds with more Rust experience than I have under my belt :).

Thanks, Jatin

jatinchowdhury18 avatar Jul 19 '21 20:07 jatinchowdhury18

thanks for the detailed response

also i found the cxx module, which allows to interprop Rust and C++

would be cool to try out someday

mishushakov avatar Jul 19 '21 21:07 mishushakov

I've put something together rather quickly today to test with some vst in rust I have https://github.com/rcelha/rtneural-rs

I haven't tested it for real-time stuff yet tho

rcelha avatar Jul 28 '23 17:07 rcelha

I've put something together rather quickly today to test with some vst in rust I have https://github.com/rcelha/rtneural-rs

I haven't tested it for real-time stuff yet tho

This looks very promising, thanks for sharing!

I had been concerned with how inter-operation might work given the highly-templated nature of RTNeural, but I see how that's working in your code with the "wrapper.h" file, basically constructing a non-templated wrapper of the template class. Very cool! I imagine it would be a little bit more complicated to support RTNeural::ModelT, since every model architecture would probably need its own wrapper class? But the wrapper classes could be generated programmatically, so it probably wouldn't be too bad.

I'd also be curious how the Rust vs. C++ performance would compare, since the inferencing calls are going through Rust's FFI. Probably won't make much difference for RTNeural::Model, but I imagine it could be more significant for RTNeural::ModelT.

Anyway, this looks really cool! I'll have to spend some time getting more familiar with it this weekend :)

jatinchowdhury18 avatar Jul 28 '23 20:07 jatinchowdhury18

I am glad you like it!

I am not too concerned over performance, I developed a plugin in C++ (JUCE) in which most of the logic was in Rust, then Rust was talking to another Library in C (OnnxRuntime). It was all mostly fine, as there is not much copying and allocating around.

I am gonna try to put an example together with nih-plug to get a few for how that is gonna behave in real world tho.

rcelha avatar Jul 28 '23 21:07 rcelha

@jatinchowdhury18 you can check out the sample project I've put together over a few hours https://github.com/rcelha/rtneural-rs/pull/1

Latency was pretty good across the board, although I didn't profile it. I was able to run a bunch models from AIDA-X with many different sample size configuration (the lowest my sound card goes is 32 samples).

*On a side note, I was trying to find some info on possible overhead between Rust/C interop: https://blog.rust-lang.org/2015/04/24/Rust-Once-Run-Everywhere.html

rcelha avatar Jul 30 '23 13:07 rcelha