Skip to content

Instantly share code, notes, and snippets.

@IvarWithoutBones
Last active December 5, 2024 19:23
Show Gist options
  • Save IvarWithoutBones/944af61598a7da0e798a2474bcce1ceb to your computer and use it in GitHub Desktop.
Save IvarWithoutBones/944af61598a7da0e798a2474bcce1ceb to your computer and use it in GitHub Desktop.
A script to stream audio from any Pulseadio-enabled system to an Android phone over the internet, in my case to emulate a bluetooth adapter.
#!/usr/bin/env bash
# A script to stream audio from any Pulseadio-enabled system to an Android phone over the internet, in my case to emulate a bluetooth adapter.
# The Android app "Simple Protocol Player" can be used to connect to the stream opened by this script: https://github.com/kaytat/SimpleProtocolPlayer.
# Based off of this post: https://superuser.com/a/750324. Thanks!
# Use `pactl list | grep "Monitor Source"` to find the appropriate output source.
sourceName="alsa_output.pci-0000_00_1f.3.analog-stereo.monitor"
# Both must match with what is configured in the Simple Protocol Player app.
port="1285"
rate="48000"
set -euo pipefail
stop() {
local module="$1"
echo "unloading module '${module}'" >&2
pactl unload-module "${module}"
}
start() {
local module
module="$(pactl list | grep tcp -B1 | grep M | sed 's/[^0-9]//g' || true)"
if [ -n "${module}" ]; then
echo "module was already loaded, restarting" >&2
stop "${module}"
fi
echo "starting TCP module for source '${sourceName}' on port ${port}" >&2
module="$(pactl load-module module-simple-protocol-tcp format=s16le channels=2 record=true rate="${rate}" source="${sourceName}" port="${port}")"
# shellcheck disable=SC2064
trap "stop ${module}" EXIT # We want to expand $module now since locals wont be available in the trap subshell.
echo "press enter to stop" >&2
read -r
}
start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment