Skip to content

Commit 528cc75

Browse files
authored
Merge pull request #388 from smuppand/audio
chore(audio): replace expr with POSIX arithmetic in audio_common.sh
2 parents c611ade + 4582648 commit 528cc75

File tree

1 file changed

+16
-26
lines changed

1 file changed

+16
-26
lines changed

Runner/utils/audio_common.sh

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ audio_timeout_run() {
345345
wait "$pid" 2>/dev/null
346346
return 143
347347
fi
348-
sleep 1; t=$(expr $t + 1)
348+
sleep 1; t=$((t + 1))
349349
done
350350
wait "$pid"; return $?
351351
}
@@ -567,12 +567,6 @@ setup_overlay_audio_environment() {
567567
}
568568

569569
# ---------- PipeWire control helpers (bounded; never hang) ----------
570-
pwctl_status_safe() {
571-
# Prints wpctl status on stdout; returns nonzero on timeout/failure.
572-
command -v wpctl >/dev/null 2>&1 || return 1
573-
audio_exec_with_timeout 2s wpctl status 2>/dev/null
574-
}
575-
576570
pwctl_inspect_safe() {
577571
# Prints wpctl inspect <id> on stdout; returns nonzero on timeout/failure.
578572
id="$1"
@@ -581,9 +575,6 @@ pwctl_inspect_safe() {
581575
audio_exec_with_timeout 2s wpctl inspect "$id" 2>/dev/null
582576
}
583577

584-
audio_pw_ctl_ok() {
585-
pwctl_status_safe >/dev/null 2>&1
586-
}
587578
# ---------- PipeWire: sinks (playback) ----------
588579
pw_default_speakers() {
589580
st="$(pwctl_status_safe 2>/dev/null)" || { printf '%s\n' ""; return 0; }
@@ -654,11 +645,6 @@ pw_default_null_source() {
654645
printf '%s\n' "$id"
655646
}
656647

657-
pw_set_default_source() {
658-
[ -n "$1" ] || return 1
659-
audio_exec_with_timeout 2s wpctl set-default "$1" >/dev/null 2>&1
660-
}
661-
662648
pw_source_label_safe() {
663649
id="$1"; [ -n "$id" ] || { echo ""; return 1; }
664650

@@ -945,7 +931,7 @@ audio_parse_secs() {
945931
in="$*"
946932
norm=$(printf '%s' "$in" | tr -d ' \t\r\n' | tr '[:upper:]' '[:lower:]')
947933
[ -n "$norm" ] || return 1
948-
934+
949935
case "$norm" in
950936
*:*)
951937
IFS=':' set -- "$norm"
@@ -956,7 +942,8 @@ audio_parse_secs() {
956942
*) return 1 ;;
957943
esac
958944
h_val=${h:-0}; m_val=${m:-0}; s_val=${s:-0}
959-
result=$(expr $h_val \* 3600 + $m_val \* 60 + $s_val)
945+
946+
result=$((h_val * 3600 + m_val * 60 + s_val))
960947
printf '%s\n' "$result"
961948
return 0
962949
;;
@@ -965,9 +952,9 @@ audio_parse_secs() {
965952
[0-9]*s|[0-9]*sec|[0-9]*secs|[0-9]*second|[0-9]*seconds)
966953
n=$(printf '%s' "$norm" | sed -n 's/^\([0-9][0-9]*\).*/\1/p'); printf '%s\n' "$n"; return 0 ;;
967954
[0-9]*m|[0-9]*min|[0-9]*mins|[0-9]*minute|[0-9]*minutes)
968-
n=$(printf '%s' "$norm" | sed -n 's/^\([0-9][0-9]*\).*/\1/p'); printf '%s\n' "$(expr $n \* 60)"; return 0 ;;
955+
n=$(printf '%s' "$norm" | sed -n 's/^\([0-9][0-9]*\).*/\1/p'); printf '%s\n' "$((n * 60))"; return 0 ;;
969956
[0-9]*h|[0-9]*hr|[0-9]*hrs|[0-9]*hour|[0-9]*hours)
970-
n=$(printf '%s' "$norm" | sed -n 's/^\([0-9][0-9]*\).*/\1/p'); printf '%s\n' "$(expr $n \* 3600)"; return 0 ;;
957+
n=$(printf '%s' "$norm" | sed -n 's/^\([0-9][0-9]*\).*/\1/p'); printf '%s\n' "$((n * 3600))"; return 0 ;;
971958
*)
972959
tokens=$(printf '%s' "$norm" | sed 's/\([0-9][0-9]*[a-z][a-z]*\)/\1 /g')
973960
total=0; ok=0
@@ -976,11 +963,11 @@ audio_parse_secs() {
976963
u=$(printf '%s' "$t" | sed -n 's/^[0-9][0-9]*\([a-z][a-z]*\)$/\1/p')
977964
case "$u" in
978965
s|sec|secs|second|seconds) add=$n ;;
979-
m|min|mins|minute|minutes) add=$(expr $n \* 60) ;;
980-
h|hr|hrs|hour|hours) add=$(expr $n \* 3600) ;;
966+
m|min|mins|minute|minutes) add=$((n * 60)) ;;
967+
h|hr|hrs|hour|hours) add=$((n * 3600)) ;;
981968
*) return 1 ;;
982969
esac
983-
total=$(expr $total + $add); ok=1
970+
total=$((total + add)); ok=1
984971
done
985972
[ "$ok" -eq 1 ] 2>/dev/null || return 1
986973
printf '%s\n' "$total"
@@ -993,7 +980,6 @@ audio_parse_secs() {
993980
return 0
994981
;;
995982
esac
996-
return 1
997983
}
998984

999985
# --- Local watchdog that always honors the first argument (e.g. "15" or "15s") ---
@@ -1019,7 +1005,7 @@ audio_exec_with_timeout() {
10191005
pid=$!
10201006

10211007
start="$(date +%s 2>/dev/null || echo 0)"
1022-
deadline="$(expr "$start" + "$dur_norm" 2>/dev/null || echo 0)"
1008+
deadline=$((start + dur_norm))
10231009

10241010
# Wait until exit or deadline
10251011
while kill -0 "$pid" 2>/dev/null; do
@@ -1040,7 +1026,7 @@ audio_exec_with_timeout() {
10401026
grace=0
10411027
while kill -0 "$pid" 2>/dev/null && [ "$grace" -lt 3 ]; do
10421028
sleep 1
1043-
grace="$(expr "$grace" + 1)"
1029+
grace=$((grace + 1))
10441030
done
10451031

10461032
# Still alive -> likely D-state. Do NOT wait forever.
@@ -1364,6 +1350,7 @@ parse_clip_metadata() {
13641350
fi
13651351

13661352
# Split extracted fields (rate bits channels)
1353+
# shellcheck disable=SC2086 # Intentional field splitting of generated key=value triplet.
13671354
set -- $metadata
13681355
rate="$1"; bits="$2"; channels="$3"
13691356

@@ -1388,6 +1375,7 @@ generate_clip_testcase_name() {
13881375
metadata="$(parse_clip_metadata "$filename")" || return 1
13891376

13901377
# Extract values using positional parameters and prefix stripping
1378+
# shellcheck disable=SC2086 # Intentional field splitting of generated key=value triplet.
13911379
set -- $metadata
13921380
rate="${1#rate=}"
13931381
bits="${2#bits=}"
@@ -1457,6 +1445,7 @@ validate_clip_name() {
14571445
if [ -n "$config_num" ]; then
14581446
# Generic config name - map to clip by index (1-based)
14591447
# Count total clips first using POSIX-compliant approach
1448+
# shellcheck disable=SC2086 # Intentional field splitting of generated key=value triplet.
14601449
set -- $available_clips
14611450
idx=$#
14621451

@@ -1469,7 +1458,7 @@ validate_clip_name() {
14691458
# Get clip by index (1-based) using POSIX-compliant approach
14701459
current_idx=0
14711460
for clip in $available_clips; do
1472-
current_idx=$(expr $current_idx + 1)
1461+
current_idx=$((current_idx + 1))
14731462
if [ "$current_idx" -eq "$config_num" ]; then
14741463
printf '%s\n' "$clip"
14751464
return 0
@@ -1491,6 +1480,7 @@ validate_clip_name() {
14911480
done
14921481

14931482
# No match found - count available clips for helpful message using POSIX-compliant approach
1483+
# shellcheck disable=SC2086 # Intentional field splitting of space-separated clip list.
14941484
set -- $available_clips
14951485
idx=$#
14961486

0 commit comments

Comments
 (0)