From 0f547a8c5a373800c56df7b3efb995293207784a Mon Sep 17 00:00:00 2001 From: Xenolphthalein <21215869+Xenolphthalein@users.noreply.github.com> Date: Thu, 3 Dec 2026 14:26:05 +0100 Subject: [PATCH] feat(pico4-audio): the speakers play through PICO OS's SAMI chain, as with its Sound Optimization on PICO OS's HAL plays the speakers through its SAMI sound device by default: an ACDB device (10002, or 10003 for audio type S2 in persist) whose topology runs PICO's SAMI effect module on the DSP with the stock sami_audio_config.json (loudness EQ, equaliser, compressors, limiter). pico4-audiocal now exports that device for the speaker path, picked from persist's picoconfig/audiotype as the HAL picks it; --plain gives the plain speaker device (15), PICO OS's path with the setting off. For overpressure/phoenix#184 --- phoenix/pico4-audio/PKGBUILD | 11 ++++--- phoenix/pico4-audio/pico4-audiocal | 46 ++++++++++++++++++++++------ phoenix/pico4-audio/test_audiocal.py | 46 +++++++++++++++++++++++++--- 3 files changed, 85 insertions(+), 18 deletions(-) diff --git a/phoenix/pico4-audio/PKGBUILD b/phoenix/pico4-audio/PKGBUILD index aaa851c..f2ce7c0 100644 --- a/phoenix/pico4-audio/PKGBUILD +++ b/phoenix/pico4-audio/PKGBUILD @@ -1,9 +1,10 @@ # The PICO 4's sound as on PICO OS: the audio DSP and its audio process's file server, the LPASS macros, # the sound card's UCM routes, PipeWire's and WirePlumber's settings for it, the calibration tool -# (pico4-audiocal; the image's build exports PICO OS's calibration with it) and the microphone muted while -# the headset is off the head. The preset enables the audio target; the tests run in the build. +# (pico4-audiocal, which exports PICO OS's calibration at boot, the speakers through its SAMI chain) and +# the microphone muted while the headset is off the head. The preset enables the audio target; the tests +# run in the build. pkgname=pico4-audio -pkgver=2026.11.25 +pkgver=2026.12.3 pkgrel=1 pkgdesc="PICO 4 audio: the ADSP, its audio process, the sound card's routes and calibration, the microphone mute" arch=(any) @@ -30,7 +31,7 @@ source=(51-pico4-quantum.conf test_audiocal.py test_micmute.py) sha256sums=('5789e02ea04b1a1100faabd0805891703a8916d00be1ff90f428c16db831308f' 'f1e3ca483f05861a826224860af29c377c099d85ed063cd942b9f868a771a5e9' - '4f19c286fe0abef812d62b9baf8a89e1cfe44f61dd466dcc73658c59cec790af' + '2b4a8347960f6d5713795c8faaaeae78838a4217e7f2e590b0d257cf3f1ce8b9' '5ae539517c9006af62315ec85fc8a3b4b0e498e18d6831d14c6f5de358f9ecc7' '7d0be6b99955351a499e595e33542a48bc19f22c73253df42de3881a0986c88d' '6cce0452029f0a97d04b3f9e4a7027514dd7b21ced5df6d8fcb784cbf3780f29' @@ -43,7 +44,7 @@ sha256sums=('5789e02ea04b1a1100faabd0805891703a8916d00be1ff90f428c16db831308f' '6e73ec6b6f791a857336992b81c95eaafdc8b221f64eb3f8e090c6bd2bb39c3f' 'e6cce0d239fc1389d007aecb5a00d121076deb72963a79149edf05a009c9c38f' '939fd8440eaede54fdbd64642493befc72d6d64cd500834912f2a56aec116b2f' - '4c3c9ad66225e3167338ec20b6a2a0a1e22462e6f39651abc45c83ed40678330' + 'ecba7a9a64a70f6e0cb81f9e537175441ed903dce6ebe46cc55c50953f534b89' '179f021b124a78032d57233419710aafd98bbc313e36a2e57599c51be0f05766') build() { diff --git a/phoenix/pico4-audio/pico4-audiocal b/phoenix/pico4-audio/pico4-audiocal index caaec6f..b19035d 100644 --- a/phoenix/pico4-audio/pico4-audiocal +++ b/phoenix/pico4-audio/pico4-audiocal @@ -4,11 +4,17 @@ PICO OS's audio HAL reads the calibration database (vendor acdbdata, from the user's stock firmware at build time) and hands the blocks for each sound device to its kernel. This tool -reads the same blocks for the two paths the system uses: the speakers (deep-buffer playback, -acdb device 15) and the handset microphone (record, acdb device 4). +reads the same blocks for the two paths the system uses: the speakers (deep-buffer playback) and +the handset microphone (record, acdb device 4). - pico4-audiocal show [--acdb DIR] the topologies and blocks, as JSON - pico4-audiocal export [--acdb DIR] [FILE] the file the driver loads (pico4-adsp.service, at boot) +The speakers play as on PICO OS with its "Sound Optimization" on, its default: through the HAL's SAMI +sound device, whose topology runs PICO's SAMI effect chain on the DSP (sami_audio_config.json, from the +stock files). Its ACDB device follows the unit's audio type in persist, as the HAL picks it. --plain gives +the plain speaker device (15), PICO OS's path with the setting off. + + pico4-audiocal show [--acdb DIR] [--audiotype FILE] [--plain] the topologies and blocks, as JSON + pico4-audiocal export [--acdb DIR] [--audiotype FILE] [--plain] [FILE] the file the driver loads + (pico4-adsp.service, at boot) """ from __future__ import annotations @@ -21,6 +27,11 @@ from pathlib import Path from typing import Any ACDB_DIR = "/usr/lib/pico4-audio/acdb" +#: The unit's audio type, which PICO OS copies into ro.pxr.config.audiotype (pvr.prop.default.sh). +AUDIOTYPE = "/mnt/vendor/persist/picoconfig/audiotype" +#: The HAL's SAMI sound device per audio type; its default "S0", and anything but S1 and S2, keep 10002. +SAMI_DEVICES = {"S1": 10002, "S2": 10003} +SAMI_DEFAULT = 10002 # ACDB property and table identifiers PICO OS's ACDB library looks up. PROP_ADM_TOPOLOGY = 0x113AD # device: {app type, topology} pairs @@ -164,8 +175,17 @@ def pair_value(data: bytes | None, key: int) -> int | None: return None -def lookup(acdb_dir: str) -> dict: - """Return everything the two paths need from the database.""" +def sami_device(audiotype: str | Path = AUDIOTYPE) -> int: + """Return the speakers' SAMI device for the unit's audio type, as PICO OS's HAL picks it.""" + try: + kind = Path(audiotype).read_text(encoding="ascii", errors="replace").strip() + except FileNotFoundError: + kind = "" + return SAMI_DEVICES.get(kind, SAMI_DEFAULT) + + +def lookup(acdb_dir: str, speaker: int | None = None) -> dict: + """Return everything the two paths need from the database; @p speaker replaces the speakers' device.""" files = [AcdbFile(p) for p in sorted(Path(acdb_dir).glob("*.acdb"))] shared = [f for f in files if f.kind == "GCDB"] if not shared: @@ -180,7 +200,8 @@ def lookup(acdb_dir: str) -> dict: msg = "no custom topologies in the global file" raise ValueError(msg) result: dict[str, Any] = {"custom_topologies": custom, "paths": []} - for p in PATHS: + for path in PATHS: + p = {**path, "acdb_id": speaker} if path["name"] == "speaker" and speaker is not None else path owner = next((f for f in files if f.kind == "AVDB" and p["acdb_id"] in f.devices()), None) if owner is None: msg = f"acdb device {p['acdb_id']} ({p['name']}) is in no file" @@ -246,9 +267,14 @@ def mainline_file(cal: dict) -> bytes: return out +def speaker_device(args: argparse.Namespace) -> int | None: + """Return the speakers' device: the SAMI device, or None for the plain one with --plain.""" + return None if args.plain else sami_device(args.audiotype) + + def cmd_export(args: argparse.Namespace) -> int: """Write the kernel's file.""" - data = mainline_file(lookup(args.acdb)) + data = mainline_file(lookup(args.acdb, speaker_device(args))) output = Path(args.output) output.parent.mkdir(parents=True, exist_ok=True) output.write_bytes(data) @@ -262,11 +288,13 @@ def main(argv: list[str] | None = None) -> int: for name in ("show", "export"): p = sub.add_parser(name) p.add_argument("--acdb", default=ACDB_DIR, help="directory of the .acdb files") + p.add_argument("--audiotype", default=AUDIOTYPE, help="the unit's audio type (persist)") + p.add_argument("--plain", action="store_true", help="the plain speaker device, without PICO's processing") if name == "export": p.add_argument("output", nargs="?", default=MAINLINE_FILE, help="the kernel's file") args = parser.parse_args(argv) if args.command == "show": - print(json.dumps(describe(lookup(args.acdb)), indent=2)) + print(json.dumps(describe(lookup(args.acdb, speaker_device(args))), indent=2)) return 0 return cmd_export(args) diff --git a/phoenix/pico4-audio/test_audiocal.py b/phoenix/pico4-audio/test_audiocal.py index b47ee49..9bc45ac 100644 --- a/phoenix/pico4-audio/test_audiocal.py +++ b/phoenix/pico4-audio/test_audiocal.py @@ -74,20 +74,29 @@ def write_fake_acdb(directory: Path) -> None: pool = Pool() rows = [] - for dev, app, topo, afe in ((15, 0x11130, 0x11000003, 0x112FC), (4, 0x11132, 0x10010315, 0x112FB)): + devices = ( + (15, 0x11130, 0x11000003, 0x112FC), + (10003, 0x11130, 0x20000001, 0x112FC), + (4, 0x11132, 0x10010315, 0x112FB), + ) + for dev, app, topo, afe in devices: rows.append([dev, 0x113AD, pool.add(u32(0x99999, 1, app, topo))]) rows.append([dev, 0x13150, pool.add(u32(afe))]) # Audproc of the speaker: two parameters, one of 3 bytes (padded to 4); of the mic: one. - p1, p2, p3 = pool.add(b"\x01\x02\x03"), pool.add(u32(7, 8)), pool.add(u32(9)) + # Of the SAMI device: its enable parameter, and no volume table. + p1, p2, p3, p4 = pool.add(b"\x01\x02\x03"), pool.add(u32(7, 8)), pool.add(u32(9)), pool.add(u32(1)) vol = pool.add(u32(5)) ids = u32(2, 0x10C2A, 1, 0x10C2B, 0x10C35, 0, 0x10C36) + u32(1, 0x10719, 0, 0x10911) - offsets = u32(2, p1, p2) + u32(1, p3) + ids += u32(1, 0x11111800, 0, 0x11111801) + offsets = u32(2, p1, p2) + u32(1, p3) + u32(1, p4) (directory / "Device_cal.acdb").write_bytes( database( "AVDB", { "DPROPLUT": table(rows), - "AGIILUT0": table([[15, 48000, 0x11130, 0, 0], [4, 48000, 0x11132, 28, 12]]), + "AGIILUT0": table( + [[15, 48000, 0x11130, 0, 0], [4, 48000, 0x11132, 28, 12], [10003, 48000, 0x11130, 44, 20]] + ), "AGIICDFT": ids, "AGIICDOT": offsets, "AVIILUT0": table([[15, 0x11130, 0, 0, 0]]), @@ -160,6 +169,35 @@ def test_missing_device_is_an_error(self): with self.assertRaises(ValueError): audiocal.lookup(str(self.dir)) + def test_sami_device(self): + # PICO OS's speakers with "Sound Optimization" on: the SAMI device's topology and enable parameter. + speaker, _mic = audiocal.lookup(str(self.dir), 10003)["paths"] + self.assertEqual( + (speaker["acdb_id"], speaker["adm_topology"], speaker["afe_topology"]), (10003, 0x20000001, 0x112FC) + ) + self.assertEqual(speaker["audproc"], struct.pack("