sndfileio.sndwrite_like(outfile, samples, likefile, sr=None, metadata=None)[source]

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

  • outfile (str) – the file to write to

  • samples (ndarray) – the samples to write

  • likefile (str) – the file to use as a reference for sr, format and encoding

  • sr (Optional[int]) – sample rate can be overridden

  • metadata (Optional[dict[str, str]]) – a dict {str: str}, overrides metadata in likefile. Metadata is not merged, so if metadata is given, it substitutes the metadata in likefile completely. In order to merge it, do that beforehand If None is passed, the metadata in likefile is written to outfile

Return type:



The fileformat is always determined by likefile, even if the extension of outfile would result in a different format. For example, if likefile has a flac format but outfile has a .wav extension, the resulting file will be written in flac format.


# Read a file, apply a fade-in of 0.5 seconds, save it
import numpy as np
from sndfileio import *
samples, sr = sndread("stereo.wav")
fadesize = int(0.5*sr)
ramp = np.linspace(0, 1, fadesize))
samples[:fadesize, 0] *= ramp
samples[:fadesize, 1] *= ramp
sndwrite_like(samples, "stereo.wav", "out.wav")