source: gs2-extensions/music-ir-src/trunk/perllib/plugins/jSongMinerExtractor.pm@ 30433

Last change on this file since 30433 was 30433, checked in by davidb, 8 years ago

Debugging statements commented out

  • Property svn:executable set to *
File size: 11.8 KB
Line 
1###########################################################################
2#
3# jSongMinerExtractor - helper plugin that identifies audio through
4# external web services based on either an audio
5# computed fingerprint or ID3 title and album
6#
7# A component of the Greenstone digital library software
8# from the New Zealand Digital Library Project at the
9# University of Waikato, New Zealand.
10#
11# Copyright (C) 2010 New Zealand Digital Library Project
12#
13# This program is free software; you can redistribute it and/or modify
14# it under the terms of the GNU General Public License as published by
15# the Free Software Foundation; either version 2 of the License, or
16# (at your option) any later version.
17#
18# This program is distributed in the hope that it will be useful,
19# but WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21# GNU General Public License for more details.
22#
23# You should have received a copy of the GNU General Public License
24# along with this program; if not, write to the Free Software
25# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26#
27###########################################################################
28package jSongMinerExtractor;
29
30use BaseMediaConverter;
31
32use Cwd;
33use URI::Escape;
34
35use strict;
36no strict 'refs'; # allow filehandles to be variables and viceversa
37
38
39BEGIN {
40 @jSongMinerExtractor::ISA = ('BaseMediaConverter');
41}
42
43
44my $arguments = [
45 { 'name' => "track_identification",
46 'desc' => "{jSongMinerExtractor.track_identification}",
47 'type' => "enum",
48 'list' => [{'name' => "Fingerprint then ID3 tags", 'desc' => "{jSongMinerExtractor.fingerprint_first}"},
49 {'name' => "ID3 tags only", 'desc' => "{jSongMinerExtractor.only_ids}"},
50 {'name' => "Disabled", 'desc' => "{jSongMinerExtractor.off}"} ],
51 'deft' => 'Fingerprint then ID3 tags',
52 'reqd' => "no" }
53 ];
54
55my $options = { 'name' => "jSongMinerExtractor",
56 'desc' => "{jSongMinerExtractor.desc}",
57 'abstract' => "yes",
58 'inherits' => "yes",
59 'args' => $arguments };
60
61sub new {
62 my ($class) = shift (@_);
63 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
64 push(@$pluginlist, $class);
65
66 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
67 push(@{$hashArgOptLists->{"OptList"}},$options);
68
69 my $self = new BaseMediaConverter($pluginlist, $inputargs, $hashArgOptLists, 1);
70
71 # Set controlling variables
72 my $gsdl_home = $ENV{'GSDLHOME'};
73 my $music_ir_home = $ENV{'GEXT_MUSICIR'};
74
75 $self->{'jmir_directory'} = &util::filename_cat($music_ir_home,"lib","java"); # Set the directory holding the jMIR .jar files
76
77 return bless $self, $class;
78}
79
80sub urlEncode{
81 # ARG 1: $to_encode is the string to URL encode
82 my ($to_encode) = @_;
83 return uri_escape($to_encode);
84}
85
86# URL Decode the given string
87sub urlDecode{
88 # ARG 1: $to_decode is the string to URL decode
89 my ($to_decode) = @_;
90 my $decoded= uri_unescape($to_decode);
91
92 return $decoded;
93}
94
95sub map_id3v1_genre_num
96{
97 my ($genre_num) = @_;
98
99 $genre_num =~ s/(/\(/g;
100 $genre_num =~ s/)/\)/g;
101 $genre_num =~ s/^[\(\[\{](.*)[\)\]\}]$/$1/;
102
103 if (($genre_num =~ m/^\d+$/) && ($genre_num >= 148)) {
104 return "Unknown";
105 }
106
107 my $lookup_id3v1_genre = {
108 "0" => "Blues",
109 "1" => "Classic Rock",
110 "2" => "Country",
111 "3" => "Dance",
112 "4" => "Disco",
113 "5" => "Funk",
114 "6" => "Grunge",
115 "7" => "Hip-Hop",
116 "8" => "Jazz",
117 "9" => "Metal",
118 "10" => "New Age",
119 "11" => "Oldies",
120 "12" => "Other",
121 "13" => "Pop",
122 "14" => "R&B",
123 "15" => "Rap",
124 "16" => "Reggae",
125 "17" => "Rock",
126 "18" => "Techno",
127 "19" => "Industrial",
128 "20" => "Alternative",
129 "21" => "Ska",
130 "22" => "Death Metal",
131 "23" => "Pranks",
132 "24" => "Soundtrack",
133 "25" => "Euro-Techno",
134 "26" => "Ambient",
135 "27" => "Trip-Hop",
136 "28" => "Vocal",
137 "29" => "Jazz+Funk",
138 "30" => "Fusion",
139 "31" => "Trance",
140 "32" => "Classical",
141 "33" => "Instrumental",
142 "34" => "Acid",
143 "35" => "House",
144 "36" => "Game",
145 "37" => "Sound Clip",
146 "38" => "Gospel",
147 "39" => "Noise",
148 "40" => "Alternative Rock",
149 "41" => "Bass",
150 "42" => "Soul",
151 "43" => "Punk",
152 "44" => "Space",
153 "45" => "Meditative",
154 "46" => "Instrumental Pop",
155 "47" => "Instrumental Rock",
156 "48" => "Ethnic",
157 "49" => "Gothic",
158 "50" => "Darkwave",
159 "51" => "Techno-Industrial",
160 "52" => "Electronic",
161 "53" => "Pop-Folk",
162 "54" => "Eurodance",
163 "55" => "Dream",
164 "56" => "Southern Rock",
165 "57" => "Comedy",
166 "58" => "Cult",
167 "59" => "Gangsta",
168 "60" => "Top 40",
169 "61" => "Christian Rap",
170 "62" => "Pop/Funk",
171 "63" => "Jungle",
172 "64" => "Native US",
173 "65" => "Cabaret",
174 "66" => "New Wave",
175 "67" => "Psychadelic",
176 "68" => "Rave",
177 "69" => "Showtunes",
178 "70" => "Trailer",
179 "71" => "Lo-Fi",
180 "72" => "Tribal",
181 "73" => "Acid Punk",
182 "74" => "Acid Jazz",
183 "75" => "Polka",
184 "76" => "Retro",
185 "77" => "Musical",
186 "78" => "Rock & Roll",
187 "79" => "Hard Rock",
188 "80" => "Folk",
189 "81" => "Folk-Rock",
190 "82" => "National Folk",
191 "83" => "Swing",
192 "84" => "Fast Fusion",
193 "85" => "Bebob",
194 "86" => "Latin",
195 "87" => "Revival",
196 "88" => "Celtic",
197 "89" => "Bluegrass",
198 "90" => "Avantgarde",
199 "91" => "Gothic Rock",
200 "92" => "Progressive Rock",
201 "93" => "Psychedelic Rock",
202 "94" => "Symphonic Rock",
203 "95" => "Slow Rock",
204 "96" => "Big Band",
205 "97" => "Chorus",
206 "98" => "Easy Listening",
207 "99" => "Acoustic",
208 "100" => "Humour",
209 "101" => "Speech",
210 "102" => "Chanson",
211 "103" => "Opera",
212 "104" => "Chamber Music",
213 "105" => "Sonata",
214 "106" => "Symphony",
215 "107" => "Booty Bass",
216 "108" => "Primus",
217 "109" => "Porn Groove",
218 "110" => "Satire",
219 "111" => "Slow Jam",
220 "112" => "Club",
221 "113" => "Tango",
222 "114" => "Samba",
223 "115" => "Folklore",
224 "116" => "Ballad",
225 "117" => "Power Ballad",
226 "118" => "Rhythmic Soul",
227 "119" => "Freestyle",
228 "120" => "Duet",
229 "121" => "Punk Rock",
230 "122" => "Drum Solo",
231 "123" => "Acapella",
232 "124" => "Euro-House",
233 "125" => "Dance Hall",
234 "126" => "Goa",
235 "127" => "Drum & Bass",
236 "128" => "Club - House",
237 "129" => "Hardcore",
238 "130" => "Terror",
239 "131" => "Indie",
240 "132" => "BritPop",
241 "133" => "Negerpunk",
242 "134" => "Polsk Punk",
243 "135" => "Beat",
244 "136" => "Christian Gangsta Rap",
245 "137" => "Heavy Metal",
246 "138" => "Black Metal",
247 "139" => "Crossover",
248 "140" => "Contemporary Christian",
249 "141" => "Christian Rock",
250 "142" => "Merengue",
251 "143" => "Salsa",
252 "144" => "Thrash Metal",
253 "145" => "Anime",
254 "146" => "JPop",
255 "147" => "Synthpop"
256 };
257
258 my $mapped_genre;
259
260 if (defined $lookup_id3v1_genre->{$genre_num}) {
261# print STDERR "*** changing $genre_num -> ", $lookup_id3v1_genre->{$genre_num}, "\n";
262 $mapped_genre = $lookup_id3v1_genre->{$genre_num};
263 }
264 else {
265 $mapped_genre = $genre_num;
266 }
267
268 return $mapped_genre;
269}
270
271sub check_for_existing_id3_genre
272{
273 my $self = shift @_;
274 my ($doc_obj) = @_;
275
276 my $top_section=$doc_obj->get_top_section();
277
278 # Look for ex.ID3.Genre as well, as special case
279 my $genre_md_list = $doc_obj->get_metadata($top_section,"ex.ID3.Genre");
280 my @new_genre_md_list = ();
281
282 foreach my $gv (@$genre_md_list) {
283# print STDERR "*** got match on ex.ID3.Genre -> '$gv'\n";
284 my $new_gv = map_id3v1_genre_num($gv);
285
286 push(@new_genre_md_list,$new_gv);
287 }
288
289 $doc_obj->delete_metadata($top_section,"ex.ID3.Genre");
290 foreach my $gv (@new_genre_md_list) {
291 $doc_obj->add_utf8_metadata($top_section,"ex.ID3.Genre",$gv);
292 }
293}
294
295
296sub parse_txt_metadata
297{
298 my $self = shift @_;
299 my ($doc_obj,$target_txt_file_path) = @_;
300
301 if (open(MIN,"<$target_txt_file_path")) {
302
303 my ($md_name, $md_value);
304
305 while (defined($md_name=<MIN>) && defined($md_value=<MIN>)) {
306
307 chomp $md_name;
308 chomp $md_value;
309
310 my $top_section=$doc_obj->get_top_section();
311
312 $md_name =~ s/\+//g;
313 $md_value =~ s/\+/ /g;
314
315 $md_name = urlDecode($md_name);
316 $md_value = urlDecode($md_value);
317
318 # $md_name =~ s/\s+/ /sg;
319 $md_name =~ s/\(.*?\)$//s; # can stretch over multiple lines
320 $md_name =~ s/Last\.FM/LastFM/g;
321 $md_name =~ s/:/^/g;
322 $md_name =~ s/(API)?\^/./;
323
324# print STDERR "*** md_name = '$md_name'\n";
325
326 if ($md_name =~ m/genre$/i) {
327# print STDERR "*** got match on $md_name -> $md_value\n";
328 $md_value = map_id3v1_genre_num($md_value);
329 }
330
331 $doc_obj->add_utf8_metadata($top_section,$md_name,$md_value);
332 }
333
334 close(MIN);
335
336 $self->check_for_existing_id3_genre($doc_obj);
337
338 }
339 else {
340 print STDERR "Error: Failed to open $target_txt_file_path\n";
341 print STDERR " !$\n";
342 }
343}
344
345
346sub retrieve_metadata
347{
348 my $self = shift(@_);
349 my ($source_file_path,$id3_title,$id3_artist,$convert_options) = @_;
350
351 $convert_options = "" if (!defined $convert_options);
352
353 my $outhandle = $self->{'outhandle'};
354 my $verbosity = $self->{'verbosity'};
355
356 my $source_file_no_path = &File::Basename::basename($source_file_path);
357
358 $self->init_cache_for_file($source_file_path);
359
360 my $target_txt_file_path;
361 my $target_acexml_file_path;
362
363 if ($self->{'enable_cache'}) {
364 my $cached_dir = $self->{'cached_dir'};
365 my $file_root = $self->{'cached_file_root'};
366
367 my $target_txt_file = "${file_root}_metadata.txt";
368 my $target_acexml_file = "${file_root}.xml";
369
370 $target_txt_file_path = &util::filename_cat($cached_dir,$target_txt_file);
371 $target_acexml_file_path = &util::filename_cat($cached_dir,$target_acexml_file);
372 }
373 else {
374 $target_txt_file_path = &util::get_tmp_filename("_metadata.txt");
375 $target_acexml_file_path = &util::get_tmp_filename(".xml");
376 }
377
378 my $jmir_directory = $self->{'jmir_directory'};
379
380 my $store_cwd = cwd();
381
382 if (!-d $jmir_directory) {
383 print STDERR "Error: Unable able to find directory '$jmir_directory'\n";
384 print STDERR " Cannot run jAudio\n";
385 }
386 elsif (chdir($jmir_directory)) {
387
388 my $source_file_path_os = $source_file_path;
389 if ($^O eq "cygwin") {
390 $source_file_path_os = `cygpath -w "$source_file_path"`;
391 $source_file_path_os =~ s/\s+$//;
392 }
393 my $target_txt_file_path_os = $target_txt_file_path;
394 if ($^O eq "cygwin") {
395 $target_txt_file_path_os = `cygpath -w "$target_txt_file_path"`;
396 $target_txt_file_path_os =~ s/\s+$//;
397 }
398 my $target_acexml_file_path_os = $target_acexml_file_path;
399 if ($^O eq "cygwin") {
400 $target_acexml_file_path_os = `cygpath -w "$target_acexml_file_path"`;
401 $target_acexml_file_path_os =~ s/\s+$//;
402 }
403
404
405 my $jsongminer_cmd = "java -Xmx1024M -jar \"" . $jmir_directory . "/jSongMiner.jar\" $convert_options";
406 $jsongminer_cmd .= " -title \"$id3_title\"" if defined $id3_title;
407 $jsongminer_cmd .= " -artist \"$id3_artist\"" if defined $id3_artist;
408 $jsongminer_cmd .= " -audio \"$source_file_path_os\"";
409 $jsongminer_cmd .= " -savetxtfile \"$target_txt_file_path_os\"";
410 $jsongminer_cmd .= " -saveacexmlfile \"$target_acexml_file_path_os\"";
411
412 if ($verbosity>2) {
413 print $outhandle "jSongMinerExtractor: Running ...\n";
414 print $outhandle "jSongMinerExtractor: $jsongminer_cmd\n";
415 }
416
417 my $print_info = { 'message_prefix' => "jSongMiner",
418 'message' => "jSongMinerExtractor: Retrieving audio metadata for $source_file_no_path" };
419
420 my ($regenerated,$result,$had_error)
421 = $self->autorun_general_cmd($jsongminer_cmd,$source_file_path,$target_txt_file_path,$print_info);
422
423 if ($verbosity>2) {
424 print $outhandle "jSongMinerExtractor: ...done\n";
425 }
426 }
427 else {
428 print STDERR "Error: failed to change directory to '$jmir_directory'\n";
429 print STDERR " Cannot run jAudio\n";
430 }
431
432 chdir($store_cwd);
433
434 return ($target_acexml_file_path,$target_txt_file_path);
435}
436
437
438
439
4401;
Note: See TracBrowser for help on using the repository browser.