mikeoliphant / AudioPlugSharp

Easily create VST (VST3) audio plugins in C# .NET
MIT License
177 stars 21 forks source link

Host is null when Plugin.Initialize is called #28

Closed adrianmcroft closed 4 months ago

adrianmcroft commented 4 months ago

So, I'm finding my way around the project, using MidiExample. Everything builds and any built vst3 is detected by Ableton and VstHost. Excellent.

I noticed that if I change GainParameter.DefaultValue to non-zero, AddParameter throws an exception at parameter.EditValue =x because Editor.Host is null. I can change it later once Editor.Host is valid perhaps?. My Plugin will require non-zero defaults as it's based on a hardware configuration.

I wonder, when does Editor.Host become 'concrete'. I'm tracing back through, but wondered if it's obvious and I'm missing it.

p.s. Thanks for the excellent project folks....

mikeoliphant commented 4 months ago

Host should be set before Initialize is called.

I just did a quick test with the MidiExample and set a non-zero DefaultValue without any problems.

Have you maybe made some other changes to the MidiExample code that could be causing an issue?

adrianmcroft commented 4 months ago

Thanks Mike. So, yes I didin't deliberately change anything but I do often suffer from over enthusiastic gremlins.

I'll do a fresh git-pull and try again. I really hope that that Isn't it :/

Many thanks...

mikeoliphant commented 4 months ago

I had another look at the code, and I think there actually is an issue - not sure why it didn't cause me a problem with my test.

mikeoliphant commented 4 months ago

Ok, nope - it seems like setting DefaultValue should be fine. An error could occur, though, if you tried to set "EditValue".

adrianmcroft commented 4 months ago

Thanks for looking. So I moved to a different source directory and performed the

git clone --recursive https://github.com/mikeoliphant/AudioPlugSharp cd AudioPlugSharp cd vstbuild cmake.exe -G "Visual Studio 17 2022" -A x64 ../vst3sdk

That built fine, I loaded it all up in 2022 Preview. I ran Build Solution a couple of times to ensure that all the cross references are fulfilled. It runs fine in VstHost -if defauls to the AnyCpu profile. I'm using the bin folder directly as a Plugin Path. That's all perfect.

Then modifying MidiExample.Initialize to `public override void Initialize() { base.Initialize();

OutputPorts = new AudioIOPort[] { monoOutput = new AudioIOPort("Mono Output", EAudioChannelConfiguration.Mono) };

AddParameter(gainParameter = new AudioPluginParameter
{
    ID = "gain",
    Name = "Gain",
    Type = EAudioPluginParameterType.Float,
    MinValue = -20,
    MaxValue = 6,
    DefaultValue = 10,
    ValueFormat = "{0:0.0}dB"
});

}`

virtual void AudioPlugSharpHost::BeginEdit(int parameter) throws a System.NullReferenceException: 'Object reference not set to an instance of an object.' exception as controller is null / zero.

The last line of (AudioPluginBase).AddParameter is parameter.EditValue = parameter.DefaultValue; which will then kick off the call to Editor.Host.BeginEdit.

It's no biggie, I've got tonnes of code to work through before even getting to the the actual build.

mikeoliphant commented 4 months ago

Yeah, when I initially looked at the code, I thought that could potentially be an issue. It might depend on the VST host and slightly different order of operations, or maybe just slight timing differences (since I'm not getting the error here).

I think it is an issue of Initialize() happening before the processor/controller links are set up in the native host code.

If you are up for messing with the c++ code, you could try putting null checks around the "controller" calls here:

https://github.com/mikeoliphant/AudioPlugSharp/blob/fca1f18b355c2514b7931dcf7c77381bbf43f684/AudioPlugSharpVst/AudioPlugSharpHost.h#L183-L196

I'm going to see if I can restructure things a bit to ensure that Initialize() isn't called until the controller link is valid.

mikeoliphant commented 4 months ago

I added the null checks, so the problem should be fixed if you pull the latest from GitHub.

adrianmcroft commented 4 months ago

Ah great, nice work Mike. That sorted it.

Indeed, in my old Vst2 world, I'd get all sorts of odd differences between Daws. Vst3 seems better, but there are always gotchas waiting.

Many thanks.

mikeoliphant commented 4 months ago

👍