Posted on 2007-06-11 20:26:39-07 by marroug
Audio-Wav writting just the header of wav1 to wav2 and keep audio data of wav2
I'm recording broadcast wav files for archive and production purposes. I'm using sox to sample rate convert the files, but it fails to retain the header information. I'd like to copy just the header info (slightly modified) from an original file and write it to the SRC file without loosing the audio in the SRC file. Is it possible to just write the header on top of an existing audio file?

Just using:
my $write = $SRCwav -> write( '/testSRC.wav', $origDetails );
which deletes the existing file's audio data. Using a read_raw and sending it to a new file (as seen below) works, but I really don't want or need to create another copy of the file (disk space and time intensive).

Here is my (very rudimentary perl):
#!/usr/bin/perl -w $mySRCFileName = "/testSRC.wav"; my $SRCwav = new Audio::Wav; my $SRCRead = $SRCwav -> read( $mySRCFileName ); my $SRCDetails = $SRCRead -> details( ); $SRCSamplerate = $SRCDetails -> {'sample_rate'}; $SRCBitDept = $SRCDetails -> {'bits_sample'}; print "\nSRC SampleRate-BitDepth" . " is " . $SRCSamplerate . " " . $SRCBitDept . "\n"; $myOrigFile = "/testOrig.wav"; use Audio::Wav; my $origWav = new Audio::Wav; my $origRead = $origWav -> read( $myOrigFile ); my $origDetails = $origRead -> details( ); $origSamplerate = $origDetails -> {'sample_rate'}; $origBitDept = $origDetails -> {'bits_sample'}; print "Orig SampleRate-BitDepth" . " is " . $origSamplerate . " " . $origBitDept . "\n"; $coefRateChange = ($SRCSamplerate / $origSamplerate); print "Coef Rate Change is " . $coefRateChange . "\n"; $origDetails -> {'sample_rate'} = $SRCSamplerate; $origDetails -> {'bits_sample'} = $SRCBitDept; $newOrigSamplerate = $origDetails -> {'sample_rate'}; $newOrigBitDept = $origDetails -> {'bits_sample'}; print "NEW Orig SampleRate-BitDepth" . " is " . $newOrigSamplerate . " " . $newOrigBitDept . "\n"; while ( my ($key, $value) = each(%$origDetails) ) { if ($key =~ "cue") { $cues = $value; while ( my ($key, $value) = each(%$cues) ) { while ( my ($key, $value) = each(%$value) ) { if ($key =~ "position") { print "Old " . $key . " is " . $value . "\n"; $SRCposition = int($value * $coefRateChange); $value = $SRCposition; print "new " . $key . " is " . $value . "\n"; } if ($key =~ "offset") { print "Old " . $key . " is " . $value . "\n"; $SRCoffset = int($value * $coefRateChange); $value = $SRCoffset; print "new " . $key . " is " . $value . "\n"; } } } } } my $write = $SRCwav -> write( '/testSRC.wav', $origDetails ); my $data; while ( defined( $data = $SRCRead -> read_raw( 512 ) ) ) { $write -> write_raw( $data ); } $writeNewDetails -> finish();


Thanks in advance!
Travis Gregg
Coordinator of Audio Production
Jacobs School of Music
Indiana University
http://www.music.indiana.edu/department/audio/
Direct Responses: Write a response
Perl Weekly newsletter
A free weekly newsletter for people who are busy to read all the blogs. click here to check it out.