Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
x37v committed Nov 30, 2023
2 parents db01d1b + fa9ba7a commit 15e63a4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Helper.cs.in
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -448,6 +454,20 @@ public class ${PLUGIN_NAME_ID}Handle {
return r;
}

private static List<float[]> 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<float[]>();
}
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);
Expand Down
13 changes: 13 additions & 0 deletions src/RNBOWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>(samplerate));

size_t bytes = sizeof(float) * datalen;
char * ptr = const_cast<char *>(reinterpret_cast<const char *>(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) {
Expand Down

0 comments on commit 15e63a4

Please sign in to comment.