Shortcuts

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.

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 are 8, 16, 24, 32 and 64.

  • 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)

Tutorials using save:
Audio I/O

Audio I/O

Audio I/O

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources