spotify / pedalboard

🎛 🔊 A Python library for audio.
https://spotify.github.io/pedalboard
GNU General Public License v3.0
4.96k stars 249 forks source link

Added an optional audio file parameter to the AudioStream class (issue #264) #317

Open jnitsun opened 2 months ago

jnitsun commented 2 months ago

Enhanced the AudioStream class by introducing an optional parameter to accept an audio file.

Fixed #294.

Made alterations to the AudioStream class that now takes in an optional audio file parameter. If the parameter is not specific, it works as it did before.

psobot commented 2 months ago

Hi @jnitsun! Thanks for this PR - I always appreciate it when folks send in new code to review.

I know this is your first pull request, so forgive the amount of detail below - these are all friendly notes that I hope will be helpful.

I haven't spent much time thinking about a design for this API, but if I were to apply the design principles used for the rest of Pedalboard, I'd suggest a design that allows users to pass audio buffers (i.e.: np.ndarray objects) into an AudioStream for playback:

Here's an example of how someone might use this API, showing how it would be consistent with the existing Pedalboard API:

with AudioFile("my_file.mp3") as f:
    with AudioStream(output_device_name="MacBook Pro Speakers") as stream:
        while f.tell() < f.frames:
            # Read audio from the file, decode it, and write it to the AudioStream for playback in chunks:
            while stream.samples_buffered < 8192:
                stream.write(f.read(1024))
    # Before this print() is called, stream.close() will implicitly be called,
    # which will block until all audio has been played back.
    print("Playback done!")

I know that the Pull Request you've made here will technically work (and has tests - thank you for that!) - but merging it as-is would create an API that is likely to confuse users and doesn't align with the design principles of the existing API. The API I've proposed here is probably going to be a lot of work to implement, so I don't expect you to make all of those changes in this PR unless you're feeling particularly ambitious. 😅