Skip to content

Instantly share code, notes, and snippets.

@darrenmothersele
Created January 26, 2021 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darrenmothersele/f7262e157a65f89af690e6d202a786b8 to your computer and use it in GitHub Desktop.
Save darrenmothersele/f7262e157a65f89af690e6d202a786b8 to your computer and use it in GitHub Desktop.
JUCE Oscillator using Faust DSP
#pragma once
#include "unit2.h"
template <typename Type>
class CustomOscillator
{
public:
CustomOscillator() = default;
void setParams(juce::AudioProcessorValueTreeState& parameters) {
detune1Param = parameters.getRawParameterValue("detune1");
detune2Param = parameters.getRawParameterValue("detune2");
hasParams = true;
}
//==============================================================================
void setFrequency (Type newValue, bool force = false)
{
juce::ignoreUnused(force);
fUI->setParamValue("freq", newValue);
}
//==============================================================================
void setLevel (Type newValue)
{
if (isPrepared) {
fUI->setParamValue("gain", newValue);
}
}
//==============================================================================
void reset() noexcept {
}
//==============================================================================
template <typename ProcessContext>
void process (const ProcessContext& context) noexcept
{
jassert(isPrepared);
jassert(hasParams);
fUI->setParamValue("detune1", *detune1Param);
fUI->setParamValue("detune2", *detune2Param);
auto& outputBlock = context.getOutputBlock();
fDSP->compute((int)outputBlock.getNumSamples(),
nullptr, (FAUSTFLOAT**)fBuffer->getArrayOfWritePointers());
for (size_t i = 0; i < outputBlock.getNumChannels(); i++) {
outputBlock.getSingleChannelBlock (i).copyFrom (*fBuffer);
}
}
void prepare (const juce::dsp::ProcessSpec& spec)
{
fBuffer = std::make_unique<juce::AudioBuffer<FAUSTFLOAT>>(1, spec.maximumBlockSize);
fDSP = std::make_unique<mydsp>();
fDSP->init(static_cast<int>(spec.sampleRate));
fUI = std::make_unique<MapUI>();
fDSP->buildUserInterface(fUI.get());
isPrepared = true;
}
private:
std::unique_ptr<juce::AudioBuffer<FAUSTFLOAT>> fBuffer;
std::unique_ptr<MapUI> fUI;
std::unique_ptr<dsp> fDSP;
bool isPrepared = false;
bool hasParams = false;
std::atomic<float>* detune1Param = nullptr;
std::atomic<float>* detune2Param = nullptr;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment