[英]Simple issue - How to access a variable from a 'class' inside a separate 'struct'?
I am new to C++/JUCE. 我是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.
但是我仍然只是在学习关于C ++ / JUCE的方法,以及如何声明或访问类/对象/变量。
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. IE浏览器。 Level is set by velocity * 0.15.
级别由速度* 0.15设置。
In my test set up, I already have a level knob defined under MainContentComponent like this: 在测试设置中,我已经在MainContentComponent下定义了一个电平旋钮,如下所示:
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. 因此,假设我要使用此级别滑块变量“ targetLevel”乘以上面“结构”中的速度,而不是0.15。
What do I need to type up there to be able to access and use “targetLevel”? 我需要在那里输入什么才能访问和使用“ 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. 我假设您的SineWaveVoice位于SynthAudioSource类中,并且声音是Synthesizer对象的一部分。 To access anything within your SineWaveVoice struct from MainContentComponent you need to expose this somehow via the SynthAudioSource.
要从MainContentComponent访问SineWaveVoice结构中的任何内容,您都需要通过SynthAudioSource以某种方式公开此内容。 So my suggestion is that in your SynthAudioSource add a method like this:
因此,我的建议是,在您的SynthAudioSource中添加如下方法:
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: 然后要在MainContentComponent中访问它,只需执行以下操作:
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. 但是,请注意,每次获取新的startNote事件时,该值都会被覆盖。 So you should probably store a copy of target level in your SineWaveVoice and multiply with that rather than 0.15.
因此,您可能应该在SineWaveVoice中存储目标级别的副本,然后将其乘以而不是0.15。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.