source: gs2-extensions/music-ir-src/trunk/perllib/plugins/MusicIRPlugin.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

File size: 6.8 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 BaseImporter;
30use jAudioExtractor;
31use jSongMinerExtractor;
32use pEssentiaExtractor;
33
34use strict;
35no strict 'refs'; # allow filehandles to be variables and viceversa
36no strict 'subs';
37
38use gsprintf 'gsprintf';
39
40sub BEGIN {
41 @MusicIRPlugin::ISA = ('BaseImporter', 'jAudioExtractor', 'jSongMinerExtractor', 'pEssentiaExtractor');
42}
43
44my $arguments =
45 [
46 { 'name' => "compute_mir_features",
47 'desc' => "{BaseImporter.compute_mir_features}",
48 'type' => "enum",
49 'list' => [{'name' => "true", 'desc' => "{common.true}"},
50 {'name' => "false", 'desc' => "{common.false}"}],
51 'deft' => "false",
52 'reqd' => "no" },
53 { 'name' => "compute_essentia_features",
54 'desc' => "{BaseImporter.compute_essentia_features}",
55 'type' => "enum",
56 'list' => [{'name' => "true", 'desc' => "{common.true}"},
57 {'name' => "false", 'desc' => "{common.false}"}],
58 'deft' => "false",
59 'reqd' => "no" },
60 { 'name' => "retrieve_mir_metadata",
61 'desc' => "{BaseImporter.retrieve_mir_metadata}",
62 'type' => "enum",
63 'list' => [{'name' => "true", 'desc' => "{common.true}"},
64 {'name' => "false", 'desc' => "{common.false}"}],
65 'deft' => "false",
66 'reqd' => "no" },
67 { 'name' => "process_exp",
68 'desc' => "{BaseImporter.process_exp}",
69 'type' => "regexp",
70 'deft' => &get_default_process_exp(),
71 'reqd' => "no" }
72 ];
73
74my $options = { 'name' => "MusicIRPlugin",
75 'desc' => "{MusicIRPlugin.desc}",
76 'abstract' => "no",
77 'inherits' => "yes",
78 'args' => $arguments };
79
80
81
82sub new {
83 my ($class) = shift (@_);
84 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
85 push(@$pluginlist, $class);
86
87 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
88 push(@{$hashArgOptLists->{"OptList"}},$options);
89
90
91 my $jae_self = new jAudioExtractor($pluginlist, $inputargs, $hashArgOptLists,1);
92 my $jsme_self = new jSongMinerExtractor($pluginlist, $inputargs, $hashArgOptLists,1);
93 my $pee_self = new pEssentiaExtractor($pluginlist, $inputargs, $hashArgOptLists,1);
94
95 my $base_self = new BaseImporter($pluginlist, $inputargs, $hashArgOptLists);
96
97 my $self = BaseImporter::merge_inheritance($jae_self,$jsme_self,$base_self);
98
99 return bless $self, $class;
100}
101
102sub get_default_process_exp {
103 my $self = shift (@_);
104
105 return q^(?i)(\.mp3|\.wave?|\.aif[fc]?|\.au|\.snd|\.og[ga]|\.m4a)$^;
106}
107
108sub begin {
109 my $self = shift (@_);
110 my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
111
112 $self->SUPER::begin(@_);
113 $self->jAudioExtractor::begin(@_);
114 $self->jSongMinerExtractor::begin(@_);
115 $self->pEssentiaExtractor::begin(@_);
116}
117
118
119
120
121sub process_features {
122 my $self = shift (@_);
123 my ($base_dir, $file, $doc_obj) = @_;
124
125 my $outhandle = $self->{'outhandle'};
126 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
127
128 my $top_section = $doc_obj->get_top_section();
129
130 if ($self->{'compute_mir_features'} eq "true")
131 {
132 my $utf8_filename_no_path = $self->filepath_to_utf8($filename_no_path);
133 my $url_encoded_filename = &util::rename_file($utf8_filename_no_path, $self->{'file_rename_method'});
134
135 my $features_filename = $self->compute_features($filename_full_path);
136 my ($features_ext) = ($features_filename =~ m/\.([^.]+)$/);
137
138
139 my $mime_type = ($features_ext eq "arff") ? "text/plain" : "text/xml";
140 $doc_obj->associate_file($features_filename, "jaudio.$features_ext",
141 $mime_type, $top_section);
142
143 }
144
145 if ($self->{'compute_essentia_features'} eq "true")
146 {
147 my $utf8_filename_no_path = $self->filepath_to_utf8($filename_no_path);
148 my $url_encoded_filename = &util::rename_file($utf8_filename_no_path, $self->{'file_rename_method'});
149
150 my $features_filename = $self->compute_essentia_features($filename_full_path);
151 my ($features_ext) = ($features_filename =~ m/\.([^.]+)$/);
152
153
154 my $mime_type = ($features_ext eq "json") ? "application/json" : "text/plain";
155 $doc_obj->associate_file($features_filename, "pessentia.$features_ext",
156 $mime_type, $top_section);
157
158 }
159
160}
161
162# do plugin specific processing of doc_obj
163sub process {
164 my $self = shift (@_);
165 my ($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
166
167 my $top_section = $doc_obj->get_top_section();
168
169 $self->process_features($base_dir,$file,$doc_obj);
170
171 #we have no text - adds dummy text and NoText metadata
172 $self->add_dummy_text($doc_obj, $doc_obj->get_top_section());
173
174 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
175 $doc_obj->associate_file($filename_full_path, "audio.mp3", "audio/mpeg", $top_section);
176
177 return 1;
178
179}
180
181sub post_process_doc_obj {
182 my $self = shift (@_);
183 my ($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
184
185 my $outhandle = $self->{'outhandle'};
186 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
187
188 if ($self->{'retrieve_mir_metadata'} eq "true") {
189 my $top_section = $doc_obj->get_top_section();
190
191 my $id3_titles = $doc_obj->get_metadata($top_section,"ex.ID3.Title");
192 # my $id3_title = shift @$id3_titles || "Unknown";
193 my $id3_title = shift @$id3_titles || undef;
194
195 my $id3_artists = $doc_obj->get_metadata($top_section,"ex.ID3.Artist");
196 # my $id3_artist = shift @$id3_artists || "Unknown";
197 my $id3_artist = shift @$id3_artists || undef;
198
199 my ($metadata_acexml_filename,$metadata_txt_filename)
200 = $self->retrieve_metadata($filename_full_path,$id3_title,$id3_artist);
201
202 if (-e $metadata_txt_filename) {
203 $self->parse_txt_metadata($doc_obj,$metadata_txt_filename);
204 }
205
206 ### $self->check_for_existing_id3_genre($doc_obj);
207
208 $doc_obj->associate_file($metadata_acexml_filename, "jsongminer.xml",
209 "text/xml", $top_section);
210 }
211
212
213 return 1;
214}
215
216
2171;
218
219
220
Note: See TracBrowser for help on using the repository browser.