Wav(3pm)
π Wav β Audio::Wav π
okay bestie, so this is the Wav(3pm) page from the User Contributed Perl Documentation, and it's literally the whole vibe for reading and writing Microsoft WAV files in Perl. buckle up, it's giving very useful. π
β¨ NAME β¨
Audio::Wav β Modules for reading & writing Microsoft WAV files.
πΈ SYNOPSIS πΈ
ok so first up, copying a file and adding some cue points to the output file:
# copying a file and adding some cue points to the output file
use Audio::Wav;
my $wav = new Audio::Wav;
my $read = $wav -> read( 'input.wav' );
my $write = $wav -> write( 'output.wav', $read -> details() );
print "input is ", $read -> length_seconds(), " seconds long\n";
$write -> set_info( 'software' => 'Audio::Wav' );
my $data;
#read 512 bytes
while ( defined( $data = $read -> read_raw( 512 ) ) ) {
$write -> write_raw( $data );
}
my $length = $read -> length_samples();
my( $third, $half, $twothirds ) = map int( $length / $_ ), ( 3, 2, 1.5 );
my %samp_loop = (
'start' => $third,
'end' => $twothirds,
);
$write -> add_sampler_loop( %samp_loop );
$write -> add_cue( $half, "cue label 1", "cue note 1" );
$write -> finish();
and then splitting a multi-channel file to separate mono files β slowly! β she's not fast but she gets there:
# splitting a multi-channel file to separate mono files (slowly!);
use Audio::Wav;
my $read = $wav -> read( '4ch.wav' );
my $details = $read -> details();
my %out_details = map { $_ => $details -> {$_} } 'bits_sample', 'sample_rate';
$out_details{channels} = 1;
my @out_files;
my $in_channels = $details -> {channels};
foreach my $channel ( 1 .. $in_channels ) {
push @out_files, $wav -> write( 'multi_' . $channel . '.wav', \%out_details );
}
while ( 1 ) {
my @channels = $read -> read();
last unless @channels;
foreach my $channel_id ( 0 .. $#channels ) {
$out_files[$channel_id] -> write( $channels[$channel_id] );
}
}
# not entirely necessary as finish is done in DESTROY now (if the file hasn't been finished already).
foreach my $write ( @out_files ) {
$write -> finish();
}
π¦ NOTES π¦
so, housekeeping bestie, pay attention:
- all sample positions are now in sample offsets now β unless the option
'.01compatible'is true. periodt. - there's now *very* basic support for
WAVEFORMATEXTENSIBLEβ and honestly "basic" is doing a lot of heavy lifting here, it only recognises that the file is in this format. the key'wave-ex'is used in the detail hash to denote this format when reading or writing. the author would love to do more with this, but doesn't have any hardware or software to test these files with, and also doesn't really have any spare time to do the implementation at present. π relatable. - one day the author plans to learn enough C to do the sample reading/writing in XS, but for the time being it's done using
pack/unpackin Perl and is slow. working with the raw format doesn't suffer in this way tho π - tbh it's likely that reading/writing files with bit-depth greater than 16 won't work properly. needs looking at at some point. not me ignoring my own bugs π
π DESCRIPTION π
These modules provide a method of reading & writing uncompressed Microsoft WAV files. that's the whole thing. it's iconic, it's simple, it works. β¨
π SEE ALSO π
L<Audio::Wav::Read>L<Audio::Wav::Write>
π METHODS π
new
Returns a blessed Audio::Wav object. all the parameters are optional and default to 0 π«Ά
my %options = (
'.01compatible' => 0,
'oldcooledithack' => 0,
'debug' => 0,
);
my $wav = Audio::Wav -> new( %options );
write
Returns a blessed Audio::Wav::Write object.
my $details = {
'bits_sample' => 16,
'sample_rate' => 44100,
'channels' => 2,
};
my $write = $wav -> write( 'testout.wav', $details );
my $write = Audio::Wav -> write( 'testout.wav', $details);
my $write = Audio::Wav -> write( 'testout.wav', $details, %options );
where %options is in the form of arguments for Audio::Wav::Tools.
see Audio::Wav::Write for methods. π
read
Returns a blessed Audio::Wav::Read object.
my $read = $wav -> read( 'testin.wav' );
my $read = Audio::Wav -> read( 'testin.wav' );
my $read = Audio::Wav -> read( 'testin.wav', %options );
where %options is in the form of arguments for Audio::Wav::Tools.
see Audio::Wav::Read for methods. π
set_error_handler
Specifies a subroutine for catching errors. the subroutine should take a hash as input. the keys in the hash are 'filename', 'message' (the error message), and 'warning'. if no error handler is set, die and warn will be used.
sub myErrorHandler {
my( %parameters ) = @_;
if ( $parameters{warning} ) {
# This is a non-critical warning
warn "Warning: $parameters{filename}: $parameters{message}\n";
} else {
# Critical error!
die "ERROR: $parameters{filename}: $parameters{message}\n";
}
}
$wav -> set_error_handler( \&myErrorHandler );
π COPYRIGHT π
Copyright (c) 2007-2012 Brian Szymanski <brianski@cpan.org>
Copyright (c) 1998-2006 Nick Peskett <npeskett@cpan.org>
Copyright (c) 2001 Kurt George Gjerde <KJERDE@cpan.org>
β AUTHORS β
Nick Peskett (see http://www.peskett.co.uk/ for contact details).
Brian Szymanski <ski-cpan@allafrica.com> (0.07-0.14)
Wolfram humann (pureperl 24 and 32 bit read support in 0.09)
Kurt George Gjerde <kurt.gjerde@media.uib.no>. (0.02-0.03)
see also Changes file π¦
and that's the whole page bestie!! this one shipped as part of perl v5.36.0 on 2022-10-13 β mwah, go read some WAVs ππβ¨
π¦ where this page comes from
- ubuntu noble: libaudio-wav-perl 0.14-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libaudio-wav-perl 0.14-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libaudio-wav-perl 0.14-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms