Welcome to sndfileio’s documentation!

This package provides a simple and unified API to read and write sound-files to and from numpy arrays.

sndfileio Package

SNDFILEIO

A simple module providing a unified API to read and write sound-files to and from numpy arrays. If no extra modules are installed, it uses only standard modules and numpy to read and write uncompressed formats (WAV, AIFF).

Backends

API

Read / write a file in one function.

sndinfo

Returns a SndInfo with all the information of the sound-file

sndread

Reads ALL the samples. Returns a tuple (data, samplerate)

sndwrite

Write samples to outfile

sndwrite_like

Write samples to outfile cloning another files format & encoding

Chunked IO

It is possible to stream a soundfile by reading and processing chunks. This is helpful in order to avoid allocating memory for a large. The same is possible for writing

sndread_chunked

returns a generator yielding chunks of frames

sndwrite_chunked

opens the file for writing. Returns a SndWriter. To write to the file, call write() on the returned handle

Examples

# Normalize and save as flac
from sndfileio import sndread, sndwrite
samples, sr = sndread("in.wav")
maxvalue = max(samples.max(), -samples.min())
samples *= 1/maxvalue
sndwrite(samples, sr, "out.flac")
# Process a file in chunks
from sndfileio import *
from sndfileio.dsp import
with sndwrite_chunked(44100, "out.flac") as writer:
    for buf in sndread_chunked("in.flac"):
        # do some processing, like changing the gain
        buf *= 0.5
        writer.write(buf)

Functions

asmono(samples[, channel])

convert samples to mono if they are not mono already.

bitdepth(data[, snap])

returns the number of bits actually used to represent the data.

getchannel(samples, channel)

Returns a view into a channel of samples.

numchannels(samples)

return the number of channels present in samples

resample(samples, oldsr, newsr)

Resample samples with given samplerate sr to new samplerate newsr

sndinfo(path)

Get info about a soundfile.

sndread(path[, start, end])

Read a soundfile as a numpy array.

sndread_chunked(path[, chunksize, start, stop])

Read a soundfile in chunks

sndwrite(outfile, samples, sr[, encoding, …])

Write all samples to a soundfile.

sndwrite_chunked(outfile, sr[, encoding, …])

Opens a file for writing and returns a SndWriter

sndwrite_chunked_like(likefile, outfile[, …])

Create a SndWriter with samplerate/format/encoding of the source file

sndwrite_like(samples, likefile, outfile[, …])

Write samples to outfile with samplerate/encoding taken from likefile

Classes

SndInfo(samplerate, nframes, channels, …)

A structure to hold information about a soundfile

SndWriter(backend, sr, outfile, encoding[, …])

Class returned by sndwrite_chunked() to write samples as they become available (via calling its SndWriter.write() method).