forked from m-r-s/hearingaid-prototype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommander.sh
executable file
·139 lines (128 loc) · 4.36 KB
/
commander.sh
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
# Copyright 2018 Marc René Schädler
#
# This file is part of the mobile hearing aid prototype project
# The the mobile hearing aid prototype project is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
#
# The mobile hearing aid prototype project is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with the mobile hearing aid prototype project. If not, see http://www.gnu.org/licenses/.
DIR=$(cd "$( dirname "$0" )" && pwd)
TOOLSDIR="${DIR}/tools"
mhacontrol() {
echo "$@" | nc -w 1 127.0.0.1 33337 | grep -m1 -E "\(MHA:[^)]+\)" &>/dev/null
}
mhaplay() {
local FILE="$1"
local LEVELMODE="$2"
local LEVEL="$3"
local LOOP="$4"
mhacontrol "mha.transducers.mhachain.playback.filename = "
mhacontrol "mha.transducers.mhachain.playback.levelmode = ${LEVELMODE}"
mhacontrol "mha.transducers.mhachain.playback.level = ${LEVEL}"
mhacontrol "mha.transducers.mhachain.playback.loop = ${LOOP}"
mhacontrol "mha.transducers.mhachain.playback.filename = ${FILE}"
}
text2speech() {
local RETVAL=1
local FILE=$(mktemp -p /dev/shm)
flite -t "$1" "$FILE"
mhaplay "$FILE" "rms" "70" "no"
sleep 0.1
rm "$FILE"
}
feedback() {
local DURATION="$1"
jack_disconnect MHA:out_1 system:playback_2
jack_disconnect MHA:out_2 system:playback_1
killall abhang -9 &> /dev/null
(cd "${TOOLSDIR}/signals" && ./pinknoise) 2>&1 &
sleep 0.5
jack_connect pinknoise:output_1 system:playback_2
jack_connect pinknoise:output_2 system:playback_1
jack_rec -f "/dev/shm/feedback.wav" -d "$DURATION" -b 32 pinknoise:output_1 pinknoise:output_2 system:capture_1 system:capture_2
killall pinknoise -9 &> /dev/null
(cd "${TOOLSDIR}" && taskset -c 0 nice ./update_abhang_configuration.m)
rm "/dev/shm/feedback.wav"
(cd "${TOOLSDIR}/abhang/src/jack" && taskset -c 2 ./abhang) 2>&1 &
sleep 1
jack_connect system:capture_1 abhang:input_1
jack_connect system:capture_2 abhang:input_2
jack_connect MHA:out_1 abhang:input_3
jack_connect MHA:out_2 abhang:input_4
jack_connect abhang:output_1 MHA:in_1
jack_connect abhang:output_2 MHA:in_2
jack_connect MHA:out_1 system:playback_2
jack_connect MHA:out_2 system:playback_1
}
thresholdnoise() {
local STATUS="$1"
case "$STATUS" in
on)
jack_connect thresholdnoise:output_1 abhang:input_3
jack_connect thresholdnoise:output_2 abhang:input_4
jack_connect thresholdnoise:output_1 system:playback_2
jack_connect thresholdnoise:output_2 system:playback_1
;;
off)
jack_disconnect thresholdnoise:output_1 system:playback_2
jack_disconnect thresholdnoise:output_2 system:playback_1
jack_disconnect thresholdnoise:output_1 abhang:input_3
jack_disconnect thresholdnoise:output_2 abhang:input_4
;;
esac
}
live() {
local STATUS="$1"
case "$STATUS" in
on)
jack_connect abhang:output_1 MHA:in_1
jack_connect abhang:output_2 MHA:in_2
;;
off)
jack_disconnect abhang:output_1 MHA:in_1
jack_disconnect abhang:output_2 MHA:in_2
;;
esac
}
record() {
local DURATION=$1
local TARGETFILE="${HOME}/recordings/"$(date --iso-8601=seconds)".wav"
mhacontrol "mha.transducers.mhachain.record.record = yes"
sleep "$DURATION"
mhacontrol "mha.transducers.mhachain.record.record = no"
cp "/dev/shm/recording.wav" "$TARGETFILE"
}
while true ; do
while read line; do
COMMAND="${line%% *}"
ARGUMENTS=("${line#${COMMAND} }")
echo "COMMAND '$COMMAND'"
echo "ARGUMENTS '$ARGUMENTS'"
case "$COMMAND" in
"feedback")
feedback ${ARGUMENTS[@]}
;;
"text2speech")
text2speech "${ARGUMENTS[@]}"
;;
"mhacontrol")
mhacontrol ${ARGUMENTS[@]}
;;
"mhaplay")
ARGUMENTS=(${ARGUMENTS[@]})
# level is -20*log10(20*10^-6) = 93.979
mhaplay "${ARGUMENTS[0]}" "relative" "93.979" "${ARGUMENTS[1]}"
;;
"thresholdnoise")
thresholdnoise ${ARGUMENTS[@]}
;;
"live")
live ${ARGUMENTS[@]}
;;
"record")
record ${ARGUMENTS[@]}
;;
esac
done < commandqueue
done