Skip to content

Commit

Permalink
Allocates buffers on POC to avoid pinning overhead in audio thread (o…
Browse files Browse the repository at this point in the history
…nly did it for those used by VST)
  • Loading branch information
azeno committed Nov 8, 2024
1 parent cbc19c8 commit 6da003c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
22 changes: 12 additions & 10 deletions VL.Audio/src/Core/AudioSignal.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using NAudio.Wave;

namespace VL.Audio
Expand Down Expand Up @@ -123,15 +123,17 @@ public WaveFormat WaveFormat
/// </summary>
protected int SampleRate;
protected int BufferSize;
protected float[] FReadBuffer = new float[1];
protected float[] FReadBuffer;

// used by tooltip
public void GetInternalReadBuffer(out IReadOnlyList<float> internalReadBuffer, out int lastReadCount)
{
internalReadBuffer = FReadBuffer;
internalReadBuffer = FReadBuffer ?? Array.Empty<float>();
lastReadCount = BufferSize;
}

}

public float[] ReadBuffer => FReadBuffer;

public bool NeedsBufferCopy
{
get;
Expand All @@ -145,9 +147,9 @@ public int Read(float[] buffer, int offset, int count)
if(true || NeedsBufferCopy)
{
//ensure internal buffer size and shrink size if too large
if (FReadBuffer.Length < count || FReadBuffer.Length > (count * 2))
if (FReadBuffer is null || FReadBuffer.Length < count || FReadBuffer.Length > (count * 2))
{
FReadBuffer = new float[count];
FReadBuffer = GC.AllocateArray<float>(count, pinned: true);
}

//first call per frame
Expand All @@ -167,11 +169,11 @@ public int Read(float[] buffer, int offset, int count)

return count;
}

/// <summary>
/// This method should be overwritten in the sub class to do the actual processing work
/// </summary>
/// <param name="buffer">The buffer to fill</param>
/// <param name="buffer">The buffer to fill. It is allocated on the POC and can therefor be used with <see cref="Marshal.UnsafeAddrOfPinnedArrayElement"/></param>
/// <param name="offset">Write offset for the buffer</param>
/// <param name="count">Count of samples need</param>
protected virtual void FillBuffer(float[] buffer, int offset, int count)
Expand Down
9 changes: 5 additions & 4 deletions VL.Audio/src/Core/MultiChannelSignal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;

#endregion usings

Expand Down Expand Up @@ -51,7 +52,7 @@ public MultiChannelSignal()
SetOutputCount(2);
}

protected void SetOutputCount(int newCount)
public void SetOutputCount(int newCount)
{
//recreate output signals?
if(FOutputCount != newCount)
Expand Down Expand Up @@ -84,7 +85,7 @@ protected void ManageBuffers(int count)
FReadBuffers = new float[FOutputCount][];
for (int i = 0; i < FOutputCount; i++)
{
FReadBuffers[i] = new float[count];
FReadBuffers[i] = GC.AllocateArray<float>(count, pinned: true);
(Outputs[i] as SingleSignal).SetBuffer(FReadBuffers[i]);
}
}
Expand All @@ -101,11 +102,11 @@ protected void Read(int offset, int count)

//since the buffers are already assigned to the SingleSignals nothing more to do
}

/// <summary>
/// Does the actual work
/// </summary>
/// <param name="buffers"></param>
/// <param name="buffers">The buffers are allocated on the POC and therefor already pinned.</param>
/// <param name="offset"></param>
/// <param name="count"></param>
protected virtual void FillBuffers(float[][] buffers, int offset, int count)
Expand Down

0 comments on commit 6da003c

Please sign in to comment.