Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Count only what the Mac's output adds after its mixer

Jackya8e9b818df7b
diff --git a/Research/Guest/virtio_sound.md b/Research/Guest/virtio_sound.md
index be75eecb..4d97c4d4 100644
--- a/Research/Guest/virtio_sound.md
+++ b/Research/Guest/virtio_sound.md
@@ -530,14 +530,14 @@ The speaker's output latency now follows both
- *The Mac's output.* `vphone-vm` reads the default output device from
CoreAudio, the device Virtualization's `VZHostAudioOutputStreamSink` plays
to ("the same device that AudioQueueNewOutput uses", its header): device
- latency + safety offset + its output stream's latency + IO buffer, at its
- nominal rate (`VPhoneHostAudioLatencySync`). The headers sum the device
- and stream latency, the time after the HAL's output time; the native
- player's 14.5 ms on the built-in speakers is that part (15.6 ms there).
- The safety offset and buffer lie between the mix and that output time,
- after Virtualization hands the guest its write back, so from the guest
- they count too. Built-in speakers of a MacBook Pro: 60 + 48 + 690 + 512
- frames, 27.3 ms. It sends the figure after every connect and whenever the
+ latency + its output stream's latency, at its nominal rate
+ (`VPhoneHostAudioLatencySync`), the sum the headers give for the time
+ after the HAL's output time. The native player's 14.5 ms on the built-in
+ speakers is that part (15.6 ms there). The safety offset and IO buffer,
+ mixed ahead of the output time, are left out: at the mixer the guest's
+ sound was already 8 ms ahead on the built-in speakers, so what lies before
+ the mixer is covered by the in-flight part. Built-in speakers of a MacBook
+ Pro: 60 + 690 frames, 15.6 ms. It sends the figure after every connect and whenever the
default output device or its latency, safety offset, buffer size, rate or
streams change, with `audio.host_latency` (`Research/vphoned_http_api.md`);
vphoned stores it as `VPhoneVirtIOSoundHostLatency` in
@@ -557,8 +557,8 @@ audiomxd performs the change, and that a player already playing picks the
new latency up rather than only the next one.
What to expect, from the measurements above: on the built-in speakers the
-queued part stays at 3 periods and the host adds 27.3 ms, so the guest's
-sound moves from 8 ms to about 36 ms ahead of its picture at the mixer
+queued part stays at 3 periods and the host adds 15.6 ms, so the guest's
+sound moves from 8 ms to about 24 ms ahead of its picture at the mixer
(native: 14.5 ms). On AirPods the queued part should settle at 6 periods
(512 ms) and the host add about 150 ms, about 400 ms more than before,
which would leave the guest roughly 100 ms late at the ear unless
diff --git a/Research/vphoned_http_api.md b/Research/vphoned_http_api.md
index 532160a9..c2c9cc1a 100644
--- a/Research/vphoned_http_api.md
+++ b/Research/vphoned_http_api.md
@@ -354,8 +354,7 @@ for user mobile, posts the Darwin notification
`com.vphone.audio.host-latency` so a running audiomxd reads it again, and
returns `{seconds, changed}`; a value within 10 µs of the stored one is
neither written nor posted (`changed: false`). `vphone-vm` sends the default
-output device's device latency + safety offset + output stream latency + IO
-buffer after every connect and whenever the default output device or one of
+output device's device latency + output stream latency after every connect and whenever the default output device or one of
those changes (`VPhoneHostAudioLatencySync`). See
`Research/Guest/virtio_sound.md` §6, "Picture against sound".
diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/HostDevices/VPhoneHostAudioLatencySync.swift b/VPhoneExecutable/VPhoneVirtualization/UI/HostDevices/VPhoneHostAudioLatencySync.swift
index 7bcb3b49..c8bd788a 100644
--- a/VPhoneExecutable/VPhoneVirtualization/UI/HostDevices/VPhoneHostAudioLatencySync.swift
+++ b/VPhoneExecutable/VPhoneVirtualization/UI/HostDevices/VPhoneHostAudioLatencySync.swift
@@ -10,12 +10,12 @@ import Foundation
/// its picture back by that: measured on one clock at the Mac's mixer, an
/// AVPlayer window plays its sound 14.5 ms early on the built-in speakers,
/// whose device and stream latency come to 15.6 ms, and 140 ms early on
-/// AirPods. The HAL mixes each cycle the safety offset plus one IO buffer
-/// ahead of that output time. The guest sees its writes come back from
-/// Virtualization's queue, which (Virtualization does not say; assumed) is
-/// no later than the queue's buffer is mixed, so from the guest those two
-/// count as well and the figure is all four. On a MacBook Pro's speakers:
-/// 60 + 48 + 690 + 512 frames, 27.3 ms.
+/// AirPods. That sum is the figure. The safety offset and IO buffer, which
+/// the HAL mixes ahead of the output time, are left out: measured at the
+/// Mac's mixer the guest's sound was already 8 ms ahead of its picture on the
+/// built-in speakers, so what lies before the mixer is covered by what the
+/// plugin measures in flight. They are read and logged all the same. On a
+/// MacBook Pro's speakers: 60 + 690 frames, 15.6 ms.
struct VPhoneHostAudioLatency: Equatable {
var deviceFrames: UInt32
var safetyOffsetFrames: UInt32
@@ -24,7 +24,7 @@ struct VPhoneHostAudioLatency: Equatable {
var sampleRate: Double
var frames: UInt64 {
- UInt64(deviceFrames) + UInt64(safetyOffsetFrames) + UInt64(streamFrames) + UInt64(bufferFrames)
+ UInt64(deviceFrames) + UInt64(streamFrames)
}
var seconds: Double {
@@ -33,8 +33,8 @@ struct VPhoneHostAudioLatency: Equatable {
var summary: String {
String(
- format: "%.1f ms (device %u + safety offset %u + stream %u + buffer %u frames at %.0f Hz)",
- seconds * 1000, deviceFrames, safetyOffsetFrames, streamFrames, bufferFrames, sampleRate,
+ format: "%.1f ms (device %u + stream %u frames at %.0f Hz; safety offset %u and buffer %u not counted)",
+ seconds * 1000, deviceFrames, streamFrames, sampleRate, safetyOffsetFrames, bufferFrames,
)
}
}
diff --git a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundLatency.h b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundLatency.h
index 8eba1c11..b0bfc29c 100644
--- a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundLatency.h
+++ b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundLatency.h
@@ -12,7 +12,7 @@
// the Mac's built-in speakers and 3-5 (up to 8) with AirPods. The tracker
// below follows it.
// - What the Mac's output device adds after its mixer: the device's and its
-// stream's latency, its safety offset and its IO buffer. vphone-vm reads
+// stream's latency. vphone-vm reads
// that from CoreAudio and vphoned stores it for the plugin, in seconds.
//
// The tracker sees the most in flight per window of a few seconds. The