spotify / pedalboard

🎛 🔊 A Python library for audio.

Home Page:https://spotify.github.io/pedalboard

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Actual resampled audio length != expected resampled audio length

daniel-deychakiwsky opened this issue · comments

commented

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)