spotify / pedalboard

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

Is threading supported for `VST3Plugin` instances? #278

Closed csteinmetz1 closed 7 months ago

csteinmetz1 commented 10 months ago

I followed the example in #212 to use ThreadPoolExecutor but I find that this does not work for me when using VST3 plugin objects. It does work using the internal Pedalboard effects such as Reverb() in your example. When I call the below example it will hang indefinitely. Is there a limitation with using VST3Plugin instances here? I couldn't find a mention in the docs.

Here is a minimal example:

from concurrent.futures import ThreadPoolExecutor

audio_segment = np.random.randn(2, 524288)
sample_rate = 48000

futures = []  
with ThreadPoolExecutor(max_workers=16) as executor:
   futures.append(
    executor.submit(
        pedalboard.load_plugin("MyPlugin.vst3").process,
        audio_segment,
        sample_rate,
    )
  )   
   for future in futures:
    print(future.result())
psobot commented 10 months ago

Hey Christian! Great question - I have a feeling this might be related to https://github.com/spotify/pedalboard/issues/268, which I haven't had time to dig into yet. VSTs certainly support pumping the audio callback from multiple threads (as that's what DAWs do) but we might not be properly handling a lock somewhere. I'll do a bit of digging.

psobot commented 7 months ago

Hey @csteinmetz1 - after a hectic couple months away, I've just merged a fix that should allow you to run VST3Plugin instances in background threads. Note that this is fairly experimental and may break for some plugins; the VST3 spec doesn't allow us to call some methods (like prepareToPlay) from threads other than the main thread, but Pedalboard does anyways. It's worked for every VST3 I've tried, but there will definitely be exceptions.

This change is part of v0.8.8, which has just entered CI and should be live on PyPI within a couple of hours.