###########################################################################
#
# 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 $jsme_self  = undef;
    eval("require MusicIRPlugin");
    if ($@) {
	# Useful debugging statement below if there is a syntax error in the plugin
	# and you expecting jSongMinerExtractor to be found
	# print STDERR "$@\n";
    }
    else {
	print STDERR "Dynamically loading jSongMinerExtractor\n";
	push(@AudioDBPlugin::ISA,"MusicIRPlugin");
	$jsme_self = new MusicIRPlugin($pluginlist, $inputargs, $hashArgOptLists);
    }

    my $base_self = new BasePlugin($pluginlist, $inputargs, $hashArgOptLists);

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

    $self->{'jSongMinerExtractor-found'} = (defined $jsme_self) ? 1 : 0;

    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 ($input_ext) = ($filename_full_path =~ m/\.(.*?)$/);
    $input_ext = lc($input_ext);


    my $wav_filename;
    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);

    my $ogg_filename;
    if ($input_ext ne "ogg") {	
	$ogg_filename = $self->convert_audio_format($filename_full_path,"ogg", "-acodec libvorbis");
	# $ogg_filename = $self->convert_audio_format($filename_full_path,"ogg", "-acodec libvorbis -ab 160000");
    }
    else {
	$ogg_filename = $filename_full_path;
    }
    $doc_obj->associate_file($ogg_filename, "doc.ogg", "audio/ogg", $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);
    }
    

    if ($self->{'jSongMinerExtractor-found'}) {
	# calling this version of process has the side effect of setting dummy text
	$self->MusicIRPlugin::process($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli);
    }
    else {
	#we have no text - adds dummy text and NoText metadata
	$self->add_dummy_text($doc_obj, $doc_obj->get_top_section());
    }

    return 1;
}



sub post_process_doc_obj {
    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);

    # Opportunity to use jAudioMinerExtractor if dynamically and loaded
    #
    my $retrieve_mir_metadata = $self->{'retrieve_mir_metadata'};

    if (defined $retrieve_mir_metadata && $retrieve_mir_metadata eq "true") {
	my $top_section = $doc_obj->get_top_section();

	my $id3_titles = $doc_obj->get_metadata($top_section,"ex.ID3.Title");
	# my $id3_title = shift @$id3_titles || "Unknown";
	my $id3_title = shift @$id3_titles || undef;

	my $id3_artists = $doc_obj->get_metadata($top_section,"ex.ID3.Artist");
	# my $id3_artist = shift @$id3_artists || "Unknown";
	my $id3_artist = shift @$id3_artists || undef;

	my ($metadata_acexml_filename,$metadata_txt_filename) 
	    = $self->retrieve_metadata($filename_full_path,$id3_title,$id3_artist);
	
	if (-e $metadata_txt_filename) {
	    $self->parse_txt_metadata($doc_obj,$metadata_txt_filename);
	}

	$doc_obj->associate_file($metadata_acexml_filename, "jsongminer.xml", 
				 "text/xml", $top_section);
    }


    return 1;
}


1;



