| 1 | #!/usr/bin/perl -w |
|---|
| 2 | #################################################################### |
|---|
| 3 | ### |
|---|
| 4 | ### script name : downsample.pl |
|---|
| 5 | ### version: 0.2.1 |
|---|
| 6 | ### modified by: Ken MacLean |
|---|
| 7 | ### mail: kmaclean@voxforge.org |
|---|
| 8 | ### Date: 2007.12.27 |
|---|
| 9 | ### Command: ./downsample.pl FilesToBeDownsampled 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 2 |
|---|
| 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 | |
|---|
| 25 | use strict; |
|---|
| 26 | use File::Spec; |
|---|
| 27 | |
|---|
| 28 | my ($targetrate, $codetrain, $command, $line, $file, $directories, $volume); |
|---|
| 29 | my ($original_file_type, @filename, $original_sampling_rate); |
|---|
| 30 | |
|---|
| 31 | # check usage |
|---|
| 32 | if (@ARGV != 4) { |
|---|
| 33 | print "Wrong number of arguments ... usage: $0 FilesToBeDownsampled filetype(raw/wav) original_sampling_rate(Hz) targetrate(Hz)\n\n"; |
|---|
| 34 | exit (0); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | # read in command line arguments |
|---|
| 38 | ($codetrain, $original_file_type, $original_sampling_rate, $targetrate) = @ARGV; |
|---|
| 39 | |
|---|
| 40 | # open files |
|---|
| 41 | open (FILEIN,"$codetrain") || die ("Unable to open codetrain $codetrain file for reading"); |
|---|
| 42 | |
|---|
| 43 | # process each prompt one at a time |
|---|
| 44 | while ($line = <FILEIN>) { |
|---|
| 45 | chomp ($line); |
|---|
| 46 | ($volume,$directories,$file) = File::Spec->splitpath( $line ); |
|---|
| 47 | print "downsampling:$file\n"; |
|---|
| 48 | |
|---|
| 49 | if ($original_file_type eq "wav") { |
|---|
| 50 | $command = ("sox $line -r $targetrate -b 16 \./wav/$file"); system($command) == 0 or die "system $command failed: $?"; |
|---|
| 51 | } |
|---|
| 52 | elsif ($original_file_type eq "raw") { |
|---|
| 53 | @filename=split(/./, $file); |
|---|
| 54 | $file=shift (@filename); |
|---|
| 55 | $command = ("sox -r $original_sampling_rate -s -b 16 $line -r $targetrate -b 16 \./wav/$file"); system($command) == 0 or die "system $command failed: $?"; |
|---|
| 56 | } |
|---|
| 57 | else { |
|---|
| 58 | die ("Error: wrong file type (only \'wav\' or \'raw\' permitted)"); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | } #end of while loop |
|---|
| 62 | |
|---|
| 63 | close(FILEIN); |
|---|
| 64 | |
|---|