-
Notifications
You must be signed in to change notification settings - Fork 260
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
Can I load a PyDub Audio Segment and pass it through pedalboard? #322
Comments
Hi @shuZro! Yes, PyDub segments can be read in as samples with the import numpy as np
import pydub
seg = pydub.AudioSegment.from_ogg("foobar.ogg")
array = seg.get_array_of_samples()
# Convert to NumPy
np_array = np.array(array)
# Convert to floating-point:
float_array = np_array / max(abs(np.iinfo(np_array.dtype).min), abs(np.iinfo(np_array.dtype).max))
# Convert from interlaced data to (num_channels, num_samples)
audio = float_array.reshape([-1, seg.channels]).T
samplerate = seg.frame_rate
# Now just use audio and samplerate to interact with Pedalboard APIs! ...but I would not recommend doing this. PyDub is a convenient framework, but requires loading entire from pedalboard.io import AudioFile
with AudioFile("foobar.ogg") as f:
audio = f.read(f.samplerate * 10) # read 10 seconds
f.seek(f.samplerate * 60 * 2) # seek to the 2-minute mark
audio = f.read(f.samplerate * 10) # read from 2:00 to 2:10 |
@psobot Thanks! One other question. I wanted to convert the output from pedalboard to an Audio Segment. But when doing so it gets all distorted. Any ideas? Here is a snippet:
Also my original audio was an int16 bit audio. So if the output could be in that format.
|
You can convert a 32-bit floating-point audio buffer (what Pedalboard uses) to a 16-bit signed interleaved integer representation by doing the opposite of what's done in the code above: audio: np.NDArray[np.float32] = ...
target_dtype = np.int16
# Convert to fixed-point by scaling to the maximum value of an int and then converting to int:
int_array = (audio * min(abs(np.iinfo(target_dtype).min), abs(np.iinfo(target_dtype).max))).astype(target_dtype)
# Switch from split-channel (num_channels, num_samples) to interleaved (num_samples, num_channels):
interleaved_int_array = int_array.T
# ...and pack into an AudioSegment:
seg = AudioSegment(
interleaved_int_array.tobytes(),
sample_width=np.iinfo(target_dtype).bits // 8,
frame_rate=samplerate,
channels=interleaved_int_array.shape[0]
) |
Can I load a PyDub Audio Segment and pass it through pedalboard?
The text was updated successfully, but these errors were encountered: