wxrx/record.sh

144 lines
2.7 KiB
Bash
Raw Permalink Normal View History

2022-07-06 16:57:31 -07:00
#!/usr/bin/env bash
2022-01-22 15:15:37 -08:00
## Receives a pass of a NOAA Satellite
##
## usage: __PROG__ [options]
##
2022-01-25 11:14:53 -08:00
set -o pipefail
2022-01-23 15:34:20 -08:00
prog="$0"
2022-02-02 07:27:26 -08:00
me=${HELP:-$(basename "${prog}")}
rootdir=$(dirname $(realpath $0))
2022-01-22 15:15:37 -08:00
source "${rootdir}/lib/utils.sh"
2022-01-23 15:34:20 -08:00
# defaults
freq="137M"
duration=10
gain_flag=''
sample_rate=11025
bandwidth=40000
now=$(date +%s)
output_file=./pass.wav
# bandwidth = 2 * (17kHz deviation + 2.4kHz tone) * doppler shift (~1.2kHz)
# wavfile is determined by options
# DEPRECATED
outdir=.
outfile=pass.wav
2022-01-23 15:34:20 -08:00
2022-02-15 12:54:11 -08:00
function process_args() {
2022-01-23 15:34:20 -08:00
while (( "$#" ))
do
case $1 in
2022-01-22 15:15:37 -08:00
## --freq <frequency> (default: 137M)
2022-01-23 15:34:20 -08:00
'--freq')
freq=${2}
log "frequency set to ${freq}"
shift
;;
2022-01-22 15:15:37 -08:00
## --duration <seconds> (default: 900)
2022-01-23 15:34:20 -08:00
'--duration')
duration=${2}
log "duration set to ${duration}"
shift
;;
2022-01-22 15:15:37 -08:00
## --gain <integer> (default: auto)
2022-01-23 15:34:20 -08:00
'--gain')
gain_flag="-g ${2}"
log "gain set to ${2}"
2022-01-23 15:34:20 -08:00
shift
;;
# --output|-o <file> name/path of the output file or directory
'--output' | '-o')
output_file=${2}
2022-01-23 15:34:20 -08:00
shift
;;
## --noaa-15 alias --freq 137620000
2022-01-25 08:31:51 -08:00
'--noaa-15')
freq=137620000
;;
## --noaa-18 alias --freq 137912500
2022-01-25 08:31:51 -08:00
'--noaa-18')
freq=137912500
;;
## --noaa-19 alias --freq 137100000
2022-01-25 08:31:51 -08:00
'--noaa-19')
freq=137100000
;;
2022-01-25 11:14:53 -08:00
# TODO: ## --bias-t, -T enable hardware Bias-T power
## --monitor monitor recording
'--monitor')
monitor=1
;;
2022-01-23 15:34:20 -08:00
## --help, -h
'--help' | '-h')
usage
exit
;;
2022-01-22 15:15:37 -08:00
2022-01-23 15:34:20 -08:00
*)
logerr "Unknown argument %s" ${1}
2022-01-23 15:34:20 -08:00
exit 1
;;
esac
shift
done
2022-02-15 12:54:11 -08:00
}
2022-01-22 15:15:37 -08:00
2022-01-25 11:14:53 -08:00
function demodulate_pass() {
timeout ${duration} rtl_fm -T -f ${freq} -M fm ${gain_flag} -s ${bandwidth} -r ${sample_rate} -E wav -E deemp -F 9 -A fast
2022-01-25 11:14:53 -08:00
}
function resample_pass() {
2022-07-02 19:07:28 -07:00
sox -r ${sample_rate} -t raw -e s -b 16 -c 1 -V1 - ${1}
2022-01-25 11:14:53 -08:00
}
function monitor_pass() {
( [ -z $monitor ] && cat || tee >(play -r ${sample_rate} -t raw -es -b 16 -c 1 -V1 -) )
}
2022-01-22 15:15:37 -08:00
2022-07-02 19:07:28 -07:00
function device_is_connected() {
lsusb | grep RTL2838 > /dev/null
}
2022-02-15 12:54:11 -08:00
# If sourced, return now
if [ "${0}" != "${BASH_SOURCE[0]}" ]; then
return
fi
# -- Do work below here --
process_args $@
2022-07-02 19:07:28 -07:00
if device_is_connected; then
log "Found device"
else
logerr "No devices found"
exit 2
fi
output_file=${output_file:-'./wxrx.wav'}
log "Current timestamp: %s" ${now}
2022-01-23 12:52:13 -08:00
log "Listening for signal on ${freq} for ${duration} seconds (gain: ${gain})"
2022-01-25 11:14:53 -08:00
2022-07-02 19:07:28 -07:00
demodulate_pass | monitor_pass | resample_pass ${output_file}
2022-01-25 11:14:53 -08:00
# Cleanup
log "Finished writing to ${output_file}"
2022-01-22 15:15:37 -08:00
# Verify that a wavfile was created
if [ ! -f ${output_file} ]; then
2022-07-02 19:07:28 -07:00
logerr "No output file created."
2022-01-22 15:15:37 -08:00
exit 1
fi