From 5b5315f321b442787bc8ea035e1b6c02cf5b6eea Mon Sep 17 00:00:00 2001 From: David Kramer Date: Sat, 7 Apr 2018 19:47:56 -0600 Subject: [PATCH] Add termux-microphone-record --- scripts/termux-microphone-record | 88 ++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100755 scripts/termux-microphone-record diff --git a/scripts/termux-microphone-record b/scripts/termux-microphone-record new file mode 100755 index 0000000..39bb7c3 --- /dev/null +++ b/scripts/termux-microphone-record @@ -0,0 +1,88 @@ +#!/data/data/com.termux/files/usr/bin/bash +set -e + +SCRIPTNAME=termux-microphone-record + +show_usage () { + echo "Usage: $SCRIPTNAME [args]" + echo "Record using microphone on your device" + echo + echo "-h, help Shows this help" + echo "-f Start recording to specific file" + echo "-l Start recording w/ specified limit (in seconds)" + echo "-d Start recording w/ defaults" + echo "-i Get info about current recording" + echo "-q Quits recording" + exit 0 +} + +if [ $# -eq 0 ]; then + echo "No arguments supplied" + show_usage + exit 1 +fi + +FILE_FLAG=1 +LIMIT_FLAG=2 +INFO_FLAG=4 +RECORD_FLAG=8 +QUIT_FLAG=16 + +FLAGS=0 + +call_api () { + /data/data/com.termux/files/usr/libexec/termux-api MicRecorder $@ +} + +set_flag () { + FLAGS=$((FLAGS | $1 )); +} + +usage_error () { + echo "ERROR: $@" + show_usage + exit 1 +} + +PARAMS="" + +while getopts :h,f:,l:,d,i,q option +do + case "$option" in + h) show_usage ;; + f) set_flag $FILE_FLAG; PARAMS="$PARAMS --es file $(pwd)/$OPTARG" ;; + + # Multiply by 1000 to pass to API in seconds, rather than milliseconds + l) set_flag $LIMIT_FLAG; PARAMS="$PARAMS --ei limit $((OPTARG * 1000))" ;; + + d) set_flag $RECORD_FLAG; PARAMS="$PARAMS -a record" ;; + i) set_flag $INFO_FLAG; PARAMS="-a info" ;; + q) set_flag $QUIT_FLAG; PARAMS="-a quit" ;; + ?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1; ;; + esac +done +shift $((OPTIND - 1)) + +# Validate options were set properly + +if [ $((FLAGS & QUIT_FLAG)) -ne 0 ] && [ $FLAGS -ne $QUIT_FLAG ]; then + usage_error "No other options should be specified with -q quit!" +fi + +if [ $((FLAGS & INFO_FLAG)) -ne 0 ] && [ $FLAGS -ne $INFO_FLAG ]; then + usage_error "No other options should be specified with -i info!" +fi + +# Add implied record action if not explicitly set w/ other valid options +if [ $((FLAGS & (FILE_FLAG | LIMIT_FLAG))) -ne 0 ]; then + if [ $((FLAGS & RECORD_FLAG)) -eq 0 ]; then + PARAMS="$PARAMS -a record" + fi +fi + +# If nothing was set, we know we received invalid arguments +if [ $FLAGS -eq 0 ]; then + usage_error "Invalid argument: '$@'" +fi + +call_api $PARAMS