#
# 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 mpg123:
# 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_mpg123 () {
  return 0
}

parse_param_t_mpg123 () {
  shared_parse_param_t "mpg123"
  return 0
}

extract_time_mp3 () {
  RET_WCL=0
  $mpg123_DECODER -t -q -v -n 0 "$i" >$SONGDATA 2>&1
  TS=$(cat $SONGDATA|$GREP Frame|cut -d'[' -f3|cut -d']' -f1)
  RET_WCL=$(cat $SONGDATA|wc -l)
}

extract_check_info_mp3 () {
  if [ ! -z "$TRUST_EXT" ]; then
    $mpg123_DECODER -t -v -n 1 "$i" >$SONGDATA 2>&1
    RET_MONO=2
    RET_RATE=$(cat $SONGDATA|$GREP ", Freq"|cut -d',' -f3 |cut -d':' -f2|\
               tr -d ' ')
    RET_KBITSTEREO=$(cat $SONGDATA|$GREP "^Bitrate"|cut -d',' -f1|\
                     cut -d':' -f2)
    RET_KBITSTEREO="$RET_KBITSTEREO, $(cat $SONGDATA|$GREP 'mode: '|\
                    cut -d',' -f4|cut -d':' -f2)"
    if [ $(cat $SONGDATA|$GREP ", channels"|cut -d',' -f4|cut -d':' -f2|tr -d ' ') -eq 1 ]; then
      RET_MONO=1
    fi
    return
  fi
  shared_magic_check_info
}

check_file_type_mpg123 () {
  shared_check_file_type
}

setup_vars_mp3 () {
  FLYFMT="raw"
  OUTPUT_FILE="$SPOOLDIR/$nomef.$MPGCMD"
  #if [ ! -z "$TOFMT" ]; then
  #  SOX_OUT_TYPE="mp3"
  #else
  #  SOX_OUT_TYPE=$MPGCMD
  #  TOFMT="$TO_wav"
  #fi
}

to_stdout_buffered_mp3 () {
  $mpg123_DECODER $BUFFERED_DECODE "$1"
}

to_stdout_no_buffer_mp3 () {
  $mpg123_DECODER $DECODE_TO_STDOUT "$1"
}

decode_mp3 () {
  $mpg123_DECODER $NORMALDECODE $TOFMT "$OUTPUT_FILE" "$i"
}

