###########################################################################
#
# jAudioConverter - helper plugin that does office document conversion 
#                       using jodconverter combined with MIR
#
# A component of the Greenstone digital library software
# from the New Zealand Digital Library Project at the 
# University of Waikato, New Zealand.
#
# Copyright (C) 2010 New Zealand Digital Library Project
#
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
###########################################################################
package jAudioConverter;

use BaseMediaConverter;

use strict;
no strict 'refs'; # allow filehandles to be variables and viceversa

use gsprintf 'gsprintf';

BEGIN {
    @jAudioConverter::ISA = ('BaseMediaConverter');
}


my $arguments = [
    { 'name' => "window_size",
      'desc' => "{jAudioConverter.window_size}",
      'type' => "int",
      'range' => "128,",
      'deft' => '512',
      'reqd' => "no" },
    { 'name' => "window_overlap",
      'desc' => "{jAudioConverter.window_overlap}",
      'type' => "string",
      'range' => "0.0",
      'deft' => '0.0',
      'reqd' => "no" },
    { 'name' => "sample_rate",
      'desc' => "{jAudioConverter.sample_rate}",
      'type' => "enum",
      'list' => [{'name' => "8 kHz",      'desc' => "{jAudioConverter.8000Hz}"},
		 {'name' => "11.025 kHz", 'desc' => "{jAudioConverter.11025Hz}"},
		 {'name' => "16 kHz",     'desc' => "{jAudioConverter.16000Hz}"},
		 {'name' => "22.05 kHz",  'desc' => "{jAudioConverter.22050Hz}"},
		 {'name' => "44.1 kHz",   'desc' => "{jAudioConverter.44100Hz}"} ],
      'deft' => '16 kHz',
      'reqd' => "no" },
    { 'name' => "extracted_data",
      'desc' => "{jAudioConverter.extracted_data}",
      'type' => "enum",
      'list' => [{'name' => "Overall and Windowed", 'desc' => "{jAudioConverter.overall_and_windowed}"},
		 {'name' => "Windowed only", 'desc' => "{jAudioConverter.windowed_only}"},
		 {'name' => "Overall only", 'desc' => "{jAudioConverter.overall_only}"}],
      'deft' => 'Overall and Windowed',
      'reqd' => "no" },
    { 'name' => "output_type",
      'desc' => "{jAudioConverter.output_type}",
      'type' => "enum",
      'list' => [{'name' => "ACE XML", 'desc' => "{jAudioConverter.ace_xml}"},
		 {'name' => "Weka ARFF", 'desc' => "{jAudioConverter.weka_arff}"}],
      'deft' => 'ACE XML',
      'reqd' => "no" }
    ];



my $options = { 'name' => "jAudioConverter",
		'desc' => "{jAudioConverter.desc}",
		'abstract' => "yes",
		'inherits' => "yes",
		'args' => $arguments };

sub new {
    my ($class) = shift (@_);
    my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
    push(@$pluginlist, $class);

    push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
    push(@{$hashArgOptLists->{"OptList"}},$options);

    my $self = new BaseMediaConverter($pluginlist, $inputargs, $hashArgOptLists, 1);

    return bless $self, $class;
}
	

sub convert {
    my $self = shift(@_);
    my $source_file_path = shift(@_);
    my $target_file_type = shift(@_);
    my $convert_options  = shift(@_) || "";
    my $convert_id       = shift(@_) || "";
    my $cache_mode       = shift(@_) || "";

    my $outhandle = $self->{'outhandle'};
    my $verbosity = $self->{'verbosity'};

    my $source_file_no_path = &File::Basename::basename($source_file_path);

    # Determine the full name and path of the output file
    my $target_file_path;
    if ($self->{'enable_cache'}) {
	my $cached_dir = $self->{'cached_dir'};
	my $file_root = $self->{'cached_file_root'};
	$file_root .= "_$convert_id" if ($convert_id ne "");
	my $audio_file = "$file_root.$target_file_type";
	$target_file_path = &util::filename_cat($cached_file_dir,$audio_file);
    }
    else {
	$target_file_path = &util::get_tmp_filename($target_file_type);
    }


    my jar_home = &util::filename_cat($ENV{'GEXT_MUSICIR'},"lib","java");
    my $jaudio_jar = &util::filename_cat($jar_home,"jAudio.jar");

    # Generate and run the convert command
    my $convert_command = "java -jar $jaudio_jar $convert_options \"$source_file_path\" \"$target_file_path\"";

    my $print_info = { 'message_prefix' => $convert_id,
		       'message' => "Converting $source_file_no_path to: $convert_id $target_file_type" };
    $print_info->{'cache_mode'} = $cache_mode if ($cache_mode ne "");

    my ($regenerated,$result,$had_error) 
	= $self->autorun_general_cmd($convert_command,$target_file_path,$print_info);

    return ($result,$target_file_path);
}

sub convert_without_result {
    my $self = shift(@_);

    my $source_file_path = shift(@_);
    my $target_file_type = shift(@_);
    my $convert_options  = shift(@_) || "";
    my $convert_id       = shift(@_) || "";

    return $self->convert($source_file_path,$target_file_type,
			  $convert_options,$convert_id,"without_result");
}



1;	
