Set fldigi's sound card in radio configure

fldigi gets PortAudio as its audio system, with the radio's card for
capture and playback. It opens devices by name, so radio lists PortAudio's
devices through the system library, as fldigi does, and picks PipeWire's
names for the card (through PortAudio's JACK support) where just one
device has them, or else ALSA's, with the card's number. Devices that are
unset, a default, or the same card under an old number are replaced; a
device you chose, or fldigi on PulseAudio or OSS, is left alone unless
--force.

not1mm now uses the same device list, instead of asking its own Python's
sounddevice, which reads the same library. POTACAT is the only program
whose sound card is left for you to choose.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
David Young
2026-11-28 13:19:04 -07:00
co-authored by Claude Opus 5.5
parent c2cb2bc2f4
commit 8020cdc061
5 changed files with 194 additions and 79 deletions
+113 -46
View File
@@ -654,9 +654,12 @@ xml_set() {
}
# fldigi: rig control through rigctld (Hamlib's NET rigctl, model 2), with PTT
# by Hamlib, unless it has rig control set up already
# by Hamlib, unless it has rig control set up already, and the radio's sound
# card through PortAudio. configure_fldigi CARD_NUMBER
configure_fldigi() {
local tag
local tag set_rig=1 set_sound=0 in_device out_device audio current_in current_out i
local tags=() values=()
SAID=() LEFT=()
if ! command -v fldigi > /dev/null; then
report fldigi "not installed"
return
@@ -669,28 +672,61 @@ configure_fldigi() {
report fldigi "skipped, because it's running and would overwrite its settings when it quits. Quit it and run \`radio configure\` again"
return
fi
if [ "$(xml_get "$FLDIGI_CONF" CHKUSEHAMLIBIS)" = 1 ] && [ "$(xml_get "$FLDIGI_CONF" HAMRIGMODEL)" = 2 ] &&
[ "$(xml_get "$FLDIGI_CONF" HAMRIGDEVICE)" = "$RIGCTLD_ADDR" ]; then
report fldigi "already set up"
return
fi
if (( ! FORCE )); then
set_rig=0
elif (( ! FORCE )); then
for tag in CHKUSEHAMLIBIS CHKUSERIGCATIS CHKUSEXMLRPCIS CLIENT_TO_FLRIG; do
if [ "$(xml_get "$FLDIGI_CONF" "$tag")" = 1 ]; then
report fldigi "left alone: it has rig control set up already. \`radio configure --force\` replaces that"
return
set_rig=0
LEFT+=("its rig control")
break
fi
done
fi
back_up "$FLDIGI_CONF"
if ! { xml_set "$FLDIGI_CONF" CHKUSEHAMLIBIS 1 && xml_set "$FLDIGI_CONF" CHKUSERIGCATIS 0 &&
xml_set "$FLDIGI_CONF" CHKUSEXMLRPCIS 0 && xml_set "$FLDIGI_CONF" CLIENT_TO_FLRIG 0 &&
xml_set "$FLDIGI_CONF" HAMRIGMODEL 2 && xml_set "$FLDIGI_CONF" HAMRIGDEVICE "$RIGCTLD_ADDR" &&
xml_set "$FLDIGI_CONF" HAMLIBCMDPTT 1; }; then
report fldigi "couldn't change $FLDIGI_CONF"
return 1
if (( set_rig )); then
tags+=(CHKUSEHAMLIBIS CHKUSERIGCATIS CHKUSEXMLRPCIS CLIENT_TO_FLRIG HAMRIGMODEL HAMRIGDEVICE HAMLIBCMDPTT)
values+=(1 0 0 0 2 "$RIGCTLD_ADDR" 1)
SAID+=("rig control through rigctld, PTT by Hamlib")
fi
report fldigi "rig control through rigctld, PTT by Hamlib"
if [ -n "$1" ]; then
if in_device="$(portaudio_name in "$1")" && out_device="$(portaudio_name out "$1")"; then
# AUDIOIO is 1 for PortAudio and 3 for none; 0 (OSS) and 2 (PulseAudio) are your choice
audio="$(xml_get "$FLDIGI_CONF" AUDIOIO)"
current_in="$(xml_get "$FLDIGI_CONF" PORTINDEVICE)"
current_out="$(xml_get "$FLDIGI_CONF" PORTOUTDEVICE)"
if [ "$audio" = 1 ] && [ "$current_in" = "$in_device" ] && [ "$current_out" = "$out_device" ]; then
:
elif (( FORCE )) || { [[ "${audio:-3}" =~ ^(1|3|-1)$ ]] &&
may_replace_device "$current_in" "$in_device" && may_replace_device "$current_out" "$out_device"; }; then
set_sound=1
tags+=(AUDIOIO PORTINDEVICE PORTININDEX PORTOUTDEVICE PORTOUTINDEX)
values+=(1 "$in_device" -1 "$out_device" -1)
SAID+=("sound card \"$out_device\"")
else
LEFT+=("its sound card")
fi
else
SAID+=("choose its sound card yourself: PortAudio doesn't list the radio's")
fi
fi
if [ ${#tags[@]} -gt 0 ]; then
back_up "$FLDIGI_CONF"
for i in "${!tags[@]}"; do
xml_set "$FLDIGI_CONF" "${tags[i]}" "$(xml_escape "${values[i]}")" || { report fldigi "couldn't change $FLDIGI_CONF"; return 1; }
done
fi
say_changes fldigi
}
# Escape a value for an XML element
xml_escape() {
local v="${1//&/&amp;}"
v="${v//</&lt;}"
printf '%s\n' "${v//>/&gt;}"
}
# Print the value of the first line for a Dire Wolf setting: dw_get FILE KEYWORD
@@ -779,36 +815,68 @@ configure_direwolf() {
report "Dire Wolf" "$message"
}
# Print the output devices not1mm can play to, as "NAME<tab>HOST API", one to
# a line, using the sounddevice library in not1mm's own Python
not1mm_outputs() {
local python="${UV_TOOL_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/uv/tools}/not1mm/bin/python"
[ -x "$python" ] || return 1
"$python" -I -c '
import sounddevice as sd
for d in sd.query_devices():
if d["max_output_channels"] > 0:
print(d["name"] + "\t" + sd.query_hostapis(d["hostapi"])["name"])
' 2> /dev/null
# Print the sound devices PortAudio lists, as fldigi and not1mm (through its
# sounddevice library) see them: "NAME<tab>HOST API<tab>INPUTS<tab>OUTPUTS",
# one to a line
portaudio_devices() {
python3 -I -c '
import ctypes, os, sys
class DeviceInfo(ctypes.Structure):
_fields_ = [("structVersion", ctypes.c_int), ("name", ctypes.c_char_p), ("hostApi", ctypes.c_int),
("maxInputChannels", ctypes.c_int), ("maxOutputChannels", ctypes.c_int)]
class HostApiInfo(ctypes.Structure):
_fields_ = [("structVersion", ctypes.c_int), ("type", ctypes.c_int), ("name", ctypes.c_char_p)]
try:
pa = ctypes.CDLL("libportaudio.so.2")
except OSError:
sys.exit(1)
pa.Pa_GetDeviceInfo.restype = ctypes.POINTER(DeviceInfo)
pa.Pa_GetHostApiInfo.restype = ctypes.POINTER(HostApiInfo)
# PortAudio and ALSA complain on stderr while they look for devices
saved, null = os.dup(2), os.open(os.devnull, os.O_WRONLY)
os.dup2(null, 2)
try:
if pa.Pa_Initialize() != 0:
sys.exit(1)
for i in range(pa.Pa_GetDeviceCount()):
d = pa.Pa_GetDeviceInfo(i).contents
api = pa.Pa_GetHostApiInfo(d.hostApi).contents.name.decode(errors="replace")
print("%s\t%s\t%d\t%d" % (d.name.decode(errors="replace"), api, d.maxInputChannels, d.maxOutputChannels))
sys.stdout.flush()
pa.Pa_Terminate()
finally:
os.dup2(saved, 2)
'
}
# Print the name not1mm's voice keyer should use for the radio's sound card:
# Print PortAudio's name for the radio's sound card, for input or output:
# PipeWire's description of it, through PortAudio's JACK support, which stays
# the same from boot to boot. Failing that, ALSA's name for it, which has the
# card's number in it: "USB Audio CODEC: USB Audio (hw:1,0)".
# not1mm_device CARD_NUMBER
not1mm_device() {
local outputs
outputs="$(not1mm_outputs)" || return 1
if [ -n "$PW_OUT_DESC" ] && [ "$(cut -f1 <<< "$outputs" | grep -cxF "$PW_OUT_DESC")" -eq 1 ]; then
echo "$PW_OUT_DESC"
elif [ -n "$1" ]; then
cut -f1 <<< "$outputs" | grep -F "(hw:$1," | head -1 | grep .
# the same from boot to boot, if just one device has it. Failing that, ALSA's
# name for it, which has the card's number in it: "USB Audio CODEC: USB Audio
# (hw:1,0)". portaudio_name in|out CARD_NUMBER, with PORTAUDIO_DEVICES set
portaudio_name() {
local column=3 desc="$PW_IN_DESC" names
[ "$1" = in ] || { column=4; desc="$PW_OUT_DESC"; }
names="$(awk -F'\t' -v c="$column" '$c > 0 { print $1 }' <<< "$PORTAUDIO_DEVICES")"
if [ -n "$desc" ] && [ "$(grep -cxF "$desc" <<< "$names")" -eq 1 ]; then
echo "$desc"
elif [ -n "$2" ]; then
grep -F "(hw:$2," <<< "$names" | head -1 | grep .
else
return 1
fi
}
# Whether a device setting names the card radio would set, or the same card
# under the number it had before: may_replace_device CURRENT NEW
may_replace_device() {
is_default_device "$1" || (( FORCE )) ||
{ [[ "$2" == *"(hw:"* ]] && [[ "$1" == "${2%(hw:*}(hw:"*")" ]]; }
}
# not1mm: rig control through rigctld, unless it uses flrig, TCI or another
# rigctld already, and the radio's sound card for its voice keyer.
# configure_not1mm CARD_NUMBER
@@ -845,20 +913,18 @@ configure_not1mm() {
(( ! set_rig )) || SAID+=("rig control through rigctld")
if [ -n "$1" ]; then
if device="$(not1mm_device "$1")"; then
if device="$(portaudio_name out "$1")"; then
current="$(jq -r '.sounddevice // ""' "$NOT1MM_CONF")"
if [ "$current" = "$device" ]; then
:
elif is_default_device "$current" || (( FORCE )) ||
# The same card under the number it had before
{ [[ "$device" == *"(hw:"* ]] && [[ "$current" == "${device%(hw:*}(hw:"*")" ]]; }; then
elif may_replace_device "$current" "$device"; then
set_sound=1
SAID+=("voice keyer on \"$device\"")
else
LEFT+=("its sound card ($current)")
fi
else
SAID+=("choose its sound card yourself: its sounddevice library doesn't list the radio's")
SAID+=("choose its sound card yourself: PortAudio doesn't list the radio's")
fi
fi
@@ -1234,6 +1300,7 @@ configure() {
card_number="$(card_of "$radio")"
card_number="${card_number#card}"
find_pipewire_nodes "$card_number" && pipewire=1
PORTAUDIO_DEVICES="$(portaudio_devices)" || PORTAUDIO_DEVICES=""
echo "Setting up the $name (Hamlib model $model), on $port at $baud baud"
echo
@@ -1256,7 +1323,7 @@ configure() {
report JS8Call "not installed"
fi
configure_pat
configure_fldigi
configure_fldigi "$card_number"
configure_direwolf "$card"
configure_not1mm "$card_number"
configure_qlog
@@ -1270,9 +1337,9 @@ configure() {
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 and POTACAT, which name it in ways radio can't: it's \"$PW_OUT_DESC\" ($card)."
echo "Choose the radio's sound card yourself in POTACAT, which names it in a way 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 and POTACAT."
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 and POTACAT."
fi
[ ${#BACKUPS[@]} -eq 0 ] || { echo "The settings were backed up first, to:"; printf ' %s\n' "${BACKUPS[@]}"; }
}