source: gs2-extensions/music-ir-src/trunk/perllib/plugins/pEssentiaExtractor.pm

Last change on this file was 33105, checked in by davidb, 5 years ago

Some initial work in support Essentia computed audio features using Python bindings

  • Property svn:executable set to *
File size: 10.8 KB
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' => "essentia_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 $self->{'jmir_directory'} = &util::filename_cat($music_ir_home,"lib","java"); # Set the directory holding the jMIR .jar files
117
118
119
120 return bless $self, $class;
121}
122
123
124# Create and save a temporary pEssentia batch file referring to a file to extract
125# features from
126sub prepareTempPEssentiaBatchFile
127{
128 # ARG Ob1: $new_batch_file_path refers to the path of the temporary batch file to create
129 # ARG Ob2: $model_batch_file_path refers to the path of the model batch file to base the temporary one on
130 # ARG Ob3: $input_music_file_path refers to the file to extract features with
131 # ARG Ob4: $feature_values_file_path refers to the path of the ACE XML Feature Values file that the jMIR component will output to
132 # ARG Ob5: $feature_values_file_path refers to the path of the ACE XML Feature Descriptions file that the jMIR component will output to
133 my ( $self, $new_batch_file_path, $model_batch_file_path, $input_music_file_path, $feature_values_file_path, $feature_descriptions_file_path ) = @_;
134
135 # **** TODO
136
137 # Retrieve settings for jAudioPlugin and use in batch file that is generated
138 my $sample_rate = $self->{'sample_rate'}; # sample of how to get parameter from Greenstone/GLI
139
140
141 # Read the contents of the model batch file
142 my $batch_file_contents;
143 local $/=undef;
144 open (INPUT, "$model_batch_file_path") or die "Could not read the model jAudio batch file $model_batch_file_path";
145 binmode INPUT;
146 $batch_file_contents = <INPUT>;
147 close INPUT;
148
149 # Set the batch ID tag in the temporary file
150 $batch_file_contents =~ s/<batch ID="SampleJAudioBatch">/<batch ID="$input_music_file_path">/;
151
152 # Set the input file name in the file tag in the temporary file
153 if ($^O eq "cygwin") {
154 $input_music_file_path = `cygpath -m "$input_music_file_path"`;
155 $input_music_file_path=~ s/\s+$//;
156 }
157 $batch_file_contents =~ s/<file><\/file>/<file>$input_music_file_path<\/file>/;
158
159 # Set the feature vales save path in the temporary file
160 if ($^O eq "cygwin") {
161 $feature_descriptions_file_path = `cygpath -m "$feature_descriptions_file_path"`;
162 $feature_descriptions_file_path=~ s/\s+$//;
163 }
164
165 $batch_file_contents =~ s/<destination><\/destination>/<destination>$feature_descriptions_file_path<\/destination>/;
166
167 # Set the feature vales save path in the temporary file
168 if ($^O eq "cygwin") {
169 $feature_values_file_path = `cygpath -m "$feature_values_file_path"`;
170 $feature_values_file_path=~ s/\s+$//;
171 }
172 $batch_file_contents =~ s/<destination><\/destination>/<destination>$feature_values_file_path<\/destination>/;
173
174 # Save the temporary batch file
175 open (OUTPUT, ">$new_batch_file_path") or die "Could not create the temporary jAudio batch file $new_batch_file_path";
176 print OUTPUT "$batch_file_contents";
177 close OUTPUT;
178
179 # Done
180 return 0;
181}
182
183
184sub compute_essentia_features
185{
186 my $self = shift(@_);
187 my $source_file_path = shift(@_);
188 my $convert_options = shift(@_) || "";
189
190 my $outhandle = $self->{'outhandle'};
191 my $verbosity = $self->{'verbosity'};
192
193 my $source_file_no_path = &File::Basename::basename($source_file_path);
194
195 print STDERR "******* Essentia: source_file_path = $source_file_path\n\n\n";
196
197 $self->init_cache_for_file($source_file_path);
198
199 # Determine the full name and path of the output file
200 my $target_file_path;
201 my $feature_values_file_path;
202 my $feature_descriptions_file_path;
203
204 my $target_file_type;
205 if ($self->{'output_type'} eq "CSV") {
206 $target_file_type="csv";
207 }
208 elsif ($self->{'output_type'} eq "YAML") {
209 $target_file_type="xml";
210 }
211 elsif ($self->{'output_type'} eq "ARFF") {
212 $target_file_type="arff";
213 }
214 else {
215 # JSON
216 $target_file_type="json";
217 }
218
219 if ($self->{'enable_cache'}) {
220 my $cached_dir = $self->{'cached_dir'};
221 my $file_root = $self->{'cached_file_root'};
222
223 my $target_file = "$file_root.$target_file_type";
224
225 $target_file_path = &util::filename_cat($cached_dir,$target_file);
226 }
227 else {
228 $target_file_path = &util::get_tmp_filename($target_file_type);
229 }
230
231 # **** Do-over?
232# if ($self->{'output_type'} eq "ACE XML") {
233# $feature_values_file_path = $target_file_path;
234# $feature_values_file_path =~ s/\.xml$/_FV.xml/;
235#
236# $feature_descriptions_file_path = $target_file_path;
237# $feature_descriptions_file_path =~ s/\.xml$/_FD.xml/;
238#
239# # Make target_file_path be the principle file generated by jAudio when using ACE XML
240# $target_file_path = $feature_values_file_path;
241# }
242
243 my $essentia_output_directory = $self->{'essentia_output_directory'};
244 my $jmir_directory = $self->{'jmir_directory'};
245
246
247 my $store_cwd = cwd();
248
249 # **** Do-over
250 if (!-d $jmir_directory) {
251 print STDERR "Error: Unable able to find directory '$jmir_directory'\n";
252 print STDERR " Cannot run pEssentia\n";
253 }
254 elsif (chdir($jmir_directory)) {
255
256 # Run the feature extraction.
257
258 # Specify the name for a temporary pEssentia batch file
259# my $template_batch_file_path = &util::filename_cat($jmir_directory,"SampleJAudioBatchFile.xml.in");
260# my $batch_file_path = &util::filename_cat($essentia_output_directory,"tempjaudiobatchfile.xml");
261
262# # Create the batch file
263# $self->prepareTempPEssentiaBatchFile( $batch_file_path, $template_batch_file_path,
264# $source_file_path, $feature_values_file_path, $feature_descriptions_file_path );
265
266 # Input and Output files to use are stored in the batch_file
267# my $batch_file_path_os = $batch_file_path;
268
269# $batch_file_path_os = &util::makeFilenameJavaCygwinCompatible($batch_file_path_os);
270## if ($^O eq "cygwin") {
271## $batch_file_path_os = `cygpath -w "$batch_file_path"`;
272## $batch_file_path_os =~ s/\s+$//;
273## }
274
275 # ****
276 # my $pessentia_cmd = "java -Xmx1024M -jar jaudio.jar $convert_options -b \"$batch_file_path_os\"";
277 my $pessentia_cmd = "pessentia $convert_options \"$source_file_path\" \"$target_file_path\"";
278
279 # Test the execution path
280 print("EXECUTION CMD: $pessentia_cmd\n");
281
282 my $print_info = { 'message_prefix' => "pEssentia",
283 'message' => "Extracting audio features from $source_file_no_path" };
284
285 my ($regenerated,$result,$had_error)
286 = $self->autorun_general_cmd($pessentia_cmd,$source_file_path,$target_file_path,$print_info);
287
288# # Delete the pEssentia batch file
289# unlink($batch_file_path);
290 }
291 else {
292 print STDERR "Error: failed to change directory to '$jmir_directory'\n";
293 print STDERR " Cannot run pEssentia\n";
294 }
295
296 chdir($store_cwd);
297
298 return ($target_file_path);
299}
300
301
302
303
3041;
Note: See TracBrowser for help on using the repository browser.