Files
beefy-player/tests/test_macos_tap.py
T
beefcarver 48e480d58c feat(spectrum): capture macOS output with a Core Audio process tap
SYNC mode on macOS needed a virtual loopback driver (BlackHole, Loopback,
Soundflower) routed through a Multi-Output Device, and shelled out to
'ffmpeg -f avfoundation' to read it. macOS 14.2 lets a process tap the
system mix directly, so do that instead: CATapDescription ->
AudioHardwareCreateProcessTap -> private aggregate device -> IOProc,
straight through ctypes. No driver, no helper binary, no ffmpeg.

The tap hands out native-rate interleaved float32, so the analyser can
now retune its window and band edges when the capture rate differs from
44.1 kHz, and mono-downmixes the frames before the FFT.

A tap that was never granted System Audio Recording permission is
created happily but only ever delivers digital silence: treat that as a
failed capture instead of flat bars, explain it in the status bar and
fall back to the stream tap. A tap that renders nothing *yet* (playback
still starting, or mpv pointed at another device) is not a permanent
failure either - the stream tap covers for a minute, then the tap is
retried.

Drop the avfoundation device enumeration, the loopback-name heuristics
and the microphone safeguards' tests, and document the new permission
and its diagnostics instead.
2026-10-30 22:53:29 -04:00

35 lines
1.0 KiB
Python

from __future__ import annotations
import pytest
from somafm_tui import macos_tap
def test_osstatus_decodes_four_character_codes() -> None:
# 'who?' is kAudioHardwareBadObjectError.
assert macos_tap._osstatus(int.from_bytes(b"who?", "big")) == "'who?'"
assert macos_tap._osstatus(-50) == "-50"
def test_start_raises_without_the_native_api(monkeypatch) -> None:
monkeypatch.setattr(macos_tap, "available", lambda: False)
tap = macos_tap.SystemAudioTap()
with pytest.raises(macos_tap.TapError):
tap.start()
assert tap.running is False
tap.stop() # tearing down a tap that never started is safe
def test_read_returns_none_when_not_running() -> None:
tap = macos_tap.SystemAudioTap()
assert tap.read(0.0) is None
def test_uses_no_external_helpers() -> None:
# The tap must never shell out: no helper binary, no ffmpeg, no driver.
import inspect
source = inspect.getsource(macos_tap).casefold()
assert "subprocess" not in source
assert "avfoundation" not in source