-
Notifications
You must be signed in to change notification settings - Fork 70
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
110327c
Docstrings
NicolasHug d7ec60e
Some reorg
NicolasHug ee05058
Add tuto
NicolasHug a0d7af3
Add smoke test
NicolasHug 9f15e04
Merge branch 'main' of github.com:pytorch/torchcodec into encoderdocs
NicolasHug a2a0f79
Address most comments
NicolasHug 84e0f26
Add comment
NicolasHug 5c30df9
Merge branch 'main' of github.com:pytorch/torchcodec into encoderdocs
NicolasHug File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,18 +68,27 @@ 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): | ||
| # We have two top-level galleries, one for decoding examples and one for | ||
| # encoding examples. We define the example order within each gallery | ||
| # individually. | ||
| 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", | ||
| ] | ||
|
|
||
|
Contributor
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?" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Decoding | ||
| -------- |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Encoding | ||
| -------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.