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

Less naive methods for copying audio buffers #68

Open
MWstudios opened this issue Jan 21, 2024 · 0 comments
Open

Less naive methods for copying audio buffers #68

MWstudios opened this issue Jan 21, 2024 · 0 comments

Comments

@MWstudios
Copy link

MWstudios commented Jan 21, 2024

I've noticed that VstPluginAudioProcessor and VstPluginAudioPrecisionProcessor are copying buffer values one by one. Since the pointers are already exposed, I suggest using Buffer.MemoryCopy():

using System;

// ...

unsafe
{
    float* inputBuffer = this.Buffer;
    float* outputBuffer = ((IDirectBufferAccess32)destination).Buffer;
    Buffer.MemoryCopy(inputBuffer, outputBuffer, this.SampleCount * sizeof(float), this.SampleCount * sizeof(float));
}

Or SIMD, like so:

using System.Runtime.Intristics;
using System.Runtime.Intristics.X86;

// ...

unsafe
{
    float* inputBuffer = this.Buffer;
    float* outputBuffer = ((IDirectBufferAccess32)destination).Buffer;

    int i = 0;
    if (Avx.IsSupported)
         for (; i + 8 <= this.SampleCount; i += 8)
              Avx.Store(outputBuffer + i, Avx.LoadVector256(inputBuffer + i));
    if (Sse2.IsSupported)
         for (; i + 4 <= this.SampleCount; i += 4)
              Sse2.Store(outputBuffer + i, Sse2.LoadVector128(inputBuffer + i));
    for (; i < this.SampleCount; i++)
    {
        outputBuffer[i] = inputBuffer[i];
    }
}

And likewise for doubles, just divide the jumps by 2 (though a simple memory copy would be more useful in this case).

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

1 participant