Skip to content

Commit

Permalink
Add minimal gain plugin example
Browse files Browse the repository at this point in the history
  • Loading branch information
ImJimmi committed Jan 18, 2025
1 parent 6301e11 commit efd6885
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test-runner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ jobs:
-D CMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }} \
-D JIVE_BUILD_TEST_RUNNER=ON \
-D JIVE_BUILD_DEMO_RUNNER=ON \
-D JIVE_BUILD_EXAMPLES=ON \
${{ matrix.additional-cmake-options }}
- name: CMake Build
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ add_subdirectory(jive_layouts)
add_subdirectory(jive_style_sheets)

add_subdirectory(runners)

if (JIVE_BUILD_EXAMPLES)
add_subdirectory(examples)
endif ()
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(gain-plugin)
Empty file added examples/README.md
Empty file.
37 changes: 37 additions & 0 deletions examples/gain-plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
juce_add_plugin(jive-gain-example
PRODUCT_NAME
"JIVE Gain Example"
VERSION
${PROJECT_VERSION}
FORMATS
Standalone
VST3
AU
PLUGIN_MANUFACTURER_CODE
Jive
PLUGIN_CODE
Gain
)

target_sources(jive-gain-example
PUBLIC source/CreatePluginFilter.cpp
source/Processor.h
source/View.h
)

target_compile_definitions(jive-gain-example
PRIVATE JIVE_IS_PLUGIN_PROJECT=1
JIVE_GUI_ITEMS_HAVE_STYLE_SHEETS=1
JUCE_VST3_CAN_REPLACE_VST2=0
)

target_link_libraries(jive-gain-example
PRIVATE jive::compiler_and_linker_options
jive::jive_layouts
jive::jive_style_sheets
juce::juce_audio_devices
juce::juce_audio_utils
juce::juce_recommended_config_flags
juce::juce_recommended_lto_flags
juce::juce_recommended_warning_flags
)
6 changes: 6 additions & 0 deletions examples/gain-plugin/source/CreatePluginFilter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "Processor.h"

[[nodiscard]] juce::AudioProcessor* createPluginFilter()
{
return new jive_example::GainPlugin;
}
135 changes: 135 additions & 0 deletions examples/gain-plugin/source/Processor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#include "View.h"

#include <jive_layouts/jive_layouts.h>
#include <juce_audio_processors/juce_audio_processors.h>

namespace jive_example
{
class GainPlugin : public juce::AudioProcessor
{
public:
const juce::String getName() const final
{
return "JIVE Gain Example";
}

void prepareToPlay(double, int) final
{
if (auto* editor = dynamic_cast<jive::GuiItem*>(getActiveEditor()))
jive::findItemWithID(*editor, "gain-knob")->attachToParameter(apvts.getParameter("gain"), &undoManager);
}

void releaseResources() final
{
sliderAttachment = nullptr;
}

void processBlock(juce::AudioBuffer<float>& buffer, juce::MidiBuffer&) final
{
buffer.applyGain(juce::Decibels::decibelsToGain<float>(gain));
}

double getTailLengthSeconds() const final
{
return 0.0;
}

bool acceptsMidi() const final
{
return false;
}

bool producesMidi() const final
{
return false;
}

juce::AudioProcessorEditor* createEditor() final
{
auto view = getGainPluginView();

if (auto editor = viewInterpreter.interpret(view, this))
{
if (dynamic_cast<juce::AudioProcessorEditor*>(editor.get()))
{
viewInterpreter.listenTo(*editor);
jive::findItemWithID(*editor, "gain-knob")->attachToParameter(apvts.getParameter("gain"), &undoManager);
jive::findItemWithID(*editor, "gain-label")->attachToParameter(apvts.getParameter("gain"), &undoManager);
return dynamic_cast<juce::AudioProcessorEditor*>(editor.release());
}
}

return new juce::GenericAudioProcessorEditor{ *this };
}

bool hasEditor() const final
{
return true;
}

int getNumPrograms() final
{
return 1;
}

int getCurrentProgram() final
{
return 0;
}

void setCurrentProgram(int) final
{
}

const juce::String getProgramName(int) final
{
return "";
}

void changeProgramName(int, const juce::String&) final
{
}

void getStateInformation(juce::MemoryBlock&) final
{
}

void setStateInformation(const void*, int) final
{
}

private:
jive::Interpreter viewInterpreter;

juce::UndoManager undoManager;
juce::AudioProcessorValueTreeState apvts{
*this,
&undoManager,
"APVTS",
[]() -> juce::AudioProcessorValueTreeState::ParameterLayout {
return {
std::make_unique<juce::AudioParameterFloat>(juce::ParameterID{ "gain", 1 },
"Gain",
juce::NormalisableRange<float>{ -12.0f, 12.0f },
0.0f,
juce::AudioParameterFloatAttributes{}
.withStringFromValueFunction([](float value, int) {
const auto maxLength = value < 0 ? 5 : 4;
return juce::String::toDecimalStringWithSignificantFigures(value, 3)
.paddedRight('0', maxLength)
.substring(0, maxLength);
})
.withValueFromStringFunction([](const juce::String& text) {
return text.getFloatValue();
})
.withCategory(juce::AudioProcessorParameter::Category::outputGain)
.withAutomatable(true)),
};
}(),
};
juce::AudioParameterFloat& gain{
dynamic_cast<juce::AudioParameterFloat&>(*apvts.getParameter("gain")),
};
std::unique_ptr<juce::SliderParameterAttachment> sliderAttachment;
};
} // namespace jive_example
71 changes: 71 additions & 0 deletions examples/gain-plugin/source/View.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <jive_core/jive_core.h>

namespace jive_example
{
[[nodiscard]] inline auto GainKnob()
{
return juce::ValueTree{
"Knob",
{
{ "id", "gain-knob" },
{ "width", 80 },
{ "height", 80 },
},
};
}

[[nodiscard]] inline auto GainLabel()
{
return juce::ValueTree{
"Text",
{
{ "id", "gain-label" },
{
"style",
new jive::Object{
{ "foreground", "#4BB1D9" },
{ "font-size", 20 },
{ "letter-spacing", 1.5 },
},
},
},
{
juce::ValueTree{
"Text",
{
{ "text", "dB" },
{
"style",
new jive::Object{
{ "font-weight", "bold" },
},
},
},
},
},
};
}

[[nodiscard]] inline auto getGainPluginView()
{
return juce::ValueTree{
"Editor",
{
{ "width", 400 },
{ "height", 250 },
{ "align-items", "centre" },
{ "justify-content", "centre" },
{
"style",
new jive::Object{
{ "background", "#323D42" },
},
},
},
{
GainKnob(),
GainLabel(),
}
};
}
} // namespace jive_example

0 comments on commit efd6885

Please sign in to comment.