Any modules to write a wav file

Bengt Richter bokr at oz.net
Thu Nov 7 23:30:43 EST 2002


On Fri, 8 Nov 2002 02:07:45 +0000 (UTC), Sreekant Kodela <skodela at btopenworld.com> wrote:

>Hi folks
>
>Does anyone know of a module which I can use basically to create a wav file
>or other sound format file, using the various numbers generated by a 
>different snippet.
>
>Any soundformat will do. 
>
I've used the read capability, but I haven't tried writing using the wave module.
But it looks straight forward. Check out (I am on windows 2.2.2 here):

    http://www.python.org/doc/current/lib/module-wave.html

or

 >>> import wave
 >>> help(wave)
 Help on module wave:

 NAME
     wave - Stuff to parse WAVE files.
[... about reading ...]

    Writing WAVE files:
          f = wave.open(file, 'w')
    where file is either the name of a file or an open file pointer.
    The open file pointer must have methods write(), tell(), seek(), and
    close().

    This returns an instance of a class with the following public methods:
          setnchannels(n) -- set the number of channels
          setsampwidth(n) -- set the sample width
          setframerate(n) -- set the frame rate
          setnframes(n)   -- set the number of frames
          setcomptype(type, name)
                          -- set the compression type and the
                             human-readable compression type
          setparams(tuple)
                          -- set all parameters at once
          tell()          -- return current position in output file
          writeframesraw(data)
                          -- write audio frames without pathing up the
                             file header
          writeframes(data)
                          -- write audio frames and patch up the file header
          close()         -- patch up the file header and close the
                             output file
    You should set the parameters before the first writeframesraw or
    writeframes.  The total number of frames does not need to be set,
    but when it is set to the correct value, the header does not have to
    be patched up.
    It is best to first set all parameters, perhaps possibly the
    compression type, and then write audio frames using writeframesraw.
    When all frames have been written, either call writeframes('') or
    close() to patch up the sizes in the header.
    The close() method is called automatically when the class instance
    is destroyed.

HTH

Regards,
Bengt Richter



More information about the Python-list mailing list