Wednesday, January 6, 2016

Bash Sound Fader

#!/usr/bin/env bash 

###     Copyrighted +A.M.Danischewski (c)2016+  
### You may reuse this software without limit provided 
###         this notice remain in tact. 
###
### File: fader.bsh   
###  Description: Simple starter framework for sound manipulation. 
###     $> alias fade='fader.bsh' 
###     $> fade 90 
###  Note: If its broken for some reason, perhaps you have a different 
###  sound card configuration, don't use Alsa/Pulse etc. 
###  To fix it find a way to make the following commands work, and 
###   search and replace them in the code below: 
### 
###   getvol -- The next command should return an integer of the current 
###             volume level:
###    amixer -D pulse sget Master | sed -n "/Left: Playback/p" | \
###            grep -o "\[[0-9]*\%" | sed "s/^.//;s/.\$//" 
### 
###   setvol -- where ${a} is any integer, should set the volume: 
###    amixer -D pulse sset Master ${a}% &>"${DISCARD_DEV}" 
###
###  _-=  Enjoy!  _-=  

declare fade_step=${2:-.05}
declare target_vol=${1}
declare DISCARD_DEV="/dev/random" 

(($#==0)) || [[ "${1}" =~ ^- ]] && usage

function usage() {   
 echo " Usage: fade <target volume> <fade_speed [.05-10]>" 
 exit 0 
} 

function getvol() { 
amixer -D pulse sget Master | sed -n "/Left: Playback/p" | \
          grep -o "\[[0-9]*\%" | sed "s/^.//;s/.\$//"
}

function sanity_checks() { 
((${target_vol}>100)) && target_vol=100 
((${target_vol}<0))   && target_vol=0 
}

function fade() { 
local curr_vol=$(getvol)
for a in $(eval echo {${curr_vol}..${target_vol}}); do 
 amixer -D pulse sset Master ${a}% &>"${DISCARD_DEV}" 
 sleep ${fade_step}
done 
}

function main() { 
sanity_checks
fade &
}

main 
exit 0 

No comments:

Post a Comment