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

Last change on this file since 26288 was 26288, checked in by davidb, 12 years ago

Plugin changed so it now also generates '.ogg' audio files (useful for Firefox playing audio with HTML5 <audio> element

File size: 5.5 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 my $native_source_file_path = $source_file_path;
97
98 my $native_target_file_path = $target_file_path;
99 if ($^O eq "cygwin") {
100 # Only get ffmpeg as native Windows binary => need to use Windows style path
101 $native_source_file_path=`cygpath -w \"$source_file_path\"`;
102 $native_target_file_path=`cygpath -w \"$target_file_path\"`;
103 $native_source_file_path =~ s/\s+$//;
104 $native_target_file_path =~ s/\s+$//;
105 }
106
107 # Setup and run the ffmpeg command
108 my $ffmpeg_cmd = "ffmpeg -y";
109 $ffmpeg_cmd .= " -i \"$native_source_file_path\" $convert_options";
110 $ffmpeg_cmd .= " \"$native_target_file_path\"";
111
112## print STDERR "****** cmd = $ffmpeg_cmd\n";
113
114 my $print_info = { 'message_prefix' => "ffmpeg",
115 'message' => "Converting $source_file_no_path to $target_ext format" };
116
117 my ($regenerated,$result,$had_error)
118 = $self->autorun_general_cmd($ffmpeg_cmd,$source_file_path,
119 $target_file_path,$print_info);
120
121 return $target_file_path;
122}
123
124
125
126sub compute_fft_features
127{
128 my $self = shift(@_);
129 my $source_file_path = shift(@_);
130 my $target_feature = shift(@_);
131 my $convert_options = shift(@_) || "";
132
133 my $outhandle = $self->{'outhandle'};
134 my $verbosity = $self->{'verbosity'};
135
136## print STDERR "**** wav filename = $source_file_path\n";
137
138 my $source_file_no_path = &File::Basename::basename($source_file_path);
139
140 $self->init_cache_for_file($source_file_path);
141
142 my $target_file_path;
143 my $target_ext;
144
145 if ($target_feature eq "chroma") {
146 $target_ext = "chr12";
147 $convert_options .= " -c 12";
148 }
149 elsif ($target_feature eq "power-log") {
150 $target_ext = "power";
151 $convert_options .= " -P";
152 }
153 else {
154 print STDERR "Warning: computer_fft_features(), unrecognized option '$target_feature'\n";
155 $target_ext = "txt";
156 }
157
158 if ($self->{'enable_cache'}) {
159 my $cached_dir = $self->{'cached_dir'};
160 my $file_root = $self->{'cached_file_root'};
161
162 my $target_file = "$file_root.$target_ext";
163
164 $target_file_path = &util::filename_cat($cached_dir,$target_file);
165 }
166 else {
167 $target_file_path = &util::get_tmp_filename($target_ext);
168 }
169
170 # Run fftExtract
171 my $fftExtract_cmd = "fftExtract $convert_options";
172 $fftExtract_cmd .= " \"$source_file_path\" \"$target_file_path\"";
173
174## $fftExtract_cmd =~ s/\\/\//g;
175
176## print STDERR "**** cmd = $fftExtract_cmd\n";
177
178 my $print_info = { 'message_prefix' => "fftExtract",
179 'message' => "Extracting $target_feature from $source_file_no_path" };
180
181 my ($regenerated,$result,$had_error)
182 = $self->autorun_general_cmd($fftExtract_cmd,$source_file_path,$target_file_path,$print_info);
183
184 return ($target_file_path);
185}
186
187
188
189
1901;
Note: See TracBrowser for help on using the repository browser.