Handle non-contiguous input in process() - #5
Draft
sanerdemirel wants to merge 1 commit into
Draft
Conversation
process() copied each channel's samples with std::copy under the assumption that the input buffer was C-contiguous. A transposed array, which is what librosa.load(..., mono=False) commonly returns, has its channel and sample strides swapped relative to a contiguous array, so that assumption produced silently corrupted output (gregogiudici#3). process() now reads through the array's own strides instead of assuming a fixed layout. The buffer it reads from is always copied into internally-owned memory before processing regardless of layout, so this costs nothing extra for the already-contiguous case; it just also gives the right answer for the non-contiguous one. While in there, process() now also accepts a 1-D (mono) input by promoting it to a single channel, instead of raising a TypeError, matching the layout handling proposed on the issue.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3.
The problem
process()copied each channel's samples withstd::copyunder the assumptionthat the input buffer was C-contiguous:
A transposed array — which is what
librosa.load(..., mono=False)commonlyhands you — is a view with the channel and sample strides swapped relative to a
freshly allocated one. Copying through it as if it were still contiguous reads
the wrong samples, so the output is silently corrupted rather than failing.
The change
process()now reads each sample through the array's own strides instead ofassuming a layout. The input was already being copied into internally-owned
buffers before processing, contiguous or not, so this costs nothing extra for
the contiguous case — it just also gives the right answer for the
non-contiguous one.
While in there,
process()also accepts 1-D (mono) input by promoting it to asingle channel instead of raising
TypeError, matching the layout handlingproposed on the issue.
How I checked it
(mono and stereo, several time factors).
comparison could not have detected a difference in the first place: I
confirmed the harness could tell the old and new builds apart when a
difference was deliberately introduced, before trusting the runs where it
reported none.
tests/test_noncontiguous_input.pycovers the transposed-input case in bothmono and stereo.
Note
Opened as a draft. Happy to adjust anything about the approach or the tests.