Last active
January 19, 2022 08:17
-
-
Save reneluria/051afee8f3d95fac588b9e13335d38dd to your computer and use it in GitHub Desktop.
Ensure input volume is always above a certain level
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# fix-input-volume.sh - Ensure input volume is always above a certain level | |
# Copyright (C) 2022 Rene Luria <rene@luria.ch> | |
# 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, either version 3 of the License, or | |
# (at your option) any later version. | |
# 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 <https://www.gnu.org/licenses/>. | |
MINIMUM_VOLUME=15 | |
INTERVAL=1 | |
usage() { | |
cat <<EOD | |
fix-volume.sh | |
Ensure input volume is always above a certain level | |
Usage: fix-volume.sh [-m <vol>] [-i <seconds>] | |
-m <vol> : sets minimum volume to <vol> (default: $MINIMUM_VOLUME) | |
-i <seconds> : seconds between each check (default: $INTERVAL) | |
EOD | |
} | |
get_volume() { | |
osascript -e "input volume of (get volume settings)" | |
} | |
set_volume() { | |
local volume="$1" | |
osascript -e "set volume input volume $volume" | |
} | |
min_volume=$MINIMUM_VOLUME | |
interval=$INTERVAL | |
while getopts "hm:i:" OPTION; do | |
case "$OPTION" in | |
h) | |
usage | |
exit 0 | |
;; | |
m) | |
min_volume="$OPTARG" | |
;; | |
i) | |
interval="$OPTARG" | |
;; | |
*) | |
echo "Error: bad arguments" >&2 | |
exit 1 | |
;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
if [[ $# -ne 0 ]]; then | |
echo "Error: too many arguments" >&2 | |
exit 1 | |
fi | |
# check minimum volume argument | |
if ! [[ $min_volume =~ ^[0-9]{1,3}$ ]]; then | |
echo "Error: bad volume format" >&2 | |
exit 1 | |
fi | |
if [[ $min_volume -gt 100 ]]; then | |
echo "Error: volume must be between 0 and 100" >&2 | |
exit 1 | |
fi | |
# check interval argument | |
if ! [[ $interval =~ ^[0-9]+$ ]]; then | |
echo "Error: bad interval format" >&2 | |
exit 1 | |
fi | |
while sleep "$interval"; do | |
volume=$(get_volume) | |
if [[ $volume -lt $min_volume ]]; then | |
echo "Input volume at $volume%, resetting to $min_volume" | |
set_volume "$min_volume" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment