Fix audio clock drift and adaptive jitter buffering
This commit is contained in:
@@ -139,7 +139,7 @@ internal sealed class MacAudioBackend : IAudioDeviceBackend
|
||||
|
||||
private sealed class Playback : IAudioPlayback
|
||||
{
|
||||
private readonly PcmRing pcm = new(32_768);
|
||||
private readonly AdaptivePcmBuffer pcm = new(2);
|
||||
private readonly short[] renderScratch = new short[8_192];
|
||||
private readonly AVAudioEngine engine = new();
|
||||
private readonly AVAudioFormat format;
|
||||
@@ -187,6 +187,7 @@ internal sealed class MacAudioBackend : IAudioDeviceBackend
|
||||
}
|
||||
|
||||
public void Write(ReadOnlySpan<short> stereoPcm) => pcm.TryWrite(stereoPcm);
|
||||
public int BufferMilliseconds { get => pcm.BufferMilliseconds; set => pcm.BufferMilliseconds = value; }
|
||||
|
||||
private unsafe int Render(IntPtr isSilence, IntPtr _, uint frameCount, IntPtr outputData)
|
||||
{
|
||||
@@ -199,8 +200,6 @@ internal sealed class MacAudioBackend : IAudioDeviceBackend
|
||||
int frames = checked((int)frameCount);
|
||||
int requested = checked(frames * 2);
|
||||
if (bufferCount != 2 || requested > renderScratch.Length) return Fail("Core Audio returned an invalid planar stereo playback buffer.");
|
||||
Span<short> discard = stackalloc short[1_920];
|
||||
while (pcm.Count > 11_520) pcm.Read(discard[..Math.Min(discard.Length, pcm.Count - 11_520)]);
|
||||
Span<short> source = renderScratch.AsSpan(0, requested);
|
||||
int read = pcm.Read(source);
|
||||
source[read..].Clear();
|
||||
|
||||
Reference in New Issue
Block a user