Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/vvvv/VL.Audio
Browse files Browse the repository at this point in the history
  • Loading branch information
joreg committed Nov 22, 2024
2 parents 277142c + 1dd38ea commit 47bcced
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
6 changes: 5 additions & 1 deletion VL.Audio/src/Core/AudioEngineTimer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace VL.Audio
public class AudioEngineTimer
{
protected long FSamplePosition = 0;
protected long FContinousSamplePosition = 0;
public AudioEngineTimer(int sampleRate)
{
FSampleRate = sampleRate;
Expand All @@ -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))
Expand Down Expand Up @@ -75,6 +77,8 @@ public long BufferStart
return FSamplePosition;
}
}

public long ContinousSamplePosition => FContinousSamplePosition;

double FTime = 0;
public double Time
Expand Down
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 47bcced

Please sign in to comment.