Beginner Question: Accessing a variable from another Class inside a Struct?


#1

I have been working to get a basic synth running from this FlexBox setup and the MIDI Synth tutorial.

It’s working fine already. And while it’s just an early test so none of the audio code will be used long term (I have other plans), I’m still just learning my way around C++/JUCE and how to declare or access classes/objects/variables.

I’m trying to do something that I’m stuck with.

I have the following (just excerpts to demonstrate)…

This is where the synthesizer level is set:

struct SineWaveVoice   : public SynthesiserVoice
{
SineWaveVoice() {}

bool canPlaySound (SynthesiserSound* sound) override
{
    return dynamic_cast<SineWaveSound*> (sound) != nullptr;
}

void startNote (int midiNoteNumber, float velocity,
                SynthesiserSound*, int /*currentPitchWheelPosition*/) override
{
    currentAngle = 0.0;
    level = velocity * 0.15;
    tailOff = 0.0;

Ie. Level is set by velocity * 0.15.

In my test set up, I already have a level knob defined under MainContentComponent like this:

class MainContentComponent :	public AudioAppComponent,
								private Timer

{
public:
    MainContentComponent()
		: synthAudioSource(keyboardState),
		keyboardComponent(keyboardState, MidiKeyboardComponent::horizontalKeyboard)

	{
   		LabeledSlider* control = new LabeledSlider("Frequency");
		control->slider.setRange(20.0, 20000.0);
		control->slider.setSkewFactorFromMidPoint(500.0);
		control->slider.setNumDecimalPlacesToDisplay(1);
		control->slider.setValue(currentFrequency, dontSendNotification);
		control->slider.onValueChange = [this] { targetFrequency = frequency.slider.getValue(); };
		control->slider.setTextBoxStyle(Slider::TextBoxBelow, false, 100, 20);
		control->slider.setRange(50.0, 5000.0);
		control->slider.setSkewFactorFromMidPoint(500.0);
		control->slider.setNumDecimalPlacesToDisplay(1);
		addAndMakeVisible(knobs.add(control));

		control = new LabeledSlider("Level");
		control->slider.setRange(0.0, 1.0);
		control->slider.onValueChange = [this] { targetLevel = (float)level.slider.getValue(); };
		addAndMakeVisible(knobs.add(control));

....
private:
{

float currentLevel = 0.1f, targetLevel = 0.1f;
	LabeledSlider level{ "Level" };

So let’s say I want to use this level slider variable “targetLevel” to be multiplied by the velocity in the “struct” above instead of 0.15.

What do I need to type up there to be able to access and use “targetLevel”? I tried multiple things but I can’t quite figure it out.

Thanks


#2

The easiest way would be to create a method within SineWaveVoice…maybe call it setTargetLevel()

void setTargetLevel (float level)
{
    targetLevel = level;
}

Now you just need to call this within your sliderValueChanged() for each voice.