Index: gs3-extensions/audioDB/trunk/src/perllib/plugins/AudioDBPlugin.pm
===================================================================
--- gs3-extensions/audioDB/trunk/src/perllib/plugins/AudioDBPlugin.pm	(revision 24338)
+++ gs3-extensions/audioDB/trunk/src/perllib/plugins/AudioDBPlugin.pm	(revision 24338)
@@ -0,0 +1,189 @@
+###########################################################################
+#
+# 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, "audio.wav", "audio/wav", $top_section);
+
+    if ($self->{'enable_streaming'} ne "disabled") {
+	
+	my $streamable_ext = lc($self->{'enable_streaming'});
+
+	if ($input_ext ne $streamable_ext) {
+	    # make streamable version
+	    my $streamable_filename 
+		= $self->convert_audio_format($filename_full_path,$streamable_ext);
+
+	    $doc_obj->associate_file($streamable_filename,
+				     "streamable.$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;
+
+
+
Index: gs3-extensions/audioDB/trunk/src/perllib/plugins/FFTExtractor.pm
===================================================================
--- gs3-extensions/audioDB/trunk/src/perllib/plugins/FFTExtractor.pm	(revision 24338)
+++ gs3-extensions/audioDB/trunk/src/perllib/plugins/FFTExtractor.pm	(revision 24338)
@@ -0,0 +1,177 @@
+###########################################################################
+#
+# 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);
+    }
+
+    # Setup and run the ffmpeg command
+    my $ffmpeg_cmd = "ffmpeg $convert_options";
+    $ffmpeg_cmd .= " -i \"$source_file_path\"";
+    $ffmpeg_cmd .= " \"$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\"";
+		
+##    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;	
