我通过以下教程学习C ++和JUCE。但是我仍然坚持做简单的事情。以下只是用于基于MIDI数据(简单的正弦合成器)生成正弦波的代码。
在代码中的一点上,正弦波的音量通过乘以一个常数(当前为0.15)来设置。我想乘以级别滑块的值(也就是currentLevel或targetLevel或“ level.slider.getValue()”)
但是,我不知道该怎么做。如何在“ struct SineWaveVoice”内部引用来自MainComponentClass的“ level.slider.getValue()”?
这可能吗?
谢谢
class LabeledSlider : public GroupComponent
{
public:
LabeledSlider (const String& name)
{
setText (name);
setTextLabelPosition (Justification::centredTop);
addAndMakeVisible (slider);
}
void resized() override
{
slider.setBounds (getLocalBounds().reduced (10));
}
Slider slider
{
Slider::RotaryHorizontalVerticalDrag, Slider::TextBoxBelow
};
};
//==============================================================================
//SINE WAVE PROGRAM
//==============================================================================
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; ///************** HERE IS WHERE I WANT TO MULTIPLY BY LEVEL SLIDER *******************************
tailOff = 0.0;
auto cyclesPerSecond = MidiMessage::getMidiNoteInHertz(midiNoteNumber);
auto cyclesPerSample = cyclesPerSecond / getSampleRate();
angleDelta = cyclesPerSample * 2.0 * MathConstants<double>::pi;
}
//==============================================================================
//MAINCONTENTCOMPONENT
//==============================================================================
class MainContentComponent : public AudioAppComponent,
private Timer
{
public:
MainContentComponent()
: synthAudioSource(keyboardState),
keyboardComponent(keyboardState, MidiKeyboardComponent::horizontalKeyboard)
{
LabeledSlider *control = new LabeledSlider("Frequency");
control->slider.onValueChange = [this] { targetFrequency = frequency.slider.getValue(); };
control->slider.setTextBoxStyle(Slider::TextBoxBelow, false, 100, 20);
addAndMakeVisible(knobs.add(control));
control = new LabeledSlider("Level"); //// HERE IS THE LEVEL SLIDER
control->slider.setRange(0.0, 1.0);
control->slider.onValueChange = [this] { targetLevel = (float)level.slider.getValue(); };
addAndMakeVisible(knobs.add(control));
}
private:
double currentSampleRate = 0.0, currentAngle = 0.0, angleDelta = 0.0;
double currentFrequency = 500.0, targetFrequency = 500.0;
float currentLevel = 0.1f, targetLevel = 0.1f;
int rotaryDiam = 100;
LabeledSlider frequency{ "Frequency" };
LabeledSlider level{ "Level" }; //// HERE IS THE LEVEL SLIDER AGAIN
OwnedArray<LabeledSlider> knobs;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent)
};
答案 0 :(得分:0)
在结构内部不能使用类的变量(除非在类内部定义了结构)。
或者,您可以执行的操作是将level.slider.getValue作为函数中的另一个参数发送到调用startNote的位置,即
void startNote(int midiNoteNumber, float velocity,
SynthesiserSound*, int levelSlider, int /*currentPitchWheelPosition*/) override
{
currentAngle = 0.0;
level = velocity * levelSlider; //Mutliply by the parameter levelSlider
tailOff = 0.0;
auto cyclesPerSecond = MidiMessage::getMidiNoteInHertz(midiNoteNumber);
auto cyclesPerSample = cyclesPerSecond / getSampleRate();
angleDelta = cyclesPerSample * 2.0 * MathConstants<double>::pi;
}
LevelSlider应该具有MainContentComponent类的对象中的值。