Files
jmj 4ac2e959e0 ai-speaker MCP server 初始提交
- MCP Streamable HTTP (:8765 HTTP / :8766 HTTPS 自签)
- 工具:speak / play_url / play_file / download / stop / set_volume / list_voices / list_music / player_status / status
- 音量:上限 100,默认 30,夜间 22:00-08:30 静默压到 60
- CORS + Origin 白名单(局域网网段 + Android WebView 虚拟域)
- audio.py:PipeWire 蓝牙输出、JBL 绝对音量校验、播放状态/进度/设备信息
- tts.py:edge-tts 合成 + 缓存
- bt-play.sh:命令行播放工具
2026-11-30 02:20:37 +08:00

119 lines
4.3 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/sh
# bt-play.sh —— 把音频文件通过蓝牙音箱播放(A2DP,经 PipeWire)
#
# 用法:
# bt-play.sh [选项] <音频文件或URL>
#
# 选项:
# -v <0-100> 音量百分比(默认 10),会保存为下次默认值
# -s <名称> 指定音箱关键字(默认 JBL),用于查找蓝牙输出节点
# -t <秒> 只播放前 N 秒(默认播放完整)
# -q 静默,不打印状态
#
# 示例:
# bt-play.sh song.mp3 # 以默认音量播放整首
# bt-play.sh -v 20 song.m4a # 音量 20% 播放整首
# bt-play.sh -t 15 song.m4a # 试听前 15 秒
# bt-play.sh "https://example.com/a.mp3" # 直接播 URL(ffmpeg 解码)
#
# 环境:依赖系统级 pipewire.service 提供的运行时目录与会话总线
set -u
VOL_FILE=/root/.config/bt-play/volume
SINK_FILE=/root/.config/bt-play/sink
BT_MAC=43:5A:DC:68:1B:F8
mkdir -p "$(dirname "$VOL_FILE")"
[ -f "$VOL_FILE" ] && DEF_VOL=$(cat "$VOL_FILE") || DEF_VOL=10
[ -f "$SINK_FILE" ] && DEF_SINK=$(cat "$SINK_FILE") || DEF_SINK=JBL
VOL=""; SINK=""; DUR=""; QUIET=0
while getopts "v:s:t:q" o; do
case "$o" in
v) VOL="$OPTARG" ;;
s) SINK="$OPTARG" ;;
t) DUR="$OPTARG" ;;
q) QUIET=1 ;;
*) echo "用法: $0 [-v 音量] [-s 音箱] [-t 秒] <文件>"; exit 2 ;;
esac
done
shift $((OPTIND-1))
[ $# -ge 1 ] || { echo "用法: $0 [-v 音量] [-s 音箱] [-t 秒] <文件>"; exit 2; }
SRC="$1"
[ -f "$SRC" ] || echo "$SRC" | grep -qE '^https?://' || { echo "文件不存在: $SRC"; exit 1; }
say() { [ "$QUIET" = 1 ] || echo "$@"; }
# ---------- 1. 音量 ----------
VOL="${VOL:-$DEF_VOL}"
echo "$VOL" | grep -qE '^[0-9]+$' && [ "$VOL" -ge 0 ] && [ "$VOL" -le 100 ] || { echo "音量需为 0-100 的整数"; exit 2; }
echo "$VOL" > "$VOL_FILE"
# ---------- 2. 连接音箱 ----------
CONNECTED=$(bluetoothctl info "$BT_MAC" 2>/dev/null | grep -c "Connected: yes")
if [ "$CONNECTED" = 0 ]; then
say "连接音箱 $BT_MAC ..."
timeout 30 bluetoothctl connect "$BT_MAC" >/dev/null 2>&1
sleep 3
fi
# ---------- 3. 找到 PipeWire 蓝牙输出节点 ----------
SINK="${SINK:-$DEF_SINK}"
echo "$SINK" > "$SINK_FILE"
export PIPEWIRE_RUNTIME_DIR=/run/user/0
export DBUS_SESSION_BUS_ADDRESS="$(cat /run/pipewire-dbus-address 2>/dev/null)"
NODE_ID=""
for i in 1 2 3 4 5 6 7 8; do
# wpctl status 中,Sinks 段下的行形如: " │ * 36. JBL PS3500 [vol: 0.10]"
# 先截取 Sinks: 段(到下一个 ├─ 或 └─ 为止),再在段内按音箱名匹配 ID
NODE_ID=$(timeout 5 wpctl status 2>/dev/null \
| sed -E 's/\x1b\[[0-9;]*m//g' \
| awk -v kw="$SINK" '
/Sinks:/ {insink=1; next}
/^[ │]*[├└]─/ {insink=0}
insink && tolower($0) ~ tolower(kw) {
match($0, /[0-9]+\./); print substr($0, RSTART, RLENGTH-1); exit
}'
)
[ -n "$NODE_ID" ] && break
sleep 1
done
[ -n "$NODE_ID" ] || { echo "找不到蓝牙输出节点(音箱名含 '$SINK')。当前状态:"; timeout 5 wpctl status 2>&1 | sed -E 's/\x1b\[[0-9;]*m//g' | grep -iE "bluez|Sinks" ; exit 1; }
VOL_F=$(awk "BEGIN{printf \"%.4f\", $VOL/100}")
# JBL 等支持 AVRCP 绝对音量的音箱会把自身音量回传覆盖软件设置,
# 因此设完后等待并复读,若被覆盖则再设一次(最多 3 轮)
i=0
while [ $i -lt 3 ]; do
timeout 5 wpctl set-volume "$NODE_ID" "$VOL_F"
sleep 1
CUR=$(timeout 5 wpctl get-volume "$NODE_ID" 2>/dev/null | grep -oE '[0-9.]+' | head -1)
# 用 awk 比较是否在目标 ±0.02 内
OK=$(awk -v a="$CUR" -v b="$VOL_F" 'BEGIN{print (a-b<0.02 && b-a<0.02)?1:0}')
[ "$OK" = 1 ] && break
i=$((i+1))
done
say "音箱节点: $NODE_ID 音量: ${VOL}% (当前 ${CUR:-?})"
# ---------- 4. 播放 ----------
FF_ARGS="-v error -i"
[ -n "$DUR" ] && FF_ARGS="-v error -t $DUR -i"
if [ -n "$DUR" ]; then
say "播放前 ${DUR}s: $SRC"
else
say "播放: $SRC"
fi
# POSIX sh 没有 PIPESTATUS,用 ffmpeg 的 stderr 文件判断是否出错
timeout 5 wpctl set-mute "$NODE_ID" 0 2>/dev/null
: > /tmp/bt-play-fferr
ffmpeg $FF_ARGS "$SRC" -f wav -ac 2 -ar 44100 - 2>>/tmp/bt-play-fferr \
| pw-cat --playback - --target "$NODE_ID" 2>/dev/null
if [ -s /tmp/bt-play-fferr ]; then
echo "ffmpeg 出错:"; head -3 /tmp/bt-play-fferr; rm -f /tmp/bt-play-fferr; exit 1
fi
rm -f /tmp/bt-play-fferr
say "播放完成"