How to update AudioProcessorParameter from midi input?


#1

I want to update AudioProcessorParameters (and therefore, the ValueTree state) from CC messages. HOW can i do this safely?


#2

I guess the safest way is with a callAsync. I think this is how we do it in equator.


#3

Can i please have a little bit more help on how to do that? Thanks!


#4

I can imagine something like this is meant:

void ScoreProcessor::processBlock(AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
{
    if (!midiMessages.isEmpty())
    {
        MidiBuffer::Iterator midiIterator(midiMessages);
        MidiMessage m;
        int midiEventPos, numSamples = buffer.getNumSamples();

        while (midiIterator.getNextEvent(m, midiEventPos) && midiEventPos < numSamples)
        {
            if (m.isControllerOfType(CC_number))
            {
                MessageManager::callAsync([&]
               {
                    myParamToUpdate->setValueNotifyingHost(m.getControllerValue() / 127.0f);
               });
            }
        }
    }
}

#5

This is ok for parameters that are not expected to change much. Better is to cache the value somewhere in the processor (for example by using CachedValue) and then have a Timer on the message thread read the parameters. This way you ensure that you are not thrashing the message thread with tons of callbacks.


#6

thanks very much guys :smiley:


#7

You’re right. And if that wasn’t bad enough, I just found out MessageManager::callAsync([&] also invokes a call to new, which isn’t an advisable thing to do in the processblock. So I hereby withdraw my suggestion above… :slight_smile: