Skip to content

Fix #2867#4200

Open
Whning0513 wants to merge 1 commit into
pytorch:mainfrom
Whning0513:fix-issue-2867
Open

Fix #2867#4200
Whning0513 wants to merge 1 commit into
pytorch:mainfrom
Whning0513:fix-issue-2867

Conversation

@Whning0513

Copy link
Copy Markdown

Fix #2867

问题描述

使用 torchaudio.load() 加载某些 MP3 文件时返回空 tensor。

根因分析

torchaudio/_backend/soundfile_backend.pyload 函数中,第 222 行存在 dtype 判断逻辑缺陷:

with soundfile.SoundFile(filepath, "r") as file_:
    if file_.format != "WAV" or normalize:
        dtype = "float32"       # <-- 这里硬编码为 float32

soundfile.SoundFile"r" 模式打开 MP3 文件时,由于 format != "WAV",代码进入该分支并将 dtype 硬编码为 "float32"。然而,某些 MP3 文件使用 DOUBLE 编码格式,其 data 段需要 float64 精度才能正确读取。使用 float32 读取此类文件会导致返回空 tensor 或数据丢失。

修复方案

float32 降级之前,先检查文件实际的 subtype

subtype_dtype = _SUBTYPE2DTYPE.get(file_.subtype)
if subtype_dtype in ("float64",):
    dtype = "float64"
else:
    dtype = "float32"

修改后逻辑

  • 非 WAV 格式(MP3/FLAC/OGG 等)且 normalize=True

    • 如果文件 subtype 为 "DOUBLE",则使用 "float64" 读取,避免精度损失
    • 如果 subtype 为 "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: 分支)。

@Whning0513 Whning0513 requested a review from a team as a code owner June 19, 2026 06:38
@pytorch-bot

pytorch-bot Bot commented Jun 19, 2026

Copy link
Copy Markdown

🔗 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.

@meta-cla

meta-cla Bot commented Jun 19, 2026

Copy link
Copy Markdown

Hi @Whning0513!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla Bot added the CLA Signed label Jun 19, 2026
@meta-cla

meta-cla Bot commented Jun 19, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

read mp3 file fail

1 participant