From fa9ba7a6fbda63c14a91eaae29196f7860413195 Mon Sep 17 00:00:00 2001 From: Alex Norman Date: Wed, 25 Oct 2023 08:29:43 -0700 Subject: [PATCH] provide an LoadUnsafeReadOnlyDataRef for loading read only datarefs without making copies re #32 --- src/Helper.cs.in | 20 ++++++++++++++++++++ src/RNBOWrapper.cpp | 13 +++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/Helper.cs.in b/src/Helper.cs.in index c8752aa..431662e 100644 --- a/src/Helper.cs.in +++ b/src/Helper.cs.in @@ -119,6 +119,12 @@ public class ${PLUGIN_NAME_ID}Handle { [DllImport("${PLUGIN_NAME_ID}")] private static extern bool RNBOCopyLoadDataRef(int key, IntPtr id, [MarshalAs(UnmanagedType.LPArray)] System.Single[] data, IntPtr datalen, IntPtr channels, IntPtr samplerate); + [DllImport("${PLUGIN_NAME_ID}")] + private static extern bool RNBOUnsafeLoadDataRef(int key, IntPtr id, [MarshalAs(UnmanagedType.LPArray)] System.Single[] data, IntPtr datalen, IntPtr channels, IntPtr samplerate); + + [DllImport("${PLUGIN_NAME_ID}")] + private static extern bool RNBOUnsafeLoadReadOnlyDataRef(int key, IntPtr id, [MarshalAs(UnmanagedType.LPArray)] System.Single[] data, IntPtr datalen, IntPtr channels, IntPtr samplerate); + [DllImport("${PLUGIN_NAME_ID}")] private static extern bool RNBOReleaseDataRef(int key, IntPtr id); @@ -448,6 +454,20 @@ public class ${PLUGIN_NAME_ID}Handle { return r; } + private static List unsafeDataRefReferences; + public bool LoadUnsafeReadOnlyDataRef(string id, float[] data, int channels, int samplerate) { + //keep a reference to the data so it doesn't get cleaned up + if (unsafeDataRefReferences == null) { + unsafeDataRefReferences = new List(); + } + unsafeDataRefReferences.Add(data); + + IntPtr idPtr = (IntPtr)Marshal.StringToHGlobalAnsi(id); + var r = RNBOUnsafeLoadReadOnlyDataRef(PluginKey, idPtr, data, (IntPtr)data.Length, (IntPtr)channels, (IntPtr)samplerate); + Marshal.FreeHGlobal(idPtr); + return r; + } + public bool ReleaseDataRef(string id) { IntPtr idPtr = (IntPtr)Marshal.StringToHGlobalAnsi(id); var r = RNBOReleaseDataRef(PluginKey, idPtr); diff --git a/src/RNBOWrapper.cpp b/src/RNBOWrapper.cpp index 007b2fb..14036d4 100644 --- a/src/RNBOWrapper.cpp +++ b/src/RNBOWrapper.cpp @@ -841,6 +841,19 @@ extern "C" UNITY_AUDIODSP_EXPORT_API bool AUDIO_CALLING_CONVENTION RNBOCopyLoadD }); } +// here we simply send over the pointer to the data, since RNBO buffers are read/write, you have to be sure that your code doesn't try to write or resize +// TODO can we indicate a real release? +extern "C" UNITY_AUDIODSP_EXPORT_API bool AUDIO_CALLING_CONVENTION RNBOUnsafeLoadReadOnlyDataRef(int32_t key, const char * id, const float * data, size_t datalen, size_t channels, size_t samplerate) +{ + return with_instance(key, [id, data, datalen, channels, samplerate](RNBOUnity::InnerData * inner) { + RNBO::Float32AudioBuffer bufferType(channels, static_cast(samplerate)); + + size_t bytes = sizeof(float) * datalen; + char * ptr = const_cast(reinterpret_cast(data)); + inner->mCore.setExternalData(id, ptr, bytes, bufferType, nullptr); + }); +} + extern "C" UNITY_AUDIODSP_EXPORT_API bool AUDIO_CALLING_CONVENTION RNBOReleaseDataRef(int32_t key, const char * id) { return with_instance(key, [id](RNBOUnity::InnerData * inner) {