Run a virtual iPhone on an Apple Silicon Mac.
Swift
Record what the Mac's microphone hears on an iPhone guest
Jackyc812bafc7b7a
diff --git a/Research/0_binary_patch_comparison.md b/Research/0_binary_patch_comparison.mdindex 8ac1b70f..fe4d6143 100644--- a/Research/0_binary_patch_comparison.md+++ b/Research/0_binary_patch_comparison.md@@ -2545,3 +2545,31 @@ both sites written in one run, a second run reporting both already patched.The handler's volume-mode test (`and x28, x0, #0x1ffffffff` at 0xea3fc) isa branch between two builds of the route, not a precondition, and is nottouched.++## Guest audio through a Mac's microphone (2026-10-04)++An iPhone guest's audio node and tuning set are the D47's. Three changes take+out what they assume about its microphones; the measurements are in+`Research/Guest/virtio_sound_microphone.md` §7.++| Patch | Component | Effect |+| --- | --- | --- |+| `devicetree-cfw-product_audio_microphone_array` (new) | Every guest's DeviceTree, `fw patch` | Removes `supports-spatial-audio-capture` and `supports-audio-mix` from `/product/audio` (`DeviceTreePatcher.removeMicrophoneArrayClaims(from:)`). With them iOS 27's Voice Memos records through the four-microphone spatial route and the recording is silent. |+| `preboot-cfw-devicetree_microphone_array` (new) | Restored Preboot `devicetree.img4`, `cfw install` and `cfw update-environment`, every guest | The same removal for a VM patched before it (`vphone-cli cfw patch-dt-microphone-array`). |+| `system-virtualaudio-cfw-microphone_graph_chains` (new) | `/Library/Audio/Tunings/<AID>/VAD/graph_configurations.plist` and each `*_mic*_measurement.austrip` beside it | Every `<mic>_general` configuration with a `<mic>_measurement` sibling takes the sibling's `graph`, `austrip` and `propstrip` (`patch-virtualaudio-microphone-chains`), and every AUNBandEQ in those strips has its global gain set to 0 dB (`patch-virtualaudio-microphone-gain`; +18 dB on `bottom_mic_measurement`). A set with no such pair is left alone. |++All three are on in `standard`, need no firmware change, and are in the+installer's late list, so `cfw update-environment` brings them to a VM whose+plan predates them. The tuning files themselves are untouched except for the+one parameter word in each measurement strip; a strip whose saved state is+not whole parameter records is refused rather than rewritten.++Validation on `mictest-iphone` (iPhone99,11, iOS 27.0, cloudOS 26.4):+`vphone-cli cfw patch-virtualaudio-microphone-chains` on the guest's own+plist reported `back_mic_general`, `beamformed_mic_general`,+`bottom_mic2_general`, `bottom_mic_general` and `front_mic_general`, and a+second run none; `patch-virtualaudio-microphone-gain` on its+`bottom_mic_measurement.austrip` reported `AUNBEQ_2 global gain 18.0 dB -> 0+dB`, changing one word of the saved state. After `cfw update-environment` and a+boot, audiomxd built the recording route with `DSP chain+'bottom_mic_measurement'`.diff --git a/Research/Guest/virtio_sound_microphone.md b/Research/Guest/virtio_sound_microphone.mdindex 8eb1a4eb..4294a746 100644--- a/Research/Guest/virtio_sound_microphone.md+++ b/Research/Guest/virtio_sound_microphone.md@@ -26,6 +26,7 @@ local:| Guest kernel | `AppleVirtIOSound` reports stream 0 as an input stream (`direction 1, formats 0xa0020, rates 0x480, channels 1-2`) and serves selector 9, the receive transfer the macOS plugin uses (§6). || Guest HAL plugin | Publishes a second device, the microphone, with the input stream (§5). || Guest routing | The microphone device must be named `Digital Mic`, the route handler for play-and-record needs its speaker-protection gate opened, and the streams must accept the format that route sets on them (§4). With the three, Voice Memos records on an iPad guest (iPadOS 26.6.2). On an iPhone guest (iOS 27.0) the route builds and Voice Memos fails before any I/O, in the capture stack (§6). |+| Guest tunings | An iPhone guest's device tree and tuning set are the D47's, made for its four microphones and its speaker. With them a recording was silent, then loud and noisy; §7 is what was removed and what the same recording measures now. |## 1. The reference implementation: `AVIOStream` input in the macOS plugin@@ -591,7 +592,148 @@ Not done:that publishes the microphone anyway are in`ios27_capture_microphone_source.md`.-## 7. Open+## 7. The tunings were the board's, the microphone is the Mac's (2026-10-04)++With the capture source published (`ios27_capture_microphone_source.md`)+Voice Memos on the iPhone guest recorded, and what it recorded went through+four states. Everything here was measured on `mictest-iphone` (iPhone99,11,+iOS 27.0) with the Mac's built-in microphone, by decoding the recording on+the host and, from the third state on, comparing it with the capture as the+plugin received it (`/var/mobile/vpinput.raw`, below).++### Silence: the spatial capture route++The recording was a `.qta` with a four-channel ambisonic track and a stereo+one, both digital silence, while the HAL's own level report on the input+showed sound (`RTAID … node=-Input … rms:[-20.5], peaks:[-1.4]`). The+session's mode was `SpatialCapture` and the route's DSP chain+`flexible_video_recording`, whose graph+(`/Library/Audio/Tunings/AID8018/VAD/flexible_video_recording.dspg`) begins+`[def numMics 4]`: four microphones in, wind suppression and a beamformer+between them, first-order ambisonics and a stereo fallback out. The plugin+has two channels, both the Mac's one. Where in that graph they become zero+was not traced. audiomxd also logs, on every route with a microphone,++```+EDT Accessor error 'Could not construct' for path: IODeviceTree:/product/audio ; key: mic-trim-gains-1 did not return any data+FDRDataImpl.cpp:356 Trim Gain key 'mic-trim-gains-2' using version 31091+FDRDataImpl.cpp:395 EXCEPTION (std::runtime_error): "Unrecognized FDRVersion: 31091 using key mic-trim-gains-2"+```++31091 is 0x7973, the first two bytes of the placeholder `syscfg/MiGB` that no+VM's syscfg fills. The exception is caught and the route goes on; whether it+matters to the silence is not known, and the routes below work with it still+logged.++Voice Memos asks for that mode because the guest says it can: the audio node+`devicetree-cfw-product_audio_node` copies from the D47 carries+`supports-spatial-audio-capture` and `supports-audio-mix`. The second is+what puts the Audio Mix analysis in the capture graph, whose neural net+faults in cameracaptured (`ios27_capture_microphone_source.md` §7). Neither+is true of a VM. `devicetree-cfw-product_audio_microphone_array` removes+both from every guest's tree and `preboot-cfw-devicetree_microphone_array`+(`vphone-cli cfw patch-dt-microphone-array`) from a tree already restored.+`supports-spatial-facetime` and `stereo-sound-recording` stay: nothing run+here depended on them.++Without them the session is `PlayAndRecord` / default mode, the route+`[vdef] (cpar/imdf) port pmbi`, and the recording a mono `.m4a` with sound+in it.++### Loud, and it sounded like wind: the general microphone chain++That route's chain was `bottom_mic_general`: the per-microphone correction+(`built_in_mic_hardware`: trim gains, one FIR each), `CoexKill`, a channel+selector, an equalizer, a loudness normalizer (`AULDNM`), a multiband+compressor (`AUMeisterStueck`) and a limiter (`AUControlFreak`). Against the+capture, band by band (dBFS RMS over the same 13 s):++| | below 60 Hz | 60–120 | 120–300 | 300–1k | 1k–4k | above 4k | peak |+| --- | --- | --- | --- | --- | --- | --- | --- |+| capture | -40.4 | -30.6 | -26.7 | -29.0 | -40.8 | -58.1 | -6.7 |+| recording | -32.5 | -22.7 | -18.8 | -20.7 | -32.3 | -49.8 | -0.6 |++About 8 dB everywhere, speech against the limiter, and the pauses lifted from+-47 to -37 dBFS. The same microphone on an iPad guest (tuning set AID2019)+goes through a chain named `placeholder` and comes out as it went in.++The D47 set has a second configuration for each microphone,+`<mic>_measurement`, for `AVAudioSessionModeMeasurement`: the correction,+`CoexKill`, the selector and two `AUNBandEQ`s, no dynamics.+`system-virtualaudio-cfw-microphone_graph_chains` gives every+`<mic>_general` entry in `graph_configurations.plist` its sibling's `graph`,+`austrip` and `propstrip` (`vphone-cli cfw+patch-virtualaudio-microphone-chains`); the entry's channel map and other+properties stay.++### Clipped: the board microphone's gain++Through the measurement chain the recording was 18 dB above the capture in+every band (-5.7 dBFS RMS, peaks at +14 dBFS). The strip's second equalizer+carries it: decoding `bottom_mic_measurement.austrip`, `AUNBEQ_2`'s saved+state is one record of 81 parameters whose parameter 0, AUNBandEQ's global+gain, is 18.0; `AUNBEQ_1`'s is 0, with one active band, a high-pass at 30 Hz+(the preset is named `windFilterNBandEQPreset`). The gain is the digital+gain of the D47's own microphone. The same patch sets every AUNBandEQ's+global gain in each `*_mic*_measurement.austrip` to 0+(`vphone-cli cfw patch-virtualaudio-microphone-gain`); the state's layout is+big-endian records of scope, element, count and `count` pairs of parameter ID+and Float32, and a strip that does not parse that way is refused.++After it the recording has the capture's levels: speech at -18 to -22 dBFS+and pauses at -45 dBFS in both, peaks at -3.1 against -3.4 dBFS. (The two+were not the same span, so no band table: the dump stops at 16 MB and the+recording ran on.)++### The Mac's rumble: a low cut in the plugin++What was left is in the capture itself. In its pauses (dBFS RMS):++| 20–40 Hz | 40–60 | 60–80 | 80–100 | 100–150 | 150–250 | 250–500 | 500–1k | 1k–4k |+| --- | --- | --- | --- | --- | --- | --- | --- | --- |+| -62.4 | -57.2 | -54.9 | -55.4 | -56.7 | -62.6 | -67.3 | -73.9 | -76.3 |++A phone's microphone does not pass much of that range; the D47 chain's only+high-pass is the 30 Hz one above. The plugin now runs each captured period+through a fourth-order Butterworth high-pass at 120 Hz before the HAL reads+it (`VPVirtIOSoundFilter.c`, on the input stream's completion queue, state+reset at each start). On the capture above, offline, that takes the pauses+from -46.8 to -55.9 dBFS and speech down 2.2 dB.++Against a recording made by the Mac's own Voice Memos a few minutes apart+(different words, same room and microphone):++| | peak | whole | pauses | below 60 Hz | 60–120 | 120–300 | 300–1k | 1k–4k |+| --- | --- | --- | --- | --- | --- | --- | --- | --- |+| Mac's Voice Memos | -6.2 | -25.0 | -45 to -46 | -42.2 | -32.3 | -27.6 | -30.7 | -43.7 |+| guest's capture | -7.6 | -25.7 | -44 to -45 | -41.6 | -33.5 | -28.8 | -30.6 | -40.8 |+| guest's recording | -7.0 | -26.4 | -50 to -54 | -45.3 | -34.9 | -29.5 | -30.9 | -41.2 |++### What the plugin reports++Each stop of the input stream now also logs the level of what the host sent,+before the low cut: `stream 0: captured level -23.6 / -23.6 dBFS RMS, peak+-6.7 / -6.7 dBFS` (both channels are the Mac's one microphone). A start that+finds `/var/mobile/vpinput.dump` also writes the capture, as received, to+`/var/mobile/vpinput.raw` (interleaved Float32, 48 kHz, 2 channels, at most+16 MB) — the file the tables above compare recordings with. `files.read`+returns 512 KB unless given a `limit`.++### Not done++* An iPad guest was not run again. Its set pairs no general microphone chain+ with a measurement one in the logs taken earlier (`placeholder`), so the+ chain patch should find nothing there; the low cut and the device tree+ removal apply to it and were not measured on it.+* Only the default-mode recording route was exercised. Voice processing+ (`mic_voice_*` chains: calls, Siri) still gets the Mac's level where it+ expects the board microphone's, 18 dB lower by the measurement strip's+ account.+* 120 Hz is a judgement between the rumble and a low voice's fundamental+ (this one's was 147 Hz, where the filter's response is -0.8 dB). It was not tried on other voices or+ other Macs.++## 8. Open* Whether the kernel returns reads still out at RELEASE is not known. Theplugin does not depend on it in the normal stop; the one-second fallbackdiff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CustomFirmware/CustomFirmwarePostRestoreDeviceTree.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CustomFirmware/CustomFirmwarePostRestoreDeviceTree.swiftindex 5ffb9d55..bd5bbf22 100644--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CustomFirmware/CustomFirmwarePostRestoreDeviceTree.swift+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CustomFirmware/CustomFirmwarePostRestoreDeviceTree.swift@@ -117,6 +117,21 @@ public enum CustomFirmwarePostRestoreDeviceTree {}}+ /// Remove the microphone array claims from `/product/audio` in a guest's+ /// `devicetree.img4` (or bare `.im4p`), in place. See+ /// `DeviceTreePatcher.removeMicrophoneArrayClaims(from:)`.+ @discardableResult+ public static func removeMicrophoneArrayClaims(+ at url: URL,+ dryRun: Bool = false,+ verbose: Bool = true,+ ) throws -> Outcome {+ try rewrite(at: url, dryRun: dryRun, verbose: verbose) { blob in+ let (newBlob, changes, delta) = try withoutMicrophoneArrayClaims(blob)+ return (newBlob, changes, blob.count + delta)+ }+ }+/// The container, payload and flat tree of a device tree file. Refuses a/// payload that is not a device tree, is encrypted, or came back still/// compressed.@@ -317,6 +332,23 @@ public enum CustomFirmwarePostRestoreDeviceTree {return (serializeNode(root), [record], -removed.count)}+ /// `blob` without the microphone array claims in `/product/audio`, through+ /// `DeviceTreePatcher.removeMicrophoneArrayClaims(from:)`, which fw patch+ /// uses for every new guest. Returns the size change with the changes: each+ /// property's entry out. A tree with none of them comes back unchanged.+ public static func withoutMicrophoneArrayClaims(_ blob: Data) throws -> (Data, [Change], Int) {+ let root = try parseTree(blob, label: "DT")+ let removed = DeviceTreePatcher.removeMicrophoneArrayClaims(from: root)+ guard !removed.isEmpty else {+ return (blob, [], 0)+ }+ let records = removed.map {+ Change(property: "product/audio/\($0.name)", before: $0.value.hex, after: "absent")+ }+ let delta = removed.reduce(0) { $0 - 36 - align4($1.value.count) }+ return (serializeNode(root), records, delta)+ }+/// The root of a flat tree that must fill `blob` exactly.private static func parseTree(_ blob: Data, label: String) throws -> DeviceTreePatcher.DTNode {let (root, end) = try parseNode(blob, at: 0)diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CustomFirmware/CustomFirmwareVirtualAudioMicrophoneChains.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CustomFirmware/CustomFirmwareVirtualAudioMicrophoneChains.swiftnew file mode 100644index 00000000..c09e904d--- /dev/null+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CustomFirmware/CustomFirmwareVirtualAudioMicrophoneChains.swift@@ -0,0 +1,285 @@+// CustomFirmwareVirtualAudioMicrophoneChains.swift — microphone chains without+// the board's dynamics in graph_configurations.plist.+//+// A tuning set gives each built-in microphone a `<mic>_general` configuration+// for ordinary recording and a `<mic>_measurement` one for+// AVAudioSessionModeMeasurement. On the D47 set (AID8018) the general graph is+// microphone correction, a channel selector, an equalizer, a loudness+// normalizer, a multiband compressor and a limiter; the measurement graph+// stops after the correction, the selector and two equalizers.+//+// The general chain's gain and dynamics are tuned to the sensitivity of the+// board's own microphone. A VM's microphone is the Mac's, which arrives+// already at the level macOS set. Measured on an iPhone guest: the chain adds+// about 8 dB across the whole band, speech reaches the limiter (-0.6 dBFS+// peaks) and the capture's noise floor goes from -47 to -37 dBFS, most of it+// rumble below 120 Hz — a recording that sounds like wind. An iPad guest's+// set (AID2019) routes the same microphone through no processing at all, and+// its recordings are the Mac's capture as it is.+//+// Each `<mic>_general` configuration with a `<mic>_measurement` sibling takes+// the sibling's `graph`, `austrip` and `propstrip`; everything else in the+// entry stays, so the route still finds its channel map and its properties.+// The tuning files themselves are not touched. See+// `Research/Guest/virtio_sound_microphone.md`.++import Foundation+import VPhonePatchKit++/// Points every `<mic>_general` configuration in a `graph_configurations.plist`+/// at its `<mic>_measurement` sibling's tunings.+public enum CustomFirmwareVirtualAudioMicrophoneChains {+ static let configurationsKey = "Configurations"++ /// What marks a microphone configuration (`bottom_mic_general`,+ /// `bottom_mic2_general`; not `speaker_general`), and the suffix of an+ /// ordinary recording configuration and of the measurement one it is+ /// paired with.+ static let microphoneMark = "_mic"+ static let generalSuffix = "_general"+ static let measurementSuffix = "_measurement"++ /// The keys naming a configuration's tuning files.+ static let tuningKeys = ["graph", "austrip", "propstrip"]++ /// What a patch attempt did.+ public enum Outcome: Sendable, Equatable {+ /// The set pairs no general configuration with a measurement one.+ case noPairs+ /// Every paired configuration already names its sibling's tunings.+ case alreadyMeasurementChains([String])+ /// Entries would change but `dryRun` suppressed the write.+ case dryRun(changed: [String])+ /// The file was rewritten with the changed entries.+ case rewritten(changed: [String])++ /// True only when bytes landed on disk.+ public var didWrite: Bool {+ if case .rewritten = self {+ return true+ }+ return false+ }+ }++ // MARK: - Patching++ /// Give every `<mic>_general` configuration at `url` its+ /// `<mic>_measurement` sibling's tunings.+ ///+ /// - Throws: ``PatcherError/invalidFormat(_:)`` when the file is not a+ /// plist, its root is not a dictionary, `Configurations` is missing or+ /// not a dictionary, or one of a pair is not a dictionary or names no+ /// `graph`. A set with no pairs is not an error: not every board has+ /// them.+ @discardableResult+ public static func patch(+ at url: URL,+ dryRun: Bool = false,+ verbose: Bool = true,+ ) throws -> Outcome {+ let data: Data+ do {+ data = try Data(contentsOfFileToRewrite: url)+ } catch {+ throw PatcherError.fileNotFound(url.path)+ }+ let format = CustomFirmwareBuildVersion.detectFormat(data)++ guard let parsed = try? PropertyListSerialization.propertyList(from: data, options: [], format: nil) else {+ throw PatcherError.invalidFormat("cannot parse as plist: \(url.path)")+ }+ guard let plist = parsed as? [String: Any] else {+ throw PatcherError.invalidFormat("top-level plist is \(type(of: parsed)), expected dict: \(url.path)")+ }+ guard var configurations = plist[configurationsKey] as? [String: Any] else {+ throw PatcherError.invalidFormat("no '\(configurationsKey)' dict present: \(url.path)")+ }++ func entry(_ name: String) throws -> [String: Any] {+ guard let entry = configurations[name] as? [String: Any] else {+ throw PatcherError.invalidFormat(+ "configuration '\(name)' is \(type(of: configurations[name])), expected dict: \(url.path)",+ )+ }+ guard entry["graph"] is String else {+ throw PatcherError.invalidFormat("configuration '\(name)' has no 'graph' string: \(url.path)")+ }+ return entry+ }++ var paired: [String] = []+ var changed: [String] = []+ for name in configurations.keys.sorted() where name.contains(microphoneMark) && name.hasSuffix(generalSuffix) {+ let sibling = String(name.dropLast(generalSuffix.count)) + measurementSuffix+ guard configurations[sibling] != nil else { continue }+ paired.append(name)+ var general = try entry(name)+ let measurement = try entry(sibling)+ var differs = false+ for key in tuningKeys {+ let wanted = measurement[key] as? String+ guard general[key] as? String != wanted else { continue }+ differs = true+ general[key] = wanted+ }+ guard differs else { continue }+ changed.append(name)+ configurations[name] = general+ if verbose {+ print(" [+] \(url.lastPathComponent): \(name) takes the tunings of \(sibling)")+ }+ }++ if paired.isEmpty {+ if verbose {+ print(" [.] \(url.lastPathComponent): no general microphone chain has a measurement sibling")+ }+ return .noPairs+ }+ if changed.isEmpty {+ if verbose {+ print(" [.] \(url.lastPathComponent): \(paired.count) microphone chains already on measurement tunings")+ }+ return .alreadyMeasurementChains(paired)+ }+ if dryRun {+ if verbose {+ print(" [.] dry-run — not writing back")+ }+ return .dryRun(changed: changed)+ }++ var patched = plist+ patched[configurationsKey] = configurations+ let output = try PropertyListSerialization.data(fromPropertyList: patched, format: format, options: 0)+ try output.write(to: url)+ return .rewritten(changed: changed)+ }++ // MARK: - Measurement gain++ /// One equalizer whose global gain `neutralizeGain` set to 0 dB.+ public struct GainChange: Sendable, Equatable {+ /// The effect's `displayname` in the strip.+ public let effect: String+ /// The gain it had, in decibels.+ public let before: Float+ }++ /// `aufx`/`nbeq`: AUNBandEQ, whose parameter 0 is its global gain.+ static let bandEqualizerSubtype: UInt32 = 0x6E62_6571+ static let globalGainParameter: UInt32 = 0++ /// Set the global gain of every AUNBandEQ in the `.austrip` at `url` to+ /// 0 dB, and return the ones that changed.+ ///+ /// A measurement strip's equalizers shape nothing the Mac's microphone+ /// needs, but one of them carries the digital gain of the board's own+ /// microphone: +18 dB on the D47's `bottom_mic_measurement`. The Mac's+ /// capture is already at the level macOS set, so with that gain speech+ /// passes full scale (measured: +14 dBFS peaks) and the recording clips.+ ///+ /// - Throws: ``PatcherError/invalidFormat(_:)`` when the file is not a+ /// plist of `strips` → `effects`, or an equalizer's preset data is not+ /// whole (scope, element, count, then `count` parameter pairs) records.+ @discardableResult+ public static func neutralizeGain(+ at url: URL,+ dryRun: Bool = false,+ verbose: Bool = true,+ ) throws -> [GainChange] {+ let data: Data+ do {+ data = try Data(contentsOfFileToRewrite: url)+ } catch {+ throw PatcherError.fileNotFound(url.path)+ }+ let format = CustomFirmwareBuildVersion.detectFormat(data)+ guard+ var plist = (try? PropertyListSerialization.propertyList(from: data, options: [], format: nil)) as? [String: Any],+ var strips = plist["strips"] as? [[String: Any]]+ else {+ throw PatcherError.invalidFormat("not a plist with a 'strips' array: \(url.path)")+ }++ var changes: [GainChange] = []+ for stripIndex in strips.indices {+ guard var effects = strips[stripIndex]["effects"] as? [[String: Any]] else {+ throw PatcherError.invalidFormat("strip \(stripIndex) has no 'effects' array: \(url.path)")+ }+ for effectIndex in effects.indices {+ guard+ let unit = effects[effectIndex]["unit"] as? [String: Any],+ (unit["subtype"] as? NSNumber)?.uint32Value == bandEqualizerSubtype,+ var preset = effects[effectIndex]["aupreset"] as? [String: Any],+ let state = preset["data"] as? Data+ else { continue }+ let name = effects[effectIndex]["displayname"] as? String ?? "effect \(effectIndex)"+ guard let (patched, before) = try withoutGlobalGain(state, effect: name, path: url.path) else {+ continue+ }+ preset["data"] = patched+ effects[effectIndex]["aupreset"] = preset+ changes.append(GainChange(effect: name, before: before))+ if verbose {+ print(" [+] \(url.lastPathComponent): \(name) global gain \(before) dB -> 0 dB")+ }+ }+ strips[stripIndex]["effects"] = effects+ }++ if changes.isEmpty {+ if verbose {+ print(" [.] \(url.lastPathComponent): no equalizer gain to remove")+ }+ return []+ }+ if dryRun {+ if verbose {+ print(" [.] dry-run — not writing back")+ }+ return changes+ }+ plist["strips"] = strips+ let output = try PropertyListSerialization.data(fromPropertyList: plist, format: format, options: 0)+ try output.write(to: url)+ return changes+ }++ /// `state` with the global-scope gain parameter at 0, and the gain it had;+ /// nil when it is already 0 or the preset does not carry it.+ ///+ /// An AU's saved parameters are big-endian records: scope, element, count,+ /// then `count` pairs of parameter ID and Float32 value.+ static func withoutGlobalGain(_ state: Data, effect: String, path: String) throws -> (Data, Float)? {+ var bytes = [UInt8](state)+ func word(_ offset: Int) -> UInt32 {+ bytes[offset ..< offset + 4].reduce(0) { $0 << 8 | UInt32($1) }+ }+ var found: Float?+ var offset = 0+ while offset < bytes.count {+ guard offset + 12 <= bytes.count else {+ throw PatcherError.invalidFormat("\(effect): parameter data ends inside a record header: \(path)")+ }+ let scope = word(offset)+ let count = Int(word(offset + 8))+ offset += 12+ guard count <= (bytes.count - offset) / 8 else {+ throw PatcherError.invalidFormat("\(effect): parameter data ends inside a record: \(path)")+ }+ for _ in 0 ..< count {+ if scope == 0, word(offset) == globalGainParameter {+ let gain = Float(bitPattern: word(offset + 4))+ if gain != 0 {+ found = gain+ bytes.replaceSubrange(offset + 4 ..< offset + 8, with: [0, 0, 0, 0])+ }+ }+ offset += 8+ }+ }+ return found.map { (Data(bytes), $0) }+ }+}diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/DeviceTree/DeviceTreeGuestDevicePatches.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/DeviceTree/DeviceTreeGuestDevicePatches.swiftindex 5fcfd3b7..7ba86934 100644--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/DeviceTree/DeviceTreeGuestDevicePatches.swift+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/DeviceTree/DeviceTreeGuestDevicePatches.swift@@ -57,6 +57,7 @@ extension DeviceTreePatcher {static let iPadIdentityPatch = "devicetree-cfw-ipad_identity"static let iPadAudioPatch = "devicetree-cfw-ipad_audio"static let hapticsPatch = "devicetree-cfw-product_haptics_node"+ static let microphoneArrayPatch = "devicetree-cfw-product_audio_microphone_array"// MARK: - What Is Copied@@ -292,6 +293,39 @@ extension DeviceTreePatcher {return serialize(haptics)}+ // MARK: - Microphone Array++ /// `/product/audio` properties that promise processing built on the+ /// board's microphone array.+ static let microphoneArrayProperties = ["supports-spatial-audio-capture", "supports-audio-mix"]++ /// Remove `microphoneArrayProperties` from `root`'s `/product/audio`.+ /// Returns each removed property with its value, in that order; empty when+ /// the tree has no audio node or none of them.+ ///+ /// The iPhone guest's audio node is the D47's, and an iPad guest's is its+ /// board's; either can say the device captures spatial audio and analyses+ /// an Audio Mix. A VM's microphone is the Mac's, one or two channels+ /// through virtio-snd, not the four-microphone array those answers stand+ /// for. With them iOS 27's Voice Memos records through the SpatialCapture+ /// route, whose DSP graph (`flexible_video_recording`, four microphones in,+ /// first-order ambisonics out) gives silence for the two real channels,+ /// and through the Audio Mix analysis, whose neural net faults in+ /// cameracaptured. See `Research/Guest/ios27_capture_microphone_source.md`.+ static func removeMicrophoneArrayClaims(from root: DTNode) -> [(name: String, value: Data)] {+ guard+ let product = optionalChild(of: root, named: "product"),+ let audio = optionalChild(of: product, named: "audio")+ else { return [] }+ var removed: [(name: String, value: Data)] = []+ for name in microphoneArrayProperties {+ guard let index = audio.properties.firstIndex(where: { $0.name == name }) else { continue }+ removed.append((name, audio.properties[index].value))+ audio.properties.remove(at: index)+ }+ return removed+ }+/// The flat encoding of one node and its children, for the patch record.private static func serialize(_ node: DTNode) -> Data {var out = Data()@@ -400,4 +434,24 @@ extension DeviceTreePatcher {print(" -node : /product/haptics, no actuator or haptic server on a VM (\(removed.count)B) [\(Self.hapticsPatch)]")}}++ /// Applies `removeMicrophoneArrayClaims(from:)` to the parsed tree,+ /// whichever guest and role it is for, and records each removal.+ func applyMicrophoneArrayRemoval(root: DTNode) {+ guard gateAllows(Self.microphoneArrayPatch) else { return }+ for (name, value) in Self.removeMicrophoneArrayClaims(from: root) {+ patches.append(PatchRecord(+ patchID: Self.microphoneArrayPatch,+ component: component,+ fileOffset: 0,+ virtualAddress: nil,+ originalBytes: value,+ patchedBytes: Data(),+ description: "Remove /device-tree/product/audio/\(name), which needs a microphone array no VM has",+ ))+ if verbose {+ print(" -prop : /product/audio/\(name) \(value.hex) → absent [\(Self.microphoneArrayPatch)]")+ }+ }+ }}diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/DeviceTree/DeviceTreeNodeAdditions.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/DeviceTree/DeviceTreeNodeAdditions.swiftindex 2bbeeedd..df6125ed 100644--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/DeviceTree/DeviceTreeNodeAdditions.swift+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/DeviceTree/DeviceTreeNodeAdditions.swift@@ -165,6 +165,11 @@ extension DeviceTreePatcher {// from d47 wouldn't make the VM's actual mic/speaker hardware// match, but downstream code already handles missing-syscfg// gracefully.+ //+ // `supports-spatial-audio-capture` and `supports-audio-mix` are+ // taken out again after this node is added, by+ // `devicetree-cfw-product_audio_microphone_array`: they stand for+ // the D47's microphone array, which no VM has.AddChildNodePatch(parentPath: ["device-tree", "product"],nodeName: "audio",diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/DeviceTree/DeviceTreePatcher.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/DeviceTree/DeviceTreePatcher.swiftindex 7a282994..66a606d2 100644--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/DeviceTree/DeviceTreePatcher.swift+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/DeviceTree/DeviceTreePatcher.swift@@ -388,6 +388,8 @@ public final class DeviceTreePatcher: BufferedPatcher {// Every guest, every tree: no VM has the haptics the node promises.applyHapticsRemoval(root: root)+ // Nor the microphone array its audio node answers for.+ applyMicrophoneArrayRemoval(root: root)}/// Property patches that describe the virtual board rather than a phone,diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareDeviceTreePatchSet.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareDeviceTreePatchSet.swiftindex 0d9b4e35..894fbf5a 100644--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareDeviceTreePatchSet.swift+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareDeviceTreePatchSet.swift@@ -176,6 +176,11 @@ public enum FirmwareDeviceTreePatchSet {"Haptics node","Removes the Taptic Engine no VM has, so tones play without the haptic track it cannot drive.",),+ property(+ "devicetree-cfw-product_audio_microphone_array",+ "Microphone array claims",+ "Removes spatial audio capture and Audio Mix, which need a microphone array no VM has, so apps record plain audio.",+ ),// MARK: iPad Guestdiff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareGuestSystemPatchSet.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareGuestSystemPatchSet.swiftindex 1fb5ff60..7517275e 100644--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareGuestSystemPatchSet.swift+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareGuestSystemPatchSet.swift@@ -253,6 +253,17 @@ public enum FirmwareGuestSystemPatchSet {""",target: .guestFile(path: "/Library/Audio/Tunings"),),+ VPhonePatchDeclaration(+ identifier: virtualAudioMicrophoneChains,+ title: "VirtualAudio microphone chains",+ summary: """+ Gives every <mic>_general entry in the tuning set's graph_configurations.plist the \+ tunings of its <mic>_measurement sibling. The general chain's loudness normalizer, \+ compressor and limiter are tuned to the board's own microphone; on the Mac's they \+ raise the noise floor about 10 dB and a recording sounds like wind.+ """,+ target: .guestFile(path: "/Library/Audio/Tunings"),+ ),VPhonePatchDeclaration(identifier: prebootBoardAudio,title: "iPad audio configuration",@@ -275,6 +286,17 @@ public enum FirmwareGuestSystemPatchSet {""",target: .prebootDeviceTree,),+ VPhonePatchDeclaration(+ identifier: prebootMicrophoneArray,+ title: "Microphone array claims",+ summary: """+ Removes spatial audio capture and Audio Mix from the audio node of a guest's \+ restored Preboot device tree, as fw patch now does. Both stand for a \+ four-microphone array; with them iOS 27's Voice Memos records through the spatial \+ capture route, which gives silence for the Mac's one or two channels.+ """,+ target: .prebootDeviceTree,+ ),VPhonePatchDeclaration(identifier: "system-systemversion-cfw-build_version",@@ -320,6 +342,11 @@ public enum FirmwareGuestSystemPatchSet {/// Speaker device a VM does not have.public static let virtualAudioGraphConfigurations = "system-virtualaudio-cfw-speaker_graph_chains"+ /// The general microphone chains in graph_configurations.plist, pointed at+ /// their measurement siblings so a recording is not pushed through+ /// dynamics tuned to a microphone the VM does not have.+ public static let virtualAudioMicrophoneChains = "system-virtualaudio-cfw-microphone_graph_chains"+/// The iPad audio node repair in the restored Preboot device tree, for an/// iPad VM patched before `fw patch` copied the board's node.public static let prebootBoardAudio = "preboot-cfw-devicetree_board_audio"@@ -328,6 +355,11 @@ public enum FirmwareGuestSystemPatchSet {/// VM patched before `fw patch` removed it from every guest's tree.public static let prebootHaptics = "preboot-cfw-devicetree_haptics"+ /// The microphone array claims removed from the restored Preboot device+ /// tree, for any VM patched before `fw patch` removed them from every+ /// guest's tree.+ public static let prebootMicrophoneArray = "preboot-cfw-devicetree_microphone_array"+/// The preset parameter `system-systemversion-cfw-build_version` reads.public static let buildVersionParameter = "BuildVersion"}diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/Core/DeviceTreeGuestDeviceTests.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/Core/DeviceTreeGuestDeviceTests.swiftindex 2bece615..76fe34b1 100644--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/Core/DeviceTreeGuestDeviceTests.swift+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/Core/DeviceTreeGuestDeviceTests.swift@@ -408,6 +408,63 @@ struct DeviceTreeGuestDeviceTests {#expect(twice == once)#expect(!records.contains { $0.patchID == "devicetree-cfw-product_haptics_node" })}++ // MARK: - Microphone array++ /// The claims among the properties around them on the D47 audio node.+ static let audio = Node(+ properties: [+ .string("name", "audio"),+ .integer("acoustic-id", 8018),+ .integer("stereo-sound-recording", 1),+ .integer("supports-audio-mix", 1),+ .integer("supports-spatial-audio-capture", 1),+ .integer("supports-spatial-facetime", 1),+ ],+ children: [],+ )++ static let microphoneArrayPatch = "devicetree-cfw-product_audio_microphone_array"++ @Test(arguments: Guest.allCases)+ func `every guest's audio node loses the microphone array claims`(guest: Guest) throws {+ // The node stands where the haptics node does in the other fixture.+ let tree = Self.guestTree(haptics: Self.audio).serialized()+ let (patched, records) = try Self.patched(tree, as: guest)++ let audio = try #require(Self.read(patched)["device-tree/product/audio"])+ #expect(audio["supports-spatial-audio-capture"] == nil)+ #expect(audio["supports-audio-mix"] == nil)+ // Its neighbours stay, the other spatial answer among them.+ #expect(Self.integer(audio["acoustic-id"]) == 8018)+ #expect(Self.integer(audio["stereo-sound-recording"]) == 1)+ #expect(Self.integer(audio["supports-spatial-facetime"]) == 1)++ let removed = records.filter { $0.patchID == Self.microphoneArrayPatch }+ #expect(removed.count == 2)+ #expect(removed.allSatisfy { $0.patchedBytes.isEmpty && $0.originalBytes == Data([1, 0, 0, 0]) })+ }++ @Test(arguments: Guest.allCases)+ func `the claims stay when their patch is off`(guest: Guest) throws {+ let declared = Set(FirmwareDeviceTreePatchSet.manifest.patches.map(\.identifier))+ #expect(declared.contains(Self.microphoneArrayPatch))+ let gate = VPhonePatchGate(declared: declared, enabled: declared.subtracting([Self.microphoneArrayPatch]))+ let (patched, records) = try Self.patched(Self.guestTree(haptics: Self.audio).serialized(), as: guest, gate: gate)++ #expect(!records.contains { $0.patchID == Self.microphoneArrayPatch })+ let audio = try #require(Self.read(patched)["device-tree/product/audio"])+ #expect(Self.integer(audio["supports-spatial-audio-capture"]) == 1)+ #expect(Self.integer(audio["supports-audio-mix"]) == 1)+ }++ @Test(arguments: Guest.allCases)+ func `removing the claims a second time changes nothing`(guest: Guest) throws {+ let (once, _) = try Self.patched(Self.guestTree(haptics: Self.audio).serialized(), as: guest)+ let (twice, records) = try Self.patched(once, as: guest)+ #expect(twice == once)+ #expect(!records.contains { $0.patchID == Self.microphoneArrayPatch })+ }}private extension Data {diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/CustomFirmware/CustomFirmwareMicrophoneArrayTests.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/CustomFirmware/CustomFirmwareMicrophoneArrayTests.swiftnew file mode 100644index 00000000..a5619c72--- /dev/null+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/CustomFirmware/CustomFirmwareMicrophoneArrayTests.swift@@ -0,0 +1,103 @@+// CustomFirmwareMicrophoneArrayTests.swift — no guest claims a microphone array.+//+// `withoutMicrophoneArrayClaims` (the Preboot repair) and+// `DeviceTreePatcher.removeMicrophoneArrayClaims` (what `fw patch` runs for+// every guest's tree) take `supports-spatial-audio-capture` and+// `supports-audio-mix` out of `/product/audio`: a VM's microphone is the Mac's,+// one or two channels, not the array those answers stand for. The trees here+// are built in the flat format the restored tree uses, so the parse, the edit+// and the serialization all run.++@testable import FirmwarePatcher+import Foundation+import Testing++@Suite("Microphone array claims removal")+struct CustomFirmwareMicrophoneArrayTests {+ private typealias Node = FlatDeviceTreeNode++ /// A tree whose `/product/audio` holds `properties`, between two siblings.+ private static func tree(audio properties: [(String, Data)]?) -> Data {+ let product = Node(+ "product",+ [("product-name", Data("iPhone\0".utf8))],+ children: [Node("maps", [("regulatory", Node.uint32(1))])]+ + (properties.map { [Node("audio", $0)] } ?? [])+ + [Node("util", [("ramdisk", Node.uint32(0))])],+ )+ return Node("device-tree", [("model", Data("iPhone99,11\0".utf8))], children: [product]).serialized+ }++ /// The claims among the properties around them on the D47 node.+ private static let claimed: [(String, Data)] = [+ ("acoustic-id", Node.uint32(8018)),+ ("stereo-sound-recording", Node.uint32(1)),+ ("supports-audio-mix", Node.uint32(1)),+ ("supports-auto-mic-mode", Node.uint32(1)),+ ("supports-spatial-audio-capture", Node.uint32(1)),+ ("supports-spatial-facetime", Node.uint32(1)),+ ]++ private static let unclaimed = claimed.filter {+ !DeviceTreePatcher.microphoneArrayProperties.contains($0.0)+ }++ // MARK: - Tests++ @Test func `removes both claims and nothing else`() throws {+ let original = Self.tree(audio: Self.claimed)+ let (patched, changes, delta) = try CustomFirmwarePostRestoreDeviceTree.withoutMicrophoneArrayClaims(original)+ #expect(changes.map(\.property) == [+ "product/audio/supports-spatial-audio-capture",+ "product/audio/supports-audio-mix",+ ])+ #expect(changes.allSatisfy { $0.after == "absent" })+ // Each entry is a 32-byte name, a length, flags and a 4-byte value.+ #expect(delta == -2 * 40)+ #expect(patched.count == original.count + delta)+ // The other properties survive, in order, with the siblings around the node.+ #expect(patched == Self.tree(audio: Self.unclaimed))+ }++ @Test func `removes the one claim a tree has`() throws {+ let original = Self.tree(audio: Self.claimed.filter { $0.0 != "supports-audio-mix" })+ let (patched, changes, delta) = try CustomFirmwarePostRestoreDeviceTree.withoutMicrophoneArrayClaims(original)+ #expect(changes.map(\.property) == ["product/audio/supports-spatial-audio-capture"])+ #expect(delta == -40)+ #expect(patched == Self.tree(audio: Self.unclaimed))+ }++ @Test func `a tree with no claims is left alone`() throws {+ for original in [Self.tree(audio: Self.unclaimed), Self.tree(audio: nil)] {+ let (patched, changes, delta) = try CustomFirmwarePostRestoreDeviceTree.withoutMicrophoneArrayClaims(original)+ #expect(changes.isEmpty)+ #expect(delta == 0)+ #expect(patched == original)+ }+ }++ @Test func `removing twice changes nothing the second time`() throws {+ let (once, _, _) = try CustomFirmwarePostRestoreDeviceTree.withoutMicrophoneArrayClaims(Self.tree(audio: Self.claimed))+ let (twice, changes, _) = try CustomFirmwarePostRestoreDeviceTree.withoutMicrophoneArrayClaims(once)+ #expect(changes.isEmpty)+ #expect(twice == once)+ }++ @Test func `an audio node outside product is not touched`() throws {+ let original = Node(+ "device-tree",+ [("model", Data("iPhone99,11\0".utf8))],+ children: [Node("product"), Node("arm-io", children: [Node("audio", Self.claimed)])],+ ).serialized+ let (patched, changes, _) = try CustomFirmwarePostRestoreDeviceTree.withoutMicrophoneArrayClaims(original)+ #expect(changes.isEmpty)+ #expect(patched == original)+ }++ @Test func `a truncated tree is refused`() {+ let original = Self.tree(audio: Self.claimed)+ #expect(throws: (any Error).self) {+ try CustomFirmwarePostRestoreDeviceTree.withoutMicrophoneArrayClaims(original.prefix(original.count - 4))+ }+ }+}diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/CustomFirmware/CustomFirmwareVirtualAudioMicrophoneChainsTests.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/CustomFirmware/CustomFirmwareVirtualAudioMicrophoneChainsTests.swiftnew file mode 100644index 00000000..914258d1--- /dev/null+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/CustomFirmware/CustomFirmwareVirtualAudioMicrophoneChainsTests.swift@@ -0,0 +1,281 @@+// CustomFirmwareVirtualAudioMicrophoneChainsTests.swift — general microphone+// chains onto their measurement siblings, and the board microphone's gain out+// of a measurement strip.+//+// The fixtures mirror the D47 tuning set (AID8018) name for name: the+// configurations `graph_configurations.plist` pairs, and a measurement strip+// whose second AUNBandEQ carries +18 dB as parameter 0 of its saved state.++import Foundation+import Testing+@testable import FirmwarePatcher+import VPhonePatchKit++@Suite("CustomFirmwareVirtualAudioMicrophoneChains")+struct CustomFirmwareVirtualAudioMicrophoneChainsTests {+ typealias Patcher = CustomFirmwareVirtualAudioMicrophoneChains++ // MARK: - Fixtures++ static func configuration(_ tunings: String, properties: [[String: Any]] = []) -> [String: Any] {+ ["chainType": "dflt", "graph": tunings, "austrip": tunings, "propstrip": tunings,+ "busChannelCounts": [[4, 1]], "properties": properties]+ }++ static func tuningSet() -> [String: Any] {+ [+ "CommonData": ["tuningPath": "/Library/Audio/Tunings/AID8018/VAD"],+ "Configurations": [+ "bottom_mic_general": configuration("bottom_mic_general", properties: [["ID": "chsl"], ["ID": "cots"]]),+ "bottom_mic_measurement": configuration("bottom_mic_measurement", properties: [["ID": "chsl"]]),+ "bottom_mic2_general": configuration("bottom_mic2_general"),+ "bottom_mic2_measurement": configuration("bottom_mic2_measurement"),+ // Its tunings are not named after the configuration.+ "beamformed_mic_general": configuration("beam_mic_general"),+ "beamformed_mic_measurement": configuration("beam_mic_measurement"),+ // No measurement sibling.+ "bottom_mic_voice_messages": configuration("bottom_mic_voice_messages"),+ // A pair, but not a microphone.+ "speaker_general": configuration("speaker_general"),+ "speaker_measurement": configuration("speaker_measurement"),+ ],+ ]+ }++ /// An AU's saved parameters: one record of big-endian (ID, Float32) pairs.+ static func state(scope: UInt32 = 0, _ parameters: [(UInt32, Float)]) -> Data {+ var data = Data()+ func append(_ value: UInt32) {+ withUnsafeBytes(of: value.bigEndian) { data.append(contentsOf: $0) }+ }+ append(scope)+ append(0)+ append(UInt32(parameters.count))+ for (identifier, value) in parameters {+ append(identifier)+ append(value.bitPattern)+ }+ return data+ }++ static func effect(_ name: String, subtype: UInt32, data: Data?) -> [String: Any] {+ var preset: [String: Any] = ["name": "preset", "subtype": NSNumber(value: subtype)]+ preset["data"] = data+ return ["displayname": name, "bypass": 0, "aupreset": preset, "unit": ["subtype": NSNumber(value: subtype)]]+ }++ static let selector: UInt32 = 0x636C_736C // 'clsl'++ static func strip(secondGain: Float = 18) -> [String: Any] {+ ["strips": [["effects": [+ effect("AUChannelSelector", subtype: selector, data: nil),+ effect("AUNBEQ_1", subtype: Patcher.bandEqualizerSubtype, data: state([(0, 0), (1000, 0), (3000, 80)])),+ effect("AUNBEQ_2", subtype: Patcher.bandEqualizerSubtype, data: state([(0, secondGain), (1000, 1), (3000, 1000)])),+ ]]]]+ }++ static func write(+ _ plist: [String: Any],+ format: PropertyListSerialization.PropertyListFormat = .binary,+ ) throws -> URL {+ let url = FileManager.default.temporaryDirectory.appendingPathComponent("vphone-mic-\(UUID().uuidString).plist")+ try PropertyListSerialization.data(fromPropertyList: plist, format: format, options: 0).write(to: url)+ return url+ }++ static func read(_ url: URL) throws -> [String: Any] {+ try #require(try PropertyListSerialization.propertyList(from: Data(contentsOf: url), format: nil) as? [String: Any])+ }++ static func configurations(_ url: URL) throws -> [String: [String: Any]] {+ try #require(try read(url)["Configurations"] as? [String: [String: Any]])+ }++ // MARK: - Chains++ @Test func `general microphone chains take their measurement sibling's tunings`() throws {+ let url = try Self.write(Self.tuningSet())+ defer { try? FileManager.default.removeItem(at: url) }++ let outcome = try Patcher.patch(at: url, verbose: false)+ #expect(outcome == .rewritten(changed: ["beamformed_mic_general", "bottom_mic2_general", "bottom_mic_general"]))++ let patched = try Self.configurations(url)+ for key in Patcher.tuningKeys {+ #expect(patched["bottom_mic_general"]?[key] as? String == "bottom_mic_measurement")+ #expect(patched["bottom_mic2_general"]?[key] as? String == "bottom_mic2_measurement")+ #expect(patched["beamformed_mic_general"]?[key] as? String == "beam_mic_measurement")+ }+ // The rest of the entry is the general configuration's own.+ #expect((patched["bottom_mic_general"]?["properties"] as? [[String: Any]])?.count == 2)+ #expect(patched["bottom_mic_general"]?["chainType"] as? String == "dflt")+ }++ @Test func `nothing but the paired microphone chains moves`() throws {+ let url = try Self.write(Self.tuningSet())+ defer { try? FileManager.default.removeItem(at: url) }+ try Patcher.patch(at: url, verbose: false)++ let patched = try Self.configurations(url)+ let original = try #require(Self.tuningSet()["Configurations"] as? [String: [String: Any]])+ for name in ["bottom_mic_measurement", "bottom_mic_voice_messages", "speaker_general", "speaker_measurement"] {+ #expect(NSDictionary(dictionary: try #require(patched[name])) == NSDictionary(dictionary: try #require(original[name])))+ }+ #expect(try Self.read(url)["CommonData"] != nil)+ }++ @Test func `a tuning key the sibling lacks is removed`() throws {+ var set = Self.tuningSet()+ var configurations = try #require(set["Configurations"] as? [String: [String: Any]])+ configurations["bottom_mic_measurement"]?["propstrip"] = nil+ set["Configurations"] = configurations+ let url = try Self.write(set)+ defer { try? FileManager.default.removeItem(at: url) }++ try Patcher.patch(at: url, verbose: false)+ let patched = try Self.configurations(url)+ #expect(patched["bottom_mic_general"]?["propstrip"] == nil)+ #expect(patched["bottom_mic_general"]?["graph"] as? String == "bottom_mic_measurement")+ }++ @Test func `a second run writes nothing`() throws {+ let url = try Self.write(Self.tuningSet())+ defer { try? FileManager.default.removeItem(at: url) }+ try Patcher.patch(at: url, verbose: false)+ let once = try Data(contentsOf: url)++ let outcome = try Patcher.patch(at: url, verbose: false)+ #expect(outcome == .alreadyMeasurementChains(["beamformed_mic_general", "bottom_mic2_general", "bottom_mic_general"]))+ #expect(try Data(contentsOf: url) == once)+ }++ @Test func `a dry run reports and leaves the file alone`() throws {+ let url = try Self.write(Self.tuningSet())+ defer { try? FileManager.default.removeItem(at: url) }+ let before = try Data(contentsOf: url)++ let outcome = try Patcher.patch(at: url, dryRun: true, verbose: false)+ #expect(outcome == .dryRun(changed: ["beamformed_mic_general", "bottom_mic2_general", "bottom_mic_general"]))+ #expect(try Data(contentsOf: url) == before)+ }++ @Test func `a set with no pairs is left alone`() throws {+ let set: [String: Any] = ["Configurations": [+ "speaker_general": Self.configuration("speaker_general"),+ "beamformed_mic_general": Self.configuration("placeholder"),+ ]]+ let url = try Self.write(set)+ defer { try? FileManager.default.removeItem(at: url) }+ let before = try Data(contentsOf: url)++ #expect(try Patcher.patch(at: url, verbose: false) == .noPairs)+ #expect(try Data(contentsOf: url) == before)+ }++ @Test(arguments: [PropertyListSerialization.PropertyListFormat.binary, .xml])+ func `the file keeps its format`(format: PropertyListSerialization.PropertyListFormat) throws {+ let url = try Self.write(Self.tuningSet(), format: format)+ defer { try? FileManager.default.removeItem(at: url) }+ try Patcher.patch(at: url, verbose: false)+ #expect(CustomFirmwareBuildVersion.detectFormat(try Data(contentsOf: url)) == format)+ }++ @Test func `a plist of another shape is refused`() throws {+ for plist in [["Configurations": "none"] as [String: Any], ["Other": [:] as [String: Any]],+ ["Configurations": ["bottom_mic_general": "x", "bottom_mic_measurement": "y"]]]+ {+ let url = try Self.write(plist)+ defer { try? FileManager.default.removeItem(at: url) }+ let before = try Data(contentsOf: url)+ #expect(throws: PatcherError.self) { try Patcher.patch(at: url, verbose: false) }+ #expect(try Data(contentsOf: url) == before)+ }+ }++ // MARK: - Gain++ static func effects(_ url: URL) throws -> [[String: Any]] {+ let strips = try #require(try read(url)["strips"] as? [[String: Any]])+ return try #require(strips.first?["effects"] as? [[String: Any]])+ }++ static func data(_ effect: [String: Any]) -> Data? {+ (effect["aupreset"] as? [String: Any])?["data"] as? Data+ }++ @Test func `the equalizer's gain goes to zero and nothing else moves`() throws {+ let url = try Self.write(Self.strip())+ defer { try? FileManager.default.removeItem(at: url) }++ let changes = try Patcher.neutralizeGain(at: url, verbose: false)+ #expect(changes == [Patcher.GainChange(effect: "AUNBEQ_2", before: 18)])++ let patched = try Self.effects(url)+ #expect(Self.data(patched[0]) == nil)+ #expect(Self.data(patched[1]) == Self.state([(0, 0), (1000, 0), (3000, 80)]))+ #expect(Self.data(patched[2]) == Self.state([(0, 0), (1000, 1), (3000, 1000)]))+ #expect(patched[2]["displayname"] as? String == "AUNBEQ_2")+ }++ @Test func `a strip with no gain is not rewritten`() throws {+ let url = try Self.write(Self.strip(secondGain: 0))+ defer { try? FileManager.default.removeItem(at: url) }+ let before = try Data(contentsOf: url)++ #expect(try Patcher.neutralizeGain(at: url, verbose: false).isEmpty)+ #expect(try Data(contentsOf: url) == before)+ }++ @Test func `removing the gain twice changes nothing the second time`() throws {+ let url = try Self.write(Self.strip())+ defer { try? FileManager.default.removeItem(at: url) }+ try Patcher.neutralizeGain(at: url, verbose: false)+ let once = try Data(contentsOf: url)++ #expect(try Patcher.neutralizeGain(at: url, verbose: false).isEmpty)+ #expect(try Data(contentsOf: url) == once)+ }++ @Test func `a dry run reports the gain and leaves the strip alone`() throws {+ let url = try Self.write(Self.strip())+ defer { try? FileManager.default.removeItem(at: url) }+ let before = try Data(contentsOf: url)++ #expect(try Patcher.neutralizeGain(at: url, dryRun: true, verbose: false).map(\.before) == [18])+ #expect(try Data(contentsOf: url) == before)+ }++ @Test func `only the global scope's parameter 0 is the gain`() throws {+ // Parameter 0 of another scope, and other parameters holding 18.+ let other = Self.state(scope: 1, [(0, 18)]) + Self.state([(4000, 18), (1000, 1)])+ #expect(try Patcher.withoutGlobalGain(other, effect: "eq", path: "strip") == nil)++ let both = Self.state(scope: 1, [(0, 18)]) + Self.state([(4000, 18), (0, -6)])+ let (patched, before) = try #require(try Patcher.withoutGlobalGain(both, effect: "eq", path: "strip"))+ #expect(before == -6)+ #expect(patched == Self.state(scope: 1, [(0, 18)]) + Self.state([(4000, 18), (0, 0)]))+ }++ @Test func `an effect that is not an equalizer keeps its data`() throws {+ var strip = Self.strip(secondGain: 0)+ let foreign = Self.state([(0, 18)])+ strip["strips"] = [["effects": [Self.effect("AUChannelSelector", subtype: Self.selector, data: foreign)]]]+ let url = try Self.write(strip)+ defer { try? FileManager.default.removeItem(at: url) }++ #expect(try Patcher.neutralizeGain(at: url, verbose: false).isEmpty)+ #expect(Self.data(try Self.effects(url)[0]) == foreign)+ }++ @Test func `parameter data that is not whole records is refused`() throws {+ let whole = Self.state([(0, 18), (1000, 1)])+ for broken in [whole.dropLast(4), whole.prefix(10), whole + Data([0, 0, 0, 0])] {+ #expect(throws: PatcherError.self) {+ try Patcher.withoutGlobalGain(Data(broken), effect: "eq", path: "strip")+ }+ }+ let url = try Self.write(["effects": []])+ defer { try? FileManager.default.removeItem(at: url) }+ #expect(throws: PatcherError.self) { try Patcher.neutralizeGain(at: url, verbose: false) }+ }+}diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneCustomFirmwareInstaller.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneCustomFirmwareInstaller.swiftindex ae821672..5a43b6a5 100644--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneCustomFirmwareInstaller.swift+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneCustomFirmwareInstaller.swift@@ -428,6 +428,11 @@ struct VPhoneCustomFirmwareInstaller {if plan?.isEnabled(FirmwareGuestSystemPatchSet.virtualAudioGraphConfigurations) ?? true {try patchVirtualAudioGraphConfigurations(system: system, work: work)}+ if plan?.isEnabled(FirmwareGuestSystemPatchSet.virtualAudioMicrophoneChains) ?? true {+ try patchVirtualAudioGraphConfigurations(+ system: system, work: work, verb: "patch-virtualaudio-microphone-chains",+ )+ }}/// Host bookkeeping in the caller's folder, written with the caller's@@ -752,6 +757,11 @@ struct VPhoneCustomFirmwareInstaller {if on(FirmwareGuestSystemPatchSet.virtualAudioGraphConfigurations) {try patchVirtualAudioGraphConfigurations(system: system, work: work)}+ if on(FirmwareGuestSystemPatchSet.virtualAudioMicrophoneChains) {+ try patchVirtualAudioGraphConfigurations(+ system: system, work: work, verb: "patch-virtualaudio-microphone-chains",+ )+ }if on("system-launchd-boot-jetsam_panic_guard_bypass") {try patchMachO(system: system,@@ -779,11 +789,13 @@ struct VPhoneCustomFirmwareInstaller {FirmwareGuestSystemPatchSet.virtioSoundDriver,FirmwareGuestSystemPatchSet.prebootBoardAudio,FirmwareGuestSystemPatchSet.prebootHaptics,+ FirmwareGuestSystemPatchSet.prebootMicrophoneArray,FirmwareGuestSystemPatchSet.virtualAudioSpeakerRouteThrows,FirmwareGuestSystemPatchSet.virtualAudioMuteSetThrow,FirmwareGuestSystemPatchSet.virtualAudioSpeakerProtectionGate,FirmwareGuestSystemPatchSet.virtualAudioVolumeModePrecondition,FirmwareGuestSystemPatchSet.virtualAudioGraphConfigurations,+ FirmwareGuestSystemPatchSet.virtualAudioMicrophoneChains,]/// `plan`, with each late guest patch it leaves out turned on when the@@ -1199,6 +1211,9 @@ struct VPhoneCustomFirmwareInstaller {if on(FirmwareGuestSystemPatchSet.prebootHaptics) {repairs.append(("patch-dt-haptics", []))}+ if on(FirmwareGuestSystemPatchSet.prebootMicrophoneArray) {+ repairs.append(("patch-dt-microphone-array", []))+ }guard rewriteIdentity || !repairs.isEmpty || !(spoofBuild ?? "").isEmpty else { return }guardlet preboot = volumes.first(where: { ($0["Roles"] as? [String])?.contains("Preboot") == true }),@@ -1316,8 +1331,8 @@ struct VPhoneCustomFirmwareInstaller {)}- /// Flip the speaker chains in every tuning set's graph_configurations.plist- /// onto the generic graph path. The plist lives under the acoustic ID the+ /// Run `verb` on every tuning set's graph_configurations.plist: by default+ /// the one that flips the speaker chains onto the generic graph path. The plist lives under the acoustic ID the/// image ships for (`Library/Audio/Tunings/<AID>/VAD/`), which varies by/// board, so the directory is walked rather than named. An image with no/// tuning sets — iOS 27's VirtualAudio reads no such plist — is left@@ -1325,6 +1340,7 @@ struct VPhoneCustomFirmwareInstaller {private func patchVirtualAudioGraphConfigurations(system: VPhoneConfinedDirectory,work: WorkDirectory,+ verb: String = "patch-virtualaudio-graph-configurations",) throws {let tunings = "Library/Audio/Tunings"guard try system.exists(tunings), try system.isDirectory(tunings) else {@@ -1335,8 +1351,18 @@ struct VPhoneCustomFirmwareInstaller {for acousticID in try system.directory(tunings).entries().sorted() {let plist = "\(tunings)/\(acousticID)/VAD/graph_configurations.plist"guard try system.isRegularFile(plist) else { continue }- try patchCopy(of: plist, in: system, work: work, verb: "patch-virtualaudio-graph-configurations")+ try patchCopy(of: plist, in: system, work: work, verb: verb)patched += 1+ guard verb == "patch-virtualaudio-microphone-chains" else { continue }+ // The strips those chains now record through, without the gain+ // of the board's own microphone.+ let strips = "\(tunings)/\(acousticID)/VAD"+ for name in try system.directory(strips).entries().sorted()+ where name.contains("_mic") && name.hasSuffix("_measurement.austrip")+ {+ guard try system.isRegularFile("\(strips)/\(name)") else { continue }+ try patchCopy(of: "\(strips)/\(name)", in: system, work: work, verb: "patch-virtualaudio-microphone-gain")+ }}if patched == 0 {print(" [·] \(tunings): no graph_configurations.plist under any tuning set, left out")diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneCustomFirmwarePatchCommand.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneCustomFirmwarePatchCommand.swiftindex bd17df93..1eeb4f3c 100644--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneCustomFirmwarePatchCommand.swift+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneCustomFirmwarePatchCommand.swift@@ -252,6 +252,70 @@ struct VPhoneCustomFirmwarePatchVirtualAudioGraphConfigurationsCommand: Parsable}}+// MARK: - patch-virtualaudio-microphone-chains++struct VPhoneCustomFirmwarePatchVirtualAudioMicrophoneChainsCommand: ParsableCommand {+ static let configuration = CommandConfiguration(+ commandName: "patch-virtualaudio-microphone-chains",+ abstract: "Record through VirtualAudio's measurement microphone chains",+ discussion: """+ graph_configurations.plist gives each built-in microphone a+ <mic>_general configuration for ordinary recording and a+ <mic>_measurement one. The general chain adds a loudness normalizer, a+ multiband compressor and a limiter tuned to the board's own+ microphone; on the Mac's microphone they raise everything by about+ 8 dB, the noise floor with it, and a recording sounds like wind.++ Every <mic>_general entry with a <mic>_measurement sibling takes the+ sibling's graph, austrip and propstrip; the rest of the entry and the+ tuning files are left alone. The plist sits on the sealed system+ volume, so the install stages a copy and this patches the copy.++ Idempotent — a re-run on an already-patched plist, or on a set with no+ such pairs, reports and exits without rewriting.+ """,+ )++ @Argument(help: "Path to graph_configurations.plist", transform: URL.init(fileURLWithPath:))+ var plist: URL++ @Flag(name: .customLong("dry-run"), help: "Report what would change and exit")+ var dryRun = false++ func run() throws {+ try CustomFirmwareVirtualAudioMicrophoneChains.patch(at: plist, dryRun: dryRun, verbose: true)+ }+}++// MARK: - patch-virtualaudio-microphone-gain++struct VPhoneCustomFirmwarePatchVirtualAudioMicrophoneGainCommand: ParsableCommand {+ static let configuration = CommandConfiguration(+ commandName: "patch-virtualaudio-microphone-gain",+ abstract: "Remove the board microphone's gain from a measurement tuning strip",+ discussion: """+ A <mic>_measurement.austrip carries the digital gain of the board's own+ microphone as the global gain of an AUNBandEQ: +18 dB on an+ iPhone17,3. The Mac's microphone arrives at the level macOS set, so+ with that gain a recording clips. Every AUNBandEQ's global gain in the+ strip is set to 0 dB; nothing else in it changes.++ Idempotent — a strip with no gain left reports and exits without+ rewriting.+ """,+ )++ @Argument(help: "Path to a <mic>_measurement.austrip", transform: URL.init(fileURLWithPath:))+ var strip: URL++ @Flag(name: .customLong("dry-run"), help: "Report what would change and exit")+ var dryRun = false++ func run() throws {+ try CustomFirmwareVirtualAudioMicrophoneChains.neutralizeGain(at: strip, dryRun: dryRun, verbose: true)+ }+}+// MARK: - patch-campo-entitlementsstruct VPhoneCustomFirmwarePatchCampoEntitlementsCommand: ParsableCommand {@@ -383,3 +447,33 @@ struct VPhoneCustomFirmwarePatchHapticsCommand: ParsableCommand {try CustomFirmwarePostRestoreDeviceTree.removeHaptics(at: deviceTree, dryRun: dryRun, verbose: true)}}++// MARK: - patch-dt-microphone-array++struct VPhoneCustomFirmwarePatchMicrophoneArrayCommand: ParsableCommand {+ static let configuration = CommandConfiguration(+ commandName: "patch-dt-microphone-array",+ abstract: "Remove the microphone array claims from a guest's device tree",+ discussion: """+ Removes supports-spatial-audio-capture and supports-audio-mix from+ /product/audio in a restored device tree, in place, preserving the+ container's compression, manifest and restore info. Both stand for a+ four-microphone array; a VM's microphone is the Mac's, one or two+ channels. With them iOS 27's Voice Memos records through the spatial+ capture route and its Audio Mix analysis, and the recording is silent.+ A tree with neither property is left as it is.++ Takes a devicetree.img4 (preferred) or a bare .im4p.+ """,+ )++ @Argument(help: "Path to devicetree.img4 or devicetree.im4p", transform: URL.init(fileURLWithPath:))+ var deviceTree: URL++ @Flag(name: .customLong("dry-run"), help: "Report what would change and exit")+ var dryRun = false++ func run() throws {+ try CustomFirmwarePostRestoreDeviceTree.removeMicrophoneArrayClaims(at: deviceTree, dryRun: dryRun, verbose: true)+ }+}diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Restore/VPhoneRestoreCommand.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Restore/VPhoneRestoreCommand.swiftindex eba742f5..49b4b487 100644--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Restore/VPhoneRestoreCommand.swift+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Restore/VPhoneRestoreCommand.swift@@ -182,10 +182,13 @@ struct VPhoneCustomFirmwareCommand: ParsableCommand {VPhoneCustomFirmwareInjectDylibCommand.self,VPhoneCustomFirmwarePatchBuildVersionCommand.self,VPhoneCustomFirmwarePatchVirtualAudioGraphConfigurationsCommand.self,+ VPhoneCustomFirmwarePatchVirtualAudioMicrophoneChainsCommand.self,+ VPhoneCustomFirmwarePatchVirtualAudioMicrophoneGainCommand.self,VPhoneCustomFirmwarePatchCampoEntitlementsCommand.self,VPhoneCustomFirmwarePatchPostRestoreDeviceTreeCommand.self,VPhoneCustomFirmwarePatchBoardAudioCommand.self,VPhoneCustomFirmwarePatchHapticsCommand.self,+ VPhoneCustomFirmwarePatchMicrophoneArrayCommand.self,] + VPhoneCustomFirmwareMachOVerbs.all + VPhoneCustomFirmwareDyldSharedCacheVerbs.all,)}diff --git a/VPhoneGuestComponents/Makefile b/VPhoneGuestComponents/Makefileindex e4580d56..fccc13bc 100644--- a/VPhoneGuestComponents/Makefile+++ b/VPhoneGuestComponents/Makefile@@ -115,12 +115,13 @@ test-injection-environment:@$(OUT)/InjectionEnvironmentTests# Host proof harness for the virtio-snd format choice, the rings, the input-# reader, the clock and the capture rate converter.+# reader, the capture's low cut, the clock and the capture rate converter.test-virtiosound:@mkdir -p $(OUT)xcrun --sdk macosx clang -O1 -Wall -Wextra -Werror -IVirtIOSound \-o $(OUT)/VirtIOSoundTests \- Tests/VirtIOSoundTests.c VirtIOSound/VPVirtIOSoundProtocol.c VirtIOSound/VPVirtIOSoundRing.c+ Tests/VirtIOSoundTests.c VirtIOSound/VPVirtIOSoundProtocol.c VirtIOSound/VPVirtIOSoundRing.c \+ VirtIOSound/VPVirtIOSoundFilter.c@$(OUT)/VirtIOSoundTestsxcrun --sdk macosx clang -O1 -Wall -Wextra -Werror -IVirtIOSound \-o $(OUT)/VirtIOSoundClockTests \diff --git a/VPhoneGuestComponents/Tests/VirtIOSoundTests.c b/VPhoneGuestComponents/Tests/VirtIOSoundTests.cindex 7fac2e8e..adbac111 100644--- a/VPhoneGuestComponents/Tests/VirtIOSoundTests.c+++ b/VPhoneGuestComponents/Tests/VirtIOSoundTests.c@@ -2,10 +2,12 @@// the format the plugin asks the device for, the rings' counters, and how// captured frames are served.+#include <math.h>#include <stdio.h>#include <stdlib.h>#include <string.h>+#include "VPVirtIOSoundFilter.h"#include "VPVirtIOSoundProtocol.h"#include "VPVirtIOSoundRing.h"@@ -428,6 +430,86 @@ static void testReaderDropsWhatTheLastRunLeft(void) {VPVirtIOSoundInputRingDestroy(&ring);}+// MARK: - High-pass++/// The gain, in decibels, the filter gives a `hertz` sine at 48 kHz on+/// channel `channel` of `channels`, measured once it has settled.+static double highPassGain(VPVirtIOSoundHighPass *filter, double hertz, uint32_t channels, uint32_t channel) {+ enum { kFrames = 48000, kPeriod = 1024 };+ static float samples[kPeriod * 2];+ double in = 0, out = 0;+ VPVirtIOSoundHighPassReset(filter);+ for (uint32_t start = 0; start < kFrames; start += kPeriod) {+ for (uint32_t frame = 0; frame < kPeriod; frame++) {+ for (uint32_t c = 0; c < channels; c++) {+ samples[frame * channels + c] = (float)(0.5 * sin(2 * M_PI * hertz * (start + frame) / 48000.0));+ }+ }+ VPVirtIOSoundHighPassProcess(filter, samples, kPeriod, channels);+ // The second half second: the transient is over by then.+ if (start < kFrames / 2) {+ continue;+ }+ for (uint32_t frame = 0; frame < kPeriod; frame++) {+ double reference = 0.5 * sin(2 * M_PI * hertz * (start + frame) / 48000.0);+ double sample = samples[frame * channels + channel];+ in += reference * reference;+ out += sample * sample;+ }+ }+ return 10 * log10(out / in);+}++static void testHighPassCutsRumbleAndKeepsSpeech(void) {+ VPVirtIOSoundHighPass filter;+ VPVirtIOSoundHighPassConfigure(&filter, 48000, 120);+ for (uint32_t channel = 0; channel < 2; channel++) {+ // Fourth order: 3 dB down at the cutoff, 24 dB an octave below it.+ CHECK(fabs(highPassGain(&filter, 120, 2, channel) + 3.01) < 0.05);+ CHECK(fabs(highPassGain(&filter, 60, 2, channel) + 24.1) < 0.3);+ CHECK(highPassGain(&filter, 30, 2, channel) < -47);+ // Flat where speech is.+ CHECK(fabs(highPassGain(&filter, 250, 2, channel)) < 0.1);+ CHECK(fabs(highPassGain(&filter, 1000, 2, channel)) < 0.01);+ CHECK(fabs(highPassGain(&filter, 8000, 2, channel)) < 0.01);+ }+ // One channel works alone.+ CHECK(fabs(highPassGain(&filter, 1000, 1, 0)) < 0.01);+ CHECK(highPassGain(&filter, 30, 1, 0) < -47);+}++static void testHighPassRemovesAnOffsetAndSurvivesBadSamples(void) {+ VPVirtIOSoundHighPass filter;+ VPVirtIOSoundHighPassConfigure(&filter, 48000, 120);+ static float samples[4800 * 2];+ for (int i = 0; i < 4800 * 2; i++) {+ samples[i] = 0.25f;+ }+ VPVirtIOSoundHighPassProcess(&filter, samples, 4800, 2);+ CHECK(fabsf(samples[4799 * 2]) < 1e-4f && fabsf(samples[4799 * 2 + 1]) < 1e-4f);++ // A sample that is not a number is taken as silence, not kept.+ float bad[8] = {NAN, INFINITY, 0, 0, 0, 0, 0, 0};+ VPVirtIOSoundHighPassReset(&filter);+ VPVirtIOSoundHighPassProcess(&filter, bad, 4, 2);+ for (int i = 0; i < 8; i++) {+ CHECK(bad[i] == 0);+ }++ // A reset forgets the past: silence in is silence out.+ VPVirtIOSoundHighPassProcess(&filter, samples, 4800, 2);+ VPVirtIOSoundHighPassReset(&filter);+ float quiet[4] = {0, 0, 0, 0};+ VPVirtIOSoundHighPassProcess(&filter, quiet, 2, 2);+ CHECK(quiet[0] == 0 && quiet[1] == 0 && quiet[2] == 0 && quiet[3] == 0);++ // A third channel is passed through untouched.+ float three[6] = {0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f};+ VPVirtIOSoundHighPassReset(&filter);+ VPVirtIOSoundHighPassProcess(&filter, three, 2, 3);+ CHECK(three[2] == 0.5f && three[5] == 0.5f && three[0] != 0.5f);+}+int main(void) {testPrefersFloat48k();testFallsBackToInteger();@@ -444,6 +526,8 @@ int main(void) {testReaderWaitsForTheLead();testReaderBoundsTheBacklog();testReaderDropsWhatTheLastRunLeft();+ testHighPassCutsRumbleAndKeepsSpeech();+ testHighPassRemovesAnOffsetAndSurvivesBadSamples();if (failures) {fprintf(stderr, "VirtIOSoundTests: %d failure(s)\n", failures);return 1;diff --git a/VPhoneGuestComponents/VCamCaptured/Microphone/VCamMicrophoneRemix.m b/VPhoneGuestComponents/VCamCaptured/Microphone/VCamMicrophoneRemix.mindex e57a423a..ad3bf1e9 100644--- a/VPhoneGuestComponents/VCamCaptured/Microphone/VCamMicrophoneRemix.m+++ b/VPhoneGuestComponents/VCamCaptured/Microphone/VCamMicrophoneRemix.m@@ -51,6 +51,7 @@ static int vcc_remix_start_hook(id self, SEL _cmd) {static IMP vcc_remix_render_orig = NULL;static unsigned vcc_level_buffers = 0;static unsigned vcc_level_zero_buffers = 0;+static uint64_t vcc_level_not_finite = 0;static uint64_t vcc_level_frames = 0;static float vcc_level_peak = 0;@@ -92,6 +93,11 @@ static void vcc_measure_level(CMSampleBufferRef sbuf) {const float *s = list->mBuffers[b].mData;size_t n = list->mBuffers[b].mDataByteSize / sizeof(float);for (size_t i = 0; s && i < n; i++) {+ // A NaN compares false with everything and would read as silence.+ if (!isfinite(s[i])) {+ vcc_level_not_finite++;+ continue;+ }float v = fabsf(s[i]);if (v > peak) peak = v;}@@ -102,12 +108,14 @@ static void vcc_measure_level(CMSampleBufferRef sbuf) {if (peak > vcc_level_peak) vcc_level_peak = peak;// About every 2 s at 48 kHz.if (vcc_level_frames >= 96000) {- vcc_log(@" remix input: %u ch, %u buffers, %llu frames, %u all zero, peak %.1f dBFS",+ vcc_log(@" remix input: %u ch, %u buffers, %llu frames, %u all zero, %llu samples not finite, peak %.1f dBFS",(unsigned)asbd->mChannelsPerFrame, vcc_level_buffers,(unsigned long long)vcc_level_frames, vcc_level_zero_buffers,+ (unsigned long long)vcc_level_not_finite,vcc_level_peak > 0 ? 20 * log10f(vcc_level_peak) : -INFINITY);vcc_level_buffers = vcc_level_zero_buffers = 0;vcc_level_frames = 0;+ vcc_level_not_finite = 0;vcc_level_peak = 0;}}diff --git a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundFilter.c b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundFilter.cnew file mode 100644index 00000000..2bc6ddf2--- /dev/null+++ b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundFilter.c@@ -0,0 +1,56 @@+// VPVirtIOSoundFilter.c — see VPVirtIOSoundFilter.h.++#include "VPVirtIOSoundFilter.h"++#include <math.h>+#include <string.h>++/// The quality factors of a fourth-order Butterworth's two sections:+/// 1 / (2 cos(pi/8)) and 1 / (2 cos(3 pi/8)).+static const double kSectionQ[kVPVirtIOSoundHighPassSections] = {0.54119610014619698, 1.3065629648763766};++void VPVirtIOSoundHighPassConfigure(VPVirtIOSoundHighPass *filter, double sampleRate, double cutoff) {+ double omega = 2 * M_PI * cutoff / sampleRate;+ double cosine = cos(omega);+ for (int section = 0; section < kVPVirtIOSoundHighPassSections; section++) {+ double alpha = sin(omega) / (2 * kSectionQ[section]);+ double a0 = 1 + alpha;+ double *c = filter->coefficients[section];+ c[0] = (1 + cosine) / 2 / a0;+ c[1] = -(1 + cosine) / a0;+ c[2] = (1 + cosine) / 2 / a0;+ c[3] = -2 * cosine / a0;+ c[4] = (1 - alpha) / a0;+ }+ VPVirtIOSoundHighPassReset(filter);+}++void VPVirtIOSoundHighPassReset(VPVirtIOSoundHighPass *filter) {+ memset(filter->state, 0, sizeof(filter->state));+}++void VPVirtIOSoundHighPassProcess(VPVirtIOSoundHighPass *filter, float *samples, uint32_t frames,+ uint32_t channels) {+ uint32_t filtered = channels < kVPVirtIOSoundHighPassChannels ? channels : kVPVirtIOSoundHighPassChannels;+ for (uint32_t channel = 0; channel < filtered; channel++) {+ for (int section = 0; section < kVPVirtIOSoundHighPassSections; section++) {+ const double *c = filter->coefficients[section];+ double z1 = filter->state[channel][section][0];+ double z2 = filter->state[channel][section][1];+ for (uint32_t frame = 0; frame < frames; frame++) {+ float *sample = &samples[(size_t)frame * channels + channel];+ double input = *sample;+ // What is not a number would stay in the state for good.+ if (!isfinite(input)) {+ input = 0;+ }+ double output = c[0] * input + z1;+ z1 = c[1] * input - c[3] * output + z2;+ z2 = c[2] * input - c[4] * output;+ *sample = (float)output;+ }+ filter->state[channel][section][0] = z1;+ filter->state[channel][section][1] = z2;+ }+ }+}diff --git a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundFilter.h b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundFilter.hnew file mode 100644index 00000000..fdc7cbed--- /dev/null+++ b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundFilter.h@@ -0,0 +1,42 @@+// VPVirtIOSoundFilter.h — the low cut on what the host's microphone captures.+//+// A phone's microphone does not pick up much below 100 Hz, and the tunings+// iOS records through are built on that: the D47 set's only high-pass is at+// 30 Hz. The Mac's built-in microphone, taken as Virtualization hands it+// over, carries the machine's own rumble instead. Measured in the pauses of a+// capture: -55 to -57 dBFS in each of the bands from 40 to 150 Hz, against+// -67 dBFS and lower above 250 Hz — a recording that sounds like wind.+//+// The filter is a fourth-order Butterworth high-pass, two biquads in+// transposed direct form II, run over each captured period before the HAL+// reads it. It is not real-time code: the input stream calls it from its+// completion queue.++#ifndef VPVirtIOSoundFilter_h+#define VPVirtIOSoundFilter_h++#include <stdint.h>++enum {+ kVPVirtIOSoundHighPassSections = 2,+ kVPVirtIOSoundHighPassChannels = 2,+};++typedef struct {+ /// Per section: b0, b1, b2, a1, a2, with a0 normalized to 1.+ double coefficients[kVPVirtIOSoundHighPassSections][5];+ double state[kVPVirtIOSoundHighPassChannels][kVPVirtIOSoundHighPassSections][2];+} VPVirtIOSoundHighPass;++/// Set the filter up for `cutoff` Hz at `sampleRate`, with its state clear.+void VPVirtIOSoundHighPassConfigure(VPVirtIOSoundHighPass *filter, double sampleRate, double cutoff);++/// Forget what the filter has seen, for a run that starts on new audio.+void VPVirtIOSoundHighPassReset(VPVirtIOSoundHighPass *filter);++/// Filter `frames` interleaved Float32 frames of `channels` channels in+/// place. Channels past `kVPVirtIOSoundHighPassChannels` are left as they are.+void VPVirtIOSoundHighPassProcess(VPVirtIOSoundHighPass *filter, float *samples, uint32_t frames,+ uint32_t channels);++#endif /* VPVirtIOSoundFilter_h */diff --git a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.m b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.mindex 99fcbc75..1a848ae5 100644--- a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.m+++ b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.m@@ -52,6 +52,7 @@#include "VPVirtIOSoundClock.h"#include "VPVirtIOSoundConverter.h"+#include "VPVirtIOSoundFilter.h"#include "VPVirtIOSoundProtocol.h"#include "VPVirtIOSoundRing.h"@@ -80,6 +81,14 @@ static const uint32_t kInputMaximumBacklogPeriods = 4;/// How long a stopped input stream waits for its reads to come back before/// it releases the device with them still out.static const uint64_t kInputDrainNanoseconds = NSEC_PER_SEC;+/// A start that finds the trigger file also writes what it captures, as the+/// host sent it, to the path beside it, up to the limit. For comparing the+/// capture with what a recording app makes of it.+static const char *const kInputDumpTrigger = "/var/mobile/vpinput.dump";+static const char *const kInputDumpPath = "/var/mobile/vpinput.raw";+static const uint64_t kInputDumpMaximumBytes = 16 * 1024 * 1024;+/// Where the capture's low cut sits. See VPVirtIOSoundFilter.h.+static const double kInputHighPassHertz = 120;/// The speaker volume control's range. VirtualAudio maps the guest's volume/// onto it in a straight line, so the bottom decides how loud half volume@@ -1268,6 +1277,17 @@ static void VPCaptureRead(VPCaptureState *capture, void *buffer, UInt32 frameCouBOOL _loggedFirstRead;/// Reads the device returned since CoreAudio last started the stream.uint64_t _completions;+ /// The level of what those reads held, per channel for the first two:+ /// the largest sample and the sum of squares over `_measuredFrames`.+ float _peak[2];+ double _squares[2];+ uint64_t _measuredFrames;+ /// Where the captured periods are also written, when+ /// `kInputDumpTrigger` exists at a start. Diagnostic only.+ FILE *_dump;+ uint64_t _dumpedBytes;+ /// The low cut each captured period goes through before the HAL reads it.+ VPVirtIOSoundHighPass _highPass;/// The device's mute control. Its volume is not applied: what the host/// captures is already at the level the Mac's input is set to._Atomic bool _muted;@@ -1342,6 +1362,9 @@ static void VPCaptureRead(VPCaptureState *capture, void *buffer, UInt32 frameCouatomic_store(&_capture.halRate, (uint32_t)kVPAlternateRate);}}+ // The low cut runs on each captured period in the ring, at the wire rate,+ // before the converter above reads it.+ VPVirtIOSoundHighPassConfigure(&_highPass, format.sampleRate, kInputHighPassHertz);[self installReadBlock];[self logFormatWithLeadPeriods:kInputLeadPeriods];return self;@@ -1416,6 +1439,54 @@ static void VPCaptureRead(VPCaptureState *capture, void *buffer, UInt32 frameCou(unsigned long long)framesOut, seconds > 0 ? framesOut / seconds : 0,(unsigned long long)atomic_load(&_reader.silentFrames),(unsigned long long)atomic_load(&_reader.skippedBytes));+ if (_measuredFrames > 0) {+ double level[2] = {0, 0};+ double peak[2] = {0, 0};+ for (unsigned channel = 0; channel < 2; channel++) {+ level[channel] = 10 * log10(MAX(_squares[channel] / (double)_measuredFrames, 1e-12));+ peak[channel] = 20 * log10(MAX((double)_peak[channel], 1e-6));+ }+ VPLogToFile("stream %u: captured level %.1f / %.1f dBFS RMS, peak %.1f / %.1f dBFS",+ _streamID, level[0], level[1], peak[0], peak[1]);+ }+ if (_dump) {+ fclose(_dump);+ _dump = NULL;+ VPLogToFile("stream %u: wrote %llu captured bytes to %s",+ _streamID, (unsigned long long)_dumpedBytes, kInputDumpPath);+ }+}++/// Adds one returned period to the level counters, and to the dump when+/// there is one, then runs it through the low cut. Float samples only, which+/// is what the host sends.+- (void)measurePeriodAtOffset:(uint32_t)offset {+ if (!_format.isFloat || _format.bitsPerChannel != 32 || _format.channels == 0) {+ return;+ }+ const float *samples = (const float *)(_ring.bytes + offset);+ uint32_t channels = _format.channels;+ uint32_t frames = _periodBytes / _format.bytesPerFrame;+ for (uint32_t frame = 0; frame < frames; frame++) {+ for (uint32_t channel = 0; channel < MIN(channels, 2u); channel++) {+ float sample = samples[frame * channels + channel];+ if (!isfinite(sample)) {+ continue;+ }+ _peak[channel] = MAX(_peak[channel], fabsf(sample));+ _squares[channel] += (double)sample * sample;+ }+ }+ if (channels == 1) {+ _peak[1] = _peak[0];+ _squares[1] = _squares[0];+ }+ _measuredFrames += frames;+ if (_dump && _dumpedBytes < kInputDumpMaximumBytes) {+ _dumpedBytes += fwrite(samples, 1, _periodBytes, _dump);+ }+ // Measured and dumped as the host sent it; the HAL reads it filtered.+ VPVirtIOSoundHighPassProcess(&_highPass, (float *)(_ring.bytes + offset), frames, channels);}// MARK: Transfers@@ -1482,6 +1553,8 @@ static void VPCaptureRead(VPCaptureState *capture, void *buffer, UInt32 frameCou// Whatever the slot holds is not this period's capture.memset(_ring.bytes + offset, 0, _periodBytes);[self reportTransferError:result length:_periodBytes];+ } else if (_state == VPStreamStateRunning) {+ [self measurePeriodAtOffset:offset];}VPVirtIOSoundInputRingDidComplete(&_ring);_completions++;@@ -1518,6 +1591,14 @@ static void VPCaptureRead(VPCaptureState *capture, void *buffer, UInt32 frameCou- (void)startStream {dispatch_sync(_queue, ^{self->_completions = 0;+ memset(self->_peak, 0, sizeof(self->_peak));+ memset(self->_squares, 0, sizeof(self->_squares));+ self->_measuredFrames = 0;+ self->_dumpedBytes = 0;+ VPVirtIOSoundHighPassReset(&self->_highPass);+ if (!self->_dump && access(kInputDumpTrigger, F_OK) == 0) {+ self->_dump = fopen(kInputDumpPath, "w");+ }atomic_store(&self->_reader.servedFrames, 0);atomic_store(&self->_reader.silentFrames, 0);atomic_store(&self->_reader.skippedBytes, 0);