Skip to content

FIX AudioSpeedConverter: document that pitch shifts with speed and reject empty output - #3814

Merged
Adrian Gavrila (adrian-gavrila) merged 3 commits into
microsoft:mainfrom
VISHNU0906:fix-audio-speed-pitch-docs
Nov 4, 2026
Merged

Adrian Gavrila (adrian-gavrila) merged 3 commits into
microsoft:mainfrom
VISHNU0906:fix-audio-speed-pitch-docs

Conversation

@VISHNU0906

@VISHNU0906 KVR Vishnu (VISHNU0906) commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #3714.

AudioSpeedConverter changes speed by resampling the samples, so the pitch moves together with the speed. A 440 Hz tone at 2x comes out at about 880 Hz. The class docstring, the convert_async docstring and the audio converters doc page all said pitch was preserved, which is not what the code does. The issue also showed that a very large factor on a short input silently writes a file with zero samples.

The issue offered two routes: switch to a pitch-preserving time stretch, or correct the documented contract. I went with the smaller one here:

  • the docstrings and the doc page (both the .py and the paired notebook) now say the pitch shifts with the speed, with the 440 Hz to 880 Hz example
  • _resample_audio raises a clear ValueError when the factor would leave no samples, instead of writing empty audio

Behaviour for normal inputs is unchanged. If you would rather have a real pitch-preserving stretch, I am happy to look at that as a follow-up instead.

cc Roman Lutz (@romanlutz) (issue author). Happy to adjust anything.

Tests and Documentation

Added two tests in tests/unit/converter/test_audio_speed_converter.py:

  • a 2x conversion of a 440 Hz sine shifts the peak to about 880 Hz, which pins the behaviour the docs now describe
  • a 2-sample input at factor 100 raises ValueError instead of producing empty audio (fails on main, passes with the fix)

Locally, on a fresh clone of main:

  • uv run pre-commit run --all-files: all hooks passed
  • ty check pyrit tests/unit (the make ty target): all checks passed
  • full unit suite (pytest -n 4 --dist=loadfile tests/unit): 32981 passed, 32 skipped

Docs updated in doc/code/converters/2_audio_converters.py and .ipynb (two comment lines each).

…ject empty output

The converter resamples the signal, so pitch changes together with speed (2x turns 440 Hz into 880 Hz), but the docstrings and the audio converter docs said pitch was preserved. Correct the docs, and raise a clear ValueError when the speed factor is so large that the output would have zero samples instead of silently writing empty audio.

Fixes microsoft#3714

Signed-off-by: KVR Vishnu <46535652+VISHNU0906@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 2, 2026 21:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The two new async tests lack the _async naming suffix and parameter/return type annotations required by the style guide.

1 open issue
What changed in this PR

Addresses #3714 by aligning AudioSpeedConverter documentation with its resampling behavior and preventing empty audio output, without changing normal conversions.

Changes:

  • Clarifies that pitch shifts with playback speed.
  • Raises ValueError when resampling would produce zero samples.
  • Adds regression tests for pitch shifting and short-input rejection.
File Description
tests/​unit/​converter/​test_audio_speed_converter.py Adds pitch-shift and empty-output regression tests.
pyrit/​converter/​audio_speed_converter.py Corrects docstrings and rejects zero-sample output.
doc/​code/​converters/​2_audio_converters.py Updates documentation comments to describe pitch shifting.
doc/​code/​converters/​2_audio_converters.ipynb Mirrors the documentation corrections in the notebook.

Review effort: Balanced

…dioSpeedConverter tests

Signed-off-by: KVR Vishnu <46535652+VISHNU0906@users.noreply.github.com>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the fix!

Merged via the queue into microsoft:main with commit 351ea8d Nov 4, 2026
93 of 136 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AudioSpeedConverter changes pitch despite promising pitch preservation

3 participants