###########################################################################
#
# FFTExtractor - helper plugin that computers audio features for 
#                        music information retrieval use,using audioDB
#
# 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 FFTExtractor;

use BaseMediaConverter;

use Cwd;

use strict;
no strict 'refs'; # allow filehandles to be variables and viceversa


BEGIN {
    @FFTExtractor::ISA = ('BaseMediaConverter');
}


my $arguments = [
    ];



my $options = { 'name' => "FFTExtractor",
		'desc' => "{FFTExtractor.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_audio_format
{
    my $self = shift(@_);
    my $source_file_path = shift(@_);
    my $target_ext = shift(@_);
    my $convert_options  = shift(@_) || "";

    my $outhandle = $self->{'outhandle'};
    my $verbosity = $self->{'verbosity'};

    my $source_file_no_path = &File::Basename::basename($source_file_path);

    $self->init_cache_for_file($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'};

	my $target_file = "$file_root.$target_ext";

	$target_file_path = &util::filename_cat($cached_dir,$target_file);
    }
    else {
	$target_file_path = &util::get_tmp_filename($target_ext);
    }

    my $native_source_file_path = $source_file_path;

    my $native_target_file_path = $target_file_path;
    if ($^O eq "cygwin") {
	# Only get ffmpeg as native Windows binary => need to use Windows style path
	$native_source_file_path=`cygpath -w \"$source_file_path\"`;
	$native_target_file_path=`cygpath -w \"$target_file_path\"`;
	$native_source_file_path =~ s/\s+$//;
	$native_target_file_path =~ s/\s+$//;
    }

    # Setup and run the ffmpeg command
    my $ffmpeg_cmd = "ffmpeg -y";
    $ffmpeg_cmd .= " -i \"$native_source_file_path\" $convert_options";
    $ffmpeg_cmd .= " \"$native_target_file_path\"";
		
##    print STDERR "****** cmd = $ffmpeg_cmd\n";

    my $print_info = { 'message_prefix' => "ffmpeg",
		       'message' => "Converting $source_file_no_path to $target_ext format" };
    
    my ($regenerated,$result,$had_error) 
	= $self->autorun_general_cmd($ffmpeg_cmd,$source_file_path,
				     $target_file_path,$print_info);
	
    return $target_file_path;
}



sub compute_fft_features
{
    my $self = shift(@_);
    my $source_file_path = shift(@_);
    my $target_feature   = shift(@_);
    my $convert_options  = shift(@_) || "";

    my $outhandle = $self->{'outhandle'};
    my $verbosity = $self->{'verbosity'};

##    print STDERR "**** wav filename = $source_file_path\n";

    my $source_file_no_path = &File::Basename::basename($source_file_path);

    $self->init_cache_for_file($source_file_path);

    my $target_file_path;
    my $target_ext;

    if ($target_feature eq "chroma") {
	$target_ext = "chr12";
	$convert_options .= " -c 12";
    }
    elsif ($target_feature eq "power-log") {
	$target_ext = "power";
	$convert_options .= " -P";
    }
    else {
	print STDERR "Warning: computer_fft_features(), unrecognized option '$target_feature'\n";
	$target_ext = "txt";
    }

    if ($self->{'enable_cache'}) {
	my $cached_dir = $self->{'cached_dir'};
	my $file_root = $self->{'cached_file_root'};

	my $target_file = "$file_root.$target_ext";

	$target_file_path = &util::filename_cat($cached_dir,$target_file);
    }
    else {
	$target_file_path = &util::get_tmp_filename($target_ext);
    }
    
    # Run fftExtract
    my $fftExtract_cmd = "fftExtract $convert_options";
    $fftExtract_cmd .= " \"$source_file_path\" \"$target_file_path\"";
		
##    $fftExtract_cmd =~ s/\\/\//g; 

##    print STDERR "**** cmd = $fftExtract_cmd\n";

    my $print_info = { 'message_prefix' => "fftExtract",
		       'message' => "Extracting $target_feature from $source_file_no_path" };
    
    my ($regenerated,$result,$had_error) 
	= $self->autorun_general_cmd($fftExtract_cmd,$source_file_path,$target_file_path,$print_info);
	
    return ($target_file_path);
}
    



1;	
