C++ 简单问题-如何从';类别';在一个单独的';结构';?

C++ 简单问题-如何从';类别';在一个单独的';结构';?,c++,class,variables,struct,juce,C++,Class,Variables,Struct,Juce,我是C++/JUCE新手。我一直在努力让一个基本的synth运行,只是测试一些东西来学习诀窍 它已经很好用了。但我仍在学习C++/JUCE,以及如何声明或访问类/对象/变量 我正试图对我一直坚持的东西进行修改 我有以下内容(只是摘录来演示) 这是设置合成器电平的地方: struct SineWaveVoice : public SynthesiserVoice { SineWaveVoice() {} bool canPlaySound (SynthesiserSound* sound)

我是C++/JUCE新手。我一直在努力让一个基本的synth运行,只是测试一些东西来学习诀窍

它已经很好用了。但我仍在学习C++/JUCE,以及如何声明或访问类/对象/变量

我正试图对我一直坚持的东西进行修改

我有以下内容(只是摘录来演示)

这是设置合成器电平的地方:

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;
假设我想用这个水平滑块变量“targetLevel”乘以上面“struct”中的速度,而不是0.15

我需要在上面键入什么才能访问和使用“targetLevel”?我尝试了多种方法,但我还是不太明白


谢谢

我假设您的正弦波语音位于SynthAudioSource类中,并且该语音是合成器对象的一部分。要从MainContentComponent访问SineWaveVoice结构中的任何内容,您需要通过SynthAudioSource以某种方式公开它。因此,我的建议是在SynthAudioSource中添加如下方法:

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" };
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;
    }
}

但是,请注意,每次获得新的startNote事件时,该值都将被覆盖。因此,您可能应该在SineWaveVoice中存储一个目标级别的副本,并与之相乘,而不是0.15。

每隔一分钟,删除currentLevel、targetLevel等周围的大括号。
control->slider.onValueChange = [this] {
    targetLevel = (float)level.slider.getValue();
    synthAudioSource.setLevel(targetLevel);
};