Files
beefy-player/somafm_tui/__main__.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

127 lines
3.6 KiB
Python

"""Console entry point."""
from __future__ import annotations
import argparse
import asyncio
from collections.abc import Sequence
from . import __version__, api
from .fallback import fallback_channels
from .spectrum import SOURCE_MODES
def _build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
prog="somafm-tui",
description="A flashy terminal radio for somafm.com — live spectrum, "
"song history and desktop media integration.",
)
parser.add_argument("--station", metavar="ID", help="station id to select")
parser.add_argument(
"--play",
dest="autoplay",
action="store_true",
default=None,
help="start playing immediately",
)
parser.add_argument(
"--no-autoplay",
dest="autoplay",
action="store_false",
help="never start playing on launch",
)
parser.add_argument("--volume", type=int, metavar="0-100", help="initial volume")
parser.add_argument(
"--no-mpris",
"--no-media",
dest="no_mpris",
action="store_true",
help="do not publish to the desktop media session (MPRIS on Linux, "
"Now Playing on macOS) and ignore the media keys",
)
parser.add_argument(
"--offline",
action="store_true",
help="do not touch the network (cached/bundled stations only)",
)
parser.add_argument(
"--no-audio",
action="store_true",
help="UI only; skip mpv/ffmpeg/MPRIS (useful for demos)",
)
parser.add_argument(
"--spectrum-source",
choices=SOURCE_MODES,
default="auto",
help="spectrum audio source: auto (monitor→stream→sim), monitor "
"(parec on Linux, a Core Audio process tap on macOS), "
"stream (muted second ffmpeg decode), or sim",
)
parser.add_argument(
"--diagnose-images",
action="store_true",
help="print terminal/image-protocol diagnostics and exit",
)
parser.add_argument(
"--list",
action="store_true",
help="print known station ids and exit",
)
parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
return parser
def _list_stations(offline: bool) -> int:
stations = api.load_cached_channels()
if not stations and not offline:
async def load():
client = api.make_client()
try:
return await api.fetch_channels(client)
finally:
await client.aclose()
try:
stations = asyncio.run(load())
except Exception:
stations = []
if not stations:
stations = api.parse_channels({"channels": fallback_channels()})
for station in sorted(stations, key=lambda item: item.title.casefold()):
print(f"{station.id:<20} {station.title}")
return 0
def main(argv: Sequence[str] | None = None) -> int:
args = _build_parser().parse_args(argv)
if args.diagnose_images:
from . import artwork
for key, value in artwork.diagnostics().items():
print(f"{key}: {value}")
return 0
if args.list:
return _list_stations(args.offline)
# Import the app lazily so --list/--version do not probe the terminal for
# image protocols.
from .app import SomaFMApp
app = SomaFMApp(
autoplay=args.autoplay,
station_id=args.station,
volume=args.volume,
media=not args.no_mpris,
offline=args.offline,
audio=not args.no_audio,
spectrum_source=args.spectrum_source,
)
app.run()
return 0
if __name__ == "__main__":
raise SystemExit(main())