sndfileio.sndwrite_chunked(outfile, sr, encoding='auto', fileformat=None, metadata=None, **options)[source]

Opens a file for writing and returns a SndWriter

The write() method of the returned SndWriter can be called to write samples to the file

Raises SndfileError if the format does not support the given encoding.

Not all file formats support all encodings. If set to ‘auto’, an encoding will be selected based on the file-format and on the data. The bitdepth of the data is measured, and if the file-format supports it, it will be used. For bitdepths of 8, 16 and 24 bits, a PCM encoding will be used. For a bitdepth of 32 bits, a FLOAT encoding will be used, or the next lower supported encoding

  • outfile (str) – The name of the outfile. the extension will determine the file-format. The formats supported depend on the available backends.

  • sr (int) – Sampling-rate

  • encoding – one of ‘auto’, ‘pcm16’, ‘pcm24’, ‘pcm32’, ‘float32’, ‘float64’.

  • fileformat (Optional[str]) – needed only if the format cannot be determined from the extension (for example, if saving to an outfile with a non-traditional extension)

  • metadata (Optional[dict[str, str]]) – a dict {str: str} with possible keys: ‘comment’, ‘title’, ‘artist’, ‘album’, ‘tracknumber’, ‘software’ (the creator of a soundfile)

  • options – available options depend on the fileformat. For mp3, options are: bitrate (int, default=128) and quality (1-7, where 1 is highest and 7 is fastest, default=2) For non-destructive formats, like wav, aif, flac, etc., there are no extra options

Return type:



a SndWriter, whose method write() can be called to write samples


from snfileio import *
with sndwrite_chunked("out.flac", 44100) as writer:
    for buf in sndread_chunked("in.flac"):
        # do some processing, like changing the gain
        buf *= 0.5