diff --git a/scripts/termux-microphone-record b/scripts/termux-microphone-record new file mode 100644 index 0000000..a127ece --- /dev/null +++ b/scripts/termux-microphone-record @@ -0,0 +1,77 @@ +#!/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 Shows this help" + echo "-d Start recording w/ defaults" + echo "-f Start recording to specific file" + echo "-l Start recording w/ specified limit (in seconds)" + echo "-i Get info about current recording" + echo "-q Quits recording" +} + +usage_error () { + echo + show_usage + exit 1 +} + +sole() { + if [ "$1 $2" != "-$option $OPTARG" ] || [ $# -gt 2 ]; then + echo "ERROR: No other options should be specified with -$option!" + usage_error + fi +} + +add_params() { + for i in "$@"; do + params[index++]="$i" + done +} + +call_api () { + /data/data/com.termux/files/usr/libexec/termux-api MicRecorder "$@" +} + +while getopts h,f:,l:,d,i,q option +do + case "$option" in + h) sole "$@" + ;; + f) record=yes + dir="$(dirname "$OPTARG")" + file="$(basename "$OPTARG")" + dir="$(realpath "$dir")" + add_params --es file "$dir/$file" + ;; + # API takes limit in milliseconds + l) record=yes + add_params --ei limit $((OPTARG * 1000)) + ;; + d) record=yes + ;; + i) sole "$@" + add_params -a info + ;; + q) sole "$@" + add_params -a quit + ;; + *) usage_error + ;; + esac +done + +if [ -v record ]; then + add_params -a record +fi + +if [ -v params ]; then + call_api "${params[@]}" +else + show_usage +fi