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

This package providesa unified API to read and write sound-files to and from numpy arrays. It can read and write to wav, aif, flac, mp3 and all loss-less formats supported by libsndfile.


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

sndget

Reads the sample data, returns a tuple (samples, info), where info is a SndInfo with all the information of the sound-file


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

mp3write(outfile, samples, sr[, bitrate, ...])

Write all samples to outfile as mp3

resample(samples, oldsr, newsr)

Resample samples with given samplerate sr to new samplerate newsr

sndget(path[, start, end])

Read a soundfile and its metadata

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(outfile, likefile[, ...])

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

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

Write samples to outfile with samplerate/fileformat/encoding taken from likefile

Classes

SndInfo(samplerate, nframes, channels, ...)

A structure to hold information about a soundfile

SndWriter(sr, outfile, encoding[, ...])

Class returned by sndwrite_chunked() to write samples