#!/bin/sh
# setting-up alsa, unmutted if necessary
# 
# (c) 2006, Eko M. Budi for Vector Linux
# License: GNU GPL

# set default mixer volumes
#
function mixer() {
  amixer set "$1" "$2" unmute >/dev/null 2>&1
  amixer set "$1" unmute >/dev/null 2>&1
}

function alsa_set_mixers() {
    echo "Set default state"
    mixer Master 75%
    mixer PCM 90%
    mixer Synth 90%
    mixer CD 90%
    # mute mic
    amixer set Mic 0% mute >/dev/null 2>&1
    # ESS 1969 chipset has 2 PCM channels
    mixer PCM,1 90%
    # Trident/YMFPCI/emu10k1
    mixer Wave 100%
    mixer Music 100%
    mixer AC97 100%
    # CS4237B chipset:
    mixer 'Master Digital' 75%
    # Envy24 chips with analog outs
    mixer DAC 90%
    mixer DAC,0 90%
    mixer DAC,1 90%
    # some notebooks use headphone instead of master
    mixer Headphone 75%
    mixer Playback 100%
    # turn off digital switches
    amixer set "SB Live Analog/Digital Output Jack" off >/dev/null 2>&1
    amixer set "Audigy Analog/Digital Output Jack" off >/dev/null 2>&1
}

# Check if the sound is muted
function alsa_is_muted()
{
    amixer get Master | grep -qE "(\[[0-9]%\]|\[off\])" && return 0
    amixer get PCM    | grep -qE "(\[[0-9]%\]|\[off\])" && return 0
    return 1
}

if [ -d /proc/asound ]; then
  if alsa_is_muted; then
    echo "Setting up ALSA with default values"
    alsa_set_mixers
  fi
  exit 0
else
  echo "ERROR: Alsa is not running"
  exit 1
fi

