###########################################################################
#
# AudioDBPlugin.pm -- for extracting Chroma and Power Log features to work 
#                     with Queen Marys 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) 2011 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 AudioDBPlugin;

use BasePlugin;
use FFTExtractor;

use strict;
no strict 'refs'; # allow filehandles to be variables and viceversa
no strict 'subs';

use gsprintf 'gsprintf';

sub BEGIN {
    @AudioDBPlugin::ISA = ('BasePlugin', 'FFTExtractor');
}

my $enable_streaming_list =
    [{'name' => "disabled", 'desc' => "Do not create any video file optimised for streaming over the Internet."},
     {'name' => "flv", 'desc' => "Uses the FLV format for streaming media. Better to target old computers."},
     {'name' => "mp4", 'desc' => "Uses the MP4 container with H264 and AAC codecs. Better quality at very low bitrates but more ressources intensive."},
     {'name' => "mp3", 'desc' => "(audio only) Uses MP3 for psuedo streaming."}
     ];

my $streaming_mime_types = { "flv" => "video/flv",
			     "mp4" => "video/mp4",
			     "mp3" => "audio/mpeg" };

my $arguments =
    [ 
      { 'name' => "compute_fft_features",
	'desc' => "{FFTExtractor.compute_fft_features}",
	'type' => "enum",
	'list' => [{'name' => "true",  'desc' => "{common.true}"},
		   {'name' => "false", 'desc' => "{common.false}"}],
	'deft' => "false",
	'reqd' => "no" },
      { 'name' => "process_exp",
	'desc' => "{BasePlugin.process_exp}",
	'type' => "regexp",
	'deft' => &get_default_process_exp(),
	'reqd' => "no" },

      { 'name' => "enable_streaming",
        'desc' => "{MultimediaPlug.enable_streaming}",
        'type' => "enum",
        'list' => $enable_streaming_list,
	'deft' => "disabled",
	'reqd' => "no" }
      ];

my $options = { 'name'     => "AudioDBPlugin",
		'desc'     => "{AudioDBPlugin.desc}",
		'abstract' => "no",
		'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 $ffte_self = new FFTExtractor($pluginlist, $inputargs, $hashArgOptLists,1);
    my $base_self = new BasePlugin($pluginlist, $inputargs, $hashArgOptLists);

    my $self = BasePlugin::merge_inheritance($ffte_self,$base_self);

    return bless $self, $class;
}

sub get_default_process_exp {
    my $self = shift (@_);
    
    return q^(?i)(\.mp3|\.wave?|\.aif[fc]?|\.au|\.snd|\.og[ga])$^; 
}

sub begin {
    my $self = shift (@_);
    my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;

    $self->SUPER::begin(@_);
    $self->FFTExtractor::begin(@_);
}



# do plugin specific processing of doc_obj
sub process {
    my $self = shift (@_);
    my ($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;

    my $outhandle = $self->{'outhandle'};
    my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);

    my $top_section = $doc_obj->get_top_section();

    # associate original file
    # if filename ext not .wav convert (optionally cached) to wav

    my $wav_filename;

    my ($input_ext) = ($filename_full_path =~ m/\.(.*?)$/);
    $input_ext = lc($input_ext);


    if ($input_ext ne "wav") {	
	$wav_filename = $self->convert_audio_format($filename_full_path,"wav");
    }
    else {
	$wav_filename = $filename_full_path;
    }
    $doc_obj->associate_file($wav_filename, "doc.wav", "audio/wav", $top_section);

    if ($self->{'enable_streaming'} ne "disabled") {
	
	my $streamable_ext = lc($self->{'enable_streaming'});
	my $streamable_filename;

	if ($input_ext ne $streamable_ext) {
	    # make streamable version
	    $streamable_filename 
		= $self->convert_audio_format($filename_full_path,$streamable_ext);
	}
	else {
	    $streamable_filename = $filename_full_path;
	}

	$doc_obj->associate_file($streamable_filename,
				 "doc.$streamable_ext", 
				 $streaming_mime_types->{$streamable_ext},
				 $top_section);

    }

    #
    # Opportunity here to segment, use beat-tracking ...
    #

    if ($self->{'compute_fft_features'} eq "true")
    {
	# compute chroma 12 and associate file
	my $chr12_filename = $self->compute_fft_features($wav_filename,"chroma");
	$doc_obj->associate_file($chr12_filename, "doc.chr12", 
				 "text/plain", $top_section);

	# compute power log and associate file
	my $powerlog_filename = $self->compute_fft_features($wav_filename,"power-log");
	$doc_obj->associate_file($powerlog_filename, "doc.power", 
				 "text/plain", $top_section);
    }
    
    #we have no text - adds dummy text and NoText metadata
    $self->add_dummy_text($doc_obj, $doc_obj->get_top_section());

    return 1;
}



1;



