Hmmm not sure why it’s not working. I’m attaching a working version of the JuceDemoPlugin here:
PluginProcessor.cpp
/*
==============================================================================
This file was auto-generated by the Jucer!
It contains the basic startup code for a Juce application.
==============================================================================
*/
#include "PluginProcessor.h"
#include "PluginEditor.h"
AudioProcessor* JUCE_CALLTYPE createPluginFilter();
//==============================================================================
/** A demo synth sound that's just a basic sine wave.. */
class SineWaveSound : public SynthesiserSound
{
public:
SineWaveSound() {}
bool appliesToNote (int /*midiNoteNumber*/) override { return true; }
bool appliesToChannel (int /*midiChannel*/) override { return true; }
};
//==============================================================================
/** A simple demo synth voice that just plays a sine wave.. */
class SineWaveVoice : public SynthesiserVoice
{
public:
SineWaveVoice()
: angleDelta (0.0),
tailOff (0.0)
{
}
bool canPlaySound (SynthesiserSound* sound) override
{
return dynamic_cast<SineWaveSound*> (sound) != nullptr;
}
void startNote (int midiNoteNumber, float velocity,
SynthesiserSound* /*sound*/,
int /*currentPitchWheelPosition*/) override
{
currentAngle = 0.0;
level = velocity * 0.15;
tailOff = 0.0;
double cyclesPerSecond = MidiMessage::getMidiNoteInHertz (midiNoteNumber);
double cyclesPerSample = cyclesPerSecond / getSampleRate();
angleDelta = cyclesPerSample * 2.0 * double_Pi;
}
void stopNote (float /*velocity*/, bool allowTailOff) override
{
if (allowTailOff)
{
// start a tail-off by setting this flag. The render callback will pick up on
// this and do a fade out, calling clearCurrentNote() when it's finished.
if (tailOff == 0.0) // we only need to begin a tail-off if it's not already doing so - the
// stopNote method could be called more than once.
tailOff = 1.0;
}
else
{
// we're being told to stop playing immediately, so reset everything..
clearCurrentNote();
angleDelta = 0.0;
}
}
void pitchWheelMoved (int /*newValue*/) override
{
// can't be bothered implementing this for the demo!
}
void controllerMoved (int /*controllerNumber*/, int /*newValue*/) override
{
// not interested in controllers in this case.
}
void renderNextBlock (AudioBuffer<float>& outputBuffer, int startSample, int numSamples) override
{
processBlock (outputBuffer, startSample, numSamples);
}
void renderNextBlock (AudioBuffer<double>& outputBuffer, int startSample, int numSamples) override
{
processBlock (outputBuffer, startSample, numSamples);
}
private:
template <typename FloatType>
void processBlock (AudioBuffer<FloatType>& outputBuffer, int startSample, int numSamples)
{
if (angleDelta != 0.0)
{
if (tailOff > 0)
{
while (--numSamples >= 0)
{
const FloatType currentSample =
static_cast<FloatType> (std::sin (currentAngle) * level * tailOff);
for (int i = outputBuffer.getNumChannels(); --i >= 0;)
outputBuffer.addSample (i, startSample, currentSample);
currentAngle += angleDelta;
++startSample;
tailOff *= 0.99;
if (tailOff <= 0.005)
{
clearCurrentNote();
angleDelta = 0.0;
break;
}
}
}
else
{
while (--numSamples >= 0)
{
const FloatType currentSample = static_cast<FloatType> (std::sin (currentAngle) * level);
for (int i = outputBuffer.getNumChannels(); --i >= 0;)
outputBuffer.addSample (i, startSample, currentSample);
currentAngle += angleDelta;
++startSample;
}
}
}
}
double currentAngle, angleDelta, level, tailOff;
};
//==============================================================================
JuceDemoPluginAudioProcessor::JuceDemoPluginAudioProcessor()
: AudioProcessor (getBusesProperties()),
lastUIWidth (400),
lastUIHeight (200),
pluginState (*this, nullptr),
delayPosition (0)
{
lastPosInfo.resetToDefault();
// This creates our parameters. We'll keep some raw pointers to them in this class,
// so that we can easily access them later, but the base class will take care of
// deleting them for us.
// addParameter (gainParam = new AudioParameterFloat ("gain", "Gain", 0.0f, 1.0f, 0.9f));
//addParameter (delayParam = new AudioParameterFloat ("delay", "Delay Feedback", 0.0f, 1.0f, 0.5f));
static const Identifier gainIdentifier ("gain");
static const Identifier delayIdentifier ("delay");
static const Identifier idPropertyID ("id");
static const Identifier valuePropertyID ("value");
pluginState.createAndAddParameter ("gain", "Gain", "Gain", NormalisableRange<float> (0.0f, 1.0f),
0.9f, nullptr, nullptr);
pluginState.createAndAddParameter ("delay", "Delay Feedback", "Delay Feedback",
NormalisableRange<float> (0.0f, 1.0f),
0.5f, nullptr, nullptr);
pluginState.state = ValueTree ("PARAMS");
ValueTree gainTree = pluginState.state.getChildWithProperty (idPropertyID, "gain");
ValueTree delayTree = pluginState.state.getChildWithProperty (idPropertyID, "delay");
gain. referTo (gainTree, valuePropertyID, nullptr);
delay.referTo (delayTree, valuePropertyID, nullptr);
initialiseSynth();
}
JuceDemoPluginAudioProcessor::~JuceDemoPluginAudioProcessor()
{
}
void JuceDemoPluginAudioProcessor::initialiseSynth()
{
const int numVoices = 8;
// Add some voices...
for (int i = numVoices; --i >= 0;)
synth.addVoice (new SineWaveVoice());
// ..and give the synth a sound to play
synth.addSound (new SineWaveSound());
}
//==============================================================================
bool JuceDemoPluginAudioProcessor::isBusesLayoutSupported (const BusesLayout& layouts) const
{
// Only mono/stereo and input/output must have same layout
const AudioChannelSet& mainOutput = layouts.getMainOutputChannelSet();
// input and output layout must be the same
if (wrapperType != wrapperType_Standalone && layouts.getMainInputChannelSet() != mainOutput)
return false;
// do not allow disabling the main buses
if (mainOutput.isDisabled()) return false;
// only allow stereo and mono
if (mainOutput.size() > 2) return false;
return true;
}
AudioProcessor::BusesProperties JuceDemoPluginAudioProcessor::getBusesProperties()
{
// This plug-in should not have any inputs when run as a standalone plug-in
if (PluginHostType::getPluginLoadedAs() == wrapperType_Standalone)
return BusesProperties().withOutput ("Output", AudioChannelSet::stereo(), true);
else
return BusesProperties().withInput ("Input", AudioChannelSet::stereo(), true)
.withOutput ("Output", AudioChannelSet::stereo(), true);
}
//==============================================================================
void JuceDemoPluginAudioProcessor::prepareToPlay (double newSampleRate, int /*samplesPerBlock*/)
{
// Use this method as the place to do any pre-playback
// initialisation that you need..
synth.setCurrentPlaybackSampleRate (newSampleRate);
keyboardState.reset();
if (isUsingDoublePrecision())
{
delayBufferDouble.setSize (2, 12000);
delayBufferFloat.setSize (1, 1);
}
else
{
delayBufferFloat.setSize (2, 12000);
delayBufferDouble.setSize (1, 1);
}
reset();
}
void JuceDemoPluginAudioProcessor::releaseResources()
{
// When playback stops, you can use this as an opportunity to free up any
// spare memory, etc.
keyboardState.reset();
}
void JuceDemoPluginAudioProcessor::reset()
{
// Use this method as the place to clear any delay lines, buffers, etc, as it
// means there's been a break in the audio's continuity.
delayBufferFloat.clear();
delayBufferDouble.clear();
}
template <typename FloatType>
void JuceDemoPluginAudioProcessor::process (AudioBuffer<FloatType>& buffer,
MidiBuffer& midiMessages,
AudioBuffer<FloatType>& delayBuffer)
{
const int numSamples = buffer.getNumSamples();
if (wrapperType == wrapperType_Standalone)
buffer.clear();
// Now pass any incoming midi messages to our keyboard state object, and let it
// add messages to the buffer if the user is clicking on the on-screen keys
keyboardState.processNextMidiBuffer (midiMessages, 0, numSamples, true);
// and now get our synth to process these midi events and generate its output.
synth.renderNextBlock (buffer, midiMessages, 0, numSamples);
// Apply our delay effect to the new output..
applyDelay (buffer, delayBuffer);
if (wrapperType != wrapperType_Standalone)
{
// In case we have more outputs than inputs, we'll clear any output
// channels that didn't contain input data, (because these aren't
// guaranteed to be empty - they may contain garbage).
for (int i = getTotalNumInputChannels(); i < getTotalNumOutputChannels(); ++i)
buffer.clear (i, 0, numSamples);
}
applyGain (buffer, delayBuffer); // apply our gain-change to the outgoing data..
// Now ask the host for the current time so we can store it to be displayed later...
updateCurrentTimeInfoFromHost();
}
template <typename FloatType>
void JuceDemoPluginAudioProcessor::applyGain (AudioBuffer<FloatType>& buffer, AudioBuffer<FloatType>& delayBuffer)
{
ignoreUnused (delayBuffer);
const float gainLevel = gain.get();
for (int channel = 0; channel < getTotalNumOutputChannels(); ++channel)
buffer.applyGain (channel, 0, buffer.getNumSamples(), gainLevel);
}
template <typename FloatType>
void JuceDemoPluginAudioProcessor::applyDelay (AudioBuffer<FloatType>& buffer, AudioBuffer<FloatType>& delayBuffer)
{
const int numSamples = buffer.getNumSamples();
const float delayLevel = delay.get();
int delayPos = 0;
for (int channel = 0; channel < getTotalNumOutputChannels(); ++channel)
{
FloatType* const channelData = buffer.getWritePointer (channel);
FloatType* const delayData = delayBuffer.getWritePointer (jmin (channel, delayBuffer.getNumChannels() - 1));
delayPos = delayPosition;
for (int i = 0; i < numSamples; ++i)
{
const FloatType in = channelData[i];
channelData[i] += delayData[delayPos];
delayData[delayPos] = (delayData[delayPos] + in) * delayLevel;
if (++delayPos >= delayBuffer.getNumSamples())
delayPos = 0;
}
}
delayPosition = delayPos;
}
void JuceDemoPluginAudioProcessor::updateCurrentTimeInfoFromHost()
{
if (AudioPlayHead* ph = getPlayHead())
{
AudioPlayHead::CurrentPositionInfo newTime;
if (ph->getCurrentPosition (newTime))
{
lastPosInfo = newTime; // Successfully got the current time from the host..
return;
}
}
// If the host fails to provide the current time, we'll just reset our copy to a default..
lastPosInfo.resetToDefault();
}
//==============================================================================
AudioProcessorEditor* JuceDemoPluginAudioProcessor::createEditor()
{
return new JuceDemoPluginAudioProcessorEditor (*this);
}
//==============================================================================
void JuceDemoPluginAudioProcessor::getStateInformation (MemoryBlock& destData)
{
ScopedPointer<XmlElement> element (pluginState.state.createXml());
if (element != nullptr)
copyXmlToBinary (*element, destData);
}
void JuceDemoPluginAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
{
// You should use this method to restore your parameters from this memory block,
// whose contents will have been created by the getStateInformation() call.
// This getXmlFromBinary() helper function retrieves our XML from the binary blob..
ScopedPointer<XmlElement> xmlState (getXmlFromBinary (data, sizeInBytes));
if (xmlState != nullptr)
pluginState.state = ValueTree::fromXml (*xmlState);
}
//==============================================================================
// This creates new instances of the plugin..
AudioProcessor* JUCE_CALLTYPE createPluginFilter()
{
return new JuceDemoPluginAudioProcessor();
}
PluginProcessor.h
/*
==============================================================================
This file was auto-generated by the Jucer!
It contains the basic startup code for a Juce application.
==============================================================================
*/
#pragma once
#include "../JuceLibraryCode/JuceHeader.h"
//==============================================================================
/**
As the name suggest, this class does the actual audio processing.
*/
class JuceDemoPluginAudioProcessor : public AudioProcessor
{
public:
//==============================================================================
JuceDemoPluginAudioProcessor();
~JuceDemoPluginAudioProcessor();
//==============================================================================
bool isBusesLayoutSupported (const BusesLayout& layouts) const override;
void prepareToPlay (double sampleRate, int samplesPerBlock) override;
void releaseResources() override;
void reset() override;
//==============================================================================
void processBlock (AudioBuffer<float>& buffer, MidiBuffer& midiMessages) override
{
jassert (! isUsingDoublePrecision());
process (buffer, midiMessages, delayBufferFloat);
}
void processBlock (AudioBuffer<double>& buffer, MidiBuffer& midiMessages) override
{
jassert (isUsingDoublePrecision());
process (buffer, midiMessages, delayBufferDouble);
}
//==============================================================================
bool hasEditor() const override { return true; }
AudioProcessorEditor* createEditor() override;
//==============================================================================
const String getName() const override { return JucePlugin_Name; }
bool acceptsMidi() const override { return true; }
bool producesMidi() const override { return true; }
double getTailLengthSeconds() const override { return 0.0; }
//==============================================================================
int getNumPrograms() override { return 0; }
int getCurrentProgram() override { return 0; }
void setCurrentProgram (int /*index*/) override {}
const String getProgramName (int /*index*/) override { return String(); }
void changeProgramName (int /*index*/, const String& /*name*/) override {}
//==============================================================================
void getStateInformation (MemoryBlock&) override;
void setStateInformation (const void* data, int sizeInBytes) override;
//==============================================================================
// These properties are public so that our editor component can access them
// A bit of a hacky way to do it, but it's only a demo! Obviously in your own
// code you'll do this much more neatly..
// this is kept up to date with the midi messages that arrive, and the UI component
// registers with it so it can represent the incoming messages
MidiKeyboardState keyboardState;
// this keeps a copy of the last set of time info that was acquired during an audio
// callback - the UI component will read this and display it.
AudioPlayHead::CurrentPositionInfo lastPosInfo;
// these are used to persist the UI's size - the values are stored along with the
// filter's other parameters, and the UI component will update them when it gets
// resized.
int lastUIWidth, lastUIHeight;
// Our parameters
AudioProcessorValueTreeState pluginState;
CachedValue<float> gain, delay;
private:
//==============================================================================
template <typename FloatType>
void process (AudioBuffer<FloatType>& buffer, MidiBuffer& midiMessages, AudioBuffer<FloatType>& delayBuffer);
template <typename FloatType>
void applyGain (AudioBuffer<FloatType>&, AudioBuffer<FloatType>& delayBuffer);
template <typename FloatType>
void applyDelay (AudioBuffer<FloatType>&, AudioBuffer<FloatType>& delayBuffer);
AudioBuffer<float> delayBufferFloat;
AudioBuffer<double> delayBufferDouble;
int delayPosition;
Synthesiser synth;
void initialiseSynth();
void updateCurrentTimeInfoFromHost();
static BusesProperties getBusesProperties();
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JuceDemoPluginAudioProcessor)
};
PluginEditor.h
/*
==============================================================================
This file was auto-generated by the Jucer!
It contains the basic startup code for a Juce application.
==============================================================================
*/
#pragma once
#include "../JuceLibraryCode/JuceHeader.h"
#include "PluginProcessor.h"
//==============================================================================
/** This is the editor component that our filter will display.
*/
class JuceDemoPluginAudioProcessorEditor : public AudioProcessorEditor,
private Timer
{
public:
JuceDemoPluginAudioProcessorEditor (JuceDemoPluginAudioProcessor&);
~JuceDemoPluginAudioProcessorEditor();
//==============================================================================
void paint (Graphics&) override;
void resized() override;
void timerCallback() override;
private:
class ParameterSlider;
MidiKeyboardComponent midiKeyboard;
Label timecodeDisplayLabel, gainLabel, delayLabel;
Slider gainSlider, delaySlider;
AudioProcessorValueTreeState::SliderAttachment gainSliderAttachment, delaySliderAttachment;
//==============================================================================
JuceDemoPluginAudioProcessor& getProcessor() const
{
return static_cast<JuceDemoPluginAudioProcessor&> (processor);
}
void updateTimecodeDisplay (AudioPlayHead::CurrentPositionInfo);
};
PluginEditor.cpp
/*
==============================================================================
This file was auto-generated by the Jucer!
It contains the basic startup code for a Juce application.
==============================================================================
*/
#include "PluginProcessor.h"
#include "PluginEditor.h"
//==============================================================================
// This is a handy slider subclass that controls an AudioProcessorParameter
// (may move this class into the library itself at some point in the future..)
class JuceDemoPluginAudioProcessorEditor::ParameterSlider : public Slider,
private Timer
{
public:
ParameterSlider (AudioProcessorParameter& p)
: Slider (p.getName (256)), param (p)
{
setRange (0.0, 1.0, 0.0);
startTimerHz (30);
updateSliderPos();
}
void valueChanged() override
{
if (isMouseButtonDown())
param.setValueNotifyingHost ((float) Slider::getValue());
else
param.setValue ((float) Slider::getValue());
}
void timerCallback() override { updateSliderPos(); }
void startedDragging() override { param.beginChangeGesture(); }
void stoppedDragging() override { param.endChangeGesture(); }
double getValueFromText (const String& text) override { return param.getValueForText (text); }
String getTextFromValue (double value) override { return param.getText ((float) value, 1024); }
void updateSliderPos()
{
const float newValue = param.getValue();
if (newValue != (float) Slider::getValue() && ! isMouseButtonDown())
Slider::setValue (newValue);
}
AudioProcessorParameter& param;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ParameterSlider)
};
//==============================================================================
JuceDemoPluginAudioProcessorEditor::JuceDemoPluginAudioProcessorEditor (JuceDemoPluginAudioProcessor& owner)
: AudioProcessorEditor (owner),
midiKeyboard (owner.keyboardState, MidiKeyboardComponent::horizontalKeyboard),
timecodeDisplayLabel (String()),
gainLabel (String(), "Throughput level:"),
delayLabel (String(), "Delay:"),
gainSlider (owner.pluginState.getParameter ("gain") ->name),
delaySlider (owner.pluginState.getParameter ("delay")->name),
gainSliderAttachment (owner.pluginState, "gain", gainSlider),
delaySliderAttachment (owner.pluginState, "delay", delaySlider)
{
// add some sliders..
addAndMakeVisible (gainSlider);
gainSlider.setSliderStyle (Slider::Rotary);
addAndMakeVisible (delaySlider);
delaySlider.setSliderStyle (Slider::Rotary);
// add some labels for the sliders..
gainLabel.attachToComponent (&gainSlider, false);
gainLabel.setFont (Font (11.0f));
delayLabel.attachToComponent (&delaySlider, false);
delayLabel.setFont (Font (11.0f));
// add the midi keyboard component..
addAndMakeVisible (midiKeyboard);
// add a label that will display the current timecode and status..
addAndMakeVisible (timecodeDisplayLabel);
timecodeDisplayLabel.setColour (Label::textColourId, Colours::blue);
timecodeDisplayLabel.setFont (Font (Font::getDefaultMonospacedFontName(), 15.0f, Font::plain));
// set resize limits for this plug-in
setResizeLimits (400, 200, 800, 300);
// set our component's initial size to be the last one that was stored in the filter's settings
setSize (owner.lastUIWidth,
owner.lastUIHeight);
// start a timer which will keep our timecode display updated
startTimerHz (30);
}
JuceDemoPluginAudioProcessorEditor::~JuceDemoPluginAudioProcessorEditor()
{
}
//==============================================================================
void JuceDemoPluginAudioProcessorEditor::paint (Graphics& g)
{
g.setGradientFill (ColourGradient (Colours::white, 0, 0,
Colours::lightgrey, 0, (float) getHeight(), false));
g.fillAll();
}
void JuceDemoPluginAudioProcessorEditor::resized()
{
// This lays out our child components...
Rectangle<int> r (getLocalBounds().reduced (8));
timecodeDisplayLabel.setBounds (r.removeFromTop (26));
midiKeyboard.setBounds (r.removeFromBottom (70));
r.removeFromTop (30);
Rectangle<int> sliderArea (r.removeFromTop (50));
gainSlider.setBounds (sliderArea.removeFromLeft (jmin (180, sliderArea.getWidth() / 2)));
delaySlider.setBounds (sliderArea.removeFromLeft (jmin (180, sliderArea.getWidth())));
getProcessor().lastUIWidth = getWidth();
getProcessor().lastUIHeight = getHeight();
}
//==============================================================================
void JuceDemoPluginAudioProcessorEditor::timerCallback()
{
updateTimecodeDisplay (getProcessor().lastPosInfo);
}
//==============================================================================
// quick-and-dirty function to format a timecode string
static String timeToTimecodeString (double seconds)
{
const int millisecs = roundToInt (seconds * 1000.0);
const int absMillisecs = std::abs (millisecs);
return String::formatted ("%02d:%02d:%02d.%03d",
millisecs / 360000,
(absMillisecs / 60000) % 60,
(absMillisecs / 1000) % 60,
absMillisecs % 1000);
}
// quick-and-dirty function to format a bars/beats string
static String quarterNotePositionToBarsBeatsString (double quarterNotes, int numerator, int denominator)
{
if (numerator == 0 || denominator == 0)
return "1|1|000";
const int quarterNotesPerBar = (numerator * 4 / denominator);
const double beats = (fmod (quarterNotes, quarterNotesPerBar) / quarterNotesPerBar) * numerator;
const int bar = ((int) quarterNotes) / quarterNotesPerBar + 1;
const int beat = ((int) beats) + 1;
const int ticks = ((int) (fmod (beats, 1.0) * 960.0 + 0.5));
return String::formatted ("%d|%d|%03d", bar, beat, ticks);
}
// Updates the text in our position label.
void JuceDemoPluginAudioProcessorEditor::updateTimecodeDisplay (AudioPlayHead::CurrentPositionInfo pos)
{
MemoryOutputStream displayText;
displayText << "[" << SystemStats::getJUCEVersion() << "] "
<< String (pos.bpm, 2) << " bpm, "
<< pos.timeSigNumerator << '/' << pos.timeSigDenominator
<< " - " << timeToTimecodeString (pos.timeInSeconds)
<< " - " << quarterNotePositionToBarsBeatsString (pos.ppqPosition,
pos.timeSigNumerator,
pos.timeSigDenominator);
if (pos.isRecording)
displayText << " (recording)";
else if (pos.isPlaying)
displayText << " (playing)";
timecodeDisplayLabel.setText (displayText.toString(), dontSendNotification);
}