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
AudioDecoderthat is specialized for WAV files. See TorchCodec Performance Tips and Best Practices for more details.Unlike
AudioDecoder, this decoder does not support resampling (sample_rateparameter) or channel remixing (num_channelsparameter). If you need those features, useAudioDecoder.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
bytesobject ortorch.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) -> bytesandseek(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
bytesobject ortorch.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) -> bytesandseek(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
bytesobject ortorch.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) -> bytesandseek(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
bytesobject ortorch.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) -> bytesandseek(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:- 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:
- 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_secondsandstop_secondsto their default values, or useget_all_samples()as a more convenient alias.- Parameters:
- Returns:
The samples within the specified range.
- Return type: