torchaudio.save¶
- torchaudio.save(uri: Union[BinaryIO, str, PathLike], src: Tensor, sample_rate: int, channels_first: bool = True, format: Optional[str] = None, encoding: Optional[str] = None, bits_per_sample: Optional[int] = None, buffer_size: int = 4096, backend: Optional[str] = None)¶
Save audio data to file.
Note
The formats this function can handle depend on the availability of backends. Please use the following functions to fetch the supported formats.
SoundFile: Refer to the official document.
- Parameters:
uri (str or pathlib.Path) – Path to audio file.
src (torch.Tensor) – Audio data to save. must be 2D tensor.
sample_rate (int) – sampling rate
channels_first (bool, optional) – If
True
, the given tensor is interpreted as [channel, time], otherwise [time, channel].format (str or None, optional) –
Override the audio format. When
uri
argument is path-like object, audio format is inferred from file extension. If the file extension is missing or different, you can specify the correct format with this argument.When
uri
argument is file-like object, this argument is required.Valid values are
"wav"
,"ogg"
, and"flac"
.encoding (str or None, optional) –
Changes the encoding for supported formats. This argument is effective only for supported formats, i.e.
"wav"
and""flac"`
. Valid values are"PCM_S"
(signed integer Linear PCM)"PCM_U"
(unsigned integer Linear PCM)"PCM_F"
(floating point PCM)"ULAW"
(mu-law)"ALAW"
(a-law)
bits_per_sample (int or None, optional) – Changes the bit depth for the supported formats. When
format
is one of"wav"
and"flac"
, you can change the bit depth. Valid values are8
,16
,24
,32
and64
.buffer_size (int, optional) – Size of buffer to use when processing file-like objects, in bytes. (Default:
4096
)backend (str or None, optional) –
I/O backend to use. If
None
, function selects backend given input and available backends. Otherwise, must be one of ["ffmpeg"
,"sox"
,"soundfile"
], with the corresponding backend being available. (Default:None
)See also
- Tutorials using
save
: - Audio I/O