Skip to content

Instantly share code, notes, and snippets.

@ffAudio
Created June 20, 2017 10:47
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ffAudio/4ed4d0d8d04a7e5e8214aad11c73cadf to your computer and use it in GitHub Desktop.
Save ffAudio/4ed4d0d8d04a7e5e8214aad11c73cadf to your computer and use it in GitHub Desktop.
A simple JUCE plugin that saves text in float parameters
/*
==============================================================================
PluginEditor.cpp
==============================================================================
*/
#include "PluginProcessor.h"
#include "PluginEditor.h"
//==============================================================================
TextPluginAudioProcessorEditor::TextPluginAudioProcessorEditor (TextPluginAudioProcessor& p)
: AudioProcessorEditor (&p), processor (p), updating (false)
{
// Make sure that before the constructor has finished, you've set the
// editor's size to whatever you need it to be.
addAndMakeVisible (label = new Label());
label->setText (processor.getTextValue ("myAutomatedText"), dontSendNotification);
label->setEditable (true);
label->addListener (this);
setSize (400, 300);
}
TextPluginAudioProcessorEditor::~TextPluginAudioProcessorEditor()
{
}
//==============================================================================
void TextPluginAudioProcessorEditor::paint (Graphics& g)
{
// (Our component is opaque, so we must completely fill the background with a solid colour)
g.fillAll (getLookAndFeel().findColour (ResizableWindow::backgroundColourId));
}
void TextPluginAudioProcessorEditor::resized()
{
// This is generally where you'll want to lay out the positions of any
// subcomponents in your editor..
label->setBounds (getLocalBounds());
}
void TextPluginAudioProcessorEditor::labelTextChanged (Label *labelThatHasChanged)
{
if (!updating) {
updating = true;
processor.setTextValue ("myAutomatedText", labelThatHasChanged->getText());
updating = false;
}
}
void TextPluginAudioProcessorEditor::parameterChanged (const String &parameterID, float newValue)
{
if (!updating) {
updating = true;
label->setText (processor.getTextValue("myAutomatedText"), dontSendNotification);
updating = false;
}
}
/*
==============================================================================
PluginEditor.h
==============================================================================
*/
#pragma once
#include "../JuceLibraryCode/JuceHeader.h"
#include "PluginProcessor.h"
//==============================================================================
/**
*/
class TextPluginAudioProcessorEditor : public AudioProcessorEditor,
public AudioProcessorValueTreeState::Listener,
public Label::Listener
{
public:
TextPluginAudioProcessorEditor (TextPluginAudioProcessor&);
~TextPluginAudioProcessorEditor();
//==============================================================================
void paint (Graphics&) override;
void resized() override;
void labelTextChanged (Label *labelThatHasChanged) override;
void parameterChanged (const String &parameterID, float newValue) override;
private:
// This reference is provided as a quick way for your editor to
// access the processor object that created it.
TextPluginAudioProcessor& processor;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TextPluginAudioProcessorEditor)
ScopedPointer<Label> label;
bool updating;
};
/*
==============================================================================
TextPluginAudioProcessor.cpp
==============================================================================
*/
#include "PluginProcessor.h"
#include "PluginEditor.h"
//==============================================================================
TextPluginAudioProcessor::TextPluginAudioProcessor()
: AudioProcessor (BusesProperties()
#if ! JucePlugin_IsMidiEffect
#if ! JucePlugin_IsSynth
.withInput ("Input", AudioChannelSet::stereo(), true)
#endif
.withOutput ("Output", AudioChannelSet::stereo(), true)
#endif
),
parms (*this, nullptr)
{
parms.createAndAddParameter("myAutomatedText",
"Text",
String(),
NormalisableRange<float> (0.0f, 1.0f),
1.0f,
[](float value)
{
String text;
for (int i=0; i<32; ++i) {
long column = static_cast<long> (value * pow (100.0, i)) % 100;
if (column > 0 && column < 95)
text.append (String::charToString (column + 32), 1);
}
return text;
},
[](const String& text)
{
float value = 0.0;
for (int i=0; i<text.length(); ++i) {
value += (text [i] - 32) / pow (100.0, i+1);
}
return value;
});
parms.state = ValueTree ("TextPlugin");
}
TextPluginAudioProcessor::~TextPluginAudioProcessor()
{
}
void TextPluginAudioProcessor::setTextValue (const StringRef paramID, const StringRef text)
{
if (auto parameter = parms.getParameter(paramID)) {
float value = 0.0;
for (int i=0; i<text.length(); ++i) {
value += (text [i] - 32) / pow (100.0, i+1);
}
parameter->setValueNotifyingHost (value);
}
}
String TextPluginAudioProcessor::getTextValue (const StringRef paramID)
{
if (auto parameter = parms.getParameter(paramID)) {
float value = parameter->getValue();
String text;
for (int i=0; i<32; ++i) {
long column = static_cast<long> (value * pow (100.0, i)) % 100;
if (column > 0 && column < 95)
text.append (String::charToString (column + 32), 1);
}
return text;
}
return "";
}
//==============================================================================
const String TextPluginAudioProcessor::getName() const
{
return JucePlugin_Name;
}
bool TextPluginAudioProcessor::acceptsMidi() const
{
#if JucePlugin_WantsMidiInput
return true;
#else
return false;
#endif
}
bool TextPluginAudioProcessor::producesMidi() const
{
#if JucePlugin_ProducesMidiOutput
return true;
#else
return false;
#endif
}
double TextPluginAudioProcessor::getTailLengthSeconds() const
{
return 0.0;
}
int TextPluginAudioProcessor::getNumPrograms()
{
return 1; // NB: some hosts don't cope very well if you tell them there are 0 programs,
// so this should be at least 1, even if you're not really implementing programs.
}
int TextPluginAudioProcessor::getCurrentProgram()
{
return 0;
}
void TextPluginAudioProcessor::setCurrentProgram (int index)
{
}
const String TextPluginAudioProcessor::getProgramName (int index)
{
return {};
}
void TextPluginAudioProcessor::changeProgramName (int index, const String& newName)
{
}
//==============================================================================
void TextPluginAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
{
// Use this method as the place to do any pre-playback
// initialisation that you need..
}
void TextPluginAudioProcessor::releaseResources()
{
// When playback stops, you can use this as an opportunity to free up any
// spare memory, etc.
}
#ifndef JucePlugin_PreferredChannelConfigurations
bool TextPluginAudioProcessor::isBusesLayoutSupported (const BusesLayout& layouts) const
{
#if JucePlugin_IsMidiEffect
ignoreUnused (layouts);
return true;
#else
// This is the place where you check if the layout is supported.
// In this template code we only support mono or stereo.
if (layouts.getMainOutputChannelSet() != AudioChannelSet::mono()
&& layouts.getMainOutputChannelSet() != AudioChannelSet::stereo())
return false;
// This checks if the input layout matches the output layout
#if ! JucePlugin_IsSynth
if (layouts.getMainOutputChannelSet() != layouts.getMainInputChannelSet())
return false;
#endif
return true;
#endif
}
#endif
void TextPluginAudioProcessor::processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
{
const int totalNumInputChannels = getTotalNumInputChannels();
const int totalNumOutputChannels = getTotalNumOutputChannels();
// In case we have more outputs than inputs, this code clears any output
// channels that didn't contain input data, (because these aren't
// guaranteed to be empty - they may contain garbage).
// This is here to avoid people getting screaming feedback
// when they first compile a plugin, but obviously you don't need to keep
// this code if your algorithm always overwrites all the output channels.
for (int i = totalNumInputChannels; i < totalNumOutputChannels; ++i)
buffer.clear (i, 0, buffer.getNumSamples());
// This is the place where you'd normally do the guts of your plugin's
// audio processing...
for (int channel = 0; channel < totalNumInputChannels; ++channel)
{
float* channelData = buffer.getWritePointer (channel);
// ..do something to the data...
}
}
//==============================================================================
bool TextPluginAudioProcessor::hasEditor() const
{
return true; // (change this to false if you choose to not supply an editor)
}
AudioProcessorEditor* TextPluginAudioProcessor::createEditor()
{
TextPluginAudioProcessorEditor* editor = new TextPluginAudioProcessorEditor (*this);
parms.addParameterListener ("myAutomatedText", editor);
return editor;
}
//==============================================================================
void TextPluginAudioProcessor::getStateInformation (MemoryBlock& destData)
{
// You should use this method to store your parameters in the memory block.
// You could do that either as raw data, or use the XML or ValueTree classes
// as intermediaries to make it easy to save and load complex data.
MemoryOutputStream stream(destData, false);
parms.state.writeToStream (stream);
}
void TextPluginAudioProcessor::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.
ValueTree tree = ValueTree::readFromData (data, sizeInBytes);
if (tree.isValid()) {
parms.state = tree;
}
}
//==============================================================================
// This creates new instances of the plugin..
AudioProcessor* JUCE_CALLTYPE createPluginFilter()
{
return new TextPluginAudioProcessor();
}
/*
==============================================================================
PluginProcessor.h
==============================================================================
*/
#pragma once
#include "../JuceLibraryCode/JuceHeader.h"
//==============================================================================
/**
*/
class TextPluginAudioProcessor : public AudioProcessor
{
public:
//==============================================================================
TextPluginAudioProcessor();
~TextPluginAudioProcessor();
void setTextValue (const StringRef paramID, const StringRef text);
String getTextValue (const StringRef paramID);
//==============================================================================
void prepareToPlay (double sampleRate, int samplesPerBlock) override;
void releaseResources() override;
#ifndef JucePlugin_PreferredChannelConfigurations
bool isBusesLayoutSupported (const BusesLayout& layouts) const override;
#endif
void processBlock (AudioSampleBuffer&, MidiBuffer&) override;
//==============================================================================
AudioProcessorEditor* createEditor() override;
bool hasEditor() const override;
//==============================================================================
const String getName() const override;
bool acceptsMidi() const override;
bool producesMidi() const override;
double getTailLengthSeconds() const override;
//==============================================================================
int getNumPrograms() override;
int getCurrentProgram() override;
void setCurrentProgram (int index) override;
const String getProgramName (int index) override;
void changeProgramName (int index, const String& newName) override;
//==============================================================================
void getStateInformation (MemoryBlock& destData) override;
void setStateInformation (const void* data, int sizeInBytes) override;
private:
//==============================================================================
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TextPluginAudioProcessor)
AudioProcessorValueTreeState parms;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment