Skip to content

Less naive methods for copying audio buffers #68

Open
@MWstudios

Description

@MWstudios

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions