-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_simo.sh
178 lines (158 loc) · 4.53 KB
/
example_simo.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/bash
#
# This file is part of the sdrterm distribution
# (https://github.com/peads/sdrterm).
# with code originally part of the demodulator distribution
# (https://github.com/peads/demodulator).
# Copyright (c) 2023-2024 Patrick Eads.
#
# This program 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, version 3.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
#
####
# Usage: ./example_simo.sh -i [<file> | <host>:<port>] -eB -r1024k -d25 -c"-1.2E+3" -t123.456M" --vfos=15000,-15000,30000" -w5k
####
OIFS=$IFS
function log {
echo "$(date +'%F %T')Z: ${1}";
}
ts=$(date +%Y%m%d%H%M);
log "timestamp: ${ts}";
if [[ -z ${SDRTERM_EXEC} ]]; then
SDRTERM_EXEC="python -m sdrterm";
fi
if [[ -z ${DSD_CMD} ]]; then
DSD_CMD="dsd -q -i - -o /dev/null -n";
fi
if [[ -z ${OUT_PATH} ]]; then
OUT_PATH=/tmp;
fi
declare -A pids;
port=0;
params=""
i="\0";
set -u
for i in ${@:1}; do
params+="${i} ";
done
unset i;
cmd="${SDRTERM_EXEC} ${params} --simo 2>&1";
set -u;
echo "LOG: ${cmd}";
coproc SDRTERM { eval "$cmd"; }
exec {SDR_IN}<&${SDRTERM[0]}- {SDR_OUT}>&${SDRTERM[1]}-
unset cmd;
pids[0]="${SDRTERM_PID}";
host="";
port="";
decimatedFs="";
mainPid="";
declare -a outFiles;
function cleanup {
kill $SDRTERM_PID;
kill $mainPid;
kill "${pids[@]}";
i="\0";
set -u;
for i in "${!logFiles[@]}"; do
while IFS= ; read -r line; do
log "${line}";
done < ${logFiles["$i"]}
rm "${logFiles[$i]}";
done
unset i;
i="\0";
set -u;
for i in "${outFiles[@]}"; do
outFilesStr="$(stat -c"%n:%s" ${i})";
rm -f "${outFilesStr//:44/}";
done
unset i;
}
function generateRegex {
echo "s/^\s*\"*${1}\"*:\s*${2},*\s*$/\1/g"
}
while IFS= ; read -r line; do
log "${line}"
if [[ ! -z $(echo $line | grep "host" -) ]]; then
host=$(sed -E "`generateRegex "host" '\"(([a-zA-Z0-9]+)+)\"'`" <<< $line);
fi
if [[ ! -z $(echo $line | grep "vfos" -) ]]; then
vfos=$(sed -E "`generateRegex "vfos" '\"((-*[0-9]+,*)+)\"'`" <<< $line);
fi
if [[ ! -z $(echo $line | grep "tunedFreq" -) ]]; then
tuned=$(sed -E "`generateRegex "tunedFreq" "([0-9]+)"`" <<< $line);
fi
if [[ ! -z "$(echo $line | grep "decimatedFs" -)" ]]; then
decimatedFs=$(sed -E "`generateRegex "decimatedFs" "([0-9]+)"`" <<< $line);
fi
if [[ ! -z $(echo $line | grep "Started proc Main") ]]; then
mainPid=$(sed -E "`generateRegex "Started proc Main" "([0-9]+)"`" <<< $line);
pids[1]=$mainPid;
fi
if [[ $line == *"Accepting"* ]]; then
port=$(echo $line | grep "Accepting" - | sed -E "s/^.*\('[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+',\s*([0-9]+)\).*$/\1/g");
break;
fi
done <&"${SDR_IN}"
declare -A logFiles;
i="\0";
set -u;
IFS=, ; read -r -a vfos <<< "$vfos";
for i in "${vfos[@]}"; do
tmp_in="\0";
set -u;
tmp_out="\0";
set -u;
freq=$(( i + tuned ));
set -u;
coprocName="SOCAT_${freq}";
set -u;
fileName="/tmp/log-${freq}-${ts}";
## swap this out if you're not using the dsd code from: https://github.com/peads/dsd
# outFile="${OUT_PATH}/out-${freq}-${ts}.wav"
# set -u;
# cmd="socat TCP4:${host}:${port} - | sox -q -D -B -traw -b64 -ef -r${decimatedFs} - -traw -b16 -es -r48k - 2>/dev/null | ${DSD_CMD} -w ${outFile} 2>${fileName}"
outFile="${OUT_PATH}/out-${freq}-${ts}.mp3"
set -u;
cmd="socat TCP4:${host}:${port} - |
sox -q -D -B -traw -b64 -ef -r${decimatedFs} - -traw -b16 -es -r48k - 2>/dev/null |
${DSD_CMD} -w - 2>${fileName} |
lame -q0 --bitwidth 16 --signed -s8 -r -mm --preset medium - ${outFile} 2>&1 > /dev/null";
set -u;
log "${cmd}";
eval "coproc ${coprocName} { ${cmd}; }"
eval "exec {tmp_in}<&\${${coprocName}[0]}- {tmp_out}>&\${${coprocName}[1]}-";
eval "pids[\"${coprocName}\"]=\${${coprocName}_PID}";
logFiles["${coprocName}"]=${fileName};
outFiles+=(${outFile});
unset fileName;
unset cmd;
unset freq;
unset coprocName;
unset tmp_in;
unset tmp_out;
sleep 0.1; #TODO figure out a better way to sync this
done
unset i;
while IFS= ; read -r line; do
log "${line}"
if [[ $line == *"established"* ]]; then
break;
fi
done <&"${SDR_IN}"
trap cleanup EXIT;
log "Awaiting sdrterm";
wait $SDRTERM_PID;
ret=$?;
log "sdrterm returned: ${ret}";
exit;