Skip to content

Instantly share code, notes, and snippets.

@adde88
Last active June 5, 2025 15:00
Show Gist options
  • Save adde88/09e51b527915684b39deb3e3b0fe54f4 to your computer and use it in GitHub Desktop.
Save adde88/09e51b527915684b39deb3e3b0fe54f4 to your computer and use it in GitHub Desktop.
FM Radio Audio Transmitter Script for Raserry Pi3B+. Requires working "pi_fm_adv". Supports: wav, mp3, m4a, ogg.
#!/bin/bash
# Author: Andreas Nilsen
# Email: [email protected]
# Github: https://www.github.com/adde88
# License: GPL-3
# Check arguments
if [[ $# -ne 2 ]]; then
echo "Usage: $0 /path/to/file.{wav,mp3,m4a,ogg} frequency_in_MHz"
echo "Example: $0 /home/pi/audio.mp3 100.1"
exit 1
fi
# Input arguments
INPUT_FILE="$1"
FREQ="$2"
# Constants
PIFMADV_PATH="/root/PiFmAdv/src"
PIFM_EXEC="$PIFMADV_PATH/pi_fm_adv"
TMP_WAV="/tmp/tmp_fm_audio.wav"
# Check PiFmAdv binary
if [[ ! -f "$PIFM_EXEC" ]]; then
echo "Error: pi_fm_adv not found at $PIFM_EXEC"
exit 2
fi
# Check input file
if [[ ! -f "$INPUT_FILE" ]]; then
echo "Error: Input file $INPUT_FILE not found"
exit 3
fi
# Get file extension (lowercased)
EXT="${INPUT_FILE##*.}"
EXT="${EXT,,}"
# Handle file conversion if needed
case "$EXT" in
wav)
FINAL_FILE="$INPUT_FILE"
;;
mp3|m4a|ogg)
echo "[*] Converting $EXT to WAV..."
ffmpeg -y -i "$INPUT_FILE" -ar 22050 -ac 1 "$TMP_WAV" >/dev/null 2>&1
if [[ ! -f "$TMP_WAV" ]]; then
echo "Error: Failed to convert to WAV."
exit 4
fi
FINAL_FILE="$TMP_WAV"
;;
*)
echo "Error: Unsupported file format '$EXT'. Supported: wav, mp3, m4a, ogg"
exit 5
;;
esac
# Transmit using PiFmAdv
echo "[*] Starting FM transmission on $FREQ MHz..."
sudo "$PIFM_EXEC" -f "$FREQ" -music "$FINAL_FILE"
# Cleanup if temporary file was used
if [[ "$FINAL_FILE" == "$TMP_WAV" ]]; then
echo "[*] Cleaning up temporary WAV file..."
rm -f "$TMP_WAV"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment