root/gs3-extensions/mars-src/trunk/perllib/plugins/pEssentiaExtractor.pm @ 34360

Revision 34360, 10.2 KB (checked in by davidb, 7 weeks ago)

Collating of python essensia custom scripts and essentia perl plugin code

  • Property svn:executable set to *
Line 
1###########################################################################
2#
3# pEssentiaExtractor - helper plugin that computers audio features for
4#                        music information retrieval use, using python bindings of Essentia
5#
6# A component of the Greenstone digital library software
7# from the New Zealand Digital Library Project at the
8# University of Waikato, New Zealand.
9#
10# Copyright (C) 2010 New Zealand Digital Library Project
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25#
26###########################################################################
27package pEssentiaExtractor;
28
29use BaseMediaConverter;
30
31use Cwd;
32
33use FileUtils;
34
35use strict;
36no strict 'refs'; # allow filehandles to be variables and viceversa
37
38
39BEGIN {
40    @pEssentiaExtractor::ISA = ('BaseMediaConverter');
41}
42
43
44my $arguments = [
45    { 'name' => "window_size",
46      'desc' => "{jAudioExtractor.window_size}",
47      'type' => "int",
48      'range' => "128,",
49      'deft' => '512',
50      'reqd' => "no" },
51    { 'name' => "window_overlap",
52      'desc' => "{jAudioExtractor.window_overlap}",
53      'type' => "string",
54      'range' => "0.0",
55      'deft' => '0.0',
56      'reqd' => "no" },
57    { 'name' => "sample_rate",
58      'desc' => "{jAudioExtractor.sample_rate}",
59      'type' => "enum",
60      'list' => [{'name' => "8 kHz",      'desc' => "{jAudioExtractor.8000Hz}"},
61         {'name' => "11.025 kHz", 'desc' => "{jAudioExtractor.11025Hz}"},
62         {'name' => "16 kHz",     'desc' => "{jAudioExtractor.16000Hz}"},
63         {'name' => "22.05 kHz",  'desc' => "{jAudioExtractor.22050Hz}"},
64         {'name' => "44.1 kHz",   'desc' => "{jAudioExtractor.44100Hz}"} ],
65      'deft' => '16 kHz',
66      'reqd' => "no" },
67    { 'name' => "extracted_data",
68      'desc' => "{pEssentiaExtractor.extracted_data}", # ****** delete or change
69      'type' => "enum",
70      'list' => [{'name' => "Overall and Windowed", 'desc' => "{jAudioExtractor.overall_and_windowed}"},
71         {'name' => "Windowed only", 'desc' => "{jAudioExtractor.windowed_only}"},
72         {'name' => "Overall only", 'desc' => "{jAudioExtractor.overall_only}"}],
73      'deft' => 'Overall and Windowed',
74      'reqd' => "no" },
75    { 'name' => "output_type",
76      'desc' => "{pEssentiaExtractor.output_type}",
77      'type' => "enum",
78      'list' => [
79      {'name' => "JSON", 'desc' => "{pEssentiaExtractor.json}"},
80      {'name' => "CSV",  'desc' => "{pEssentiaExtractor.csv}"},
81      {'name' => "YAML", 'desc' => "{pEssentiaExtractor.yaml}"},
82      {'name' => "Weka ARFF", 'desc' => "{jAudioExtractor.weka_arff}"}
83      ],
84      'deft' => 'JSON',
85      'reqd' => "no" }
86    ];
87
88
89
90my $options = { 'name' => "pEssentiaExtractor",
91        'desc' => "{pEssentiaExtractor.desc}",
92        'abstract' => "yes",
93        'inherits' => "yes",
94        'args' => $arguments };
95
96sub new {
97    my ($class) = shift (@_);
98    my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
99    push(@$pluginlist, $class);
100
101    push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
102    push(@{$hashArgOptLists->{"OptList"}},$options);
103
104    my $self = new BaseMediaConverter($pluginlist, $inputargs, $hashArgOptLists, 1);
105
106    # Set controlling variables
107    my $gsdl_home = $ENV{'GSDLHOME'};
108    my $music_ir_home = $ENV{'GEXT_MUSICIR'};
109
110    my $essentia_output_directory = &util::filename_cat($gsdl_home,"tmp"); # Set the directory to save the the ACE XML output files in
111    if (!FileUtils::directoryExists($essentia_output_directory)) {
112    FileUtils::makeDirectory($essentia_output_directory);
113    }
114    $self->{'essentia_output_directory'} = $essentia_output_directory;
115
116
117    return bless $self, $class;
118}
119
120
121# Create and save a temporary pEssentia batch file referring to a file to extract
122# features from
123sub prepareTempPEssentiaBatchFile
124{
125    # ARG Ob1: $new_batch_file_path refers to the path of the temporary batch file to create
126    # ARG Ob2: $model_batch_file_path refers to the path of the model batch file to base the temporary one on
127    # ARG Ob3: $input_music_file_path refers to the file to extract features with
128    # ARG Ob4: $feature_values_file_path refers to the path of the ACE XML Feature Values file that the jMIR component will output to
129    # ARG Ob5: $feature_values_file_path refers to the path of the ACE XML Feature Descriptions file that the jMIR component will output to
130    my ( $self, $new_batch_file_path, $model_batch_file_path, $input_music_file_path, $feature_values_file_path, $feature_descriptions_file_path ) = @_;
131
132    # **** TODO
133   
134    # Retrieve settings for jAudioPlugin and use in batch file that is generated
135    my $sample_rate = $self->{'sample_rate'}; # sample of how to get parameter from Greenstone/GLI
136
137
138    # Read the contents of the model batch file
139    my $batch_file_contents;
140    local $/=undef;
141    open (INPUT, "$model_batch_file_path") or die "Could not read the model jAudio batch file $model_batch_file_path";
142    binmode INPUT;
143    $batch_file_contents = <INPUT>;
144    close INPUT;
145
146    # Set the batch ID tag in the temporary file
147    $batch_file_contents =~ s/<batch ID="SampleJAudioBatch">/<batch ID="$input_music_file_path">/;
148
149    # Set the input file name in the file tag in the temporary file
150    if ($^O eq "cygwin") {
151    $input_music_file_path = `cygpath -m "$input_music_file_path"`;
152    $input_music_file_path=~ s/\s+$//;
153    }
154    $batch_file_contents =~ s/<file><\/file>/<file>$input_music_file_path<\/file>/;
155
156    # Set the feature vales save path in the temporary file
157    if ($^O eq "cygwin") {
158    $feature_descriptions_file_path = `cygpath -m "$feature_descriptions_file_path"`;
159    $feature_descriptions_file_path=~ s/\s+$//;
160    }
161
162    $batch_file_contents =~ s/<destination><\/destination>/<destination>$feature_descriptions_file_path<\/destination>/;
163
164    # Set the feature vales save path in the temporary file
165    if ($^O eq "cygwin") {
166    $feature_values_file_path = `cygpath -m "$feature_values_file_path"`;
167    $feature_values_file_path=~ s/\s+$//;
168    }
169    $batch_file_contents =~ s/<destination><\/destination>/<destination>$feature_values_file_path<\/destination>/;
170
171    # Save the temporary batch file
172    open (OUTPUT, ">$new_batch_file_path") or die "Could not create the temporary jAudio batch file $new_batch_file_path";
173    print OUTPUT "$batch_file_contents";
174    close OUTPUT;
175   
176    # Done
177    return 0;
178}
179
180
181sub compute_essentia_features
182{
183    my $self = shift(@_);
184    my $source_file_path = shift(@_);
185    my $convert_options  = shift(@_) || "";
186
187    my $outhandle = $self->{'outhandle'};
188    my $verbosity = $self->{'verbosity'};
189
190    my $source_file_no_path = &File::Basename::basename($source_file_path);
191
192    print STDERR "******* Essentia: source_file_path = $source_file_path\n\n\n";
193   
194    $self->init_cache_for_file($source_file_path);
195
196    # Determine the full name and path of the output file
197    my $target_file_path;
198    my $feature_values_file_path;
199    my $feature_descriptions_file_path;
200
201    my $target_file_type;
202    if ($self->{'output_type'} eq "CSV") {
203    $target_file_type="csv";
204    }
205    elsif ($self->{'output_type'} eq "YAML") {
206    $target_file_type="xml";
207    }
208    elsif ($self->{'output_type'} eq "ARFF") {
209    $target_file_type="arff";
210    }
211    else {
212    # JSON
213    $target_file_type="json";
214    }
215
216    if ($self->{'enable_cache'}) {
217    my $cached_dir = $self->{'cached_dir'};
218    my $file_root = $self->{'cached_file_root'};
219
220    my $target_file = "$file_root.$target_file_type";
221
222    $target_file_path = &util::filename_cat($cached_dir,$target_file);
223    }
224    else {
225    $target_file_path = &util::get_tmp_filename($target_file_type);
226    }
227
228    # **** Do-over?
229#    if ($self->{'output_type'} eq "ACE XML") {
230#   $feature_values_file_path = $target_file_path;
231#   $feature_values_file_path =~ s/\.xml$/_FV.xml/;
232#
233#   $feature_descriptions_file_path = $target_file_path;
234#   $feature_descriptions_file_path =~ s/\.xml$/_FD.xml/;
235#
236#   # Make target_file_path be the principle file generated by jAudio when using ACE XML
237#   $target_file_path = $feature_values_file_path;
238#    }
239
240    my $essentia_output_directory = $self->{'essentia_output_directory'};
241
242    # **** Do-over
243
244    # Run the feature extraction.
245
246    # Specify the name for a temporary pEssentia batch file
247#   my $template_batch_file_path = &util::filename_cat($jmir_directory,"SampleJAudioBatchFile.xml.in");
248#   my $batch_file_path = &util::filename_cat($essentia_output_directory,"tempjaudiobatchfile.xml");
249   
250#   # Create the batch file
251#   $self->prepareTempPEssentiaBatchFile( $batch_file_path, $template_batch_file_path,
252#                         $source_file_path, $feature_values_file_path, $feature_descriptions_file_path );
253   
254    # Input and Output files to use are stored in the batch_file
255#   my $batch_file_path_os = $batch_file_path;
256
257#   $batch_file_path_os = &util::makeFilenameJavaCygwinCompatible($batch_file_path_os);
258##  if ($^O eq "cygwin") {
259##      $batch_file_path_os = `cygpath -w "$batch_file_path"`;
260##      $batch_file_path_os =~ s/\s+$//;
261##  }
262
263    # ****
264    # my $pessentia_cmd = "java -Xmx1024M -jar jaudio.jar $convert_options -b \"$batch_file_path_os\"";
265    my $pessentia_cmd = "pessentia.sh $convert_options \"$source_file_path\" \"$target_file_path\"";
266       
267    # Test the execution path
268    print("EXECUTION CMD: $pessentia_cmd\n");
269   
270    my $print_info = { 'message_prefix' => "pEssentia",
271               'message' => "Extracting audio features from $source_file_no_path" };
272   
273    my ($regenerated,$result,$had_error)
274        = $self->autorun_general_cmd($pessentia_cmd,$source_file_path,$target_file_path,$print_info);
275   
276#   # Delete the pEssentia batch file
277#   unlink($batch_file_path);
278
279    return ($target_file_path);
280}
281   
282
283
284
2851; 
Note: See TracBrowser for help on using the browser.