#!/bin/bash

[ ! "$SPEAKER" ] && . $HOME/.pmusic/pmusicrc
#Detect active soundcard
if [ "$SOUND_DEVICE" = "auto" ]; then
	if [ -f /etc/asound.conf ]; then #see if MultipleSoundCardWizard was used
		TMP=$(head -1 /etc/asound.conf)
		CARD_NR=${TMP##* } 
	fi
	if [ ! "$CARD_NR" ]; then
		TMP="`aplay -l | grep -m 1 -F card`" #grab the first card
		CARD_NR="`echo $TMP | cut -d':' -f1 | cut -d' ' -f2`"
	fi
else
	CARD_NR="`echo $SOUND_DEVICE | cut -d':' -f1 | cut -d' ' -f2`"
fi


case $1 in
-activate_effects)
	rm $WORKDIR/ffmpeg_filter
	AF_STRING=""
	[ "$FADE_IN" != "0" ] && AF_STRING="${AF_STRING}afade=t=in:d=$FADE_IN," #afade has to be BREFORE tempo
	[ "$TREBLE" != "0" ] && AF_STRING="${AF_STRING}treble=gain=$TREBLE,"
	[ "$BASS" != "0" ] && AF_STRING="${AF_STRING}bass=gain=$BASS,"
	if [ "$TEMPO" != "100" ]; then
		TEMPO="`echo "scale=2; $TEMPO / 100" | bc`"
		AF_STRING="${AF_STRING}atempo=$TEMPO,"
	fi
	if [ "`grep -E "afade|treble|bass|atempo" <<< "$AF_STRING"`" ]; then echo -n "$AF_STRING" > $WORKDIR/ffmpeg_filter; fi #remove trailing , in -af "..." which corrupts ffmpeg
	#Note that normalize is stored in rc - it has to be calculated before playing actual track
	;;
-gui)
	$0 -get_levels
	GUI_PEQUALIZER="`/usr/local/pequalizer/pequalizer -e 2> /dev/null`"
	[ ! "$GUI_PEQUALIZER" ] && GUI_PEQUALIZER='<vbox space-expand="true" space-fill="true"><frame><pixmap><input file stock="gtk-dialog-error"></input></pixmap><hbox homogeneous="true" width-request="250"><text><label>pEqualizer is not installed</label></text></hbox></frame></vbox>'
	. $APPDIR/gui_mixer
	$APPDIR/func_svg -heading "$(gettext 'Sound processing')" > /dev/null

	S='
	<window title="pMusic - '$(gettext 'Sound processing')'" icon-name="pmusic" window_position="2">
	 <vbox space-expand="true" space-fill="true">
	   <vbox space-expand="false" space-fill="false">
      <hbox spacing="0" height="80" scrollable="true" space-expand="true" space-fill="true" vscrollbar-policy="2" hscrollbar-policy="2">
	   <eventbox name="svgheading">
	    <hbox space-expand="true" space-fill="true">
	     <text width-request="20" space-expand="false" space-fill="false"><label>""</label></text>
		 <text ypad="40" use-markup="true" space-expand="false" space-fill="false"><label>"<i>'$(gettext 'Speaker channel')': <b>'$SPEAKER'</b>
'$(gettext 'Sound card')': <b>'$(echo "$SOUND_DEVICE" | cut -d[ -f1 | cut -d: -f2)'</b></i>"</label></text>
		 <text space-expand="true" space-fill="true"><label>""</label></text>
		 <vbox space-expand="false" space-fill="false">
		  <button width-request="40" height-request="40" can-focus="false" image-position="2" space-expand="false" space-fill="false">
		   <input file stock="gtk-preferences"></input>
		   <action>'$APPDIR'/func_config -preferences -playback</action>
		  </button>
		  <text space-expand="true" space-fill="true"><label>""</label></text>
		 </vbox>
	    </hbox>
	   </eventbox>
	  </hbox>
	   </vbox>
	  <notebook labels="'$(gettext 'Output')'|'$(gettext 'Input')'" space-expand="true" space-fill="true">
	  <vbox space-expand="true" space-fill="true">
	  <hbox space-expand="true" space-fill="true">
	  <vbox margin="10" space-expand="true" space-fill="true">
	   <hbox space-expand="false" space-fill="false">
	    <button space-expand="false" space-fill="false">
	     <label>'$(gettext 'Bypass equalizer')'</label>
	     <action>IMG=info; FRAME="'$(gettext 'Bypass')'"; TXT1="'$(gettext 'To NOT send signal through the equalizer, you have to lock sound driver (alsa) to pMusic. This is set in the preferences.')'"; TXT2="'$(gettext 'Be aware that locking the sound driver to pMusic affects your system: Other programs will not play audio as long as pMusic is active. On the other hand, pMusic will not play if sound driver is in use by other program. Normally, it is not recommended to lock your sound driver.')'"; . '$APPDIR'/box_ok</action>
	    
	    </button>
	    <text space-expand="true" space-fill="true"><label>""</label></text>
	   </hbox>
	   <hseparator></hseparator>
	   <hbox space-expand="true" space-fill="true">
	    '$GUI_PEQUALIZER'
	    <variable>HBOX_EQ</variable>'
	    [ ! "$SOUND_DEVICE" = "auto" ] && S=$S'<sensitive>false</sensitive>'
	   S=$S'</hbox>
	  </vbox>
	  <text space-expand="true" space-fill="true"><label>""</label></text>

	  <eventbox name="mixer_background">
	   <vbox space-expand="true" space-fill="true">
	    '$GUI_MIXER'
	   </vbox>
	  </eventbox>
	 </hbox>
	 </vbox>
	   <vbox margin="10">
	    '$GUI_SOUND_INPUT'
	   </vbox>
	  </notebook>
	 </vbox>
	</window>
	'
	[ $TOOLTIPS = false ] && S="`echo "$S" | sed 's%tooltip-text%tooltipXXXtext%g'`" #deactivate tooltips
	echo "$S" | sed 's/##.*//' > $WORKDIR/xml_pmusic_mixer #I use double hash (##) for comments. --> as #FF0000 is valid xml-code
	. $APPDIR/func -reset_gtk
	$GTKDIALOG -f $WORKDIR/xml_pmusic_mixer
	;;
-mute)
	if [ `cat $WORKDIR/func_mixer-MUTE` = true ]; then
		amixer -c $CARD_NR set "$SPEAKER" unmute > /dev/null 2>&1
		echo false > $WORKDIR/func_mixer-MUTE
	else
		amixer -c $CARD_NR set "$SPEAKER" mute > /dev/null 2>&1
		echo true > $WORKDIR/func_mixer-MUTE
	fi
	;;
-check_stereo)
	if [ "`amixer -c $CARD_NR get "$SPEAKER" | grep 'Mono:' | cut -d ':' -f 2`" ]; then #mono
		TXT1="$(gettext 'Speaker channel is mono, so balance will not work. Select another speaker channel in the preferences.')"
		TXT2="$(gettext 'Speaker channel:') $SPEAKER"
		. $APPDIR/box_ok
		echo 100 > $WORKDIR/BALANCE
	fi
	;;
-set_volume)
	#in case it is muted, and user tries to adjust volume
	amixer -c $CARD_NR set "$SPEAKER" unmute > /dev/null 2>&1
	echo false > $WORKDIR/func_mixer-MUTE
	##---
	if [ "`amixer -c $CARD_NR get "$SPEAKER" | grep 'Mono:' | cut -d ':' -f 2`" ]; then #mono
		amixer -c $CARD_NR set "$SPEAKER" $VOLUME"%" > /dev/null 2>&1
	else
		[ ! "$BALANCE" ] && BALANCE=100 #in case no balance slider is provided in gui
		if [ $BALANCE -le 100 ]; then
			VOL_LEFT=$VOLUME
			VOL_RIGHT=$((($BALANCE*$VOLUME)/100))
		else
			VOL_LEFT=$((((200-$BALANCE)*$VOLUME)/100))
			VOL_RIGHT=$VOLUME
		fi
		amixer -c $CARD_NR set "$SPEAKER" $VOL_LEFT"%",$VOL_RIGHT"%" > /dev/null 2>&1
	fi
	;;
-get_levels)
	  if [ ! "`amixer -c $CARD_NR get "$SPEAKER" | grep 'Mono:' | cut -d ':' -f 2`" ]; then #mono
		VOL_L=`amixer -c $CARD_NR get "$SPEAKER" | grep -m1 'Left:' | cut -d '%' -f 1 | cut -d '[' -f 2`
		VOL_R=`amixer -c $CARD_NR get "$SPEAKER" | grep -m1 'Right:' | cut -d '%' -f 1 | cut -d '[' -f 2`
		[ $VOL_L = 0 ] && VOL_L=1 #1 to avoid dividing with 0
		[ $VOL_R = 0 ] && VOL_R=1
		#check which channel is dominant. Make that the shown volume
		if [ "$VOL_L" -ge "$VOL_R" ]; then
			echo $VOL_L > $WORKDIR/VOLUME
			TMP=$((($VOL_R*100)/$VOL_L)) 
			echo "$TMP" > $WORKDIR/BALANCE
		else
			echo $VOL_R > $WORKDIR/VOLUME
			TMP=$(((200-($VOL_L*100)/$VOL_R)))
			echo "$TMP" > $WORKDIR/BALANCE
		fi
	else #mono
		amixer -c $CARD_NR get "$SPEAKER" | grep -FA5 'Mono:' | grep -Fm1 '%' | cut -d '%' -f 1 | cut -d '[' -f 2 > $WORKDIR/VOLUME
		echo 100 > $WORKDIR/BALANCE
	fi
	;;
-volume_up)
	$0 -get_levels
	VOLUME=($(<"$WORKDIR/VOLUME"))
	VOLUME=$(($VOLUME+10))
	BALANCE=($(<"$WORKDIR/BALANCE"))
	[ "$VOLUME" -lt 1 ] && VOLUME=0
	[ "$VOLUME" -gt 99 ] && VOLUME=100
	. $APPDIR/func_mixer -set_volume
	;;
-volume_down)
	$0 -get_levels
	VOLUME=($(<"$WORKDIR/VOLUME"))
	VOLUME=$(($VOLUME-10))
	BALANCE=($(<"$WORKDIR/BALANCE"))
	[ "$VOLUME" -lt 1 ] && VOLUME=0
	[ "$VOLUME" -gt 99 ] && VOLUME=100
	. $APPDIR/func_mixer -set_volume
	;;
-balance_right)
	$0 -get_levels
	VOLUME=($(<"$WORKDIR/VOLUME"))
	BALANCE=($(<"$WORKDIR/BALANCE"))
	BALANCE=$(($BALANCE+20))
	[ "$BALANCE" -lt 1 ] && BALANCE=0
	[ "$BALANCE" -gt 199 ] && BALANCE=200
	. $APPDIR/func_mixer -set_volume
	;;
-balance_left)
	$0 -get_levels
	VOLUME=($(<"$WORKDIR/VOLUME"))
	BALANCE=($(<"$WORKDIR/BALANCE"))
	BALANCE=$(($BALANCE-20))
	[ "$BALANCE" -lt 1 ] && BALANCE=0
	[ "$BALANCE" -gt 199 ] && BALANCE=200
	. $APPDIR/func_mixer -set_volume
	;;
-mute)
	if [ "`cat $WORKDIR/func_mixer-MUTE`" = "true" ]; then
		amixer set "$SPEAKER" unmute
		echo false > $WORKDIR/func_mixer-MUTE
	else
		amixer set "$SPEAKER" mute
		echo true > $WORKDIR/func_mixer-MUTE
	fi
	;;
esac
