I am new to C++/JUCE. I have been working to get a basic synth running and just testing some things out to learn the ropes.
It's working fine already. But I'm still just learning my way around C++/JUCE and how to declare or access classes/objects/variables.
I'm trying to make a modification to 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
I am assuming that your SineWaveVoice lies within your SynthAudioSource class and that the voice is a part of a Synthesizer object. To access anything within your SineWaveVoice struct from MainContentComponent you need to expose this somehow via the SynthAudioSource. So my suggestion is that in your SynthAudioSource add a method like this:
void setLevel(double lvl)
{
for (auto i=0; i<synth.getNumVoices(); i++)
{
SineWaveVoice *v = dynamic_cast<SineWaveVoice *>(synth.getVoice(i));
v->level = lvl * v->velocity;
}
}
Then to access this in your MainContentComponent you just do this:
control->slider.onValueChange = [this] {
targetLevel = (float)level.slider.getValue();
synthAudioSource.setLevel(targetLevel);
};
However, notice that this value will be overwritten every time you get a new startNote event. So you should probably store a copy of target level in your SineWaveVoice and multiply with that rather than 0.15.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.