Files
alice-backend/station/station-supervisor.sh
T

71 lines
2.7 KiB
Bash
Executable File

#!/bin/sh
# Long-lived supervisor. Logs are replaced before each run to stay bounded on tmpfs.
set -u
cd /data/opt/alice-backend || exit 1
umask 077
export ALICE_PLAYER=/data/opt/alice-backend/pulse-player.sh
exec 9>/tmp/alice-backend-mic.lock
flock -n 9 || exit 1
# Match stock boot configuration and expose the physical microphone mute input.
echo 1 > /proc/sys/vm/overcommit_memory || echo 'Cannot set overcommit_memory' >&2
if [ ! -d /sys/class/gpio/gpio414 ]; then
echo 414 > /sys/class/gpio/export || echo 'Cannot export GPIO414' >&2
if [ -d /sys/class/gpio/gpio414 ]; then
echo in > /sys/class/gpio/gpio414/direction || echo 'Cannot set GPIO414 direction' >&2
fi
fi
child=
pc_receiver=
timer=
stopping=0
stop() {
stopping=1
for p in "$child" "$pc_receiver" "$timer"; do [ -z "$p" ] || kill -TERM "$p" 2>/dev/null || :; done
}
trap stop TERM INT HUP
reset_duck() {
helper=${ALICE_MUSIC_HELPER:-/data/opt/alice-backend/music-control}
if [ -x "$helper" ]; then
printf '%s\n' '{"name":"duck","arguments":{"active":false}}' | "$helper" >/dev/null 2>&1 || :
fi
}
reset_duck
while [ "$stopping" -eq 0 ]; do
# Let DHCP and time synchronization finish before accepting voice turns.
if [ ! -r device-token ] || [ "$(date +%s)" -lt 1700000000 ] || ! ip route | grep -q '^default '; then
sleep 3 & timer=$!; wait "$timer" || :; timer=
continue
fi
DEVICE_TOKEN=$(cat device-token)
export DEVICE_TOKEN
if [ -x ./scream-receiver ] && [ -r pc-audio-receiver.conf ]; then
: >/tmp/alice-pc-audio.log
(
read -r listen
read -r source
exec ./scream-receiver -listen "$listen" -source "$source"
) <pc-audio-receiver.conf >>/tmp/alice-pc-audio.log 2>&1 9>&- &
pc_receiver=$!
fi
: > /tmp/alice-station.log
./alice-station -mode wake -voice-threshold 12 -vqe-helper /data/opt/alice-backend/vqe-capture.sh -vqe-wake-model /system/vendor/quasar/spotter_models/activation/alisa >> /tmp/alice-station.log 2>&1 9>&- &
child=$!
while [ "$stopping" -eq 0 ] && kill -0 "$child" 2>/dev/null; do
if [ "$(wc -c < /tmp/alice-station.log)" -gt 524288 ]; then : >/tmp/alice-station.log; fi
if [ -n "$pc_receiver" ] && [ "$(wc -c < /tmp/alice-pc-audio.log)" -gt 524288 ]; then : >/tmp/alice-pc-audio.log; fi
sleep 3 & timer=$!; wait "$timer" || :; timer=
done
wait "$child" || :
# A signal interrupts wait; reap the station before releasing the microphone lock.
[ "$stopping" -eq 0 ] || wait "$child" 2>/dev/null || :
child=
if [ -n "$pc_receiver" ]; then
kill -TERM "$pc_receiver" 2>/dev/null || :
wait "$pc_receiver" 2>/dev/null || :
pc_receiver=
fi
[ "$stopping" -ne 0 ] && break
sleep 5 & timer=$!; wait "$timer" || :; timer=
done
reset_duck