Skip to content

Instantly share code, notes, and snippets.

@AkBKukU
Created February 9, 2016 18:18
Show Gist options
  • Select an option

  • Save AkBKukU/b1c4ac785f92579bbba0 to your computer and use it in GitHub Desktop.

Select an option

Save AkBKukU/b1c4ac785f92579bbba0 to your computer and use it in GitHub Desktop.
#!/bin/bash
MAXVOL=40
MAXMICVOL=25
SINKAPP="mpd"
readfile=true
if [ "d$1" != "d" ]
then
MAXVOL=$1
echo "$MAXVOL" > ~/.maxvol
fi
RIGHTPTOP=162311
RIGHTPBOT=114070
RIGHTPRANBOT=$(expr $RIGHTPBOT - 10)
RIGHTPDIF=$(expr $RIGHTPTOP - $RIGHTPBOT)
maxdiv="0.$(echo $MAXVOL)"
maxmicdiv="0.$(echo $MAXMICVOL)"
paused="0"
voladjust=$(expr 100 - $MAXVOL)
#--Gets input from the joystick device for one 1/100 of a second
#-The awk numbr varible selects the analog axis input
function getInput()
{
input=$(timeout 0.05s cat /dev/input/js0 | od | awk '{print $8}')
}
#--Checks for input range and calls to set volume
function inputToVol
{
getInput
getMaxVol
input=( $(echo $input | tr "\n" " ") )
input="${input[6]}"
if [ "$input" -lt $RIGHTPTOP ] && [ "$input" -gt $RIGHTPRANBOT ]
then
input="$(expr $input - $RIGHTPBOT)00"
percent="$(expr $input / $RIGHTPDIF)"
int=$(bc <<< "$percent * $maxdiv")
int=${int%.*}
if [ "$int" -lt 5 ]
then
setVol "0"
if [ "$paused" == "0" ]
then
mpc pause
paused="1"
sleep 0.5
fi
else
setVol $int
if [ "$paused" == "1" ]
then
mpc play
paused="0"
fi
fi
else
setVol $MAXVOL
fi
if [ "$input" -gt 0 ] && [ "$input" -lt 37175 ]
then
input="$(expr 37174 - $input)00"
percent="$(expr $input / 37174)"
int=$(bc <<< "$percent * $maxmicdiv")
int=${int%.*}
if [ "$int" -lt 5 ]
then
setMicVol 0
else
setMicVol ${int%.*}
fi
else
setMicVol $MAXMICVOL
fi
}
#--Sets volume from percent parameter
function setVol()
{
pactl set-sink-input-volume $sink $1%
}
#--Sets volume from percent parameter
function getMaxVol()
{
if [ $readfile ]
then
if [ -f ~/.maxvol ]
then
MAXVOL=$(cat ~/.maxvol)
maxdiv="0.$(echo $MAXVOL)"
else
echo "No file"
echo "$MAXVOL" > ~/.maxvol
fi
fi
}
#--Sets volume from percent parameter
function setMicVol()
{
pactl set-source-volume alsa_input.pci-0000_00_1b.0.analog-stereo $1%
}
function getSink()
{
sinks=$(pactl list sink-inputs)
sinkbinaryindex=$(awk -v sinks="$sinks" -v app="$SINKAPP" 'BEGIN {print index(sinks,app)}' )
sink=$(echo "${sinks:0:$sinkbinaryindex}" | tr "\n" "[racecar]" | rev | tr "[racecar]" "\n" | grep -m 1 "tupnI" | tr -d "#" | awk '{print $1}' | rev )
echo "$cutatapprev"
}
# rootTest
# Tests if the script was run qith root permissions and tells the user they need to if they didn't.
function rootTest ()
{
if [[ $EUID -ne 0 ]]
then
echo "You need to run this as root to be able to make system changes"
echo -e "Run \"sudo $0\" next time"
exit 1
fi
}
#---------------- Logic ----------------#
rootTest
getSink
echo "Using sink: $sink"
#--Main Loop
while true
do
inputToVol
done
@Signy13

Signy13 commented Feb 14, 2022

Copy link
Copy Markdown

Thank you for my inspiration by getSink() function. I learnt couple of new things :-) . Just a few notes:

  • If the code tr "\n" "[racecar]" and tr "[racecar]" "\n" suppose to change end of line for something and then back, it does not work in this way (tr replaces single chars). I modified your code for myself in this way:
    sink=$(echo "${sinks:0:$sinkbinaryindex}" | awk 1 ORS='my_end_of_line' | rev | sed -r -e 's/enil_fo_dne_ym/\n/g' | grep -m 1 "tupnI" | cut -d'#' -f1 | rev )
  • $cutatapprev was not set so it prints an empty line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment