Manipulation binary files

Discussion in 'Perl Misc' started by Chris, Oct 19, 2003.

  1. Chris

    Chris Guest

    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
     
    Chris, Oct 19, 2003
    #1
    1. Advertising

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

    "Chris" <> wrote in
    news:cSvkb.498$:

    > 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-----
     
    Eric J. Roode, Oct 19, 2003
    #2
    1. Advertising

  3. "Eric J. Roode" wrote:
    >
    > "Chris" <> wrote in
    > news:cSvkb.498$:
    > >
    > > 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";


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


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Oct 19, 2003
    #3
  4. Chris

    Anno Siegel Guest

    John W. Krahn <> wrote in comp.lang.perl.misc:
    > "Eric J. Roode" wrote:
    > >
    > > "Chris" <> wrote in
    > > news:cSvkb.498$:
    > > >
    > > > 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";

    >
    > 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
     
    Anno Siegel, Oct 23, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew A. Berglund

    Binary number manipulation

    Matthew A. Berglund, Nov 28, 2003, in forum: Python
    Replies:
    1
    Views:
    380
    Irmen de Jong
    Nov 28, 2003
  2. Replies:
    5
    Views:
    343
    Tim Roberts
    Dec 7, 2003
  3. nguser3552
    Replies:
    3
    Views:
    457
  4. Jim
    Replies:
    6
    Views:
    768
  5. Monty

    Binary file manipulation

    Monty, Apr 11, 2006, in forum: Perl Misc
    Replies:
    15
    Views:
    208
    Monty
    Apr 13, 2006
Loading...

Share This Page