How do I connect a separate component to the audio thread in MainComponent?


Following this tutorial, but all my components are separated out so tutorial is almost useless…

I honestly don’t even know how to word this question, but, here it goes…

I want to create a slider that adjusts the master volume of my synth.

I have a MasterVolume.h and .cpp that will be the component for this. It is inheriting from Component (because I read online that you should only have one AudioAppComponent, which MainComponent is already inheriting.)
In MasterVolume, I have declared in instance of a DecibelSlider class and its constructor has this code…

decibelSlider.setRange (-100, -12);
decibelSlider.setTextBoxStyle (juce::Slider::TextBoxRight, false, 100, 20);
decibelSlider.onValueChange = [this] { level = juce::Decibels::decibelsToGain ((float) decibelSlider.getValue()); };
decibelSlider.setValue (juce::Decibels::gainToDecibels (level));
decibelLabel.setText (“Noise Level in dB”, juce::dontSendNotification);

addAndMakeVisible (decibelSlider);
addAndMakeVisible (decibelLabel);

But in MainComponent’s getNextAudioBlock, I assume I put this code…
auto currentLevel = level;
auto levelScale = currentLevel * 2.0f;

    for (auto channel = 0; channel < bufferToFill.buffer->getNumChannels(); ++channel)
    {
        auto* buffer = bufferToFill.buffer->getWritePointer (channel, bufferToFill.startSample);

        for (auto sample = 0; sample < bufferToFill.numSamples; ++sample)
            buffer[sample] = random.nextFloat() * levelScale - currentLevel;
    }

But, is that right? How would I get my decibel slider to get the value from MainComponent if I am instantiating an instance of MasterVolume inside of MainComponent? Do I pass buffer[sample] to the instance of MasterVolume? Just really confused how to make the slider, which is not inheriting anything from MainComponent, display the audio data that MainComponent is reading in getNextAudioBlock

Also… This forum design is terrrrrrible. Had a heck of a time writing this post with code…Are people more frequent here or reddit?

Take a look at ValueTree. If you use processors, which ain’t your case, you can use AudioProcessorValueTreeState. Those provide a data layer that “interconnect” the audio thread and your main (gui) thread.

About the forum design: you better don’t writte your code here, but just copy from your IDE and paste it here using the “preformatted text” option.

Hmm… I looked it over. It seems very generic. Maybe so much so that I am not really sure how I could use if for my situation. I will need to learn more about it. Would I create a ValueTree then add all the MainComponent and all the other children that will need to access the audio thread? I guess my question is, if I am making a synth, and trying to make everything a component, how do I interconnect everything without getting circular importing, you know? My C++ is a little rusty too, so I could be forgetting something with that.

I just really wish the tutorials actually matched the default project structures (i.e. Audio Application creating Main.cpp and MainComponent, tc) and provided instructions that used components in separate files, instead of the tutorials having everything in one .h file.

Looking over my code. I am thinking that the MasterVolume is going to get rebuild over and over or something… Because, even the .h example doesn’t show any apparent way that the value gets updated. Something is going on behind the scenes that I am not sure of.

Yes I know what you mean: keeping files in .cpp and .h separate helps avoiding “circular” header includes. If you are planning to make a synth maybe try go for an AudioProcessor approach (it can be compiled as a standalone app, not only as a plugin) and you can use AudioProcessorValueTreeState which you have good tutorials here that already deal with all the parameter part you want.
I wouldn’t worry too much though, this is the kind of stuff that may be a bit hard to get in the beggining or to get it running, but once you got it working it’s pretty simple.

IIRC tutorials are a bit old and in need for a review, specially as they may be confusing for newcommers (and they are mainly the target for those). But you can find lots of code in the forum looking for people using the APVTS or having trouble using it and how they fixed the problems.

Thanks very much, John. Forgot that the plugins can run in standalone. I will give that a shot and hopefully have a better time.

I realize that an “Audio Application” may be something that doesn’t even have a UI. So I see why it may not be the best tool for building a synth. Again, their tutorials are a bit misleading. Ah well, part of the learning.