Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pedalboard/io/ResampledReadableAudioFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class ResampledReadableAudioFile
length -= (std::round(resampler.getOutputLatency()) -
resampler.getOutputLatency());
}
return (long)length;
return (long)std::round(length);
}

double getDuration() const {
Expand Down
11 changes: 7 additions & 4 deletions pedalboard/io/StreamResampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,24 @@ template <typename SampleType = float> class StreamResampler {
targetSampleRate / sourceSampleRate) -
totalSamplesOutput);

// TODO: Don't copy the entire input buffer multiple times here!
int roundedExpectedResampledSamples =
(int)std::round(expectedResampledSamples);

// TODO: Dont copy the entire input buffer multiple times here!
juce::AudioBuffer<SampleType> output(input.getNumChannels(),
(int)expectedResampledSamples);
roundedExpectedResampledSamples);

for (size_t c = 0; c < input.getNumChannels(); c++) {
if (input.getNumSamples() > 0) {
long long inputSamplesConsumed = resamplers[c].process(
resamplerRatio, input.getReadPointer(c), output.getWritePointer(c),
(int)expectedResampledSamples);
roundedExpectedResampledSamples);

if (c == 0) {
if (!isFlushing) {
totalSamplesInput += inputSamplesConsumed;
}
totalSamplesOutput += (int)expectedResampledSamples;
totalSamplesOutput += roundedExpectedResampledSamples;
}

if (!isFlushing) {
Expand Down
14 changes: 10 additions & 4 deletions tests/test_stream_resampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,27 @@
@pytest.mark.parametrize(
"quality", TOLERANCE_PER_QUALITY.keys(), ids=[q.name for q in TOLERANCE_PER_QUALITY.keys()]
)
@pytest.mark.parametrize("num_seconds", [1.0, 1.23])
def test_stream_resample(
fundamental_hz: float,
sample_rate: float,
target_sample_rate: float,
buffer_size: int,
num_channels: int,
quality: Resample.Quality,
num_seconds: float
):
sine_wave = generate_sine_at(
sample_rate,
fundamental_hz,
num_channels=num_channels,
num_seconds=1,
num_seconds=num_seconds,
).astype(np.float32)
expected_sine_wave = generate_sine_at(
target_sample_rate,
fundamental_hz,
num_channels=num_channels,
num_seconds=1,
num_seconds=num_seconds,
).astype(np.float32)
if num_channels == 1:
sine_wave = np.expand_dims(sine_wave, 0)
Expand All @@ -73,8 +75,12 @@ def test_stream_resample(
outputs.append(resampler.process(None))
output = np.concatenate(outputs, axis=1)

num_samples = min(output.shape[1], expected_sine_wave.shape[1])
# In case we have a round number of input and output samples,
# we check that the number of output samples is as expected
if (num_seconds * sample_rate).is_integer() and (num_seconds * target_sample_rate).is_integer():
assert output.shape[1] == expected_sine_wave.shape[1]

num_samples = min(output.shape[1], expected_sine_wave.shape[1])
np.testing.assert_allclose(
expected_sine_wave[:, :num_samples],
output[:, :num_samples],
Expand Down Expand Up @@ -178,7 +184,7 @@ def test_flush(sample_rate: float, target_sample_rate: float, quality: Resample.
@pytest.mark.parametrize(
"quality", TOLERANCE_PER_QUALITY.keys(), ids=[q.name for q in TOLERANCE_PER_QUALITY.keys()]
)
def test_returned_sample_count(
def test_returned_sample_count_from_chunks(
sample_rate: float, target_sample_rate: float, chunk_size: int, quality
):
input_signal = np.linspace(0, 3, num=int(sample_rate), dtype=np.float32)
Expand Down
Loading