######################################################################
#
# AudioPlugin.pm -- plugin for processing video largely based on ImagePlug
# A component of the Greenstone digital library software
# from the New Zealand Digital Library Project at the 
# University of Waikato, New Zealand.
#
# Copyright (C) 1999 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 StructuredAudioPlugin;

use strict;
no strict 'refs'; # allow filehandles to be variables and viceversa
no strict 'subs';

use gsprintf;
use FileUtils;

use AudioPlugin;


sub BEGIN {
    @StructuredAudioPlugin::ISA = ('AudioPlugin');
}

my $arguments =
    [ { 'name' => "segment_audio",
	'desc' => "{StructuredAudioPlugin.segment_audio}",
	'type' => "flag",
	'reqd' => "no" }
];

my $options = { 'name'     => "StructuredAudioPlugin",
		'desc'     => "{StructuredAudioPlugin.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 $self = new AudioPlugin($pluginlist, $inputargs, $hashArgOptLists);

    return bless $self, $class;
}


sub begin {
    my $self = shift (@_);
    my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;

    $self->SUPER::begin(@_);
}


sub init {
    my $self = shift (@_);
    my ($verbosity, $outhandle, $failhandle) = @_;

    $self->SUPER::init(@_);

     my @pyannote_monitor   = ( 'monitor_init'   , "MultimediaConverter::unbuffered_monitor_init",
                                'monitor_line'   , "StructuredAudioPlugin::pyannote_monitor_line",
                                'monitor_deinit' , "MultimediaConverter::unbuffered_monitor_deinit" );
                                        
    $self->{'pyannote_monitor'} = \@pyannote_monitor;
}


sub pyannote_monitor_line
{
    my ($line) = @_;
    
    my $had_error = 0;
    my $generate_dot = 0;
        
    if ($line =~ m/^.*$/) 
    {
        $generate_dot = 1;
    }

    return ($had_error,$generate_dot);
}




sub run_convert {
    my $self = shift (@_);
    my ($base_dir,$filename,$file,$doc_obj) = @_;
    
    my $section = $doc_obj->get_top_section();
    
    my $verbosity = $self->{'verbosity'};
    my $outhandle = $self->{'outhandle'};

   $self->SUPER::run_convert(@_);

   if ($self->{'segment_audio'}) {

        $self->init_cache_for_file($filename);
        my $cached_audio_dir = $self->{'cached_dir'};
	    # my $audio_root = $self->{'cached_file_root'};

        # my $filename_no_path = &File::Basename::basename($filename);

        my $ofile = "structured-audio.csv";
        my $ofilename = &FileUtils::filenameConcatenate($cached_audio_dir,"structured-audio.csv");
        my $pyannote_cmd = "diarization.py \"$filename\" \"$ofilename\"";

        my $pyannote_regenerated;
        my $pyannote_result;
        my $pyannote_error;
        
        my $pyannote_options = { @{$self->{'pyannote_monitor'}},
                    'message_prefix' => "pyannote",
                    'message' => "Segmenting audio" }; 

        ($pyannote_regenerated,$pyannote_result,$pyannote_error)
            = $self->run_cached_general_cmd($pyannote_cmd,$filename,$ofilename,$pyannote_options);

        $doc_obj->associate_file($ofilename,$ofile,"text/csv",$section);

   }

    return "csv";
}


1;











