use strict; use warnings; use Audio::Wav; # ** 2 / sqrt formula taken from http://www.fourelectronics.com/Can-you-calculate-a-44-1k-wav-streams-volume-by-looking-at-the-si-2830944.html my ($chapter, $read, $array_slice_length); my $slices_per_second = 100; #how many slices to take per second; check beforehand to make sure it works out to a whole number my $in_folder = 'mp3s'; my $out_folder = 'wav'; my $wav = new Audio::Wav; $chapter = '40001'; convert_to_wav(); get_wav_info(); get_slices(); sub get_slices { print "Getting slices...\n"; my @values; my $length = $read->length(); while ($read->position() < $length) { my ($value) = $read->read(); #it's mono, so don't need both channels push @values, $value; if (scalar @values == $array_slice_length) { my $amplitude = get_slice_value(@values); } } } sub get_slice_value { my $out = 0; foreach my $value (@_) { $out += $value ** 2; } return sqrt $out; } sub get_wav_info { print "Getting wav info...\n"; $read = $wav->read("$out_folder/$chapter.wav"); my $ref = $read->details(); die "Uneven slice length ($ref->{sample_rate} % $slices_per_second)" if ($ref->{sample_rate} % $slices_per_second); $array_slice_length = $ref->{sample_rate} / $slices_per_second; } sub convert_to_wav { print "Converting to wav...\n"; my $in = $in_folder; `lame --decode "$in_folder\\$chapter.mp3" "$out_folder\\$chapter.wav"`; }