#!/bin/csh -f


#    mkpreset.csh v.1.10
#            by
# L. Padilla (e-mail: padilla at domain "gae ucm es")
#  Madrid, June 2002 - October 2005
#
# Latest version at http://www.gae.ucm.es/~padilla/extrawork/mkpreset.csh


# This script makes an auto preset file for xmms with equalized volume levels.
#
# It requires sox and mpg123, plus some standard unix commands, to be in the
# execution path.
#
# Its input is the xmms.m3u file taken from the current directory and its
# output is the eq.auto_preset file left in the current directory. It uses two
# optional arguments, the first one is the volume level reference and the
# second one is the starting preset number. Both arguments only have meaning if
# you are generating a second preset file you want to add to a former. If you
# are generating your first preset file simply don't use arguments.
#
# File names must be unique in xmms.m3u because auto presets are based on file
# name solely (not on path), otherwise rename files.
#
# The character '%' must not be present in the xmms.m3u file, otherwise rename
# files or do a search and replace of that character in this script for other
# one not appearing in xmms.m3u.


grep -v "^#" xmms.m3u > mkpreset.m3u


set line = 1
set lines = `wc mkpreset.m3u | awk '{print $1}'`

while ($line <= $lines)

  file "`head -$line mkpreset.m3u | tail -1`" >> mkpreset.tmp
  set mp3 = "`tail -1 mkpreset.tmp | egrep ': MP[123], |: MP3 file | WAVE audio, MPEG '`"

  if ("$mp3" != "") then

    # For mp3 files:

    (mpg123 -y -m -s "`head -$line mkpreset.m3u | tail -1`" | sox -t raw -s -w -c 1 -r 44100 - -e stat) >>& mkpreset.tmp

  else

    # For wav files and other types:

    sox "`head -$line mkpreset.m3u | tail -1`" -e stat >& mkpreset.tmp2

    if ($status) then

      if ("`tail -1 mkpreset.tmp2 | egrep 'MP3|MPEG'`" != "") then

        # For wav files with mp3 encoding (and not recognized with file command):

        (mpg123 -y -m -s "`head -$line mkpreset.m3u | tail -1`" | sox -t raw -s -w -c 1 -r 44100 - -e stat) >>& mkpreset.tmp

      else

        echo "RMS     amplitude: nan" >> mkpreset.tmp

      endif

    else

      cat mkpreset.tmp2 >> mkpreset.tmp

    endif

  endif

  @ line = $line + 1

end


# Character '%' must not be present in xmms.m3u, otherwise replace it here and
# below or rename files:

grep "^RMS     amp" mkpreset.tmp | awk '{print "% " $3}' > mkpreset.tmp2
# mv -f mkpreset.tmp mkpreset_raw.out
paste mkpreset.m3u mkpreset.tmp2 | tr '\t' ' ' > mkpreset.tmp

rm -f mkpreset.tmp2


@ line = $lines / 2 + 1

if ("$1" != "") then

  set volref = "$1"

else

# Here we sort files by volume level and take the value in the middle as
# initial reference, so that file would have preamp=0, the rest would have
# positive or negative preamp depending whether they have lower or higher
# volume level (actually we will make a correction later):

  set volref = `sort -t '%' -nk2 mkpreset.tmp | head -$line | tail -1 | cut -d '%' -f 2`

endif

echo "Using initial volref = $volref, it will be corrected along this process."

# Here we calculate initial preamp values to obtain an additional reference:

set line = 1

while ($line <= $lines)

  set vol = `head -$line mkpreset.tmp | tail -1 | cut -d '%' -f 2 | sed "s/nan/$volref/ ; s/0.000000/0.000001/"`

  set log = `echo "val=l($volref/$vol)*10.; scale=6; val/1." | bc -l`

  echo $log >> mkpreset.tmp2

  @ line = $line + 1

end


# Here we obtain the additional reference by sorting preamp values and
# calculating the mean of the five extreme values, this hopefully optimizes the
# valid dynamic preamp range:

sort -n mkpreset.tmp2 -o mkpreset.tmp2

set min = 0
set max = 0
set line = 1

while ($line <= 5)

  set tmp = `head -$line mkpreset.tmp2 | tail -1`
  set min = `echo "val=$min+$tmp; scale=6; val/1." | bc -l`

  @ lline = $lines - $line + 1

  set tmp = `head -$lline mkpreset.tmp2 | tail -1`
  set max = `echo "val=$max+$tmp; scale=6; val/1." | bc -l`

  @ line = $line + 1

end

set addref = `echo "val=($min+$max)/10.; scale=6; val/1." | bc -l`

rm -f mkpreset.tmp2

set newref = `echo "val=$volref*e($addref/10.); scale=6; val/1." | bc -l`

echo "Using final volref = $newref, write down this value, it will"
echo "help if you ever want to append new files to this preset file."


if ("$2" != "") then

  set offset = "$2"

else

  set offset = 0

endif

echo "Starting with preset $offset."

set line = 1

while ($line <= $lines)

  set vol = `head -$line mkpreset.tmp | tail -1 | cut -d '%' -f 2 | sed "s/nan/$volref/ ; s/0.000000/0.000001/"`

  set log = `echo "val=(l($volref/$vol)*10.)-($addref); scale=2; val/1." | bc -l`

  @ preset = $line + $offset - 1

  echo "Preset$preset=" >> eq.auto_preset.1

  basename "`head -$line mkpreset.m3u | tail -1`" >> eq.auto_preset.2

  echo "[`tail -1 eq.auto_preset.2`]" >> eq.auto_preset.3

  echo "Preamp=$log" >> eq.auto_preset.3
  echo "Band0=0."    >> eq.auto_preset.3
  echo "Band1=0."    >> eq.auto_preset.3
  echo "Band2=0."    >> eq.auto_preset.3
  echo "Band3=0."    >> eq.auto_preset.3
  echo "Band4=0."    >> eq.auto_preset.3
  echo "Band5=0."    >> eq.auto_preset.3
  echo "Band6=0."    >> eq.auto_preset.3
  echo "Band7=0."    >> eq.auto_preset.3
  echo "Band8=0."    >> eq.auto_preset.3
  echo "Band9=0."    >> eq.auto_preset.3
  echo ""            >> eq.auto_preset.3

  @ line = $line + 1

end

echo "[Presets]" > eq.auto_preset

paste eq.auto_preset.1 eq.auto_preset.2 | tr -d '\t' >> eq.auto_preset

echo "" >> eq.auto_preset

cat eq.auto_preset.3 >> eq.auto_preset

rm -f mkpreset.m3u mkpreset.tmp eq.auto_preset.1 eq.auto_preset.2 eq.auto_preset.3

echo "Done!"

exit
