Unexplainable behaviour (to me) in process(), MIDI noteOn event output

DSP, Plugin and Host development discussion.
Post Reply New Topic
RELATED
PRODUCTS

Post

AUTO-ADMIN: Non-MP3, WAV, OGG, SoundCloud, YouTube, Vimeo, Twitter and Facebook links in this post have been protected automatically. Once the member reaches 5 posts the links will function as normal.
Dear developer community :)

(I've posted this over at the Steinberg Forum, I'm re-posting this here in the hopes that someone may be able to help)

I've written a very basic VST instrument (plain VST SDK 3.7.11) that just periodically outputs a MIDI noteOn event. I'd like to emphasise that the code below is working :) (beside the behaviour I'm observing)

I have an unused local variable double FOOBAR_1 in the code.

(*) Now to the peculiar behaviour: when I comment the unused local variable FOOBAR_1 (so it is not in the process() function anymore), the VST instrument stops outputting MIDI noteOn events, or at least I'm not receiving any MIDI noteOn events in Cubase.

(I've attached the Xcode debugger to Cubase and in fact see that the lines creating the Vst::Event event; and eventListOutput->addEvent(event); etc. do in fact still get perfectly executed. Just no MIDI noteOn events arriving in the host.)

I'm completely at a loss why this would happen?! Does anybody of you have an idea in what direction I should investigate this or how this could be solved? For reference, I'm using Xcode on an ARM Mac.

Thank you for your help and all the best!

Here's the process() code for reference:

Code: Select all (#)

tresult PLUGIN_API TestProcessor::process (Vst::ProcessData& data)
{

    int bufferSize = data.numSamples;

    // (!) Why does commenting this unused local variable cause event output to stop working?
    double FOOBAR_1 = -1;
	
    // Event input. Does nothing in this example
    Vst::IEventList* eventListInput = data.inputEvents;
    if (eventListInput) {
        int32 numEvent = eventListInput->getEventCount ();
        for (int32 i = 0; i < numEvent; i++) {
            Vst::Event event;
            if (eventListInput->getEvent (i, event) == kResultOk) {
            }
        }
    }

    // Event output. Periodically outputs a noteOn event
    if (bufferSize > 0) {

        if (samplesToNextBeat < bufferSize) {

            Vst::IEventList* eventListOutput = data.outputEvents;

            if (eventListOutput) {

                Vst::Event event;
                event.type = Vst::Event::kNoteOnEvent;
                event.noteOn.channel = 1;
                event.noteOn.pitch = 60;
                event.noteOn.tuning = 0.f;
                event.noteOn.velocity = 0.75f;
                event.noteOn.noteId = 1;
                event.sampleOffset = samplesToNextBeat;
                eventListOutput->addEvent(event);
            }
            samplesToNextBeat += 22050;
        }
        samplesToNextBeat -= bufferSize;
    }

    // Unchanged project generator example code to output silence on the audio out
    // ...
}

Post

hyprcbe wrote: Sat May 18, 2024 4:40 pm (*) Now to the peculiar behaviour: when I comment the unused local variable FOOBAR_1 (so it is not in the process() function anymore), the VST instrument stops outputting MIDI noteOn events, or at least I'm not receiving any MIDI noteOn events in Cubase.
Usually when you see this kind of behaviour what you want to do is either run your program under valgrind/Dr.Memory/etc ... or recompile with address sanitizer.

Completely non-sensical bugs (happens sometimes randomly, irrelevant code changes behaviour, release vs. debug changes behaviour) have reasonable probability of being memory errors and often those are somewhere else in your code base. It could be in the misbehaving function, but it could equally well be anywhere else.

Post

Hi @mystran

Thank you so much for taking the time to answer and provide some directions for investigation, very much appreciated. The kind of memory errors you mention is something I'm not yet familiar with, so I'll have to read up on this :)

What I forgot to mention: the behaviour happens with a plain VST Instrument created with the VST3 Project Generator of the VST SDK 3.7.11., the example I provided is really the most minimal example that already reproduces the issue (there's no other code written by myself than what I posted).

I've further investigated regarding DAW: the behaviour happens in Cubase 12 (as soon as I comment the unused local variable FOOBAR_1, no MIDI noteOn output), but not in Live 12 (MIDI noteOn output continues to work). Does this give any more clue about what kind of problem this may be?

All the best

Post

hyprcbe wrote: Sun May 19, 2024 4:59 am Thank you so much for taking the time to answer and provide some directions for investigation, very much appreciated. The kind of memory errors you mention is something I'm not yet familiar with, so I'll have to read up on this :)
C++ does not check that a pointer is valid before it lets you access the memory. C++ does not check that an array index is in bounds when you access the array. C++ does not check (though compiler warnings sometimes do if you enable enough of them) that a variable has been initialized before it's used for something (and if it isn't then it contains random data).

So you basically trash the whole program memory pretty badly by doing invalid memory accesses (eg. invalid pointers, index out of bounds, etc) and it might not crash immediately if your accesses end up in memory that is still mapped (ie. "valid" from the point of the operating system).

Memory checkers can help with this by actually tracking what is or isn't valid. They tend to come with a moderate to heavy performance penalty, because such tracking is expensive, but when you suspect that type of bug they can often tell you the actual bad access that resulted in the problem, rather than the point (perhaps much later in execution) where the corruption from that bad access results in something weird or a crash.

Post

Thank you @mystran

Yes, these are concepts I'm familiar with :) What I'm much less familiar with is a tool chain to pinpoint and investigate non-obvious / peculiar behaviour as I encountered with the VST Project Generator template and rather simple code above.

I mean, it's working perfectly fine both in Ableton Live and Cubase, and continues to work in Live when I comment the unused variable FOOBAR_1, but in Cubase, MIDI noteOn events just stop being output (but when debugging, I still hit the corresponding code!). I wouldn't even know where to start investigating, as my code is so simple.

My hope is also that others have encountered similar weird behaviour when working with a plain plugin created with the VST SDK and the VST Project Generator. I mean, when such behaviour occurs in such simple code, there must be others who have run into the same issue?

Or there's some obvious mistake in what I've written, which would be also a likely explanation :)

All the best

Post Reply

Return to “DSP and Plugin Development”