Rate this Page

WavDecoder#

class torchcodec.decoders.WavDecoder(source: str | Path | RawIOBase | BufferedReader | bytes | Tensor)[source]#

A fast decoder for WAV audio files.

This is a lightweight, high-performance alternative to AudioDecoder that is specialized for WAV files. See TorchCodec Performance Tips and Best Practices for more details.

Unlike AudioDecoder, this decoder does not support resampling (sample_rate parameter) or channel remixing (num_channels parameter). If you need those features, use AudioDecoder.

Returned samples are float samples normalized in [-1, 1].

Parameters:
  • (str (source) –

    object): The source of the audio:

    • If str: a path to a local WAV file.

    • If Pathlib.path: a path to a local WAV file.

    • If bytes object or torch.Tensor: the raw WAV data.

    • If file-like object: we read audio data from the object on demand. The object must expose the methods read(self, size: int) -> bytes and seek(self, offset: int, whence: int) -> int.

  • Pathlib.path

    object): The source of the audio:

    • If str: a path to a local WAV file.

    • If Pathlib.path: a path to a local WAV file.

    • If bytes object or torch.Tensor: the raw WAV data.

    • If file-like object: we read audio data from the object on demand. The object must expose the methods read(self, size: int) -> bytes and seek(self, offset: int, whence: int) -> int.

  • bytes

    object): The source of the audio:

    • If str: a path to a local WAV file.

    • If Pathlib.path: a path to a local WAV file.

    • If bytes object or torch.Tensor: the raw WAV data.

    • If file-like object: we read audio data from the object on demand. The object must expose the methods read(self, size: int) -> bytes and seek(self, offset: int, whence: int) -> int.

  • file-like (torch.Tensor or) –

    object): The source of the audio:

    • If str: a path to a local WAV file.

    • If Pathlib.path: a path to a local WAV file.

    • If bytes object or torch.Tensor: the raw WAV data.

    • If file-like object: we read audio data from the object on demand. The object must expose the methods read(self, size: int) -> bytes and seek(self, offset: int, whence: int) -> int.

Variables:
  • metadata (AudioStreamMetadata) – Metadata of the audio stream.

  • stream_index (int) – The stream index. Always 0 for WAV files.

Examples using WavDecoder:

Decoding audio streams with AudioDecoder

Decoding audio streams with AudioDecoder

TorchCodec Performance Tips and Best Practices

TorchCodec Performance Tips and Best Practices
get_all_samples() AudioSamples[source]#

Returns all the audio samples from the source.

To decode samples in a specific range, use get_samples_played_in_range().

Returns:

The samples within the file.

Return type:

AudioSamples

get_samples_played_in_range(start_seconds: float = 0.0, stop_seconds: float | None = None) AudioSamples[source]#

Returns audio samples in the given range.

Samples are in the half open range [start_seconds, stop_seconds).

To decode all the samples from beginning to end, you can call this method while leaving start_seconds and stop_seconds to their default values, or use get_all_samples() as a more convenient alias.

Parameters:
  • start_seconds (float) – Time, in seconds, of the start of the range. Default: 0.

  • stop_seconds (float or None) – Time, in seconds, of the end of the range. As a half open range, the end is excluded. Default: None, which decodes samples until the end.

Returns:

The samples within the specified range.

Return type:

AudioSamples