Skip to content

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

It works?

Peter Sobot973bb43c3d50
diff --git a/pedalboard/io/ResampledReadableAudioFile.h b/pedalboard/io/ResampledReadableAudioFile.h
index cebf306..63af50b 100644
--- a/pedalboard/io/ResampledReadableAudioFile.h
+++ b/pedalboard/io/ResampledReadableAudioFile.h
@@ -263,17 +263,15 @@ public:
"is %d in source\n",
targetPosition, targetPositionInSourceSampleRate);
- // targetPositionInSourceSampleRate -=
- // inputBufferSizeFor(resampler.getQuality());
- // printf("Moving seek position back by %d samples to account "
- // "for input buffer, now %d\n",
- // inputBufferSizeFor(resampler.getQuality()),
- // targetPositionInSourceSampleRate);
-
- long long maximumOverflow =
- (long long)std::ceil(resampler.getSourceSampleRate() /
- resampler.getTargetSampleRate()) *
- 2;
+ targetPositionInSourceSampleRate -=
+ inputBufferSizeFor(resampler.getQuality());
+ printf("Moving seek position back by %d samples to account "
+ "for input buffer, now %d\n",
+ inputBufferSizeFor(resampler.getQuality()),
+ targetPositionInSourceSampleRate);
+
+ long long maximumOverflow = (long long)std::ceil(
+ resampler.getSourceSampleRate() / resampler.getTargetSampleRate());
targetPositionInSourceSampleRate -= std::max(0LL, maximumOverflow);
printf("Moving seek position back by %d samples to account "
"for maximum overflow, now %d\n",
diff --git a/tests/test_resampled_io.py b/tests/test_resampled_io.py
index de3de61..388f147 100644
--- a/tests/test_resampled_io.py
+++ b/tests/test_resampled_io.py
@@ -14,6 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
+import time
import pytest
from pedalboard import Resample
from pedalboard.io import AudioFile, StreamResampler, ResampledReadableAudioFile
@@ -138,6 +139,27 @@ def test_seek_resampled(sample_rate: float, target_sample_rate: float, chunk_siz
np.testing.assert_allclose(expected, actual)
[email protected]("sample_rate", [8000, 11025, 22050, 44100, 48000])
[email protected]("target_sample_rate", [8000, 11025, 12345.67, 22050, 44100, 48000])
[email protected]("quality", QUALITIES)
+def test_seek_resampled_is_constant_time(sample_rate: float, target_sample_rate: float, quality):
+ signal = np.linspace(1, sample_rate, sample_rate).astype(np.float32)
+
+ read_buffer = BytesIO()
+ read_buffer.name = "test.wav"
+ with AudioFile(read_buffer, "w", sample_rate, 1, bit_depth=32) as f:
+ f.write(signal)
+
+ with AudioFile(BytesIO(read_buffer.getvalue())).resampled_to(target_sample_rate, quality) as f:
+ timings = []
+ for i in range(f.frames):
+ a = time.time()
+ f.seek(i)
+ b = time.time()
+ timings.append(b - a)
+ assert np.std(timings) < 0.1
+
+
@pytest.mark.parametrize("sample_rate", [8000, 11025, 22050, 44100, 48000])
@pytest.mark.parametrize("target_sample_rate", [8000, 11025, 12345.67, 22050, 44100, 48000])
@pytest.mark.parametrize("chunk_size", [1000])