From 7f80767810547f299d9edbfba8216be8d6d64f31 Mon Sep 17 00:00:00 2001 From: Adeeb Rahman Date: Tue, 24 Nov 2026 09:07:08 -0500 Subject: [PATCH] Refill a bounded native audio playback window Signed-off-by: Adeeb Rahman --- app/Sources/Xenora/main.swift | 44 ++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/app/Sources/Xenora/main.swift b/app/Sources/Xenora/main.swift index b569e2c..72ff97a 100644 --- a/app/Sources/Xenora/main.swift +++ b/app/Sources/Xenora/main.swift @@ -188,6 +188,7 @@ private enum FixturePlaybackError: Error { private struct FixturePlayback { let frames: Int let buffers: Int + let refills: Int let outputDevice: AudioDeviceID let outputSampleRate: Double let outputChannels: AVAudioChannelCount @@ -246,14 +247,18 @@ private func audioOutputDevices() throws -> [AudioDeviceID] { }.sorted() } -private func playFixturePCM(_ pcm: [UInt8], requestedOutput: AudioDeviceID? = nil) throws -> FixturePlayback { +private func playFixturePCM( + _ pcm: [UInt8], requestedOutput: AudioDeviceID? = nil, streamingFixture: Bool = false +) throws -> FixturePlayback { // The embedded audio/VFS fixture submits mono 48 kHz S16LE frames. guard !pcm.isEmpty, pcm.count.isMultiple(of: 2), let format = AVAudioFormat(standardFormatWithSampleRate: 48_000, channels: 1) else { throw FixturePlaybackError.invalidPCM } - let frames = pcm.count / 2 - let framesPerBuffer = 128 + let sourceFrames = pcm.count / 2 + let frames = sourceFrames * (streamingFixture ? 32 : 1) + let framesPerBuffer = streamingFixture ? 1024 : 128 + let bufferWindow = 3 let engine = AVAudioEngine() guard let outputUnit = engine.outputNode.audioUnit else { throw FixturePlaybackError.outputUnitUnavailable @@ -290,8 +295,10 @@ private func playFixturePCM(_ pcm: [UInt8], requestedOutput: AudioDeviceID? = ni } let output = engine.outputNode.outputFormat(forBus: 0) let presentationLatency = engine.outputNode.presentationLatency + var nextFrame = 0 var bufferCount = 0 - for first in stride(from: 0, to: frames, by: framesPerBuffer) { + func scheduleNextBuffer() throws { + let first = nextFrame let count = min(framesPerBuffer, frames - first) guard let buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: AVAudioFrameCount(count)), let samples = buffer.floatChannelData?[0] else { @@ -299,7 +306,7 @@ private func playFixturePCM(_ pcm: [UInt8], requestedOutput: AudioDeviceID? = ni } buffer.frameLength = AVAudioFrameCount(count) for offset in 0.. 0 && played.outputChannels > 0 ? 0 : 1) + let played = try playFixturePCM( + frame.pcm, requestedOutput: requestedOutput, streamingFixture: nativeAudioStreamSmoke + ) + print("smoke-native-audio: playedBackFrames=\(played.frames) playedBackBuffers=\(played.buffers) refills=\(played.refills) outputDevice=\(played.outputDevice) outputRate=\(played.outputSampleRate) outputChannels=\(played.outputChannels) presentationLatencyMs=\(played.presentationLatency * 1000) completionElapsedMs=\(played.completionElapsed * 1000)") + let expected = nativeAudioStreamSmoke ? (8192, 8, 5) : (256, 2, 0) + exit((played.frames, played.buffers, played.refills) == expected + && played.outputSampleRate > 0 && played.outputChannels > 0 ? 0 : 1) } catch { print("smoke-native-audio: \(error)") exit(1)