#
# Copyright (C) by Stefano Falsetto
# e-mail contact ....: mailto:stefko5@inwind.it
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#
# Functions for madplay:
# FORMAT FILE: mp3

# DEPENDENCIES:
plugin_present_sox 2>/dev/null
if [ $? -ne 0 ]; then
  # This plugin require sox plugin!
  . $PLUGINDIR/general/sox
fi 

# Shared functions about mp3s
. $PLUGINDIR/general/mp3funcs

plugin_present_madplay () {
  return 0
}

parse_param_t_madplay () {
  shared_parse_param_t "madplay"
  return 0 
}

extract_time_mp3 () {
  TS=$($MADTIME "$1"|tr -s ' '|cut -d' ' -f 6)
  local tshour=${TS:0:1}
  tshour=$[ tshour * 60 ]
  TS="${TS:2}"
  TS="${TS/,/.}"
  if [ ${#TS} -lt 8 ]; then
    TS="${TS}0"
  fi
  if [ $tshour -ne 0 ]; then
    tsmin=${TS:0:2}
    tsmin=$[ 10#$tsmin + tshour ]
    TS="$tsmin:${TS:3}"
  fi
  # TODO: Method to understand if mp3 has some error...
  RET_WCL=0
}

extract_check_info_mp3() {
  RET_MONO=2
  if [ ! -z "$TRUST_EXT" ]; then
    $madplay_DECODER -v -o null:/dev/null -t 1/44 "$i" >$SONGDATA 2>&1
    RET_RATE=$(cat $SONGDATA|grep Layer|cut -d',' -f3|cut -d' ' -f2)
    RET_KBITSTEREO=$(cat $SONGDATA|grep Layer|cut -d',' -f2)
    RET_KBITSTEREO="$RET_KBITSTEREO, $(cat $SONGDATA|grep Layer|cut -d',' -f4)"
    if [ -z "$(cat $SONGDATA|grep -i stereo)" ]; then
      RET_MONO=1
    fi
    return
  fi
  shared_magic_check_info
}

check_file_type_madplay () {
  shared_check_file_type
}


setup_vars_mp3 () {
  FLYFMT="wav"
  OUTPUT_FILE="$SPOOLDIR/$nomef.$MPGCMD"
}

to_stdout_buffered_mp3 () {
  to_stdout_no_buffer_mp3 "$1"
}

to_stdout_no_buffer_mp3 () {
  $madplay_DECODER -v --no-tty-control -o $FLYFMT:/dev/stdout "$1"
}

decode_mp3 () {
  $madplay_DECODER -v --no-tty-control -o $TOFMT:"$OUTPUT_FILE" "$1"
}
