Skip to content

Instantly share code, notes, and snippets.

@dnedrow
Last active November 7, 2023 18:26
Show Gist options
  • Select an option

  • Save dnedrow/01dd349ce520eadf3f979aebe5c9a064 to your computer and use it in GitHub Desktop.

Select an option

Save dnedrow/01dd349ce520eadf3f979aebe5c9a064 to your computer and use it in GitHub Desktop.
Returns a percentage value for use with scripts that play sounds on the Mac
#!/usr/bin/env zsh
# Usage:
# Source this and then use the function with afplay
# afplay -v $( getsuggestedvolume ) foo.wav
function getsuggestedvolume() {
(( systemVolume=$(osascript -e 'output volume of (get volume settings)') ))
(( currentVolume=`echo "scale=2; $systemVolume/100" | bc -l` ))
(( normalizedVolume=`echo "scale=2; $currentVolume/3" | bc -l` ))
(( maxVolume=0.33 ))
printf "%.2f" $(( $normalizedVolume<=$maxVolume?$normalizedVolume:$maxVolume ))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment