check_audio_input.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env bash
  2. # Checks --audio-input, --audio-input-volume and --audio-input-threshold against a private PulseAudio server (never the
  3. # desktop's, tools/wecompare/wine/audio_sink.sh): a tone plays into a null sink that stands for a microphone while the
  4. # default output stays silent, and the engine's own capture statistics (LWE_AUDIO_STATS) say what it listened to.
  5. # Usage: tools/check_audio_input.sh <engine binary> [scene dir] [assets dir]
  6. set -uo pipefail
  7. HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  8. BINARY=${1:?engine binary}
  9. SCENE=${2:-/workspace/SteamLibrary/steamapps/workshop/content/431960/1509527803}
  10. ASSETS=${3:-/workspace/SteamLibrary/steamapps/common/wallpaper_engine/assets}
  11. SINK="$HERE/wecompare/wine/audio_sink.sh"
  12. work=$(mktemp -d /tmp/lwe-audio-input.XXXXXX)
  13. trap 'kill $(jobs -p) 2>/dev/null; "$SINK" stop > /dev/null 2>&1; rm -rf "$work"' EXIT
  14. [ -d "$SCENE" ] || { echo "SKIP: $SCENE is not installed"; exit 0; }
  15. "$SINK" start || { echo "SKIP: no private PulseAudio"; exit 0; }
  16. eval "$("$SINK" env)"
  17. export HEADLESS_RENDER_PULSE_SERVER=$PULSE_SERVER
  18. unset PULSE_SERVER
  19. private_pactl () { PULSE_SERVER=$HEADLESS_RENDER_PULSE_SERVER pactl "$@"; }
  20. private_pactl load-module module-null-sink sink_name=lwe_mic rate=44100 channels=2 sink_properties=device.description=lwe_mic > /dev/null || exit 1
  21. failures=0
  22. fail () { echo "FAIL $1"; failures=$((failures + 1)); }
  23. # a 440 Hz tone (sine's 1/8 amplitude times the given factor) into the "microphone" sink for 14 seconds
  24. play_tone () {
  25. PULSE_SERVER=$HEADLESS_RENDER_PULSE_SERVER ffmpeg -loglevel quiet -f lavfi -i "sine=frequency=440:sample_rate=44100" -af "volume=$1,aformat=channel_layouts=stereo" \
  26. -t 14 -f pulse -device lwe_mic tone > /dev/null 2>&1 &
  27. }
  28. # runs the engine for a few seconds and keeps its capture statistics lines in $work/<label>.stats
  29. run_engine () {
  30. local label=$1
  31. shift
  32. : > "$work/run.log"
  33. LWE_AUDIO_STATS=1 HEADLESS_RENDER_DELAY=4000 HEADLESS_RENDER_TIMEOUT=9 HEADLESS_RENDER_SIZE=640x360 \
  34. HEADLESS_RENDER_KEEP_RUNNING=1 "$HERE/headless_render.sh" "$BINARY" "$work/shot.png" --assets-dir "$ASSETS" \
  35. "$@" "$SCENE" > "$work/run.log" 2>&1
  36. grep "Audio capture:" "$work/run.log" > "$work/$label.stats"
  37. }
  38. field () {
  39. # the largest value of the last three statistics lines, $1 label, $2 field name
  40. local pattern="$2 \\([0-9.e+-]*\\)"
  41. [ "$2" = blocks ] && pattern="\\([0-9]*\\) blocks"
  42. grep "Audio capture:" "$work/$1.stats" | tail -3 | sed -n "s/.*[ ,]$pattern.*/\\1/p" | sort -g | tail -1
  43. }
  44. play_tone 3
  45. sleep 1
  46. run_engine default
  47. peak=$(field default peak)
  48. [ -n "$peak" ] && awk "BEGIN { exit !($peak == 0) }" && echo "ok the default output stays silent (peak $peak)" \
  49. || fail "default capture heard the microphone sink (peak ${peak:-none})"
  50. wait
  51. play_tone 3
  52. sleep 1
  53. run_engine mic --audio-input lwe_mic.monitor
  54. peak=$(field mic peak)
  55. blocks=$(field mic blocks)
  56. awk "BEGIN { exit !(${peak:-0} > 0.2) }" && echo "ok --audio-input hears the tone (peak $peak, $blocks blocks)" \
  57. || fail "--audio-input lwe_mic.monitor heard nothing (peak ${peak:-none})"
  58. low50=$(field mic low)
  59. wait
  60. play_tone 3
  61. sleep 1
  62. run_engine named --audio-input "Monitor of lwe_mic"
  63. peak=$(field named peak)
  64. awk "BEGIN { exit !(${peak:-0} > 0.2) }" && echo "ok a source is found by its description (peak $peak)" \
  65. || fail "--audio-input by description heard nothing (peak ${peak:-none})"
  66. wait
  67. play_tone 3
  68. sleep 1
  69. run_engine sinkname --audio-input lwe_mic
  70. peak=$(field sinkname peak)
  71. awk "BEGIN { exit !(${peak:-0} > 0.2) }" && echo "ok a sink name selects its monitor (peak $peak)" \
  72. || fail "--audio-input lwe_mic (a sink) heard nothing (peak ${peak:-none})"
  73. wait
  74. play_tone 3
  75. sleep 1
  76. run_engine loud --audio-input lwe_mic.monitor --audio-input-volume 100
  77. low100=$(field loud low)
  78. if awk "BEGIN { exit !(${low50:-0} > 0 && ${low100:-0} / ${low50:-1} > 1.8 && ${low100:-0} / ${low50:-1} < 2.2) }"; then
  79. echo "ok --audio-input-volume 100 doubles the bands ($low50 -> $low100)"
  80. else
  81. fail "volume 100 should double the bands of volume 50 ($low50 -> ${low100:-none})"
  82. fi
  83. wait
  84. play_tone 3
  85. sleep 1
  86. run_engine gated --audio-input lwe_mic.monitor --audio-input-threshold 900
  87. blocks=$(field gated blocks)
  88. [ "${blocks:-1}" = "0" ] && echo "ok a threshold above the tone's level silences it" \
  89. || fail "threshold 900 should gate the tone (blocks ${blocks:-none})"
  90. wait
  91. play_tone 3
  92. sleep 1
  93. run_engine open --audio-input lwe_mic.monitor --audio-input-threshold 100
  94. blocks=$(field open blocks)
  95. awk "BEGIN { exit !(${blocks:-0} > 0) }" && echo "ok a threshold below the tone's level lets it through" \
  96. || fail "threshold 100 should pass the tone (blocks ${blocks:-none})"
  97. run_engine missing --audio-input no_such_source
  98. grep -q "Audio input 'no_such_source' not found" "$work/run.log" && echo "ok an unknown input falls back with a message" \
  99. || fail "unknown input was not reported"
  100. exit $((failures > 0))