voxforge.org
VoxForge Dev

root/Trunk/Scripts/Testing_scripts/mp3_testing/downsample.pl

Revision 2270, 3.0 kB (checked in by kmaclean, 1 year ago)

Flac audio processing updates & GPL v3 updates

  • Property svn:executable set to *
Line 
1 #!/usr/bin/perl -w
2 ####################################################################
3 ###
4 ###     script name : downsample.pl
5 ###     version: 0.3
6 ###             modified by: Ken MacLean
7 ###             mail: kmaclean@voxforge.org
8 ###             Date: 2006.9.26
9 ###     Command: ./downsample.pl directory filetype(raw/wav) original_sampling_rate targetrate
10 ###
11 ### Copyright (C) 2006 Ken MacLean
12 ###
13 ### This program is free software; you can redistribute it and/or
14 ### modify it under the terms of the GNU General Public License
15 ### as published by the Free Software Foundation; either version 3
16 ### of the License, or (at your option) any later version.
17 ###
18 ### This program is distributed in the hope that it will be useful,
19 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ### GNU General Public License for more details.
22 ###                                                                 
23 ####################################################################
24 use strict;
25 use File::Spec;
26
27 my ($targetrate, @command);
28 my ($original_file_type, @filename, $original_sampling_rate);
29 my ($wav_file, $raw_file);
30 my ($directory, $filename, $debug, $filename_nosuffix, $suffix);
31  
32 # check usage
33 if (@ARGV != 4) {
34   print "error -usage: $0 downsample_directory filetype(raw/wav) original_sampling_rate targetrate \n\n";
35   exit (1);
36 }
37
38 # read in command line arguments
39 ($directory, $original_sampling_rate, $targetrate, $debug) = @ARGV;
40
41 # open directory
42 opendir(DIR, "$directory") || die ("Unable to open directory: $directory");
43
44 # process each file name in directory one at a time
45 while ($filename = readdir(DIR)) {
46   chomp ($filename);
47   @filename = split(/\./,$filename);
48   $filename_nosuffix = shift(@filename);
49   $suffix = "@filename";
50   # sox: rate: -r 8000, 16000, 44100, 48000 ...
51   #      data size: -b = 8-bit; -w = 16-bit; -l = 32-bit
52   if ($suffix eq "wav") {
53         print "downsampling:$directory\/$filename\n" if $debug;
54     @command = ("sox", "$directory\/$filename", "-r", "$targetrate", "-w", "$directory\/temp-$filename");
55     system(@command) == 0 or die "system @command failed: $?";
56     @command = ("mv", "$directory\/temp-$filename", "$directory\/$filename");
57     system(@command) == 0 or die "system @command failed: $?";     
58     }
59     elsif ($suffix eq "raw") { # convert raw files to wav (i.e. add a wav header)
60       print "downsampling:$directory\/$filename\n" if $debug;
61       @command = ("sox", "-r", "$original_sampling_rate", "-s", "-w", "$directory\/$filename", "-t", "wav", "-r", "$targetrate", "-w", "$directory\/temp-$filename");
62       system(@command) == 0 or die "system @command failed: $?";
63       # need to leave 'raw' filename unchanged (i.e. don't rename it to wav)
64       # only change filenames using svn, otherwise lose you svn history on the file.   
65       @command = ("mv", "$directory\/temp-$filename", "$directory\/$filename");
66       system(@command) == 0 or die "system @command failed: $?";   
67     } # skip any other files
68 }
69 close(DIR);
Note: See TracBrowser for help on using the browser.