Skip to content

Expose libswresample options in AudioResampler#2296

Open
WyattBlue wants to merge 1 commit into
mainfrom
resampler-swr-options
Open

Expose libswresample options in AudioResampler#2296
WyattBlue wants to merge 1 commit into
mainfrom
resampler-swr-options

Conversation

@WyattBlue

Copy link
Copy Markdown
Member

Closes #2262

Summary

Adds an options parameter to av.AudioResampler so callers can pass libswresample options through to the underlying resampler:

resampler = av.AudioResampler(
    format="fltp",
    layout="mono",
    rate=16000,
    options={"resampler": "soxr", "precision": "28", "cutoff": "0.95"},
)

How it works

AudioResampler builds a small filter graph (abuffer → aformat → abuffersink). The aformat filter is metadata-only — the actual resampling is done by an aresample filter, which owns the SwrContext and exposes every swr option as a child AVOption.

When options is supplied, an explicit aresample filter is inserted into the chain:

abuffer → aresample(**options) → aformat → abuffersink

instead of relying on the one FFmpeg auto-inserts. Options flow through avfilter_init_dict, which searches child contexts, so they land on the SwrContext. No new C/Cython bindings are required — this stays within the existing filter API.

Notes

  • An unknown option surfaces as a clear ValueError: unused config: <name> rather than being silently ignored.
  • resampler=soxr additionally requires FFmpeg to be built --enable-libsoxr; on builds without it, FFmpeg raises Invalid argument (a build-time decision, unrelated to this API). The native swr knobs (filter_size, phase_shift, cutoff, etc.) work on any build.

Tests

  • test_swr_options — verifies options pass through and output params are correct.
  • test_swr_options_invalid — verifies an unknown option raises a clear error.

Existing resampler tests and mypy are green.

closes #2262

Add an `options` parameter to `AudioResampler` so callers can pass
`libswresample` options (e.g. `resampler`, `filter_size`, `phase_shift`,
`cutoff`, `precision`) through to the underlying resampler.

When options are supplied, the conversion is performed by an explicit
`aresample` filter (which owns the SwrContext) instead of the one FFmpeg
auto-inserts before `aformat`. Options are forwarded as filter options,
which search child contexts and reach the SwrContext. No new C bindings
are required.
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.

Feature Request: Expose libswresample options in AudioResampler

1 participant