Search code examples
pythonaudiopcm

How do I write a file in headerless PCM format?


How do I write a file in headerless PCM format? The source data is in a numpy array, and I have an application that is expecting the data in a headerless PCM file, but I can't find any documentation of the file format? Is it basically the same as the data chunk in a wave file?


Solution

  • The problem is that there is no one "headerless PCM" format. For example, 8-bit mono 22K and little-endian 16-bit stereo 48K are both perfectly fine examples of PCM, and the whole point of "headerless" is that you need to know that information through some out-of-band channel.

    But, assuming you have the expected format, it's as simple as it sounds.

    for sample in samples:
        for channel in channels:
            f.write(the_bytes_in_the_right_endianness)
    

    And if you've already got data in that format, just write it to the file as one big block.

    The "default" codec for WAV files—and the only one supported by Python's wave module—is PCM. So, yes, if you have a WAV file in the same format you want, you can just copy its raw frame data into a headerless file, and you've got a headerless PCM file. (And if you have a wav in the wrong format, you can usually use a simple transformation, or at worst something out of audioop, to convert it.)