#!/bin/sh
#
# This is optimal setup script for SBLive! 5.1 + DTT2200 (unlike emu-script :)
# Created by Mr.Zdeeck/Borg Solutions <zdeeck@borg.cz>
#
# Change values below according to your needs.
#

# PCM1 (ie. /dev/dsp1) should be actually routed to rear only
# but someone may want to route it to all speakers
PCM1TOALL="no"

# Analog loop is intended for routing analog inputs (CD/AUX/...)
# to 5.1 and tone-controlling them using internal DSP.
# Unfortunatelly, this (obviously) degrades the quality.
ANALOGLOOP="no"

# Which mixer control to use for CD/Spdif (reasonable: cd,digi1)
CDSPDIFMIX="cd"

# Front speakers boost
FRONTBOOST="no"

# Tone control
TONECONTROL="no"

# DSP Patches path
DSPP=/usr/local/share/emu10k1

# Binaries
EMU_DSPMGR=/usr/local/bin/emu-dspmgr
EMU_CONFIG=/usr/local/bin/emu-config

# Route to all speakers (actually 4)
route_to_5() {
    # Create routes
    if [ "$4" != "justvol" ]; then
	# Front speakers
	if [ "$3" = "a" -o "$3" = "f" ]; then
	    $EMU_DSPMGR -a"${1} L:Front L"
	    $EMU_DSPMGR -a"${1} R:Front R"
	fi
	# Rear speakers
	if [ "$3" = "a" -o "$3" = "r" ]; then
	    $EMU_DSPMGR -a"${1} L:Rear L"
	    $EMU_DSPMGR -a"${1} R:Rear R"
	fi
# We're unable to control them, so comented out...
#	$EMU_DSPMGR -a"${1}:Analog Center"
#	$EMU_DSPMGR -a"${1}:Analog LFE"
    fi

    # Create volume control
    $EMU_DSPMGR -p"${1} Vol" -l"${1}" -f$DSPP/vol_2.bin -c"Vol_L" -m"${2}_l" -c"Vol_R" -m"${2}_r"    
}

##############################################
# Init
##############################################
# Stop DSP
$EMU_DSPMGR -x
# Reinit
$EMU_DSPMGR -z
# Analog
$EMU_CONFIG -a
# Front boost
if [ "$FRONTBOOST" = "yes" ]; then
    $EMU_CONFIG -B on
else
    $EMU_CONFIG -B off
fi
# 5.1 channel offset = 8
$EMU_CONFIG -m 8

##############################################
# Routes
##############################################
# Route PCM to all
route_to_5 "Pcm" "pcm" "a"
# Route Analog input to ADC Rec
$EMU_DSPMGR -a"Analog:ADC Rec"
# Route CD/Spdif to all
route_to_5 "CD-Spdif" "$CDSPDIFMIX" "a"
# Route PCM1 to all/rear only
if [ "$PCM1TOALL" = "yes" ]; then
    route_to_5 "Pcm1" "pcm2" "a"
else
    route_to_5 "Pcm1" "pcm2" "r"
fi
# Route Analog loop
if [ "$ANALOGLOOP" = "yes" ]; then
    route_to_5 "Analog" "rec" "a"
else
    route_to_5 "Analog" "rec" "a" "justvol"
fi
    

##############################################
# PCM Gain
##############################################
$EMU_DSPMGR -p "4-Channel Gain" -l"Pcm1" -l"Pcm" -f$DSPP/gain_4.bin

##############################################
# Rear speakers volume fix
##############################################
$EMU_DSPMGR -l"Rear" -p"Rear MVFix" -f$DSPP/vol_2.bin -c"Vol_L" -m"vol_l" -c"Vol_R" -m"vol_r"

##############################################
# Multichannel - 5+1
##############################################
$EMU_DSPMGR -a"fx8:Front L"
$EMU_DSPMGR -a"fx9:Front R"
$EMU_DSPMGR -a"fx10:Rear L"
$EMU_DSPMGR -a"fx11:Rear R"
$EMU_DSPMGR -a"fx12:Analog Center"
$EMU_DSPMGR -a"fx13:Analog LFE"

# Use the full dynamic range of all inputs (taken from emu-script)
$EMU_DSPMGR -l"fx8" -l"fx9" -l"fx10" -l"fx11" -l"fx12" -l"fx13" -f$DSPP/gain_6.bin

# Use OSS Mixer knob 'mix' for controling Analog Center & LFE.
$EMU_DSPMGR -p"Center&LFE Vol" -l"fx12" -l"fx13" -f$DSPP/vol_2.bin -c"Vol_L" -m"mix_l" -c"Vol_R" -m"mix_r"

##############################################
# Tone control
##############################################
if [ "$TONECONTROL" = "yes" ]; then
    $EMU_DSPMGR -l"Front L" -f$DSPP/tone.bin -cbass -mbass -ctreble -mtreble
    $EMU_DSPMGR -l"Front R" -f$DSPP/tone.bin -cbass -mbass -ctreble -mtreble
    $EMU_DSPMGR -l"Rear R" -f$DSPP/tone.bin -cbass -mbass -ctreble -mtreble
    $EMU_DSPMGR -l"Rear L" -f$DSPP/tone.bin -cbass -mbass -ctreble -mtreble
    $EMU_DSPMGR -l"Analog Center" -f$DSPP/tone.bin -cbass -mbass -ctreble -mtreble
    $EMU_DSPMGR -l"Analog LFE" -f$DSPP/tone.bin -cbass -mbass -ctreble -mtreble
fi

# Start DSP
$EMU_DSPMGR -y

