Shortcuts

torchaudio.legacy

Legacy loading and save functions.

load

torchaudio.legacy.load(filepath, out=None, normalization=None, num_frames=0, offset=0)[source]

Loads an audio file from disk into a Tensor. The default options have changed as of torchaudio 0.2 and this function maintains option defaults from version 0.1.

Parameters
  • filepath (str) – Path to audio file

  • out (torch.Tensor, optional) – An output Tensor to use instead of creating one. (Default: None)

  • normalization (bool or number, optional) – If boolean True, then output is divided by 1 << 31 (assumes 16-bit depth audio, and normalizes to [0, 1]. If number, then output is divided by that number. (Default: None)

  • num_frames (int, optional) – Number of frames to load. -1 to load everything after the offset. (Default: 0)

  • offset (int, optional) – Number of frames from the start of the file to begin data loading. (Default: 0)

Returns

The output tensor is of size [L x C] where L is the number of audio frames, C is the number of channels. The integer is sample-rate of the audio (as listed in the metadata of the file)

Return type

Tuple[torch.Tensor, int]

Example
>>> data, sample_rate = torchaudio.legacy.load('foo.mp3')
>>> print(data.size())
torch.Size([278756, 2])
>>> print(sample_rate)
44100

save

torchaudio.legacy.save(filepath, src, sample_rate, precision=32)[source]

Saves a Tensor with audio signal to disk as a standard format like mp3, wav, etc. The default options have changed as of torchaudio 0.2 and this function maintains option defaults from version 0.1.

Parameters
  • filepath (str) – Path to audio file

  • src (torch.Tensor) – An input 2D Tensor of shape [L x C] where L is the number of audio frames, C is the number of channels

  • sample_rate (int) – The sample-rate of the audio to be saved

  • precision (int, optional) – The bit-precision of the audio to be saved. (Default: 32)

Example
>>> data, sample_rate = torchaudio.legacy.load('foo.mp3')
>>> torchaudio.legacy.save('foo.wav', data, sample_rate)

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