source: gs3-extensions/audioDB/trunk/src/perllib/plugins/FFTExtractor.pm@ 24399

Last change on this file since 24399 was 24399, checked in by davidb, 13 years ago

Latest round of changes to code, in preparing for demo to Goldsmiths

File size: 5.0 KB
Line 
1###########################################################################
2#
3# FFTExtractor - helper plugin that computers audio features for
4# music information retrieval use,using audioDB
5#
6# A component of the Greenstone digital library software
7# from the New Zealand Digital Library Project at the
8# University of Waikato, New Zealand.
9#
10# Copyright (C) 2010 New Zealand Digital Library Project
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25#
26###########################################################################
27package FFTExtractor;
28
29use BaseMediaConverter;
30
31use Cwd;
32
33use strict;
34no strict 'refs'; # allow filehandles to be variables and viceversa
35
36
37BEGIN {
38 @FFTExtractor::ISA = ('BaseMediaConverter');
39}
40
41
42my $arguments = [
43 ];
44
45
46
47my $options = { 'name' => "FFTExtractor",
48 'desc' => "{FFTExtractor.desc}",
49 'abstract' => "yes",
50 'inherits' => "yes",
51 'args' => $arguments };
52
53sub new {
54 my ($class) = shift (@_);
55 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
56 push(@$pluginlist, $class);
57
58 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
59 push(@{$hashArgOptLists->{"OptList"}},$options);
60
61 my $self = new BaseMediaConverter($pluginlist, $inputargs, $hashArgOptLists, 1);
62
63 return bless $self, $class;
64}
65
66
67sub convert_audio_format
68{
69 my $self = shift(@_);
70 my $source_file_path = shift(@_);
71 my $target_ext = shift(@_);
72 my $convert_options = shift(@_) || "";
73
74 my $outhandle = $self->{'outhandle'};
75 my $verbosity = $self->{'verbosity'};
76
77 my $source_file_no_path = &File::Basename::basename($source_file_path);
78
79 $self->init_cache_for_file($source_file_path);
80
81 # Determine the full name and path of the output file
82 my $target_file_path;
83
84 if ($self->{'enable_cache'}) {
85 my $cached_dir = $self->{'cached_dir'};
86 my $file_root = $self->{'cached_file_root'};
87
88 my $target_file = "$file_root.$target_ext";
89
90 $target_file_path = &util::filename_cat($cached_dir,$target_file);
91 }
92 else {
93 $target_file_path = &util::get_tmp_filename($target_ext);
94 }
95
96 # Setup and run the ffmpeg command
97 my $ffmpeg_cmd = "ffmpeg -y $convert_options";
98 $ffmpeg_cmd .= " -i \"$source_file_path\"";
99 $ffmpeg_cmd .= " \"$target_file_path\"";
100
101## print STDERR "****** cmd = $ffmpeg_cmd\n";
102
103 my $print_info = { 'message_prefix' => "ffmpeg",
104 'message' => "Converting $source_file_no_path to $target_ext format" };
105
106 my ($regenerated,$result,$had_error)
107 = $self->autorun_general_cmd($ffmpeg_cmd,$source_file_path,
108 $target_file_path,$print_info);
109
110 return $target_file_path;
111}
112
113
114
115sub compute_fft_features
116{
117 my $self = shift(@_);
118 my $source_file_path = shift(@_);
119 my $target_feature = shift(@_);
120 my $convert_options = shift(@_) || "";
121
122 my $outhandle = $self->{'outhandle'};
123 my $verbosity = $self->{'verbosity'};
124
125## print STDERR "**** wav filename = $source_file_path\n";
126
127 my $source_file_no_path = &File::Basename::basename($source_file_path);
128
129 $self->init_cache_for_file($source_file_path);
130
131 my $target_file_path;
132 my $target_ext;
133
134 if ($target_feature eq "chroma") {
135 $target_ext = "chr12";
136 $convert_options .= " -c 12";
137 }
138 elsif ($target_feature eq "power-log") {
139 $target_ext = "power";
140 $convert_options .= " -P";
141 }
142 else {
143 print STDERR "Warning: computer_fft_features(), unrecognized option '$target_feature'\n";
144 $target_ext = "txt";
145 }
146
147 if ($self->{'enable_cache'}) {
148 my $cached_dir = $self->{'cached_dir'};
149 my $file_root = $self->{'cached_file_root'};
150
151 my $target_file = "$file_root.$target_ext";
152
153 $target_file_path = &util::filename_cat($cached_dir,$target_file);
154 }
155 else {
156 $target_file_path = &util::get_tmp_filename($target_ext);
157 }
158
159 # Run fftExtract
160 my $fftExtract_cmd = "fftExtract $convert_options";
161 $fftExtract_cmd .= " \"$source_file_path\" \"$target_file_path\"";
162
163## print STDERR "**** cmd = $fftExtract_cmd\n";
164
165 my $print_info = { 'message_prefix' => "fftExtract",
166 'message' => "Extracting $target_feature from $source_file_no_path" };
167
168 my ($regenerated,$result,$had_error)
169 = $self->autorun_general_cmd($fftExtract_cmd,$source_file_path,$target_file_path,$print_info);
170
171 return ($target_file_path);
172}
173
174
175
176
1771;
Note: See TracBrowser for help on using the repository browser.