Hello,
I'm reading a wav file and I'd like to output normalized samples. I do it in a for loop, but I was wondering if I can do it more efficient. I also have a filling that I'm not utilizing all the features of the language to make this code more perl like. Any suggestions are welcome. Thanks.
use Audio::Wav;
my $wav = new Audio::Wav;
my $Read = $wav->read('Amp.wav');
# This is slow
my @Data;
for(my $SampleIndex = 0; $SampleIndex < $Read->length_samples(); $SampleIndex++)
{
my $Sample = $Read->move_to_sample($SampleIndex);
push @Data, $Read->read();
}
# Sort data to get maximum and minimum
@Data = sort @Data;
# Get max
my $Max = max(abs($Data[-1]), abs($Data[0]));
# Output samples, using one sample every millisecond
for(my $SampleIndex = 0; $SampleIndex < $Read->length_samples(); $SampleIndex++)
{
my $Sample = $Read->move_to_sample($SampleIndex);
# Get current samples for the only channel
my @Channels = $Read->read();
my $Value = $Channels[0] / $Max;
# Got to be at least one channel
print $Value."\n";
}
sub max
{
if($_[0] > $_[1])
{
return $_[0];
}
else
{
return $_[1];
}
}