###########################################################################
#
# AudioPlugin.pm -- Plugin for MP3 files (MPEG audio layer 3).
#
# A component of the Greenstone digital library software from the New
# Zealand Digital Library Project at the University of Waikato, New
# Zealand.
#
# Copyright (C) 2001 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 AudioPlugin;

use BasePlugin;
use AudioConverter;

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

sub BEGIN {
    @AudioPlugin::ISA = ('BasePlugin', 'AudioConverter');
}

my $arguments =
    [ { 'name' => "process_exp",
	'desc' => "{BasePlugin.process_exp}",
	'type' => "regexp",
	'deft' => &get_default_process_exp(),
	'reqd' => "no" } ];

my $options = { 'name'     => "AudioPlugin",
		'desc'     => "{AudioPlugin.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);

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

    return bless $self, $class;
}

sub init {
    my $self = shift (@_);
    my ($verbosity, $outhandle, $failhandle) = @_;

    $self->SUPER::init(@_);
    $self->AudioConverter::init();
}

sub get_default_process_exp 
{
    my $self = shift (@_);
    if (!defined $self->{'stored_process_re'}) {

	my $static_fallback_re = q^(?i)\.(aif?|au|mp3|ogg|snd|wav)$^;
	
	if ($self->{'audio_conversion_available'}) {
	    # Haven't run sox before to see what formats it supports
	    # => do so now
	    
	    my $sox_output = `sox -h`;
	    my ($output_list) = ($sox_output =~ m/SUPPORTED FILE FORMATS:\s*(.*)$/m);
	    if (defined $output_list && ($output_list ne "")) {
		$output_list =~ s/\s+/,/g;

		$self->{'stored_process_re'} = $output_list;
	    }
	    else {
		$self->{'stored_process_re'} = $static_fallback_re;
	    }
	}
	else {
	    # Audio format conversion isn't supported, but (as for images)
	    # source format might very well be OK for inclusion in web
	    # page produced for document 
	    
	    $self->{'stored_process_re'} = $static_fallback_re;
	}
    }

    print STDERR "*** returning: $self->{'stored_process_re'}\n";

    return $self->{'stored_process_re'};
	
}



# do plugin specific processing of doc_obj
sub process {
    my $self = shift (@_);
    # options??
    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();

    if ($self->{'audio_conversion_available'} == 1) {
	$self->generate_audio($filename_full_path, $filename_no_path, $doc_obj, $top_section); # should we check the return value?
    } else {
	# do some basic stuff
	# associate the audio, fileformat, mimetype, srclink, srcicon
	# do this if sox not installed. but also if generate hasn't worked??
    }
    #we have no text - adds dummy text and NoText metadata
    $self->add_dummy_text($doc_obj, $top_section);


    # *****
    # Does the code currently associate the original file with document?
    # How about it's MIME type.  This might be a bit tricky to work out
    # for a dynamically generated list

    return 1;

}





1;











