我正在使用JUCE框架来创建音频插件。我创建了一个Knob类,该类继承自Component类。我的Knob类包含对SliderLabel的引用。

在我的AudioProcessorEditor类中,我初始化了几个Knob对象。但是,在运行时看不到Component。我是在做错什么还是错过了一步?

我尝试直接在Slider类内部声明LabelAudioProcessorEditor对象。当我这样做时,我可以在运行时成功看到SliderLabel对象。因此,我感到该问题涉及我的Knob类。

旋钮

#pragma once
#include "../JuceLibraryCode/JuceHeader.h"

class Knob : public Component
{
public:
    Knob(String, AudioProcessorEditor*);
    ~Knob();

    void paint (Graphics&) override;
    void resized() override;

    String Name;
    Label *KnobLabel;
    Slider *KnobSlider;
    AudioProcessorEditor *APE;

private:
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Knob)
};

旋钮

#include "Knob.h"

Knob::Knob(String name, AudioProcessorEditor *ape)
{
    // In your constructor, you should add any child components, and
    // initialise any special settings that your component needs.

    this->Name = name;

    APE = ape;

    KnobLabel = new Label(name);
    KnobLabel->setColour(0x1000281, Colours::antiquewhite);
    KnobLabel->setAlwaysOnTop(true);
    KnobLabel->setFont(10);

    KnobSlider = new Slider();
    KnobSlider->setAlwaysOnTop(true);

    addAndMakeVisible(KnobLabel);
    addAndMakeVisible(KnobSlider);
}

void Knob::paint (Graphics& g)
{
    /* This demo code just fills the component's background and
     draws some placeholder text to get you started.

     You should replace everything in this method with your own
     drawing code..
     */
    g.setColour(Colours::white);
    g.fillAll();
}

void Knob::resized()
{
    // This method is where you should set the bounds of any child
    // components that your component contains..

    auto bounds = getLocalBounds();

    KnobSlider->setBounds(bounds.removeFromTop(getHeight()*8));
    KnobLabel->setBounds(bounds);
}

插件编辑器

#pragma once

#include "../JuceLibraryCode/JuceHeader.h"
#include "PluginProcessor.h"
#include "Knob.h"
#include "Model.h"

class MoonlightAudioProcessorEditor  : public AudioProcessorEditor
{
public:
    MoonlightAudioProcessorEditor (MoonlightAudioProcessor&);
    ~MoonlightAudioProcessorEditor();

    void paint (Graphics&) override;
    void resized() override;

    Knob *OrbitKnob;
    Knob *SpaceKnob;
    Knob *InertiaKnob;

    void ConfigureUI();

private:
    OwnedArray<Knob> Knobs;
    ComponentBoundsConstrainer ResizeBounds;
    ResizableCornerComponent *Resizer;
    MoonlightAudioProcessor& processor;
};

插件编辑器

#include "PluginProcessor.h"
#include "PluginEditor.h"

MoonlightAudioProcessorEditor::MoonlightAudioProcessorEditor (MoonlightAudioProcessor& p)
: AudioProcessorEditor (&p), processor (p)
{
    setSize (400, 300);
    ConfigureUI();
}

void MoonlightAudioProcessorEditor::ConfigureUI()
{
    OrbitKnob = new Knob("Orbit", this);
    SpaceKnob = new Knob("Space", this);
    InertiaKnob = new Knob("Inertia", this);

    Knobs.add(OrbitKnob);
    Knobs.add(SpaceKnob);
    Knobs.add(InertiaKnob);

    for (Knob *knob : Knobs)
    {
        knob->KnobSlider->addListener(this);
        addAndMakeVisible(knob);
        knob->setAlwaysOnTop(true);
    }

    ResizeBounds.setSizeLimits(DEFAULT_WIDTH,
                                DEFAULT_HEIGHT,
                                MAX_WIDTH,
                                MAX_HEIGHT);
    Resizer = new ResizableCornerComponent(this, &ResizeBounds);
    addAndMakeVisible(Resizer);

    setSize(processor._UIWidth, processor._UIHeight);
}

void MoonlightAudioProcessorEditor::paint (Graphics& g)
{
    g.setColour (Colours::black);
    g.fillAll();
}

void MoonlightAudioProcessorEditor::resized()
{
    int width = getWidth();
    int height = getHeight();

    auto bounds = getLocalBounds();
    auto graphicBounds = bounds.removeFromTop(height*.8);
    auto orbitBounds = bounds.removeFromLeft(width/3);
    auto spaceBounds = bounds.removeFromLeft(width/3);

    if (OrbitKnob != nullptr)
    {
        OrbitKnob->setBounds(orbitBounds);
    }

    if (SpaceKnob != nullptr)
    {
        SpaceKnob->setBounds(spaceBounds);
    }

    if (InertiaKnob != nullptr)
    {
        InertiaKnob->setBounds(bounds);
    }

    if (Resizer != nullptr)
    {
        Resizer->setBounds(width - 16, height - 16, 16, 16);
    }

    processor._UIWidth = width;
    processor._UIHeight = height;
}

另外,我一直在使用JUCE提供的AudioPluginHost应用程序来测试我的插件。很多时候,应用程序会因分段错误而崩溃。然后,我重建插件而不进行任何更改,它将正常工作。

最佳答案

您忽略了Component::Paint(g)中对Knob::Paint(Graphics& g)的 call

10-06 01:13