-
Notifications
You must be signed in to change notification settings - Fork 44
Add Docs for AudioEncoder #717
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.. _encoders: | ||
|
||
=================== | ||
torchcodec.encoders | ||
=================== | ||
|
||
.. currentmodule:: torchcodec.encoders | ||
|
||
|
||
For an audio decoder tutorial, see: :ref:`sphx_glr_generated_examples_encoding_audio_encoding.py`. | ||
|
||
|
||
.. autosummary:: | ||
:toctree: generated/ | ||
:nosignatures: | ||
:template: class.rst | ||
|
||
AudioEncoder |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed in this file, as well as the files renaming, are meant to separate our "tutorials" page into 2 separate sections: one for decoding, one for encoding. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,18 +68,24 @@ class CustomGalleryExampleSortKey: | |
def __init__(self, src_dir): | ||
self.src_dir = src_dir | ||
|
||
order = [ | ||
"basic_example.py", | ||
"audio_decoding.py", | ||
"basic_cuda_example.py", | ||
"file_like.py", | ||
"approximate_mode.py", | ||
"sampling.py", | ||
] | ||
|
||
def __call__(self, filename): | ||
if "examples/decoding" in self.src_dir: | ||
order = [ | ||
"basic_example.py", | ||
"audio_decoding.py", | ||
"basic_cuda_example.py", | ||
"file_like.py", | ||
"approximate_mode.py", | ||
"sampling.py", | ||
] | ||
else: | ||
assert "examples/encoding" in self.src_dir | ||
order = [ | ||
"audio_encoding.py", | ||
] | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a comment explaining that we have two top-level galleries, and for that reason, we need to figure out which gallery we're using (decoding versus encoding)? I was real confused until I concluded that must be what's going on. |
||
try: | ||
return self.order.index(filename) | ||
return order.index(filename) | ||
except ValueError as e: | ||
raise ValueError( | ||
"Looks like you added an example in the examples/ folder?" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Decoding | ||
-------- |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Encoding | ||
-------- |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
""" | ||
======================================== | ||
Encoding audio samples with AudioEncoder | ||
======================================== | ||
|
||
In this example, we'll learn how to encode audio samples to a file or to raw | ||
bytes using the :class:`~torchcodec.encoders.AudioEncoder` class. | ||
""" | ||
|
||
# %% | ||
# Let's first generate some samples to be encoded. The data to be encoded could | ||
# also just come from an :class:`~torchcodec.decoders.AudioDecoder`! | ||
import torch | ||
from IPython.display import Audio as play_audio | ||
|
||
|
||
def make_sinewave() -> tuple[torch.Tensor, int]: | ||
freq_A = 440 # Hz | ||
sample_rate = 16000 # Hz | ||
duration_seconds = 3 # seconds | ||
t = torch.linspace(0, duration_seconds, int(sample_rate * duration_seconds), dtype=torch.float32) | ||
return torch.sin(2 * torch.pi * freq_A * t), sample_rate | ||
|
||
|
||
samples, sample_rate = make_sinewave() | ||
|
||
print(f"Encoding samples with {samples.shape = } and {sample_rate = }") | ||
play_audio(samples, rate=sample_rate) | ||
|
||
# %% | ||
# We first instantiate an :class:`~torchcodec.encoders.AudioEncoder`. We pass it | ||
# the samples to be encoded. The samples must a 2D tensors of shape | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: "The samples must be a 2D tensors of shape" |
||
# ``(num_channels, num_samples)``, or in this case, a 1D tensor where | ||
# ``num_channels`` is assumed to be 1. The values must be float values | ||
# normalized in ``[-1, 1]``: this is also what the | ||
# :class:`~torchcodec.decoders.AudioDecoder` would return. | ||
# | ||
# .. note:: | ||
# | ||
# The ``sample_rate`` parameter corresponds to the sample rate of the | ||
# *input*, not the desired encoded sample rate. | ||
from torchcodec.encoders import AudioEncoder | ||
|
||
encoder = AudioEncoder(samples=samples, sample_rate=sample_rate) | ||
|
||
|
||
# %% | ||
# :class:`~torchcodec.encoders.AudioEncoder` supports encoding samples into a | ||
# file via the :meth:`~torchcodec.encoders.AudioEncoder.to_file` method, or to | ||
# raw bytes via :meth:`~torchcodec.encoders.AudioEncoder.to_tensor`. For the | ||
# purpose of this tutorial we'll use | ||
# :meth:`~torchcodec.encoders.AudioEncoder.to_tensor`, so that we can easily | ||
# re-decode the encoded samples and check their properies. The | ||
# :meth:`~torchcodec.encoders.AudioEncoder.to_file` method works very similarly. | ||
|
||
encoded_samples = encoder.to_tensor(format="mp3") | ||
print(f"{encoded_samples.shape = }, {encoded_samples.dtype = }") | ||
|
||
|
||
# %% | ||
# That's it! | ||
# | ||
# Now that we have our encoded data, we can decode it back, to make sure it | ||
# looks and sounds as expected: | ||
from torchcodec.decoders import AudioDecoder | ||
|
||
samples_back = AudioDecoder(encoded_samples).get_all_samples() | ||
|
||
print(samples_back) | ||
play_audio(samples_back.data, rate=samples_back.sample_rate) | ||
|
||
# %% | ||
# The encoder supports some encoding options that allow you to change how to | ||
# data is encoded. For example, we can decide to encode our mono data (1 | ||
# channel) into stereo data (2 channels): | ||
encoded_samples = encoder.to_tensor(format="wav", num_channels=2) | ||
|
||
stereo_samples_back = AudioDecoder(encoded_samples).get_all_samples() | ||
|
||
print(stereo_samples_back) | ||
play_audio(stereo_samples_back.data, rate=stereo_samples_back.sample_rate) | ||
|
||
# %% | ||
# Check the docstring of the encoding methods to learn about the different | ||
# encoding options. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I prefer "x and y" as opposed to "x / y" in prose.