Skip to content

perf: enhance AMR audio quality and simplify opus logic#8153

Open
WenqiOfficial wants to merge 2 commits into
AstrBotDevs:masterfrom
WenqiOfficial:perf/media-audio-optimization
Open

perf: enhance AMR audio quality and simplify opus logic#8153
WenqiOfficial wants to merge 2 commits into
AstrBotDevs:masterfrom
WenqiOfficial:perf/media-audio-optimization

Conversation

@WenqiOfficial
Copy link
Copy Markdown
Contributor

@WenqiOfficial WenqiOfficial commented May 11, 2026

主要实现Issue #7283 提及的AMR语音听觉体验优化,利用贝叶斯算法获取最佳参数。

以及小小的代码优化。

Modifications / 改动点

  • 增加-af参数,优化AMR语音

    • astrbot\core\utils\media_utils.py
  • 精简opus格式转码逻辑,复用已有代码

    • astrbot\core\utils\media_utils.py
  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

image

无参/有参优化效果对比


Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Improve AMR audio conversion quality and simplify opus conversion handling in media utilities.

Enhancements:

  • Refine AMR audio conversion parameters by adding an ffmpeg audio filter chain to enhance voice quality.
  • Simplify opus conversion by delegating to the generic audio format conversion helper instead of duplicating ffmpeg logic.

- Route Opus conversion directly through the underlying convert_audio_format.
- Remove redundant FFmpeg processing chains to improve code reusability.
- Enhance AMR audio quality via built-in FFmpeg filters.
@auto-assign auto-assign Bot requested review from LIghtJUNction and Soulter May 11, 2026 12:13
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend labels May 11, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • The new convert_audio_to_opus implementation no longer short‑circuits when the input is already an .opus file, so existing opus audio will now be re-encoded; consider preserving the previous fast-path to avoid unnecessary quality loss and CPU usage.
  • The AMR -af filter chain is embedded as a long inline string; extracting it into a named constant (or helper) would make the intent clearer and the parameters easier to tweak or reuse.
  • For AMR conversion you now specify both -ar 8000 and -af ... aresample=8000, which is redundant; consider keeping only one to avoid confusion about which stage is responsible for resampling.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `convert_audio_to_opus` implementation no longer short‑circuits when the input is already an `.opus` file, so existing opus audio will now be re-encoded; consider preserving the previous fast-path to avoid unnecessary quality loss and CPU usage.
- The AMR `-af` filter chain is embedded as a long inline string; extracting it into a named constant (or helper) would make the intent clearer and the parameters easier to tweak or reuse.
- For AMR conversion you now specify both `-ar 8000` and `-af ... aresample=8000`, which is redundant; consider keeping only one to avoid confusion about which stage is responsible for resampling.

## Individual Comments

### Comment 1
<location path="astrbot/core/utils/media_utils.py" line_range="70-72" />
<code_context>
+async def convert_audio_to_opus(audio_path: str, output_path: str | None = None) -> str:
</code_context>
<issue_to_address>
**issue (bug_risk):** The new implementation changes behavior for already-opus inputs and may introduce unnecessary re-encoding.

Previously, this function returned the original path and skipped work when `audio_path` already ended with `.opus`. Now, delegating to `convert_audio_format` forces ffmpeg to run even for existing opus files, which may re-encode (wasting CPU and degrading quality) and return a different path. If any callers assume idempotence for `.opus` inputs or that the returned path equals the input, this changes their behavior. To preserve compatibility while using the new helper, keep the `.opus` short-circuit and only call `convert_audio_format` for non-opus inputs.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread astrbot/core/utils/media_utils.py
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the convert_audio_to_opus function to utilize a centralized convert_audio_format utility and introduces a series of audio filters—including highpass, lowpass, equalizer, and normalization—to the AMR conversion process. The review feedback suggests restoring the detailed docstrings for the refactored function to maintain documentation quality and IDE support.

Comment thread astrbot/core/utils/media_utils.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant