-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhifiberry_connect_devices
executable file
·80 lines (72 loc) · 2.46 KB
/
hifiberry_connect_devices
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
SPDIF_D="hw:2,0"
USB_D="hw:1,0"
HDMI_D="hw:0,0"
FORMAT="-f S16_LE -c 2 --buffer-size=1024"
STDERR_SINK="/dev/null"
#STDERR_SINK="/dev/stderr"
QUERY_PERIOD=0.25
REGFILE=/proc/asound/sndrpihifiberry/registers
#try to terminate them. if that doesn't work, kill
reset_dump() {
if [[ !(-z $(pgrep arecord) && -z $(pgrep aplay)) ]]; then
killall arecord >$STDERR_SINK 2>&1
killall aplay >$STDERR_SINK 2>&1
fi
sleep $QUERY_PERIOD
while [[ !(-z $(pgrep arecord) && -z $(pgrep aplay)) ]]; do
killall -9 arecord >$STDERR_SINK 2>&1
killall -9 aplay >$STDERR_SINK 2>&1
done
}
#get sample rate by probing SPDIF input for one second
dummy_rec() {
if [[ ! -z $(pgrep arecord) ]]; then
echo "Already recording"
echo "Already recording" 2>$STDERR_SINK
else
arecord -D $SPDIF_D -f cd -d 1 2>$STDERR_SINK
fi
}
get_sample_rate() {
if [[ -z $1 ]]; then
reset_dump
fi
dummy_rec | while read F; do
cat $REGFILE | awk '$0~/ind freq/{print $3*1000}$0~/ lock/{print $2}$0~/intstat/{print $2}';
break;
done;
}
#the actual signal dumper
dump_signal() {
if [[ (! -z $1) ]]; then
SAMPLE_RATE=$1
reset_dump
arecord -D $SPDIF_D -r $SAMPLE_RATE $FORMAT 2>$STDERR_SINK | aplay -D $SPDIF_D -r $SAMPLE_RATE $FORMAT 2>$STDERR_SINK &
fi
sleep $QUERY_PERIOD
cat $REGFILE >$STDERR_SINK
}
#main loop
while true; do
SAMPLE_RATE_NEW=$(get_sample_rate $SAMPLE_RATE)
if [[ $(echo "$SAMPLE_RATE_NEW" | wc -l) == 3 ]]; then
CLOCK=$(echo "$SAMPLE_RATE_NEW" | head -n 1)
INTSTAT=$(echo "$SAMPLE_RATE_NEW" | head -n 3 | tail -n 1)
SAMPLE_RATE_NEW=$(echo "$SAMPLE_RATE_NEW" | head -n 2 | tail -n 1)
echo A $SAMPLE_RATE $SAMPLE_RATE_NEW $CLOCK $INTSTAT $(($INTSTAT & 0xf8)) >$STDERR_SINK
if [[ !(-z $SAMPLE_RATE_NEW) ]]; then
if [[ -z $SAMPLE_RATE || !($SAMPLE_RATE_NEW == $SAMPLE_RATE) ]]; then
SAMPLE_RATE=$SAMPLE_RATE_NEW
echo "Switching to sample rate "$SAMPLE_RATE" "$FORMAT" device: "$SPDIF_D
dump_signal $SAMPLE_RATE
echo B $SAMPLE_RATE $SAMPLE_RATE_NEW $CLOCK $INTSTAT $(($INTSTAT & 0xf8)) >$STDERR_SINK
fi
if [[ !($CLOCK == "ON") || ($(($INTSTAT & 0xf8)) == 8) ]]; then
echo $(($INTSTAT & 0xf8)) >$STDERR_SINK
unset SAMPLE_RATE
fi
fi
fi
sleep $QUERY_PERIOD
done