#!/usr/bin/env bash
# route-to-blackhole.sh — route an audio file (looping) into BlackHole.
#
# BlackHole is the "universal bus": any audio you can play can be routed into
# BlackHole, and `hub.py stream "BlackHole 2ch"` captures it into ONE live stream.
# This is how phones (via a player), SDR output, DAW, DJ gear, or any app's audio
# all converge into a single stream without extra hardware.
#
# Usage:
#   ./route-to-blackhole.sh file.wav          # loop a file into the bus
#   ./route-to-blackhole.sh file.wav 3        # play 3 times, then stop
#
# Then, in another terminal:
#   ./hub.py stream "BlackHole 2ch" --port 8123
set -euo pipefail
export PATH="/opt/homebrew/bin:/usr/local/bin:$PATH"
FILE="${1:?usage: route-to-blackhole.sh <audio_file> [repeat_count]}"
[ -f "$FILE" ] || { echo "no such file: $FILE" >&2; exit 1; }
command -v sox >/dev/null || { echo "sox missing. Install once with:  brew install sox" >&2; exit 2; }
[ -d "/Library/Audio/Plug-Ins/HAL/BlackHole2ch.driver" ] || {
  echo "BlackHole 2ch driver not installed. Run: brew install blackhole-2ch (asks for your Mac password once)" >&2; exit 3; }
REP="${2:-9999}"
sox "$FILE" -r 44100 -t coreaudio "BlackHole 2ch" repeat "$REP"
