root/gs2-extensions/music-ir-src/trunk/perllib/plugins/jSongMinerExtractor.pm @ 29656

Revision 29656, 11.8 KB (checked in by jmt12, 5 years ago)

Using the path to the jmir_directory when running the jSongMiner.jar

  • Property svn:executable set to *
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    print STDERR "**#####** jSongMiner parsing txt metadata\n";
302
303    if (open(MIN,"<$target_txt_file_path")) {
304
305    my ($md_name, $md_value);
306
307    while (defined($md_name=<MIN>) && defined($md_value=<MIN>)) {
308
309        chomp $md_name;
310        chomp $md_value;
311
312        my $top_section=$doc_obj->get_top_section();
313
314        $md_name =~ s/\+//g;
315        $md_value =~ s/\+/ /g;
316       
317        $md_name = urlDecode($md_name);
318        $md_value = urlDecode($md_value);
319
320        # $md_name =~ s/\s+/ /sg;
321        $md_name =~ s/\(.*?\)$//s; # can stretch over multiple lines
322        $md_name =~ s/Last\.FM/LastFM/g;
323        $md_name =~ s/:/^/g;
324        $md_name =~ s/(API)?\^/./;
325
326#       print STDERR "*** md_name = '$md_name'\n";
327
328        if ($md_name =~ m/genre$/i) {
329        print STDERR "*** got match on $md_name -> $md_value\n";
330        $md_value = map_id3v1_genre_num($md_value);
331        }
332
333        $doc_obj->add_utf8_metadata($top_section,$md_name,$md_value);
334    }
335
336    close(MIN);
337
338    $self->check_for_existing_id3_genre($doc_obj);
339
340    }
341    else {
342    print STDERR "Error: Failed to open $target_txt_file_path\n";
343    print STDERR "       !$\n";
344    }
345}
346
347
348sub retrieve_metadata
349{
350    my $self = shift(@_);
351    my ($source_file_path,$id3_title,$id3_artist,$convert_options) = @_;
352
353    $convert_options = "" if (!defined $convert_options);
354
355    my $outhandle = $self->{'outhandle'};
356    my $verbosity = $self->{'verbosity'};
357
358    my $source_file_no_path = &File::Basename::basename($source_file_path);
359
360    $self->init_cache_for_file($source_file_path);
361
362    my $target_txt_file_path;
363    my $target_acexml_file_path;
364
365    if ($self->{'enable_cache'}) {
366    my $cached_dir = $self->{'cached_dir'};
367    my $file_root = $self->{'cached_file_root'};
368
369    my $target_txt_file = "${file_root}_metadata.txt";
370    my $target_acexml_file = "${file_root}.xml";
371
372    $target_txt_file_path = &util::filename_cat($cached_dir,$target_txt_file);
373    $target_acexml_file_path = &util::filename_cat($cached_dir,$target_acexml_file);
374    }
375    else {
376    $target_txt_file_path = &util::get_tmp_filename("_metadata.txt");
377    $target_acexml_file_path = &util::get_tmp_filename(".xml");
378    }
379
380    my $jmir_directory = $self->{'jmir_directory'};
381
382    my $store_cwd = cwd();
383
384    if (!-d $jmir_directory) {
385    print STDERR "Error: Unable able to find directory '$jmir_directory'\n";
386    print STDERR "       Cannot run jAudio\n";
387    }
388    elsif (chdir($jmir_directory)) {
389
390    my $source_file_path_os = $source_file_path;
391    if ($^O eq "cygwin") {
392        $source_file_path_os = `cygpath -w "$source_file_path"`;
393        $source_file_path_os =~ s/\s+$//;
394    }
395    my $target_txt_file_path_os = $target_txt_file_path;
396    if ($^O eq "cygwin") {
397        $target_txt_file_path_os = `cygpath -w "$target_txt_file_path"`;
398        $target_txt_file_path_os =~ s/\s+$//;
399    }
400    my $target_acexml_file_path_os = $target_acexml_file_path;
401    if ($^O eq "cygwin") {
402        $target_acexml_file_path_os = `cygpath -w "$target_acexml_file_path"`;
403        $target_acexml_file_path_os =~ s/\s+$//;
404    }
405
406
407    my $jsongminer_cmd = "java -Xmx1024M -jar \"" . $jmir_directory . "/jSongMiner.jar\" $convert_options";
408    $jsongminer_cmd .= " -title \"$id3_title\"" if defined $id3_title;
409    $jsongminer_cmd .= " -artist \"$id3_artist\"" if defined $id3_artist;
410    $jsongminer_cmd .= " -audio \"$source_file_path_os\"";
411    $jsongminer_cmd .= " -savetxtfile \"$target_txt_file_path_os\"";
412    $jsongminer_cmd .= " -saveacexmlfile \"$target_acexml_file_path_os\"";
413   
414    if ($verbosity>2) {
415        print $outhandle "jSongMinerExtractor: Running ...\n";
416        print $outhandle "jSongMinerExtractor:   $jsongminer_cmd\n";
417    }
418   
419    my $print_info = { 'message_prefix' => "jSongMiner",
420               'message' => "jSongMinerExtractor: Retrieving audio metadata for $source_file_no_path" };
421
422    my ($regenerated,$result,$had_error)
423        = $self->autorun_general_cmd($jsongminer_cmd,$source_file_path,$target_txt_file_path,$print_info);
424   
425    if ($verbosity>2) {
426        print $outhandle "jSongMinerExtractor: ...done\n";
427    }
428    }
429    else {
430    print STDERR "Error: failed to change directory to '$jmir_directory'\n";
431    print STDERR "       Cannot run jAudio\n";
432    }
433
434    chdir($store_cwd);
435
436    return ($target_acexml_file_path,$target_txt_file_path);
437}
438   
439
440
441
4421; 
Note: See TracBrowser for help on using the browser.