Repository navigation
Preserve NumPy sample values across non-native byte orders #500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bastibe
merged 3 commits into
bastibe:master
from
antonsoo:fix/non-native-array-byteorder
Oct 15, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import io | ||
| import sys | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
| from numpy.testing import assert_array_equal | ||
|
|
||
| import soundfile as sf | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| 'dtype,subtype', | ||
| [ | ||
| ('int16', 'PCM_16'), | ||
| ('int32', 'PCM_32'), | ||
| ('float32', 'FLOAT'), | ||
| ('float64', 'DOUBLE'), | ||
| ], | ||
| ) | ||
| @pytest.mark.parametrize('channels', [1, 2]) | ||
| @pytest.mark.parametrize('strided', [False, True]) | ||
| def test_write_non_native_endian_preserves_samples(dtype, subtype, channels, strided): | ||
| order = '>' if sys.byteorder == 'little' else '<' | ||
| values = ( | ||
| np.array([1, 256, -2345, 42], dtype=dtype) | ||
| if dtype.startswith('int') | ||
| else np.array([0.25, 0.5, -0.75, 0.125], dtype=dtype) | ||
| ) | ||
| if channels == 2: | ||
| values = np.column_stack((values, -values)) | ||
| swapped = values.astype(np.dtype(dtype).newbyteorder(order)) | ||
| if strided: | ||
| backing = np.empty((len(values) * 2,) + values.shape[1:], dtype=swapped.dtype) | ||
| backing[::2] = swapped | ||
| swapped = backing[::2] | ||
| snapshot = swapped.tobytes() | ||
| output = io.BytesIO() | ||
| sf.write(output, swapped, 8000, format='WAV', subtype=subtype) | ||
| output.seek(0) | ||
| decoded, _ = sf.read(output, dtype=dtype) | ||
| assert_array_equal(decoded, values) | ||
| assert swapped.tobytes() == snapshot | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| 'dtype,subtype', | ||
| [ | ||
| ('int16', 'PCM_16'), | ||
| ('int32', 'PCM_32'), | ||
| ('float32', 'FLOAT'), | ||
| ('float64', 'DOUBLE'), | ||
| ], | ||
| ) | ||
| @pytest.mark.parametrize('channels', [1, 2]) | ||
| @pytest.mark.parametrize('fill', [None, 17]) | ||
| def test_read_non_native_endian_out_rejected_without_mutation( | ||
| dtype, subtype, channels, fill | ||
| ): | ||
| order = '>' if sys.byteorder == 'little' else '<' | ||
| values = ( | ||
| np.array([1, 256, -2345, 42], dtype=dtype) | ||
| if dtype.startswith('int') | ||
| else np.array([0.25, 0.5, -0.75, 0.125], dtype=dtype) | ||
| ) | ||
| if channels == 2: | ||
| values = np.column_stack((values, -values)) | ||
| output = io.BytesIO() | ||
| sf.write(output, values, 8000, format='WAV', subtype=subtype) | ||
| output.seek(0) | ||
| shape = (6,) if channels == 1 else (6, 2) | ||
| out = np.full(shape, 23, dtype=np.dtype(dtype).newbyteorder(order)) | ||
| snapshot = out.tobytes() | ||
| with sf.SoundFile(output) as file: | ||
| with pytest.raises(ValueError, match='native byte order'): | ||
| file.read(out=out, fill_value=fill) | ||
| assert file.tell() == 0 | ||
| assert out.tobytes() == snapshot |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.