sndwrite
- sndfileio.sndwrite(outfile, samples, sr, encoding='default', fileformat=None, normalize_if_clipping=True, metadata=None, **options)[source]
Write all samples to a soundfile.
- Parameters:
outfile (
str
) – The name of the outfile. the extension will determine the file-format. The formats supported depend on the available backends.samples (
ndarray
) – Array-like. the actual samples. The shape determines the number of channels of outfile. For 1 channel,shape=(nframes,)
orshape=(nframes, 1)
. For multichannel audio,shape=(nframes, nchannels)
.sr (
int
) – Sampling-rateencoding – one of “pcm16”, “pcm24”, “pcm32”, “pcm64”, “float32”, “float64”, “auto” to guess an encoding and “default” to use the default encoding for the given format.
fileformat (
Optional
[str
]) – if given, will override the format indicated by the extensionnormalize_if_clipping – prevent clipping by normalizing samples before writing. This only makes sense when writing pcm data
metadata (
Optional
[dict
[str
,str
]]) – a dict of str: str, with possible keys ‘title’, ‘comment’, ‘artist’, ‘album’, ‘tracknumber’, ‘software’ (the creator of a soundfile)options – available options depend on the fileformat.
- Return type:
None
Note
Not all file formats support all encodings. Raises
SndfileError
if the format does not support the given encoding. 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. If ‘default’ is given as encoding, the default encoding for the format is usedFormat
Default encoding
wav
float32
aif
float32
flac
pcm24
mp3
pcm16
Example
# Normalize and save as flac >>> samples, sr = sndread("sndfile.wav") >>> maxvalue = max(samples.max(), -samples.min()) >>> samples *= 1/maxvalue >>> sndwrite("out.flac", samples, sr)