How to Pass keyBoardState object From PluginEditor to PluginProcessor?

Hello im creating a basic audio plugin, im trying to pass my keyboardState object from PluginEditor to PluginProcessor. Im trying to do it without having these 2 headers include each other, which would be a bad idea. Anytime I add a PluginEditor object as a member to my PluginProcessor class, I include the PluginEditor.h, which causes nasty errors. I need my PluginProcessor to use the keyboardState: Below is a snippet of what im trying to accomplish

      // PluginProcessor.cpp
   void CustomAudioProcessor::processBlock (juce::AudioBuffer<float>& buffer, 
   juce::MidiBuffer& midiMessages)
   {
       juce::ScopedNoDenormals noDenormals;
      auto totalNumInputChannels  = getTotalNumInputChannels();
      auto totalNumOutputChannels = getTotalNumOutputChannels();

      for (auto i = totalNumInputChannels; i < totalNumOutputChannels; ++i)
          buffer.clear (i, 0, buffer.getNumSamples());

     pluginEditorObject.keyboardState.processNextMidiBuffer (midiMessages, 0, numSamples, true);   
}

//PluginProcessor.h
#include "PluginEditor.h"
  class CustomAudioProcessor : public juce::AudioProcessor
{
  public:
    TrapBoxPluginAudioProcessor();
   ~TrapBoxPluginAudioProcessor() override;
 private: 
      PluginEditor pluginEditorObject;
 };

     //PluginEditor.cpp


 CustomAudioProcessorEditor::CustomAudioProcessorEditor(CustomnAudioProcessor& p)
: AudioProcessorEditor(&p), audioProcessor(p),
keyboardComponent(keyboardState, MidiKeyboardComponent::horizontalKeyboard)
   {
        ...
    }
     //PluginEditor.h
      #include "PluginProcessor.h"
       class CustomAudioProcessorEditor : public AudioProcessorEditor
   {
      public:
     CustomAudioProcessorEditor(CustomAudioProcessor&);
     private:
           MidiKeyboardState keyboardState;
  }

Is there a quick way for me to pass my PluginEditor object to PluginProcessor without both headers including each other ?

It works the other way round. The separation of MidiKeyboardState and MidiKeyboardComponent is because the state is needed all the time, while the component is only needed while the editor is open.

The solution is to put the MidiKeyboardState in the processor and connect the MidiKeyboardComponent from the editor. Either make the state a public variable or add an accessor method to your processor like MidiKeybardState& getKeyboardState();

Heres a snippet of what ive modified, I added a accessor method to my PluginProcessor and for some odd reason im getting a juce memory error

   //PluginProcessor.h
     class CustomAudioProcessor : public juce::AudioProcessor
{
  public:
       CustomAudioProcessor();
  private:
      MidiKeyboardState keyBoardState;
      MidiKeyboardState *keyStateBoardPtr;
}

   //PluginProcessor.cpp
   CustomAudioProcessor::CustomAudioProcessor()
{
keyStatePtr = &keyBoardState;
 }
     MidiKeyboardState& 
 CustomAudioProcessor::getMidiKeyboardState(MidiKeyboardState *keyboardState)
 {		
         if(state != nullptr)
              {
	       keyStatePtr = state;
           }
          return *state;
   }

 void CustomAudioProcessor::processBlock (juce::AudioBuffer<float>& buffer, 
             juce::MidiBuffer& midiMessages)
    {
              ...
             keyStatePtr->keyboardState.processNextMidiBuffer (midiMessages, 0, 
             numSamples, true);   
    }

       //PluginEditor.cpp

   CustomAudioProcessorEditor::CustomAudioProcessorEditor(CustomnAudioProcessor& p)
 : AudioProcessorEditor(&p), audioProcessor(p),
 keyboardComponent(keyboardState, MidiKeyboardComponent::horizontalKeyboard)
        {
          p.getMidiKeyboardState(&keyboardState);
        }

Yeah, sorry. That’s not how it works.

Here’s the relevant part in AudioProcessor:

class CustomAudioProcessor : public juce::AudioProcessor
{
public:
    CustomAudioProcessor();

    juce::MidiKeyboardState& getKeyboardState()
    {
        return keyboardState;
    }

    void processBlock (juce::AudioBuffer<float>& buffer,  juce::MidiBuffer& midiMessages)
    {
        keyboardState.processNextMidiBuffer (midiMessages, 
                                             0, buffer.getNumSamples(), 
                                             true);   
        // ...
    }
private:
    juce::MidiKeyboardState keyboardState;
};

And connect to the state in the editor:

CustomAudioProcessorEditor::CustomAudioProcessorEditor (CustomnAudioProcessor& p)
  : AudioProcessorEditor (&p), audioProcessor (p),
    keyboardComponent (audioProcessor.getKeyboardState(),
                       juce::MidiKeyboardComponent::horizontalKeyboard)
{
    // ...
}

No need for additional pointers and stuff. In your code you were using keyboardState in your editor uninitialised.

Hope that helps

1 Like

Got it, thanks so much Daniel,…so pretty much if i need any other objects from the Processor I would need to create a accessor method and use it the other Class or vice versa. sweet!