Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 46 additions & 15 deletions asahi_firmware/audio_tuning.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@
LATTICE = "lsm2-mask-lattice.bin"
# The BuzzKill detector's quadrature matrix (buzzkill_matrix), as its 80 bytes.
BUZZKILL = "buzzkill-quadrature.bin"
BUZZKILL_KEY = "buzzkill_quadrature"
# Whether a new bundle carries that matrix. speakerguardd v2 reads a schema-2
# manifest strictly: a key it does not know refuses the whole bundle, and the
# speakers stay at the kernel's floor. No speakerguardd release reads
# `buzzkill_quadrature` yet (speakerguardd#138 adds the reader); every one up
# to 1.0.0alpha.1-42 refuses a bundle that has it. So, until a release reads
# it, the matrix is collected only on request (rigs, the parity gate), as the
# native installer does. A bundle that already carries it is still verified
# and read; `without_buzzkill` gives the same bundle without it.
COLLECT_BUZZKILL = False
# Where it is read: the AudioDSP component of the running macOS.
AUDIODSP_PATH = "/" + lsm2_lattice.AUDIODSP

Expand Down Expand Up @@ -422,10 +432,27 @@ def manifest(aid, acoustic, files, spts, build, selection=None, speaker=None,
# present when it was read; without it the voicing compiler refuses
# Buzz boxes.
if buzzkill is not None:
doc["buzzkill_quadrature"] = buzzkill_record(buzzkill)
doc[BUZZKILL_KEY] = buzzkill_record(buzzkill)
return doc


def encode_manifest(doc):
return json.dumps(doc, indent=2, sort_keys=True).encode("utf-8")


def without_buzzkill(bundle):
"""The bundle as the collector writes it without the BuzzKill matrix:
no matrix file and no `buzzkill_quadrature` record; every other file and
manifest entry unchanged. A bundle without the matrix is returned as is."""
doc = json.loads(bundle[MANIFEST])
if BUZZKILL not in bundle and BUZZKILL_KEY not in doc:
return bundle
doc.pop(BUZZKILL_KEY, None)
out = {rel: data for rel, data in bundle.items() if rel != BUZZKILL}
out[MANIFEST] = encode_manifest(doc)
return out


def tree(acoustic, files, spts, build, selection=None, source_count=None, speaker=None,
shared=None, loudness=None, source=DATABASE, lattice=None, board=None, unit=None,
fill=None, buzzkill=None):
Expand Down Expand Up @@ -488,10 +515,9 @@ def tree(acoustic, files, spts, build, selection=None, source_count=None, speake
out[lsm2_fill.NAME] = fill
if buzzkill is not None:
out[BUZZKILL] = bytes(buzzkill)
out[MANIFEST] = json.dumps(manifest(aid, acoustic, files, spts, build, selection, speaker,
shared, loudness, source, bytes(lattice), board, unit, fill,
None if buzzkill is None else bytes(buzzkill)),
indent=2, sort_keys=True).encode("utf-8")
out[MANIFEST] = encode_manifest(manifest(aid, acoustic, files, spts, build, selection, speaker,
shared, loudness, source, bytes(lattice), board, unit,
fill, None if buzzkill is None else bytes(buzzkill)))
return out


Expand Down Expand Up @@ -588,11 +614,14 @@ class AudioTuningFWCollection(object):
extracted fallback archive (path = the directory holding audio-tuning/)."""

def __init__(self, path=None, tunings_root=TUNINGS_ROOT, run=subprocess.run, audiodsp=None,
offline=None, force_lsm2_fill=False):
offline=None, force_lsm2_fill=False, buzzkill=None):
"""offline: an OfflineMac, to collect from a mounted macOS system
volume and the Mac's ADT (Linux) instead of the running macOS."""
volume and the Mac's ADT (Linux) instead of the running macOS.
buzzkill: also collect the BuzzKill quadrature matrix (default
COLLECT_BUZZKILL); a re-collection (path) keeps what the bundle has."""
self.fwfiles = []
self.bundle = {}
self.collect_buzzkill = COLLECT_BUZZKILL if buzzkill is None else bool(buzzkill)
try:
if offline is not None:
self.load(offline.tunings_root, None, offline.audiodsp, offline)
Expand Down Expand Up @@ -684,13 +713,15 @@ def load(self, tunings_root, run, audiodsp=AUDIODSP_PATH, offline=None, force_ls
except Exception as e:
log.warning(f"lsm2 fill not collected: {e}; short presets remain muted; "
"collect again on this Mac's running macOS")
# The BuzzKill matrix the same way; without one the bundle still
# serves every Mac whose graph has no Buzz box.
try:
buzzkill = buzzkill_matrix.read(audiodsp)
except ValueError as e:
log.warning(f"Speaker tuning collected without the BuzzKill quadrature matrix: {e}")
buzzkill = None
# The BuzzKill matrix the same way, when asked (COLLECT_BUZZKILL);
# without one the bundle still serves every Mac whose graph has no
# Buzz box.
buzzkill = None
if getattr(self, "collect_buzzkill", COLLECT_BUZZKILL):
try:
buzzkill = buzzkill_matrix.read(audiodsp)
except ValueError as e:
log.warning(f"Speaker tuning collected without the BuzzKill quadrature matrix: {e}")
self._publish(tree(acoustic, files, props[1], build, selection, count, speaker,
shared, loudness, source, lattice, board, unit, fill, buzzkill))
log.info(f" Collected speaker tuning AID{acoustic} ({len(files)} files"
Expand Down Expand Up @@ -797,7 +828,7 @@ def update(self, path):
or not unit or fill.get("unit_id") != unit):
raise ValueError("malformed lsm2 fill record")
records = records + [fill]
buzzkill = meta.get("buzzkill_quadrature")
buzzkill = meta.get(BUZZKILL_KEY)
if buzzkill is not None:
if not isinstance(buzzkill, dict) or buzzkill.get("path") != BUZZKILL \
or buzzkill.get("version") != buzzkill_matrix.VERSION \
Expand Down
39 changes: 35 additions & 4 deletions tests/test_audio_tuning.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,14 +728,45 @@ def test_a_bundle_collected_before_the_lattice_still_verifies_with_a_warning(sel


class BuzzKillMatrixTest(unittest.TestCase):
"""The BuzzKill quadrature matrix: carried when the component has one,
otherwise the bundle is still written without it."""
"""The BuzzKill quadrature matrix: collected on request and carried when
the component has one, otherwise the bundle is still written without
it. Not collected by default until a speakerguardd release reads it."""

def collect_with(self, root, component):
def collect_with(self, root, component, buzzkill=True):
path = root / "component"
path.write_bytes(component)
return AudioTuningFWCollection(tunings_root=str(root), run=fake_run(AID35),
audiodsp=str(path))
audiodsp=str(path), buzzkill=buzzkill)

def test_not_collected_by_default(self):
# No released speakerguardd reads the record, and every one refuses
# a manifest that has it (speakerguardd#138).
self.assertFalse(audio_tuning.COLLECT_BUZZKILL)
with tempfile.TemporaryDirectory() as tmp:
root = pathlib.Path(tmp)
tunings(root)
with self.assertNoLogs("asahi_firmware.audio_tuning", "WARNING"):
col = self.collect_with(root, self.component(), buzzkill=None)
self.assertTrue(col.bundle)
self.assertNotIn("buzzkill-quadrature.bin", col.bundle)
self.assertNotIn("buzzkill_quadrature", json.loads(col.bundle["manifest.json"]))

def test_without_buzzkill_is_the_bundle_collected_without_it(self):
with tempfile.TemporaryDirectory() as tmp:
root = pathlib.Path(tmp)
tunings(root)
carried = self.collect_with(root, self.component()).bundle
plain = self.collect_with(root, self.component(), buzzkill=False).bundle
self.assertIn("buzzkill-quadrature.bin", carried)
self.assertEqual(audio_tuning.without_buzzkill(carried), plain)
self.assertIs(audio_tuning.without_buzzkill(plain), plain)
# Still a complete bundle that reads back unchanged.
out = pathlib.Path(tmp) / "out"
col = AudioTuningFWCollection.__new__(AudioTuningFWCollection)
col.fwfiles, col.bundle = [], {}
col._publish(audio_tuning.without_buzzkill(carried))
self.assertTrue(col.write_fallback(str(out)))
self.assertEqual(AudioTuningFWCollection(str(out)).bundle, plain)

def component(self, matrix_const=None):
from test_buzzkill_matrix import const
Expand Down
43 changes: 40 additions & 3 deletions tests/test_speaker_tuning_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,19 @@ def tunings(root, acoustic=35, salt=b""):
(root / "DeviceDSPDatabase.plist").write_bytes(plistlib.dumps(db))


def bundle_dir(base, acoustic=35, salt=b"", spts=bytes(range(16)), root=None):
"""A verified audio-tuning/ under base; returns base."""
def bundle_dir(base, acoustic=35, salt=b"", spts=bytes(range(16)), root=None, buzzkill=False):
"""A verified audio-tuning/ under base; returns base. buzzkill: collect
from a component that also has a BuzzKill matrix, with the matrix."""
src = base / "tunings"
tunings(src, acoustic, salt)
col = AudioTuningFWCollection(tunings_root=str(src), run=fake_run(acoustic, spts, root=root))
audiodsp = None
if buzzkill:
from test_buzzkill_matrix import const
audiodsp = base / "AudioDSP-buzzkill"
audiodsp.write_bytes(thin(padded(synthetic_lattice()) + const()))
col = AudioTuningFWCollection(tunings_root=str(src), run=fake_run(acoustic, spts, root=root),
audiodsp=None if audiodsp is None else str(audiodsp),
buzzkill=buzzkill)
assert col.bundle
out = base / "bundle"
out.mkdir()
Expand Down Expand Up @@ -540,6 +548,35 @@ def test_newer_bundle_replaces_the_old_one(self):
with st.opened_bundle(src) as (bundle, _):
self.assertEqual(raw, bundle)

def test_a_bundle_with_the_buzzkill_matrix_installs_without_it(self):
# A collection made while the collector wrote the matrix by default:
# no released speakerguardd reads it, and every one refuses it.
src = bundle_dir(self.tmp, buzzkill=True)
with st.opened_bundle(src) as (bundle, _):
self.assertIn(audio_tuning.BUZZKILL, bundle)
plain = audio_tuning.without_buzzkill(bundle)
result, text = self.install(src)
self.assertEqual(result, "installed", text)
self.assertIn("installed without it", text)
raw, _ = st.raw_tuning(self.esp / "asahi" / "all_firmware.tar.gz")
self.assertEqual(raw, plain)
manifest = st.parse_manifest((self.esp / "vendorfw" / "manifest.txt").read_text())
self.assertNotIn("apple/audio-tuning/" + audio_tuning.BUZZKILL, manifest)
self.assertNotIn(audio_tuning.BUZZKILL_KEY,
json.loads(raw[audio_tuning.MANIFEST]))
result, text = self.install(src)
self.assertEqual(result, "identical", text)

def test_keep_buzzkill_matrix_installs_it(self):
src = bundle_dir(self.tmp, buzzkill=True)
result, text = self.install(src, buzzkill=True)
self.assertEqual(result, "installed", text)
self.assertNotIn("installed without it", text)
raw, _ = st.raw_tuning(self.esp / "asahi" / "all_firmware.tar.gz")
with st.opened_bundle(src) as (bundle, _):
self.assertEqual(raw, bundle)
self.assertIn(audio_tuning.BUZZKILL, raw)

def test_verify_running_bundle(self):
src = bundle_dir(self.tmp)
self.install(src)
Expand Down
11 changes: 11 additions & 0 deletions tools/speaker-tuning/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ component (see
If it does not find exactly one valid table, it writes nothing and exits
with an error.

The BuzzKill detector's quadrature matrix (also in AudioDSP) is collected
only with `--buzzkill-matrix`, for rigs. No speakerguardd release reads it
yet, and every release up to 1.0.0alpha.1-42 refuses a bundle that carries
it (`unknown field buzzkill_quadrature`), which leaves the speakers at the
kernel's floor. It becomes the default once a release reads it
(speakerguardd#138).

It reads both tuning layouts: the shared `DeviceDSPDatabase.plist` up to
macOS 26, and the per-Mac `AID<n>/DSP/DSPDatabase.plist` of macOS 27, and
prints which one it used.
Expand Down Expand Up @@ -128,6 +135,10 @@ since boot (it should be 0).
- A bundle collected before the collector read the lsm2 masker lattice still
installs; the Neo's lsm2 protection then gives no masking credit. Collect
it again to add the lattice.
- A tarball collected between 2026-11-13 and this change carries the BuzzKill
matrix by default. `install-linux.sh` installs it without the matrix
(it says so), so speakerguardd accepts it; `--keep-buzzkill-matrix` keeps
it for a speakerguardd that reads it.
- Collect again after a macOS update that changes the tuning. A newer bundle
for the same AID replaces the old one.
- The tarball and the backups hold this Mac's own calibration. Keep them on
Expand Down
Loading
Loading