source: gs2-extensions/music-ir-src/trunk/perllib/plugins/jAudioExtractor.pm@ 26276

Last change on this file since 26276 was 26276, checked in by davidb, 12 years ago

Some mods that resulted from testing under Cygwin

  • Property svn:executable set to *
File size: 10.0 KB
Line 
1###########################################################################
2#
3# jAudioExtractor - helper plugin that computers audio features for
4# music information retrieval use, using jAudio
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 jAudioExtractor;
28
29use BaseMediaConverter;
30
31use Cwd;
32
33use strict;
34no strict 'refs'; # allow filehandles to be variables and viceversa
35
36
37BEGIN {
38 @jAudioExtractor::ISA = ('BaseMediaConverter');
39}
40
41
42my $arguments = [
43 { 'name' => "window_size",
44 'desc' => "{jAudioExtractor.window_size}",
45 'type' => "int",
46 'range' => "128,",
47 'deft' => '512',
48 'reqd' => "no" },
49 { 'name' => "window_overlap",
50 'desc' => "{jAudioExtractor.window_overlap}",
51 'type' => "string",
52 'range' => "0.0",
53 'deft' => '0.0',
54 'reqd' => "no" },
55 { 'name' => "sample_rate",
56 'desc' => "{jAudioExtractor.sample_rate}",
57 'type' => "enum",
58 'list' => [{'name' => "8 kHz", 'desc' => "{jAudioExtractor.8000Hz}"},
59 {'name' => "11.025 kHz", 'desc' => "{jAudioExtractor.11025Hz}"},
60 {'name' => "16 kHz", 'desc' => "{jAudioExtractor.16000Hz}"},
61 {'name' => "22.05 kHz", 'desc' => "{jAudioExtractor.22050Hz}"},
62 {'name' => "44.1 kHz", 'desc' => "{jAudioExtractor.44100Hz}"} ],
63 'deft' => '16 kHz',
64 'reqd' => "no" },
65 { 'name' => "extracted_data",
66 'desc' => "{jAudioExtractor.extracted_data}",
67 'type' => "enum",
68 'list' => [{'name' => "Overall and Windowed", 'desc' => "{jAudioExtractor.overall_and_windowed}"},
69 {'name' => "Windowed only", 'desc' => "{jAudioExtractor.windowed_only}"},
70 {'name' => "Overall only", 'desc' => "{jAudioExtractor.overall_only}"}],
71 'deft' => 'Overall and Windowed',
72 'reqd' => "no" },
73 { 'name' => "output_type",
74 'desc' => "{jAudioExtractor.output_type}",
75 'type' => "enum",
76 'list' => [{'name' => "ACE XML", 'desc' => "{jAudioExtractor.ace_xml}"},
77 {'name' => "Weka ARFF", 'desc' => "{jAudioExtractor.weka_arff}"}],
78 'deft' => 'ACE XML',
79 'reqd' => "no" }
80 ];
81
82
83
84my $options = { 'name' => "jAudioExtractor",
85 'desc' => "{jAudioExtractor.desc}",
86 'abstract' => "yes",
87 'inherits' => "yes",
88 'args' => $arguments };
89
90sub new {
91 my ($class) = shift (@_);
92 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
93 push(@$pluginlist, $class);
94
95 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
96 push(@{$hashArgOptLists->{"OptList"}},$options);
97
98 my $self = new BaseMediaConverter($pluginlist, $inputargs, $hashArgOptLists, 1);
99
100 # Set controlling variables
101 my $gsdl_home = $ENV{'GSDLHOME'};
102 my $music_ir_home = $ENV{'GEXT_MUSICIR'};
103
104 my $ace_xml_output_directory = &util::filename_cat($gsdl_home,"tmp"); # Set the directory to save the the ACE XML output files in
105 if (!util::dir_exists($ace_xml_output_directory)) {
106 util::mk_dir($ace_xml_output_directory);
107 }
108 $self->{'ace_xml_output_directory'} = $ace_xml_output_directory;
109
110 $self->{'jmir_directory'} = &util::filename_cat($music_ir_home,"lib","java"); # Set the directory holding the jMIR .jar files
111
112
113
114 return bless $self, $class;
115}
116
117
118# Create and save a temporary jAudio batch file referring to a file to extract
119# features from
120sub prepareTempJAudioBatchFile
121{
122 # ARG Ob1: $new_batch_file_path refers to the path of the temporary batch file to create
123 # ARG Ob2: $model_batch_file_path refers to the path of the model batch file to base the temporary one on
124 # ARG Ob3: $input_music_file_path refers to the file to extract features with
125 # ARG Ob4: $feature_values_file_path refers to the path of the ACE XML Feature Values file that the jMIR component will output to
126 # ARG Ob5: $feature_values_file_path refers to the path of the ACE XML Feature Descriptions file that the jMIR component will output to
127 my ( $self, $new_batch_file_path, $model_batch_file_path, $input_music_file_path, $feature_values_file_path, $feature_descriptions_file_path ) = @_;
128
129 # Retrieve settings for jAudioPlugin and use in batch file that is generated
130 my $sample_rate = $self->{'sample_rate'}; # sample of how to get parameter from Greenstone/GLI
131
132
133 # Read the contents of the model batch file
134 my $batch_file_contents;
135 local $/=undef;
136 open (INPUT, "$model_batch_file_path") or die "Could not read the model jAudio batch file $model_batch_file_path";
137 binmode INPUT;
138 $batch_file_contents = <INPUT>;
139 close INPUT;
140
141 # Set the batch ID tag in the temporary file
142 $batch_file_contents =~ s/<batch ID="SampleJAudioBatch">/<batch ID="$input_music_file_path">/;
143
144 # Set the input file name in the file tag in the temporary file
145 if ($^O eq "cygwin") {
146 $input_music_file_path = `cygpath -m "$input_music_file_path"`;
147 $input_music_file_path=~ s/\s+$//;
148 }
149 $batch_file_contents =~ s/<file><\/file>/<file>$input_music_file_path<\/file>/;
150
151 # Set the feature vales save path in the temporary file
152 if ($^O eq "cygwin") {
153 $feature_descriptions_file_path = `cygpath -m "$feature_descriptions_file_path"`;
154 $feature_descriptions_file_path=~ s/\s+$//;
155 }
156
157 $batch_file_contents =~ s/<destination><\/destination>/<destination>$feature_descriptions_file_path<\/destination>/;
158
159 # Set the feature vales save path in the temporary file
160 if ($^O eq "cygwin") {
161 $feature_values_file_path = `cygpath -m "$feature_values_file_path"`;
162 $feature_values_file_path=~ s/\s+$//;
163 }
164 $batch_file_contents =~ s/<destination><\/destination>/<destination>$feature_values_file_path<\/destination>/;
165
166 # Save the temporary batch file
167 open (OUTPUT, ">$new_batch_file_path") or die "Could not create the temporary jAudio batch file $new_batch_file_path";
168 print OUTPUT "$batch_file_contents";
169 close OUTPUT;
170
171 # Done
172 return 0;
173}
174
175
176sub compute_features
177{
178 my $self = shift(@_);
179 my $source_file_path = shift(@_);
180 my $convert_options = shift(@_) || "";
181
182 my $outhandle = $self->{'outhandle'};
183 my $verbosity = $self->{'verbosity'};
184
185 my $source_file_no_path = &File::Basename::basename($source_file_path);
186
187 $self->init_cache_for_file($source_file_path);
188
189 # Determine the full name and path of the output file
190 my $target_file_path;
191 my $feature_values_file_path;
192 my $feature_descriptions_file_path;
193
194 my $target_file_type;
195 if ($self->{'output_type'} eq "ACE XML") {
196 $target_file_type="xml";
197 }
198 else {
199 # ARFF
200 $target_file_type="arff";
201 }
202
203 if ($self->{'enable_cache'}) {
204 my $cached_dir = $self->{'cached_dir'};
205 my $file_root = $self->{'cached_file_root'};
206
207 my $target_file = "$file_root.$target_file_type";
208
209 $target_file_path = &util::filename_cat($cached_dir,$target_file);
210 }
211 else {
212 $target_file_path = &util::get_tmp_filename($target_file_type);
213 }
214
215 if ($self->{'output_type'} eq "ACE XML") {
216 $feature_values_file_path = $target_file_path;
217 $feature_values_file_path =~ s/\.xml$/_FV.xml/;
218
219 $feature_descriptions_file_path = $target_file_path;
220 $feature_descriptions_file_path =~ s/\.xml$/_FD.xml/;
221
222 # Make target_file_path be the principle file generated by jAudio when using ACE XML
223 $target_file_path = $feature_values_file_path;
224 }
225
226 my $ace_xml_output_directory = $self->{'ace_xml_output_directory'};
227 my $jmir_directory = $self->{'jmir_directory'};
228
229
230 my $store_cwd = cwd();
231
232 if (!-d $jmir_directory) {
233 print STDERR "Error: Unable able to find directory '$jmir_directory'\n";
234 print STDERR " Cannot run jAudio\n";
235 }
236 elsif (chdir($jmir_directory)) {
237
238 # Run the feature extraction.
239
240 # Specify the name for a temporary jAudio batch file
241 my $template_batch_file_path = &util::filename_cat($jmir_directory,"SampleJAudioBatchFile.xml.in");
242 my $batch_file_path = &util::filename_cat($ace_xml_output_directory,"tempjaudiobatchfile.xml");
243
244 # Create the batch file
245 $self->prepareTempJAudioBatchFile( $batch_file_path, $template_batch_file_path,
246 $source_file_path, $feature_values_file_path, $feature_descriptions_file_path );
247
248 # Input and Output files to use are stored in the batch_file
249 my $batch_file_path_os = $batch_file_path;
250
251 if ($^O eq "cygwin") {
252 $batch_file_path_os = `cygpath -w "$batch_file_path"`;
253 $batch_file_path_os =~ s/\s+$//;
254 }
255
256 my $jaudio_cmd = "java -Xmx1024M -jar jAudio.jar $convert_options -b \"$batch_file_path_os\"";
257
258 # Test the execution path
259 # print("EXECUTION CMD: $jaudio_cmd\n");
260
261 my $print_info = { 'message_prefix' => "jAudio",
262 'message' => "Extracting audio features from $source_file_no_path" };
263
264 my ($regenerated,$result,$had_error)
265 = $self->autorun_general_cmd($jaudio_cmd,$source_file_path,$target_file_path,$print_info);
266
267 # Delete the jAudio batch file
268 unlink($batch_file_path);
269 }
270 else {
271 print STDERR "Error: failed to change directory to '$jmir_directory'\n";
272 print STDERR " Cannot run jAudio\n";
273 }
274
275 chdir($store_cwd);
276
277 return ($target_file_path);
278}
279
280
281
282
2831;
Note: See TracBrowser for help on using the repository browser.