diff --git a/VL.Audio/src/Core/AudioEngineTimer.cs b/VL.Audio/src/Core/AudioEngineTimer.cs index ee0b471..facabda 100644 --- a/VL.Audio/src/Core/AudioEngineTimer.cs +++ b/VL.Audio/src/Core/AudioEngineTimer.cs @@ -8,6 +8,7 @@ namespace VL.Audio public class AudioEngineTimer { protected long FSamplePosition = 0; + protected long FContinousSamplePosition = 0; public AudioEngineTimer(int sampleRate) { FSampleRate = sampleRate; @@ -19,7 +20,8 @@ public AudioEngineTimer(int sampleRate) public void Progress(int samplesCount) { FSamplePosition += samplesCount; - + FContinousSamplePosition += samplesCount; + if (Loop && FLoopSampleLength > 0) { while(FSamplePosition >= (FLoopStartSample + FLoopSampleLength)) @@ -75,6 +77,8 @@ public long BufferStart return FSamplePosition; } } + + public long ContinousSamplePosition => FContinousSamplePosition; double FTime = 0; public double Time diff --git a/VL.Audio/src/Core/AudioSignal.cs b/VL.Audio/src/Core/AudioSignal.cs index 6bc5f36..5ccf8e1 100644 --- a/VL.Audio/src/Core/AudioSignal.cs +++ b/VL.Audio/src/Core/AudioSignal.cs @@ -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 @@ -123,15 +123,17 @@ public WaveFormat WaveFormat /// protected int SampleRate; protected int BufferSize; - protected float[] FReadBuffer = new float[1]; + protected float[] FReadBuffer; // used by tooltip public void GetInternalReadBuffer(out IReadOnlyList internalReadBuffer, out int lastReadCount) { - internalReadBuffer = FReadBuffer; + internalReadBuffer = FReadBuffer ?? Array.Empty(); lastReadCount = BufferSize; - } - + } + + public float[] ReadBuffer => FReadBuffer; + public bool NeedsBufferCopy { get; @@ -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(count, pinned: true); } //first call per frame @@ -167,11 +169,11 @@ public int Read(float[] buffer, int offset, int count) return count; } - + /// /// This method should be overwritten in the sub class to do the actual processing work /// - /// The buffer to fill + /// The buffer to fill. It is allocated on the POC and can therefor be used with /// Write offset for the buffer /// Count of samples need protected virtual void FillBuffer(float[] buffer, int offset, int count) diff --git a/VL.Audio/src/Core/MultiChannelSignal.cs b/VL.Audio/src/Core/MultiChannelSignal.cs index a163f4d..af4041f 100644 --- a/VL.Audio/src/Core/MultiChannelSignal.cs +++ b/VL.Audio/src/Core/MultiChannelSignal.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.InteropServices; #endregion usings @@ -51,7 +52,7 @@ public MultiChannelSignal() SetOutputCount(2); } - protected void SetOutputCount(int newCount) + public void SetOutputCount(int newCount) { //recreate output signals? if(FOutputCount != newCount) @@ -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(count, pinned: true); (Outputs[i] as SingleSignal).SetBuffer(FReadBuffers[i]); } } @@ -101,11 +102,11 @@ protected void Read(int offset, int count) //since the buffers are already assigned to the SingleSignals nothing more to do } - + /// /// Does the actual work /// - /// + /// The buffers are allocated on the POC and therefor already pinned. /// /// protected virtual void FillBuffers(float[][] buffers, int offset, int count)