Download

What is the point of SynthCommand?

I’m referring to the method USynthComponent::SynthCommand. In the documentation, it just says “Method to execute parameter changes on game thread in audio render thread”, whatever that means.

I’m asking because so far I’ve been able to create custom USynthComponents without using this, and it works fine. For example :

void FMySynthComponent::SetFrequency(float NoteFrequency)
{
    SynthCommand([this, NoteFrequency]()
        {
            SineWaveGenerator.SetFrequency(NoteFrequency);
        });
}

Seems to works just as well as

void FMySynthComponent::SetFrequency(float NoteFrequency)
{
    SineWaveGenerator.SetFrequency(NoteFrequency);
}

The only guide on creating custom Synths that I found uses the first version, so while the second version works I’m worried that I’m not doing it the right way. However I feel like a piece of code doesn’t seem to do anything, it shouldn’t be there. Can anyone help?

It queues commands from the game thread to the audio thread so everything happens in the right order/right timeIt is one of the key features of USynthComponent. Changing variables from the game thread without it can cause some problems. If it is a simple parameter change you can probably get away with it. But it is definitely needed when things get more complicated. So probably always best to use it.

An example is if you are changing multiple audio parameters across a number of blueprints the changes will execute at different times in the audio thread. Maybe not even in the same audio block different variables will change at different times - so unpredictable results. If you use synthcommand the changes will be queued until the end of the game frame and applied at the same point in the audio thread giving a predictable result.