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

Changes in VST parameters reset to defaults when generating audio from MIDI #339

Closed dariowsz closed 2 days ago

dariowsz commented 3 weeks ago

I was using pedalboard to generate some Massive presets with Python. Every time audio was generated, the parameters I changed would reset to default values and the generated audio was always the same. However, when I changed the same parameters using the UI editor with my_plugin.show_editor() it worked just fine.

To recreate: When I run this code

from mido import Message 
from pedalboard._pedalboard import load_plugin
from scipy.io.wavfile import write

instrument = load_plugin(
    path_to_plugin_file="/Library/Audio/Plug-Ins/VST3/Massive.vst3",
)

instrument.osc1_pitch = -12.0
print(instrument.osc1_pitch)

sample_rate = 44100
audio = instrument(
    [Message("note_on", note=60), Message("note_off", note=60, time=5)],
    duration=5,  # seconds
    sample_rate=sample_rate,
)

print(instrument.osc1_pitch)

I get this output:

-12.03  # osc1_pitch value before generating audio
0.0  # osc1_pitch value after generating audio

However, when I change the value with the editor

from mido import Message 
from pedalboard._pedalboard import load_plugin
from scipy.io.wavfile import write

instrument = load_plugin(
    path_to_plugin_file="/Library/Audio/Plug-Ins/VST3/Massive.vst3",
)

instrument.show_editor()
print(instrument.osc1_pitch)

sample_rate = 44100
audio = instrument(
    [Message("note_on", note=60), Message("note_off", note=60, time=5)],
    duration=5,  # seconds
    sample_rate=sample_rate,
)

print(instrument.osc1_pitch)

I get this output:

-12.0  # osc1_pitch value before generating audio
-12.0  # osc1_pitch value after generating audio
dariowsz commented 3 weeks ago

I see there is a floating-point precision problem because when I input -12.0, it saves it as -12.03 but it seems to be unrelated because I also tried changing the osc1_amp value (where both the direct parameter modification and the editor changes output the same exact value) and the problem persists

dariowsz commented 2 days ago

Just saw there is a reset flag that resets the internal state of the plugin before the instrument generates the sample. It seems that the editor bypasses that reset somehow.

If I change the code to

audio = instrument(
    [Message("note_on", note=60), Message("note_off", note=60, time=5)],
    duration=5,  # seconds
    sample_rate=sample_rate,
    reset=False
)

The problem is solved.

I'm closing the issue and leaving this as documentation in case someone has the same issue.