Skip to content

πŸŽ› πŸ”Š A Python library for audio.

Try to track down nasty bug on Windows when writing int32.

Peter Sobot862d82a438cd
diff --git a/.github/workflows/all.yml b/.github/workflows/all.yml
index 43e28b7..d87c281 100644
--- a/.github/workflows/all.yml
+++ b/.github/workflows/all.yml
@@ -59,8 +59,8 @@ jobs:
MINIMUM_COVERAGE_PERCENTAGE: 90
strategy:
matrix:
- python-version: ['3.6', '3.7', '3.8', '3.9', '3.10']
- os: [ubuntu-18.04, ubuntu-latest, windows-latest, macos-10.15, macos-latest]
+ python-version: ['3.6'] # , '3.7', '3.8', '3.9', '3.10']
+ os: [windows-latest] # ubuntu-18.04, ubuntu-latest, windows-latest, macos-10.15, macos-latest]
name: Test with Python ${{ matrix.python-version }} on ${{ matrix.os }}
steps:
- name: Set up Python ${{ matrix.python-version }}
@@ -98,14 +98,14 @@ jobs:
if: runner.os == 'Windows'
- name: Build pedalboard locally
run: python setup.py install
- - name: Install VSTs for testing
- env:
- GCS_ASSET_BUCKET_NAME: ${{ secrets.GCS_ASSET_BUCKET_NAME }}
- GCS_READER_SERVICE_ACCOUNT_KEY: ${{ secrets.GCS_READER_SERVICE_ACCOUNT_KEY }}
- run: python ./tests/download_test_plugins.py
+ # - name: Install VSTs for testing
+ # env:
+ # GCS_ASSET_BUCKET_NAME: ${{ secrets.GCS_ASSET_BUCKET_NAME }}
+ # GCS_READER_SERVICE_ACCOUNT_KEY: ${{ secrets.GCS_READER_SERVICE_ACCOUNT_KEY }}
+ # run: python ./tests/download_test_plugins.py
- name: Run tests
if: matrix.os != 'ubuntu-latest' || matrix.python-version != '3.6'
- run: pytest -v --cov-report term --cov-fail-under=${{ env.MINIMUM_COVERAGE_PERCENTAGE }} --cov=pedalboard
+ run: pytest -v --cov-report term --cov-fail-under=${{ env.MINIMUM_COVERAGE_PERCENTAGE }} --cov=pedalboard -k int32 -s
- name: Run tests with coverage reporting
if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.6'
run: |
@@ -126,6 +126,7 @@ jobs:
run-tests-with-address-sanitizer:
runs-on: ${{ matrix.os }}
+ if: false
strategy:
matrix:
python-version: ['3.6', '3.7', '3.8', '3.9', '3.10']
diff --git a/pedalboard/io/AudioFile.h b/pedalboard/io/AudioFile.h
index a44fb22..49c59ff 100644
--- a/pedalboard/io/AudioFile.h
+++ b/pedalboard/io/AudioFile.h
@@ -446,6 +446,9 @@ public:
unsigned int bufferSize = DEFAULT_AUDIO_BUFFER_SIZE_FRAMES>
bool writeConvertingTo(const InputType **channels, int numChannels,
unsigned int numSamples) {
+ printf("in writeConvertingTo<%s, %s>(%d, %d)\n",
+ typeid(TargetType).name(), typeid(InputType).name(),
+ numChannels, numSamples);
std::vector<std::vector<TargetType>> targetTypeBuffers;
targetTypeBuffers.resize(numChannels);
@@ -459,35 +462,36 @@ public:
targetTypeBuffers[c].resize(samplesToWrite);
channelPointers[c] = targetTypeBuffers[c].data();
- for (unsigned int i = 0; i < samplesToWrite; i++) {
- if constexpr (std::is_integral<InputType>::value) {
- if constexpr (std::is_integral<TargetType>::value) {
+ if constexpr (std::is_integral<InputType>::value) {
+ if constexpr (std::is_integral<TargetType>::value) {
+ for (unsigned int i = 0; i < samplesToWrite; i++) {
// Left-align the samples to use all 32 bits, as JUCE requires:
targetTypeBuffers[c][i] =
((int)channels[c][startSample + i])
<< (std::numeric_limits<int>::digits -
std::numeric_limits<InputType>::digits);
- } else if constexpr (std::is_same<TargetType, float>::value) {
- juce::FloatVectorOperations::convertFixedToFloat(
- targetTypeBuffers[c].data(), channels[c] + startSample, 1.0,
- samplesToWrite);
- } else {
- // We should never get here - this would only be true
- // if converting to double, which no formats require:
- static_assert(std::is_integral<InputType>::value &&
- std::is_same<TargetType, double>::value,
- "Can't convert to double");
}
-
+ } else if constexpr (std::is_same<TargetType, float>::value) {
+ juce::FloatVectorOperations::convertFixedToFloat(
+ targetTypeBuffers[c].data(), channels[c] + startSample, 1.0,
+ samplesToWrite);
} else {
- if constexpr (std::is_integral<TargetType>::value) {
- // We should never get here - this would only be true
- // if converting float to int, which JUCE handles for us:
- static_assert(!std::is_integral<InputType>::value &&
- std::is_integral<TargetType>::value,
- "Can't convert int to float");
- } else {
- // Converting double to float:
+ // We should never get here - this would only be true
+ // if converting to double, which no formats require:
+ static_assert(std::is_integral<InputType>::value &&
+ std::is_same<TargetType, double>::value,
+ "Can't convert to double");
+ }
+ } else {
+ if constexpr (std::is_integral<TargetType>::value) {
+ // We should never get here - this would only be true
+ // if converting float to int, which JUCE handles for us:
+ static_assert(!std::is_integral<InputType>::value &&
+ std::is_integral<TargetType>::value,
+ "Can't convert int to float");
+ } else {
+ // Converting double to float:
+ for (unsigned int i = 0; i < samplesToWrite; i++) {
targetTypeBuffers[c][i] = channels[c][startSample + i];
}
}
@@ -505,11 +509,18 @@ public:
template <typename SampleType>
bool write(const SampleType **channels, int numChannels,
unsigned int numSamples) {
+ printf("in write<%s>(%d, %d)\n",
+ typeid(SampleType).name(), numChannels, numSamples);
if constexpr (std::is_integral<SampleType>::value) {
if constexpr (std::is_same<SampleType, int>::value) {
if (writer->isFloatingPoint()) {
return writeConvertingTo<float>(channels, numChannels, numSamples);
} else {
+ // When supplying int32 data, files come out inverted when writing on
+ // Windows! Try to get to the bottom of this:
+ printf("About to pass %d samples of %d-channel %s data to "
+ "writer->write\n",
+ numSamples, numChannels, typeid(SampleType).name());
return writer->write(channels, numSamples);
}
} else {
diff --git a/tests/test_io.py b/tests/test_io.py
index c6e31e2..5eb3af9 100644
--- a/tests/test_io.py
+++ b/tests/test_io.py
@@ -17,6 +17,7 @@
import os
import glob
+import wave
import pathlib
import shutil
import pytest
@@ -321,6 +322,27 @@ def test_basic_write(
np.testing.assert_allclose(original_audio, np.squeeze(as_written), atol=tolerance)
+def test_write_exact_int32_wav(tmp_path: pathlib.Path):
+ """
+ This test should fail on Windows, where it seems that passing int32 sample
+ data to WriteableAudioFile#write will invert (!?) the samples written.
+ """
+ filename = str(tmp_path / f"test.wav")
+ original = np.array([1, 2, 3, -1, -2, -3]).astype(np.int32)
+ signal = (original << 16).astype(np.int32)
+
+ with pedalboard.io.WriteableAudioFile(filename, samplerate=1) as af:
+ af.write(signal)
+
+ assert os.path.exists(filename)
+
+ # Read the exact wave values out with the `wave` package:
+ with wave.open(filename) as f:
+ assert f.getsampwidth() == 2
+ encoded = np.frombuffer(f.readframes(len(signal)), dtype=np.int16)
+ np.testing.assert_allclose(encoded, original)
+
+
@pytest.mark.parametrize("extension", pedalboard.io.get_supported_write_formats())
@pytest.mark.parametrize("samplerate", [1234.5, 23.0000000001])
def test_fractional_sample_rates(tmp_path: pathlib.Path, extension: str, samplerate):