Giter VIP home page Giter VIP logo

Comments (3)

psobot avatar psobot commented on July 21, 2024 1

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]
)

from pedalboard.

psobot avatar psobot commented on July 21, 2024

Hi @shuZro!

Yes, PyDub segments can be read in as samples with the get_array_of_samples function, which can then be converted into a floating-point audio array:

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 AudioSegment objects into memory, which is both slow and wasteful. If you have an audio file on disk or in memory, use pedalboard.io.AudioFile to treat the file just like a regular Python open file object instead:

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

from pedalboard.

shuZro avatar shuZro commented on July 21, 2024

@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:

audio = effect_board(audio, samplerate)

    return AudioSegment(
        audio.tobytes(),
        sample_width=4,
        frame_rate=samplerate,
        channels=2
    )

Also my original audio was an int16 bit audio. So if the output could be in that format.
Tried this too but the audio is silent.

a = np.array(audio, dtype=np.int16)
    new = AudioSegment(
        a.tobytes(),
        sample_width=2,
        frame_rate=samplerate,
        channels=2
    )

from pedalboard.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.