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

shared usage of buffers by multiple instances of rnbo? #32

Closed
chausch opened this issue Oct 23, 2023 · 8 comments
Closed

shared usage of buffers by multiple instances of rnbo? #32

chausch opened this issue Oct 23, 2023 · 8 comments
Assignees

Comments

@chausch
Copy link

chausch commented Oct 23, 2023

hi there,
is it possible to access the same buffers by multiple instances by a rnbo plugin, without the need to load the same data into multiple buffers?
i have many instances running at the same time (though not all activated at the same time) and the current implementation, by which one has to load the sample directly into the plugin quickly amounts to gigabytes of gigabytes of memory usage!
thanks for any advice!

@x37v
Copy link
Contributor

x37v commented Oct 24, 2023

Unfortunately RNBO's buffers are always read/write as far as the plugin knows so to be safe we have to make copies of the data. I figure we could make an "unsafe" interface that would let you set a buffer without doing a copy if you know for sure that your rnbo patch doesn't do any writing/resizing, and this way you might be able to point directly to audio assets and simply have them loaded once?

@chausch
Copy link
Author

chausch commented Oct 24, 2023

that would be awesome!!! i'm working with spatial grains and there will be no recording in the rnbo plugin whatsoever!

x37v added a commit that referenced this issue Oct 25, 2023
@x37v
Copy link
Contributor

x37v commented Oct 25, 2023

@chausch I just created a new develop branch and pushed a change that adds this capability.
I simply added Unsafe in the name of the method instead of marking it unsafe as I couldn't figure out how to get unity to allow me to use that code even though I set up my player to allow unsafe code or whatever it is.

The loading approach is the same as before, but you'll just have to manage having some global float [] that you pass into your LoadUnsafeReadOnlyDataRef methods.

Let me know if/how this works for you?

@chausch
Copy link
Author

chausch commented Oct 25, 2023

thank you so much! do i understand that correctly that i have to load the audio data into this float array at another location, and pass it to the rnbo plugin?

@chausch
Copy link
Author

chausch commented Oct 25, 2023

HA! it seems to work!! 🥳
i will test more and report back! thank you!!!

@x37v
Copy link
Contributor

x37v commented Oct 25, 2023

do i understand that correctly that i have to load the audio data into this float array at another location, and pass it to the rnbo plugin?

Yeah, if you look at the buffer documentation that I linked above you'll see the approach, loading the data into a float array.
If you're only using one MonoBehaviour derived class for your setup, you might be able to use a static variable to hold it?

Something like:

using UnityEngine;
using Cycling74.RNBOTypes;

public class PlayerMovement : MonoBehaviour
{
    const int instanceIndex = 1;
    BufferPlayerHelper helper;
    BufferPlayerHandle plugin;

    public float speed = 6f;
    private static float[] samples;

    [SerializeField] AudioClip buffer;

    void Start() {
        helper = BufferPlayerHelper.FindById(instanceIndex);
        plugin = helper.Plugin;

        if (buffer != null && plugin != null)
        {
            if (samples == null) {
                samples = new float[buffer.samples * buffer.channels];
            }
            buffer.GetData(samples, 0);
            plugin.LoadUnsafeReadOnlyDataRef("playback", samples, buffer.channels, buffer.frequency);
        }
    }
}

seems like you figured it out though!

@x37v
Copy link
Contributor

x37v commented Oct 25, 2023

i will test more and report back! thank you!!!

we still have to make a single copy of the data as it could be MP3, int16 wav, etc etc and we need 32-bit float, but I'm curious to hear about the memory usage you're seeing after this change.

@x37v
Copy link
Contributor

x37v commented Nov 30, 2023

merged to main

@x37v x37v closed this as completed Nov 30, 2023
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

No branches or pull requests

2 participants