vol 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/bin/bash
  2. MIX=amixer
  3. declare -i LO=0 # Minimum volume; try 10 to avoid complete silence
  4. declare -i HI=100 # Maximum volume; try 95 to avoid distortion
  5. declare -i ADJ=3 # Volume adjustment step size
  6. usage ()
  7. {
  8. echo "Usage: `basename $0` [ - | + | N ]" >&2
  9. echo " where N is a whole number between $LO and $HI, inclusive." >&2
  10. exit 1
  11. }
  12. # Zero or one argument
  13. [ $# -le 1 ] || usage
  14. # Argument must be one of: empty, -, +, number
  15. [[ $1 == ?(-|+|+([0-9])) ]] || usage
  16. ARG="$1"
  17. # Number argument
  18. if [[ $ARG == +([0-9]) ]]; then
  19. # Strip leading zeros
  20. while [[ $ARG == 0+([0-9]) ]]; do
  21. ARG=${ARG#0}
  22. done
  23. # Must be between LO and HI
  24. (( ARG >= LO && ARG <= HI )) || usage
  25. fi
  26. EXE=$(which $MIX)
  27. if [ -z "$EXE" ]; then
  28. echo "Error: $MIX not found. Try \"sudo apt-get install alsa-utils\" first." >&2
  29. exit 2
  30. fi
  31. GET=$($EXE cget numid=1)
  32. declare -i MIN=$(echo $GET | /bin/grep -E -o -e ',min=[^,]+' | /bin/grep -E -o -e '[0-9-]+')
  33. declare -i MAX=$(echo $GET | /bin/grep -E -o -e ',max=[^,]+' | /bin/grep -E -o -e '[0-9-]+')
  34. declare -i VAL=$(echo $GET | /bin/grep -E -o -e ': values=[0-9+-]+' | /bin/grep -E -o -e '[0-9-]+')
  35. if (( MIN >= MAX || VAL < MIN || VAL > MAX )); then
  36. echo "Error: could not get the right values from $MIX output." >&2
  37. exit 3
  38. fi
  39. declare -i LEN=0
  40. (( LEN = MAX - MIN ))
  41. declare -i ABS=0
  42. (( ABS = VAL - MIN ))
  43. declare -i PCT=0
  44. (( PCT = 100 * ABS / LEN ))
  45. if [ ! -z "$ARG" ]; then
  46. declare -i OLD=$PCT
  47. if [[ "$ARG" == "+" ]]; then
  48. (( PCT += ADJ ))
  49. elif [[ "$ARG" == "-" ]]; then
  50. (( PCT -= ADJ ))
  51. else
  52. PCT=$ARG
  53. fi
  54. if [[ "$PCT" != "$OLD" ]]; then
  55. (( ABS = PCT * LEN / 100 ))
  56. (( VAL = MIN + ABS ))
  57. $EXE -q cset numid=1 -- $VAL
  58. fi
  59. fi
  60. echo $PCT
  61. exit 0