#!/bin/sh
#
# setup alsa (especially the mixer config)
#
# runlevels: geexbox, debug, configure

echo "### Setting up sound card ###"

# include audio configuration file
. /etc/audio

[ ! -f /etc/asound.conf ] && echo "" > /etc/asound.conf

mixer() {
  parm=${3:-on}
  amixer -c $ALSA_CARD sset "$1" "$2" $parm >/dev/null 2>&1
  amixer -c $ALSA_CARD sset "$1" $parm >/dev/null 2>&1
}

# set alsa channel mode (6ch, 4ch, 2ch)
mixer 'Channel Mode' "${CHANNELS}ch"

# set common mixer params
mixer Master 90%
mixer Front 90%
mixer PCM 90%
mixer Synth 90%
# mute CD, since using digital audio instead
mixer CD 0% mute
# Only unmute Line and Aux if they are possibly used.
if [ -e /var/use_dxr3 -o -e /var/tvcard -o -e /var/dvbcard -o -e /var/digimatrix ]; then
  mixer Line 90%
  mixer Aux 90%
else
  mixer Line 0% mute
  mixer Aux 0% mute
fi
# mute mic
mixer Mic 0% mute
# ESS 1969 chipset has 2 PCM channels
mixer PCM,1 90%
# Trident/YMFPCI/emu10k1
mixer Wave 90%
mixer Music 90%
mixer AC97 90%
mixer Surround 90%
mixer 'Surround Digital' 90%
mixer 'Wave Surround' 90%
if [ "$CHANNELS" = 6 ]; then
  mixer 'Duplicate Front' 0% off
else
  mixer 'Duplicate Front' 90%
fi

mixer Center 90%
mixer LFE 90%
mixer Center/LFE 90%

# set SPDIF mixer params
if [ "$SOUNDCARD_MODE" = SPDIF ]; then
  mixer 'IEC958' 90%
  mixer 'IEC958 Output' 90%
  mixer 'IEC958 Playback AC97-SPSA' $AC97_SPSA
  mixer 'IEC958 Coaxial' 90%
  mixer 'IEC958 LiveDrive' 90%
  mixer 'IEC958 Optical Raw' 90%
  mixer 'SPDIF Out' 90%
  mixer 'SPDIF Front' 90%
  mixer 'SPDIF Rear' 90%
  mixer 'SPDIF Center/LFE' 90%
  mixer 'Master Digital' 80%
else
  mixer 'SPDIF Out' 0% off
  mixer 'Analog Front' 90%
  mixer 'Analog Rear' 90%
  mixer 'Analog Center/LFE' 90%
fi

exit 0
