| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 39 |
($directory, $original_sampling_rate, $targetrate, $debug) = @ARGV; |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
opendir(DIR, "$directory") || die ("Unable to open directory: $directory"); |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
while ($filename = readdir(DIR)) { |
|---|
| 46 |
chomp ($filename); |
|---|
| 47 |
@filename = split(/\./,$filename); |
|---|
| 48 |
$filename_nosuffix = shift(@filename); |
|---|
| 49 |
$suffix = "@filename"; |
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 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") { |
|---|
| 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 |
|
|---|
| 64 |
|
|---|
| 65 |
@command = ("mv", "$directory\/temp-$filename", "$directory\/$filename"); |
|---|
| 66 |
system(@command) == 0 or die "system @command failed: $?"; |
|---|
| 67 |
} |
|---|
| 68 |
} |
|---|
| 69 |
close(DIR); |
|---|