Replace soundfile+torchaudio with torchcodec AudioDecoder in load_audio#20190
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request streamlines the audio loading process by unifying media decoding under the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request effectively refactors the load_audio function to use torchcodec.AudioDecoder, replacing the previous implementation that relied on soundfile and torchaudio. This change simplifies the codebase by unifying media decoding under a single library and makes the audio loading process more efficient and cleaner. The implementation correctly handles various input sources and maintains backward compatibility of the output format. I have one suggestion to enhance the robustness of handling audio files from HTTP sources.
Note: Security Review did not run due to the size of the PR.
| response = requests.get(audio_file, stream=True, timeout=timeout) | ||
| audio_file = BytesIO(response.content) | ||
| source = response.content | ||
| response.close() |
There was a problem hiding this comment.
For improved robustness when fetching audio from URLs, it's a good practice to use a with statement for requests.get. This ensures the network connection is properly closed, even if errors occur. Adding response.raise_for_status() will also make the function fail fast for bad HTTP responses (e.g., 404 Not Found), which is more explicit than failing later during decoding. I've also removed stream=True as it's not necessary when immediately accessing .content.
| response = requests.get(audio_file, stream=True, timeout=timeout) | |
| audio_file = BytesIO(response.content) | |
| source = response.content | |
| response.close() | |
| with requests.get(audio_file, timeout=timeout) as response: | |
| response.raise_for_status() | |
| source = response.content |
Use torchcodec's AudioDecoder for audio loading, which handles decoding, resampling, and channel conversion in a single step. This aligns audio loading with the video decoding migration to torchcodec (sgl-project#20055) and removes the soundfile/torchaudio dependency from the audio path.
31c3ef8 to
9dc5c26
Compare
|
/tag-run-ci-label |
|
/rerun-failed-ci |
1 similar comment
|
/rerun-failed-ci |
Motivation
Follow-up to #20055 (video decoding migration to torchcodec). This PR migrates
load_audio()from soundfile + torchaudio to torchcodec'sAudioDecoder, unifying all media decoding under torchcodec.Changes
Replace
soundfile.read()+torchaudio.functional.resample()withtorchcodec.decoders.AudioDecoder, which handles decoding, resampling, and channel conversion in a single step. Falls back to soundfile + torchaudio on ARM / no-FFmpeg environments, reusingvideo_decoder._BACKENDflag for unified detection.Before: soundfile loads raw audio → numpy mono conversion → torchaudio polyphase resample
After:
AudioDecoder(source, sample_rate=sr, num_channels=1)→ donefile://, local path(time,), stereo →(time, channels)_BACKENDflag fromvideo_decoder.pyTest plan
file://URL inputmono=False)