Set the radio's sound card in radio configure

WSJT-X and JS8Call get the radio's sound card as PipeWire names it: radio
asks pactl for the input and output nodes on the radio's ALSA card.
WSJT-X (Qt 5, through Qt's PulseAudio plugin) keeps the node names in
SoundInName and SoundOutName; JS8Call (Qt 6) keeps them in SoundInId and
SoundOutId, with PipeWire's descriptions, quoted as Qt writes them, in
SoundInName and SoundOutName. A setting that's empty or a default
("default", "pipewire") is replaced; a device you chose is left alone
unless --force.

If Graywolf has no channels, it gets input and output devices on
plughw:CARD=...,DEV=0 and a channel named after the radio, with its
defaults (1200 baud AFSK, APRS) and PTT through rigctld, in the same
stop and start as the PTT change.

fldigi, not1mm and POTACAT name sound cards in ways radio can't write,
so the closing message names them and says which card to choose.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
David Young
2026-11-28 13:06:17 -07:00
co-authored by Claude Opus 5.5
parent 04d973039a
commit 4053664d77
5 changed files with 286 additions and 68 deletions
+151 -52
View File
@@ -489,32 +489,106 @@ running() {
pgrep -x "$1" > /dev/null 2>&1
}
# Print a string as Qt writes it to an INI file: quoted if it has characters
# Qt would read otherwise (a comma makes a list), with \ and " escaped
qt_ini_string() {
local v="${1//\\/\\\\}"
v="${v//\"/\\\"}"
if [[ "$1" =~ [,\;=\"\\] ]] || [[ "$1" == " "* ]] || [[ "$1" == *" " ]]; then
printf '"%s"\n' "$v"
else
printf '%s\n' "$1"
fi
}
# Find the radio's sound card in PipeWire, as programs that use it (through
# Qt) name it: find_pipewire_nodes CARD_NUMBER sets PW_IN_NAME and
# PW_OUT_NAME (node names, such as alsa_input.usb-...) and PW_IN_DESC and
# PW_OUT_DESC (as PipeWire describes them)
find_pipewire_nodes() {
PW_IN_NAME="" PW_IN_DESC="" PW_OUT_NAME="" PW_OUT_DESC=""
[ -n "$1" ] && command -v pactl > /dev/null || return 1
# shellcheck disable=SC2016 # $n is jq's
local query='first(.[] | select(.properties["alsa.card"] == $n and .properties["device.class"] == "sound")) | [.name, .description] | @tsv'
IFS=$'\t' read -r PW_IN_NAME PW_IN_DESC < <(pactl -f json list sources 2> /dev/null | jq -r --arg n "$1" "$query" 2> /dev/null)
IFS=$'\t' read -r PW_OUT_NAME PW_OUT_DESC < <(pactl -f json list sinks 2> /dev/null | jq -r --arg n "$1" "$query" 2> /dev/null)
[ -n "$PW_IN_NAME" ] && [ -n "$PW_OUT_NAME" ] || { PW_IN_NAME="" PW_OUT_NAME=""; return 1; }
}
# A sound device setting that names no particular card: unset, or a default
is_default_device() {
case "$1" in
"" | default | pipewire | pulse | sysdefault) return 0 ;;
*) return 1 ;;
esac
}
# Say what was set, and what was left alone and why: say_changes NAME, from
# SAID (what was set) and LEFT (what was left alone)
say_changes() {
local message="" IFS='|'
if [ ${#SAID[@]} -gt 0 ]; then
message="${SAID[*]}"
message="${message//|/, }"
fi
if [ ${#LEFT[@]} -gt 0 ]; then
local left="${LEFT[*]}"
message="${message:+$message. }Left ${left//|/ and } alone: \`radio configure --force\` replaces $( [ ${#LEFT[@]} -eq 1 ] && echo it || echo them)"
fi
report "$1" "${message:-already set up}"
}
# WSJT-X and JS8Call share their settings: point one at rigctld, with PTT by
# CAT. configure_qt_app NAME FILE PROCESS
# CAT, and at the radio's sound card in PipeWire. WSJT-X keeps the device's
# name in SoundInName; JS8Call keeps it in SoundInId, and its description in
# SoundInName. configure_qt_app NAME FILE PROCESS wsjtx|js8call
configure_qt_app() {
local name="$1" file="$2" rig net
local name="$1" file="$2" kind="$4" rig net in_key=SoundInName out_key=SoundOutName current_in current_out i
local keys=() values=()
SAID=() LEFT=()
if running "$3"; then
report "$name" "skipped, because it's running and would overwrite its settings when it quits. Quit it and run \`radio configure\` again"
return
fi
rig="$(ini_get "$file" Configuration Rig)"
net="$(ini_get "$file" Configuration CATNetworkPort)"
if [ "$rig" = "Hamlib NET rigctl" ] && [ "$net" = "$RIGCTLD_ADDR" ]; then
report "$name" "already set up"
return
:
elif [ -z "$rig" ] || [ "$rig" = None ] || (( FORCE )); then
keys+=(Rig CATNetworkPort PTTMethod)
values+=("Hamlib NET rigctl" "$RIGCTLD_ADDR" "$PTT_CAT")
SAID+=("rig control through rigctld, PTT by CAT")
else
LEFT+=("its rig ($rig)")
fi
if [ -n "$rig" ] && [ "$rig" != None ] && (( ! FORCE )); then
report "$name" "left alone: it's set up for $rig. \`radio configure --force\` replaces that"
return
if [ -n "$PW_IN_NAME" ]; then
[ "$kind" = wsjtx ] || { in_key=SoundInId out_key=SoundOutId; }
current_in="$(ini_get "$file" Configuration "$in_key")"
current_out="$(ini_get "$file" Configuration "$out_key")"
if [ "$current_in" = "$PW_IN_NAME" ] && [ "$current_out" = "$PW_OUT_NAME" ]; then
:
elif { is_default_device "$current_in" && is_default_device "$current_out"; } || (( FORCE )); then
keys+=("$in_key" "$out_key")
values+=("$PW_IN_NAME" "$PW_OUT_NAME")
if [ "$kind" = js8call ]; then
keys+=(SoundInName SoundOutName)
values+=("$(qt_ini_string "$PW_IN_DESC")" "$(qt_ini_string "$PW_OUT_DESC")")
fi
SAID+=("sound card \"$PW_OUT_DESC\"")
else
LEFT+=("its sound card (${current_in:-$current_out})")
fi
fi
back_up "$file"
if ! { ini_set "$file" Configuration Rig "Hamlib NET rigctl" &&
ini_set "$file" Configuration CATNetworkPort "$RIGCTLD_ADDR" &&
ini_set "$file" Configuration PTTMethod "$PTT_CAT"; }; then
report "$name" "couldn't change $file"
return 1
if [ ${#keys[@]} -gt 0 ]; then
back_up "$file"
for i in "${!keys[@]}"; do
ini_set "$file" Configuration "${keys[i]}" "${values[i]}" || { report "$name" "couldn't change $file"; return 1; }
done
fi
report "$name" "rig control through rigctld, PTT by CAT"
say_changes "$name"
}
# Pat: add rigctld to its rigs as "hampack", and use it for ARDOP and VARA
@@ -899,39 +973,56 @@ configure_potacat() {
report POTACAT "rig \"$1 (HamPack's rigctld)\" is rigctld, and active"
}
# Graywolf keeps its settings in graywolf.db, with PTT set per channel. Set
# PTT to rigctld on each channel that can transmit and has none (or, with
# --force, any other). graywolf_ptt check|BACKUP prints the number of
# channels to change (or changed), then the number left alone, or "none"
# with no channel that transmits, or the error. With BACKUP, it makes the
# changes, backing up the database through SQLite first, which includes
# what's still in graywolf.db-wal.
graywolf_ptt() {
# Graywolf keeps its settings in graywolf.db. If it has no channels yet and
# the radio has a sound card, add input and output devices for the card and a
# channel using them, with Graywolf's defaults (1200 baud AFSK, APRS). Then
# set PTT to rigctld on each channel that can transmit and has none (or, with
# --force, any other). graywolf_setup check|BACKUP DEVICE CHANNEL_NAME prints
# "ADDED CHANGED KEPT": whether a channel is (or was) added, the number of
# channels whose PTT is (or was) changed, and the number left alone. Or it
# prints "none" with no channel that transmits, or the error. With BACKUP it
# makes the changes, backing up the database through SQLite first, which
# includes what's still in graywolf.db-wal.
graywolf_setup() {
python3 -I -c '
import datetime, sqlite3, sys
db, backup, addr, force = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4] == "1"
db, backup, addr, force, device, channel_name = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4] == "1", sys.argv[5], sys.argv[6]
try:
con = sqlite3.connect(db)
add_channel = bool(device) and not con.execute("SELECT 1 FROM channels").fetchone()
channels = [row[0] for row in con.execute("SELECT id FROM channels WHERE output_device_id != 0 ORDER BY id")]
if not channels:
if not channels and not add_channel:
print("none")
sys.exit(0)
ptt = {row[0]: (row[1], row[2]) for row in con.execute("SELECT channel_id, method, device FROM ptt_configs")}
change, kept = [], 0
for channel in channels:
method, device = ptt.get(channel, ("none", ""))
if (method, device) == ("rigctld", addr):
method, ptt_device = ptt.get(channel, ("none", ""))
if (method, ptt_device) == ("rigctld", addr):
continue
if method in ("none", "") or force:
change.append(channel)
else:
kept += 1
if change and backup != "check":
if backup != "check" and (change or add_channel):
copy = sqlite3.connect(backup)
con.backup(copy)
copy.close()
now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S.%f+00:00")
with con:
if add_channel:
ids = {}
for direction in ("input", "output"):
row = con.execute("SELECT id FROM audio_devices WHERE direction = ? AND source_type = ? AND source_path = ?",
(direction, "soundcard", device)).fetchone()
if row is None:
row = (con.execute("INSERT INTO audio_devices (name, direction, source_type, source_path, sample_rate, "
"channels, format, gain_db, created_at, updated_at) VALUES (?, ?, ?, ?, 48000, 1, ?, 0, ?, ?)",
(channel_name + " " + direction, direction, "soundcard", device, "s16le", now, now)).lastrowid,)
ids[direction] = row[0]
channel = con.execute("INSERT INTO channels (name, input_device_id, output_device_id, created_at, updated_at) "
"VALUES (?, ?, ?, ?, ?)", (channel_name, ids["input"], ids["output"], now, now)).lastrowid
change.append(channel)
for channel in change:
if channel in ptt:
con.execute("UPDATE ptt_configs SET method = ?, device = ?, updated_at = ? WHERE channel_id = ?",
@@ -940,15 +1031,19 @@ try:
con.execute("INSERT INTO ptt_configs (channel_id, method, device, gpio_pin, ptt_method, gpio_line, "
"invert, slot_time_ms, persist, dwait_ms, created_at, updated_at) "
"VALUES (?, ?, ?, 0, 0, 0, 0, 10, 63, 0, ?, ?)", (channel, "rigctld", addr, now, now))
print(len(change), kept)
change = [c for c in change if c in channels]
print(int(add_channel), len(change), kept)
except (sqlite3.Error, OSError) as e:
print(e)
' "$GRAYWOLF_DB" "$1" "$RIGCTLD_ADDR" "$FORCE"
' "$GRAYWOLF_DB" "$1" "$RIGCTLD_ADDR" "$FORCE" "$2" "$3"
}
# configure_graywolf CARD NAME, where CARD is ALSA's name for the radio's
# sound card (hw:CARD=CODEC), or nothing
configure_graywolf() {
local result changed kept backup was_active=0 web="http://localhost:8080"
local result added changed kept backup was_active=0 web="http://localhost:8080" device=""
local manual="in its web UI ($web), set each channel's PTT to rigctld, $RIGCTLD_ADDR"
[ -z "$1" ] || device="plug$1,DEV=0"
if [ ! -x "$HOME/.local/bin/graywolf" ]; then
report Graywolf "not installed"
return
@@ -957,9 +1052,9 @@ configure_graywolf() {
report Graywolf "skipped: it has no settings yet. Open its web UI ($web) once, then \`radio configure\` again"
return
fi
result="$(graywolf_ptt check)"
read -r changed kept <<< "$result"
if [[ "$changed" =~ ^[1-9][0-9]*$ ]]; then
result="$(graywolf_setup check "$device" "$2")"
read -r added changed kept <<< "$result"
if [ "$added" = 1 ] || [[ "$changed" =~ ^[1-9][0-9]*$ ]]; then
# It runs as a system service, and reads its settings when it starts
if systemctl is-active --quiet graywolf 2> /dev/null; then
if ! sudo systemctl stop graywolf; then
@@ -969,25 +1064,23 @@ configure_graywolf() {
was_active=1
fi
backup="$GRAYWOLF_DB.bak-$(date +%Y%m%d-%H%M%S)"
result="$(graywolf_ptt "$backup")"
result="$(graywolf_setup "$backup" "$device" "$2")"
(( ! was_active )) || sudo systemctl start graywolf
read -r changed kept <<< "$result"
read -r added changed kept <<< "$result"
fi
if [ "$changed" = none ]; then
if [ "$added" = none ]; then
report Graywolf "skipped: it has no channel that transmits yet. Add one in its web UI ($web) with PTT rigctld, $RIGCTLD_ADDR"
elif ! [[ "$changed" =~ ^[0-9]+$ ]]; then
return
elif ! [[ "$added" =~ ^[01]$ ]]; then
report Graywolf "couldn't change $GRAYWOLF_DB: $result"
return 1
elif (( changed == 0 && kept == 0 )); then
report Graywolf "already set up"
else
(( changed == 0 )) || BACKUPS+=("$backup")
local message=""
(( changed == 0 )) || message="PTT through rigctld on $changed channel$( ((changed == 1)) || echo s)"
(( kept == 0 )) ||
message="${message:+$message. }Left $kept channel$( ((kept == 1)) || echo s) with PTT of its own alone: \`radio configure --force\` replaces it"
report Graywolf "$message"
fi
(( added == 0 && changed == 0 )) || BACKUPS+=("$backup")
SAID=() LEFT=()
(( added == 0 )) || SAID+=("a channel on the radio's sound card ($device), 1200 baud APRS, PTT through rigctld")
(( changed == 0 )) || SAID+=("PTT through rigctld on $changed$( (( added )) && echo " other") channel$( ((changed == 1)) || echo s)")
(( kept == 0 )) || LEFT+=("$kept channel$( ((kept == 1)) || echo s) with PTT of its own")
say_changes Graywolf
}
# Write and (re)start rigctld's user service: write_rigctld NAME HAMLIB PORT BAUD
@@ -1084,6 +1177,10 @@ configure() {
port="$(port_path "$ID_CAT_PORT")"
baud="$ID_BAUD"
card="$(card_name "$radio")"
local card_number="" pipewire=0
card_number="$(card_of "$radio")"
card_number="${card_number#card}"
find_pipewire_nodes "$card_number" && pipewire=1
echo "Setting up the $name (Hamlib model $model), on $port at $baud baud"
echo
@@ -1096,12 +1193,12 @@ configure() {
report "" "$port has no stable name, so it may be a different port after a reboot. Run \`radio configure\` again if so"
if flatpak info "$WSJTX_FLATPAK" > /dev/null 2>&1; then
configure_qt_app WSJT-X "$WSJTX_CONF" wsjtx
configure_qt_app WSJT-X "$WSJTX_CONF" wsjtx wsjtx
else
report WSJT-X "not installed"
fi
if [ -x "$HOME/.local/bin/JS8Call" ]; then
configure_qt_app JS8Call "$JS8CALL_CONF" JS8Call
configure_qt_app JS8Call "$JS8CALL_CONF" JS8Call js8call
else
report JS8Call "not installed"
fi
@@ -1111,16 +1208,18 @@ configure() {
configure_not1mm
configure_qlog
configure_potacat "$name"
configure_graywolf
configure_graywolf "$card" "$name"
mkdir -p "$HAMPACK_STATE" &&
printf 'name=%s\nhamlib=%s\nport=%s\nbaud=%s\ncard=%s\n' "$name" "$model" "$port" "$baud" "$card" > "$HAMPACK_STATE/radio"
echo
if [ -n "$card" ]; then
echo "Choose the radio's sound card in the other programs yourself: $card, \"$(attr "${CARD_USB[$(card_of "$radio")]}" product)\"."
else
if [ -z "$card" ]; then
echo "No sound card was found with the radio: choose the one it's connected to in each program yourself."
elif (( pipewire )); then
echo "Choose the radio's sound card yourself in fldigi, not1mm and POTACAT, which name it in ways radio can't: it's \"$PW_OUT_DESC\" ($card)."
else
echo "PipeWire didn't list the radio's sound card, $card (\"$(attr "${CARD_USB[$(card_of "$radio")]}" product)\"), so choose it yourself in WSJT-X, JS8Call, fldigi, not1mm and POTACAT."
fi
[ ${#BACKUPS[@]} -eq 0 ] || { echo "The settings were backed up first, to:"; printf ' %s\n' "${BACKUPS[@]}"; }
}