我正在尝试从 Juce 学习 C++ 并构建一个音频合成器。
我有一个合成器可以从振荡器类 (MaxiOsc
) 输出音频,简而言之:
class SynthVoice : public SynthesiserVoice
{
private:
MaxiOsc testOsc;
double frequency = 0; // frequency is then changed by another function by new notes from the keyboard of course ...
public:
double oscOutput()
{
return testOsc.sinewave(frequency);
}
void renderNextBlock (AudioBuffer <float> &outputBuffer, int startSample, int numSamples) override
{
for (int sample = 0; sample < numSamples; ++sample)
{
for (int channel = 0; channel < outputBuffer.getNumChannels(); ++channel)
{
outputBuffer.addSample(channel, startSample, oscOutput() //MAIN OUTPUT HERE
}
++startSample;
}
}
我正在尝试创建一个名为 ModalUnit
的新类,它继承自 MaxiOsc
并在其中创建一个 MaxiOsc
对象。这个想法是在上面的合成器代码中使用 ModalUnit
代替 MaxiOsc
并最终向 ModalUnit
类代码添加额外的处理。
这是我的 ModalUnit
类:
class ModalUnit : public MaxiOsc
{
public:
ModalUnit()
{
}
double getOutput(double frequency)
{
output = testOsc.sinewave(frequency);
return output;
}
private:
MaxiOsc testOsc;
double output = 0.0;
};
但是当我尝试在合成器代码中实现它时,它不会输出任何音频:
class SynthVoice : public SynthesiserVoice
{
private:
ModalUnit testModalUnit;
double frequency = 0; // frequency is then changed by another function by new notes from the keyboard of course ...
public:
double modalUnitOutput()
{
return testModalUnit.getOutput(frequency);
}
void renderNextBlock (AudioBuffer <float> &outputBuffer, int startSample, int numSamples) override
{
for (int sample = 0; sample < numSamples; ++sample)
{
for (int channel = 0; channel < outputBuffer.getNumChannels(); ++channel)
{
outputBuffer.addSample(channel, startSample, modalUnitOutput() //MAIN OUTPUT HERE
}
++startSample;
}
}
我在这个新类(class)创建中做错了什么?我基本上只希望 ModalUnit
类在其中创建 MaxiOsc
,然后从此开始输出。
我该怎么做或如何解决这个问题?谢谢。
最佳答案
我相信正在发生的事情是您既继承了 class MaxiOSc
又拥有它的成员。继承类时,父类的所有成员现在都是子类的成员。通过包含父类的对象,这些成员被复制。
在 ModalUnit
的 double getOutput(double frequency)
函数中,您使用 MaxiOsc
类型的成员对象进行输出。同时,当调用从 MaxiOsc
继承的方法到 ModalUnit
时,将使用继承的成员。
关于c++ - 创建一个继承自另一个类的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53455250/