Manipulation binary files

C

Chris

Dear All,

I'm trying to use perl in a way I am not usd to, and coming unstuck :-(

I have a binary file of unsigned byte values, and I wish to convert these
into a .wav file, so that I can use the soundcard DAC to generate the
waveform the binary file describes (cool huh? Cheap signal generator :)

The problem is that the wav file format needs to have all all sorts of stuff
munged into the header, including strings "RIFF" "WAVE" and "fmt " and
values that describe other paramters. SOme of these data types are 32 bit,
some are 16 some are 8.

In perl how do i ...

a) write to a file in binary mode. I have tried using binmode(FH) before the
writing beging, but it seems to make little difference

b) write strings in binary mode. I dont really have to do write 0x52 0x49
0x46 0x46 if there is a way to write "RIFF" and have perl do the donkey work

c) Declare the widths of my variables. I need to be able to assign a
variable with a value, and output all 4 bytes of that value to the file in
binary mode.

I'm guessing that all this is very easy, and I'm just looking in the wrong
place :-(

Cheers

Chris
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I have a binary file of unsigned byte values, and I wish to convert
these into a .wav file, so that I can use the soundcard DAC to
generate the waveform the binary file describes (cool huh? Cheap
signal generator :)

The problem is that the wav file format needs to have all all sorts of
stuff munged into the header, including strings "RIFF" "WAVE" and "fmt
" and values that describe other paramters. SOme of these data types
are 32 bit, some are 16 some are 8.

In perl how do i ...

a) write to a file in binary mode. I have tried using binmode(FH)
before the writing beging, but it seems to make little difference

To write to a binary file, you use "print", just like writing to a text
file. You should use binmode on the file handle beforehand, yes.

b) write strings in binary mode. I dont really have to do write 0x52
0x49 0x46 0x46 if there is a way to write "RIFF" and have perl do the
donkey work

Yes: print FH "RIFF";

c) Declare the widths of my variables. I need to be able to assign a
variable with a value, and output all 4 bytes of that value to the
file in binary mode.

You should look into the 'pack' operator. It's a pain to use (imho), but
this is exactly what it does: convert numeric variables into binary
strings, suitable for printing to your file.

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP5KSV2PeouIeTNHoEQJRCQCfUaNmZpfUwSNWUXoNZMPYzkaLoUYAoKvD
RaVBrEksKxoWpV73CyDPS3H7
=IPZU
-----END PGP SIGNATURE-----
 
J

John W. Krahn

Eric J. Roode said:
To write to a binary file, you use "print", just like writing to a text
file. You should use binmode on the file handle beforehand, yes.


Yes: print FH "RIFF";

And you may want to turn on autoflush for your output file handle. Have
a look in perlvar.pod for "OUTPUT_AUTOFLUSH".


John
 
A

Anno Siegel

John W. Krahn said:
And you may want to turn on autoflush for your output file handle. Have
a look in perlvar.pod for "OUTPUT_AUTOFLUSH".

How would that make a difference? If the file is a normal disk file,
I don't see the use of autoflush.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top