voxforge.org
VoxForge Dev

root/Trunk/Scripts/Testing_scripts/mp3_testing/Process_mp3.pm

Revision 2270, 5.2 kB (checked in by kmaclean, 10 months ago)

Flac audio processing updates & GPL v3 updates

  • Property svn:executable set to *
Line 
1 #!/usr/bin/perl -w
2 ####################################################################
3 ###
4 ### script name: Process-mp3.pm
5 ### modified by: Ken MacLean
6 ### email: contact@voxforge.org
7 ### Date: 2007.03.07
8 ### Version: 0.1
9 ###             
10 ### Copyright (C) 2007 Ken MacLean
11 ###
12 ### This program is free software; you can redistribute it and/or
13 ### modify it under the terms of the GNU General Public License
14 ### as published by the Free Software Foundation; either version 3
15 ### of the License, or (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 ### Change History:
23 ###
24 ####################################################################
25 package Process_mp3;
26 use strict;
27 use File::Spec;
28 ####################################################################
29 #  parms
30 my (%parms, $command);
31 $parms{"debug"} = 1;
32 $parms{"AudioDirectory"} = "/home/kmaclean/workspace/VoxForge/Audio/Temp/16kHz_16bit";
33 #       $parms{"wav"}  =        $parms{"AudioDirectory"} . "/wav-temp"; # test
34         $parms{"wav"}  =        $parms{"AudioDirectory"} . "/wav";     
35         $parms{"mp3"}  =        $parms{"AudioDirectory"} . "/mp3";     
36         $parms{"wav2"}  =       $parms{"AudioDirectory"} . "/wav2";     
37         $parms{"mfc"} =         $parms{"AudioDirectory"} . "/MFCC_0_D";
38 $parms{"HTKBin"}  = "/usr/local/HTK" ;
39 ####################################################################
40 Main(\%parms) || die "Repository error: $?";
41 print STDOUT "!!!!!! completed\n";
42 ####################################################################
43 # Subroutines
44 ####################################################################
45 sub Main {
46         my ($parameters ) = @_;
47         my %parms = %$parameters;
48         my $debug = $parms{"debug"};   
49         my $AudioDirectory = $parms{"AudioDirectory"}; 
50         my $wav = $parms{"wav"};
51         my $wav2 = $parms{"wav2"};
52         my $mp3 = $parms{"mp3"};                       
53         my $mfc = $parms{"mfc"};                       
54         my($dirlist) = ReadDir($wav); # $dirlist is a pointer to array created in ReadDir
55         foreach my $AudioDirName (@$dirlist) { 
56                 $command = ("mkdir $mp3/$AudioDirName");  print "$command\n" if $debug;  system($command)  ;   
57                 $command = ("mkdir $wav2/$AudioDirName");  print "$command\n" if $debug;  system($command)  ;   
58                 ConvertWav(\%parms, $AudioDirName)|| die "ConvertWav error: $?";
59                 print "MFCC completed for $mp3/$AudioDirName\n";               
60
61                 $command = ("mkdir $mfc/$AudioDirName");  print "$command\n" if $debug;  system($command)  ;                   
62                 ConvertWav2mfc(\%parms, $AudioDirName)|| die "ConvertWav2mfc error: $?";
63                 print "MFCC completed for $mfc/$AudioDirName\n";
64         }
65         print "\nRepository-mp3.pl completed!****************************************************\n\n";
66 }
67
68 sub ReadDir {
69         my($Dir) = @_;
70         my @dirlist;
71         opendir(DIR, $Dir) || die ("Unable to open file: $Dir");
72         while (my $line = readdir(DIR)) {
73                 chomp ($line);
74                 if ($line ne "." and $line ne ".." ) {
75                                 push @dirlist,$line;
76                 }
77         }       
78         close(DIR);
79         return \@dirlist; #send pointer back to caller
80 }
81
82 sub ConvertWav {
83         # convert wav file to mp3 and then back to wav
84         my ($parameters, $AudioDirName ) = @_; 
85         my %parms = %$parameters;
86                 my $debug = $parms{"debug"};   
87                 my $wav = $parms{"wav"};
88                 my $mp3 = $parms{"mp3"};
89                 my $wav2 = $parms{"wav2"};
90         my $wav_path = "$wav/$AudioDirName/wav";
91         my $mp3_path = "$mp3/$AudioDirName/mp3";
92         my $wav2_path = "$wav2/$AudioDirName/wav";             
93         $command = ("mkdir $mp3_path");  print "$command\n" if $debug;  system($command)  ;
94         $command = ("mkdir $wav2_path");  print "$command\n" if $debug;  system($command)  ;   
95         opendir(DIR, "$wav_path") || die ("Unable to open directory: $wav_path");
96         while (my $filename = readdir(DIR)) {
97                 chomp ($filename);
98                 my @filename = split(/\./,$filename);
99                 my $filename_nosuffix = shift(@filename);
100                 my $suffix = "@filename";
101                 if ($suffix eq "wav") {
102 #                       print "converting:$wav_path\/$filename\n" if $debug;
103                         # wav to mp3
104                         $command = ("lame -h -b 128 $wav_path/$filename $mp3_path/$filename_nosuffix\.mp3" ); system($command) == 0 or die "system $command failed: $?";     
105                         # mp3 to wav
106                         $command = ("sox $mp3_path/$filename_nosuffix\.mp3 $wav2_path/$filename_nosuffix\.wav" ); system($command) == 0 or die "system $command failed: $?";     
107                 }       
108         }
109         close(DIR);
110         return 1;
111 }
112
113 sub ConvertWav2mfc {
114         my ($parameters,$AudioDirName) = @_;   
115         my %parms = %$parameters;
116                 my $debug = $parms{"debug"};   
117                 my $wav2 = $parms{"wav2"};     
118                 my $mfc = $parms{"mfc"};                               
119                 my $HTKbin = $parms{"HTKBin"};
120         my $wav2_path = "$wav2/$AudioDirName/wav";
121         my $mfc_path = "$mfc/$AudioDirName/mfc";
122         $command = ("mkdir $mfc_path");  print "$command\n" if $debug;  system($command)  ;     
123         opendir(DIR, "$wav2_path") || die ("Unable to open directory: $wav2");
124         while (my $filename = readdir(DIR)) {
125                 chomp ($filename);
126                 my @filename = split(/\./,$filename);
127                 my $filename_nosuffix = shift(@filename);
128                 my $suffix = "@filename";
129                 if ($suffix eq "wav") {         
130 #                       print "converting:$wav2_path\/$filename\n" if $debug;                   
131                         $command = ("$HTKbin/HCopy -A -D -T 1 -C wav_config $wav2_path/$filename $mfc_path/$filename_nosuffix\.mfc" ); system($command) == 0 or die "system $command failed: $?";     
132                 }
133         }
134         close(DIR);
135         return 1;
136 }
137 1;
Note: See TracBrowser for help on using the browser.