Skip to content
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

Actual resampled audio length != expected resampled audio length #287

Open
daniel-deychakiwsky opened this issue Jan 16, 2024 · 0 comments

Comments

@daniel-deychakiwsky
Copy link

daniel-deychakiwsky commented Jan 16, 2024

I am not getting back the expected audio length in the target sample rate but I do w/ resampy (sinc_window). When I provide a 44_100 source sampled signal, I do get back the expected. When I provide a 48_000 source sampled signal, pedalboard is one sample shy. Any idea why this may be? It is an issue in the code because function expects to get back the expected # of samples under the target sample rate. Here's a snip to reproduce it:

import io
import numpy as np
import soundfile as sf
from pedalboard import Resample
from pedalboard.io import AudioFile, StreamResampler

# equals target_samples
s_sr = 44_100
# does not equal target_samples!
# s_sr = 48_000
t_sr = 22_050
dur = 5

source_samples = s_sr * dur
# source_samples / s_sr = 5.0 in this case so np.round or np.floor doesn't matter
target_samples = int(np.floor((source_samples / s_sr) * t_sr))

noise = np.random.rand(source_samples).astype(np.float32)

audio_filelike = io.BytesIO()

with sf.SoundFile(
    audio_filelike,
    "w",
    channels=1,
    samplerate=s_sr,
    format="WAV",
    subtype="PCM_16",
) as f:
    f.write(noise)
    
audio_filelike.seek(0)

audio = np.copy(sf.SoundFile(audio_filelike, "r").read(dtype="float32"))

resampler = StreamResampler(
    source_sample_rate=s_sr,
    target_sample_rate=t_sr,
    num_channels=1,
    quality=Resample.Quality.WindowedSinc
)
resampled = np.concatenate([resampler.process(audio), resampler.process(None)], axis=1).squeeze()

print(resampled.shape, target_samples)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant