source: gs2-extensions/music-ir-src/trunk/perllib/plugins/MusicIRPlugin.pm@ 24433

Last change on this file since 24433 was 24433, checked in by davidb, 11 years ago

Extracted metadata now has explicit 'ex.' in front

File size: 5.3 KB
Line 
1###########################################################################
2#
3# MusicIRPlugin.pm -- for augmenting audio/midi files with Music IR
4# features and tags
5# A component of the Greenstone digital library software
6# from the New Zealand Digital Library Project at the
7# University of Waikato, New Zealand.
8#
9# Copyright (C) 1999 New Zealand Digital Library Project
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24#
25###########################################################################
26
27package MusicIRPlugin;
28
29use BasePlugin;
30use jAudioExtractor;
31use jSongMinerExtractor;
32
33use strict;
34no strict 'refs'; # allow filehandles to be variables and viceversa
35no strict 'subs';
36
37use gsprintf 'gsprintf';
38
39sub BEGIN {
40 @MusicIRPlugin::ISA = ('BasePlugin', 'jAudioExtractor', 'jSongMinerExtractor');
41}
42
43my $arguments =
44 [
45 { 'name' => "compute_mir_features",
46 'desc' => "{BasePlugin.compute_mir_features}",
47 'type' => "enum",
48 'list' => [{'name' => "true", 'desc' => "{common.true}"},
49 {'name' => "false", 'desc' => "{common.false}"}],
50 'deft' => "false",
51 'reqd' => "no" },
52 { 'name' => "retrieve_mir_metadata",
53 'desc' => "{BasePlugin.retrieve_mir_metadata}",
54 'type' => "enum",
55 'list' => [{'name' => "true", 'desc' => "{common.true}"},
56 {'name' => "false", 'desc' => "{common.false}"}],
57 'deft' => "false",
58 'reqd' => "no" },
59 { 'name' => "process_exp",
60 'desc' => "{BasePlugin.process_exp}",
61 'type' => "regexp",
62 'deft' => &get_default_process_exp(),
63 'reqd' => "no" }
64 ];
65
66my $options = { 'name' => "MusicIRPlugin",
67 'desc' => "{MusicIRPlugin.desc}",
68 'abstract' => "no",
69 'inherits' => "yes",
70 'args' => $arguments };
71
72
73
74sub new {
75 my ($class) = shift (@_);
76 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
77 push(@$pluginlist, $class);
78
79 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
80 push(@{$hashArgOptLists->{"OptList"}},$options);
81
82
83 my $jae_self = new jAudioExtractor($pluginlist, $inputargs, $hashArgOptLists,1);
84 my $jsme_self = new jSongMinerExtractor($pluginlist, $inputargs, $hashArgOptLists,1);
85 my $base_self = new BasePlugin($pluginlist, $inputargs, $hashArgOptLists);
86
87 my $self = BasePlugin::merge_inheritance($jae_self,$jsme_self,$base_self);
88
89 return bless $self, $class;
90}
91
92sub get_default_process_exp {
93 my $self = shift (@_);
94
95 return q^(?i)(\.mp3|\.wave?|\.aif[fc]?|\.au|\.snd|\.og[ga])$^;
96}
97
98sub begin {
99 my $self = shift (@_);
100 my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
101
102 $self->SUPER::begin(@_);
103 $self->jAudioExtractor::begin(@_);
104 $self->jSongMinerExtractor::begin(@_);
105}
106
107
108
109# do plugin specific processing of doc_obj
110sub process {
111 my $self = shift (@_);
112 my ($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
113
114 my $outhandle = $self->{'outhandle'};
115 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
116
117 my $top_section = $doc_obj->get_top_section();
118
119 if ($self->{'compute_mir_features'} eq "true")
120 {
121 my $utf8_filename_no_path = $self->filepath_to_utf8($filename_no_path);
122 my $url_encoded_filename = &util::rename_file($utf8_filename_no_path, $self->{'file_rename_method'});
123
124 my $features_filename = $self->compute_features($filename_full_path);
125 my ($features_ext) = ($features_filename =~ m/\.([^.]+)$/);
126
127
128 my $mime_type = ($features_ext eq "arff") ? "text/plain" : "text/xml";
129 $doc_obj->associate_file($features_filename, "jaudio.$features_ext",
130 $mime_type, $top_section);
131
132 }
133
134
135 #we have no text - adds dummy text and NoText metadata
136 $self->add_dummy_text($doc_obj, $doc_obj->get_top_section());
137
138 $doc_obj->associate_file($filename_full_path, "audio.mp3", "audio/mpeg", $top_section);
139
140 return 1;
141
142}
143
144sub post_process_doc_obj {
145 my $self = shift (@_);
146 my ($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
147
148 my $outhandle = $self->{'outhandle'};
149 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
150
151 if ($self->{'retrieve_mir_metadata'} eq "true") {
152 my $top_section = $doc_obj->get_top_section();
153 my $id3_titles = $doc_obj->get_metadata($top_section,"ex.ID3.Title");
154
155 my $id3_title = shift @$id3_titles || "Unknown";
156
157 my $id3_artists = $doc_obj->get_metadata($top_section,"ex.ID3.Artist");
158 my $id3_artist = shift @$id3_artists || "Unkown";
159
160 my ($metadata_acexml_filename,$metadata_txt_filename)
161 = $self->retrieve_metadata($filename_full_path,$id3_title,$id3_artist);
162
163 if (-e $metadata_txt_filename) {
164 $self->parse_txt_metadata($doc_obj,$metadata_txt_filename);
165 }
166
167 $doc_obj->associate_file($metadata_acexml_filename, "jsongminer.xml",
168 "text/xml", $top_section);
169 }
170
171
172 return 1;
173}
174
175
1761;
177
178
179
Note: See TracBrowser for help on using the repository browser.