1 min read

Python Sunau Module

The sunau module provides an interface to the Sun AU sound format. It is compatible with the aifc and wave modules.

Note: The sunau module is deprecated as of Python 3.11 and was removed in Python 3.13.

Importing the Module

import sunau

Reading AU Files

To read an AU file, use sunau.open() with mode 'r'.

import sunau

try:
    with sunau.open('sample.au', 'r') as f:
        print("Channels:", f.getnchannels())
        print("Sample width:", f.getsampwidth())
        print("Frame rate:", f.getframerate())
        print("Number of frames:", f.getnframes())
        print("Compression type:", f.getcomptype())
        print("Compression name:", f.getcompname())
except FileNotFoundError:
    print("File not found.")
except sunau.Error as e:
    print(f"An error occurred: {e}")

Writing AU Files

To write an AU file, use sunau.open() with mode 'w'.

import sunau

# Parameters: (nchannels, sampwidth, framerate, nframes, comptype, compname)
params = (1, 2, 8000, 0, 'NONE', 'not compressed')

with sunau.open('output.au', 'w') as f:
    f.setparams(params)
    # Write dummy frames (silence)
    # 2 bytes per sample * 1 channel * 100 frames
    data = b'\x00\x00' * 100 
    f.writeframes(data)

programming/python/python