root/gs3-extensions/mars-src/trunk/perllib/plugins/EssentiaPlugin.pm @ 34360

Revision 34360, 5.1 KB (checked in by davidb, 7 weeks ago)

Collating of python essensia custom scripts and essentia perl plugin code

Line 
1###########################################################################
2#
3# EssentiaPlugin.pm -- for augmenting audio files with Essentia computed
4#                      features
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 EssentiaPlugin;
28
29use BaseImporter;
30use pEssentiaExtractor;
31
32use strict;
33no strict 'refs'; # allow filehandles to be variables and viceversa
34no strict 'subs';
35
36use gsprintf 'gsprintf';
37
38sub BEGIN {
39    @EssentiaPlugin::ISA = ('BaseImporter', 'pEssentiaExtractor');
40}
41
42my $arguments =
43    [
44      { 'name' => "compute_mir_features",
45    'desc' => "{BaseImporter.compute_mir_features}",
46    'type' => "enum",
47    'list' => [{'name' => "true",  'desc' => "{common.true}"},
48           {'name' => "false", 'desc' => "{common.false}"}],
49    'deft' => "false",
50    'reqd' => "no" },
51      { 'name' => "compute_essentia_features",
52    'desc' => "{BaseImporter.compute_essentia_features}",
53    'type' => "enum",
54    'list' => [{'name' => "true",  'desc' => "{common.true}"},
55           {'name' => "false", 'desc' => "{common.false}"}],
56    'deft' => "false",
57    'reqd' => "no" },
58      { 'name' => "retrieve_mir_metadata",
59    'desc' => "{BaseImporter.retrieve_mir_metadata}",
60    'type' => "enum",
61    'list' => [{'name' => "true",  'desc' => "{common.true}"},
62           {'name' => "false", 'desc' => "{common.false}"}],
63    'deft' => "false",
64    'reqd' => "no" },
65      { 'name' => "process_exp",
66    'desc' => "{BaseImporter.process_exp}",
67    'type' => "regexp",
68    'deft' => &get_default_process_exp(),
69    'reqd' => "no" }
70      ];
71
72my $options = { 'name'     => "EssentiaPlugin",
73        'desc'     => "{EssentiaPlugin.desc}",
74        'abstract' => "no",
75        'inherits' => "yes",
76        'args'     => $arguments };
77
78
79
80sub new {
81    my ($class) = shift (@_);
82    my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
83    push(@$pluginlist, $class);
84
85    push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
86    push(@{$hashArgOptLists->{"OptList"}},$options);
87
88   
89    my $pee_self = new pEssentiaExtractor($pluginlist, $inputargs, $hashArgOptLists,1);
90
91    my $base_self = new BaseImporter($pluginlist, $inputargs, $hashArgOptLists);
92
93    my $self = BaseImporter::merge_inheritance($pee_self,$base_self);
94
95    return bless $self, $class;
96}
97
98sub get_default_process_exp {
99    my $self = shift (@_);
100   
101    return q^(?i)(\.mp3|\.wave?|\.aif[fc]?|\.au|\.snd|\.og[ga]|\.m4a)$^;
102}
103
104sub begin {
105    my $self = shift (@_);
106    my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
107
108    $self->SUPER::begin(@_);
109    $self->pEssentiaExtractor::begin(@_);
110}
111
112
113
114
115sub process_features {
116    my $self = shift (@_);
117    my ($base_dir, $file, $doc_obj) = @_;
118
119    my $outhandle = $self->{'outhandle'};
120    my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
121
122    my $top_section = $doc_obj->get_top_section();
123
124    if ($self->{'compute_essentia_features'} eq "true")
125    {
126    my $utf8_filename_no_path = $self->filepath_to_utf8($filename_no_path);
127    my $url_encoded_filename = &util::rename_file($utf8_filename_no_path, $self->{'file_rename_method'});
128   
129    my $features_filename = $self->compute_essentia_features($filename_full_path);
130    my ($features_ext) = ($features_filename =~ m/\.([^.]+)$/);
131
132   
133    my $mime_type = ($features_ext eq "json") ? "application/json" : "text/plain";
134    $doc_obj->associate_file($features_filename, "pessentia.$features_ext",
135                 $mime_type, $top_section);
136
137    }
138
139}
140
141# do plugin specific processing of doc_obj
142sub process {
143    my $self = shift (@_);
144    my ($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
145
146    my $top_section = $doc_obj->get_top_section();
147
148    $self->process_features($base_dir,$file,$doc_obj);
149   
150    #we have no text - adds dummy text and NoText metadata
151    $self->add_dummy_text($doc_obj, $doc_obj->get_top_section());
152
153    my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
154    $doc_obj->associate_file($filename_full_path, "audio.mp3", "audio/mpeg", $top_section);
155
156    return 1;
157
158}
159
160#sub post_process_doc_obj {
161#    my $self = shift (@_); 
162#    my ($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
163#
164#    my $outhandle = $self->{'outhandle'};
165#    my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
166#
167#    return 1;
168#}
169
170
1711;
172
173
174
Note: See TracBrowser for help on using the browser.