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, callwrite()
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
|
Write all samples to outfile as mp3 |
|
Resample samples with given samplerate sr to new samplerate newsr |
|
Read a soundfile and its metadata |
|
Get info about a soundfile. |
|
Read a soundfile as a numpy array. |
|
Read a soundfile in chunks |
|
Write all samples to a soundfile. |
|
Opens a file for writing and returns a SndWriter |
|
Create a SndWriter with samplerate/format/encoding of the source file |
|
Write samples to outfile with samplerate/fileformat/encoding taken from likefile |
Classes
|
A structure to hold information about a soundfile |
|
Class returned by |