Fix #2867#4200
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/audio/4200
Note: Links to docs will display an error until the docs builds have been completed. This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
Hi @Whning0513! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
Fix #2867
问题描述
使用
torchaudio.load()加载某些 MP3 文件时返回空 tensor。根因分析
在
torchaudio/_backend/soundfile_backend.py的load函数中,第 222 行存在 dtype 判断逻辑缺陷:当
soundfile.SoundFile以"r"模式打开 MP3 文件时,由于format != "WAV",代码进入该分支并将dtype硬编码为"float32"。然而,某些 MP3 文件使用DOUBLE编码格式,其 data 段需要float64精度才能正确读取。使用float32读取此类文件会导致返回空 tensor 或数据丢失。修复方案
在
float32降级之前,先检查文件实际的subtype:修改后逻辑
非 WAV 格式(MP3/FLAC/OGG 等)且 normalize=True:
"DOUBLE",则使用"float64"读取,避免精度损失"FLOAT"或其他整形类型,则使用"float32"读取(即原行为)WAV 格式且 normalize=True:与上述逻辑一致,会自动处理 DOUBLE 类型的 WAV 文件
WAV 格式且 normalize=False:使用原始整数类型读取(行为不变)
修改文件
torchaudio/_backend/soundfile_backend.py(或torchaudio/backend/soundfile_backend.py包装器指向的实际后端文件)涉及行
load()函数中第 222 行附近(if file_.format != "WAV" or normalize:分支)。