From 5f52a4be819d17ed9ef5da38c6c99d3843fabae7 Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Sun, 26 Jun 2022 01:09:01 -0700 Subject: [PATCH] [Audio Output] Better handle latency oddities The visualization buffer now holds up to 45 seconds of loop, and the latency measurement code now caps this at 30 seconds, and restarts the output if latency exceeds 30 seconds, such as if a sound output is reset. Signed-off-by: Christopher Snowhill --- Audio/Output/OutputAVFoundation.m | 10 ++++++++-- Audio/Visualization/VisualizationController.m | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Audio/Output/OutputAVFoundation.m b/Audio/Output/OutputAVFoundation.m index f88332f2e..c43ea55e2 100644 --- a/Audio/Output/OutputAVFoundation.m +++ b/Audio/Output/OutputAVFoundation.m @@ -340,6 +340,7 @@ current_device_listener(AudioObjectID inObjectID, UInt32 inNumberAddresses, cons lastCheckpointPts = kCMTimeZero; secondsLatency = 1.0; started = NO; + restarted = NO; [self synchronizerBlock]; } @@ -989,8 +990,13 @@ current_device_listener(AudioObjectID inObjectID, UInt32 inNumberAddresses, cons if(latencySeconds < 0) latencySeconds = 0; latencyVis = latencySeconds - latencyVis; - if(latencyVis < 0) - latencyVis = 0; + if(latencyVis < 0 || latencyVis > 30.0) { + if(latencyVis > 30.0 && !self->restarted) { + self->restarted = YES; + [outputController setShouldReset:YES]; + } + latencyVis = 0.0; + } *latencySecondsOut = latencySeconds; [visController postLatency:latencyVis]; }]; diff --git a/Audio/Visualization/VisualizationController.m b/Audio/Visualization/VisualizationController.m index 36b480941..fd6f8783a 100644 --- a/Audio/Visualization/VisualizationController.m +++ b/Audio/Visualization/VisualizationController.m @@ -40,7 +40,7 @@ static VisualizationController *_sharedController = nil; @synchronized(self) { if(self->sampleRate != sampleRate) { self->sampleRate = sampleRate; - int visAudioSize = (int)(sampleRate * 30.0); + int visAudioSize = (int)(sampleRate * 45.0); void *visAudio = realloc(self->visAudio, visAudioSize * sizeof(float)); if(visAudio && visAudioSize) { if(visAudioSize > self->visAudioSize) { @@ -71,7 +71,7 @@ static VisualizationController *_sharedController = nil; - (void)postLatency:(double)latency { self->latency = latency; - assert(latency < 30.0); + assert(latency < 45.0); } - (double)readSampleRate {