Skip to content

Instantly share code, notes, and snippets.

@olilarkin
Created January 24, 2019 19:03
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 olilarkin/4886aaf034de2b7a9434e72a4a6991fc to your computer and use it in GitHub Desktop.
Save olilarkin/4886aaf034de2b7a9434e72a4a6991fc to your computer and use it in GitHub Desktop.
Demo drawing something based on BPM
#include "IPlugBeatSyncColors.h"
#include "IPlug_include_in_plug_src.h"
#include "IControls.h"
IPlugBeatSyncColors::IPlugBeatSyncColors(IPlugInstanceInfo instanceInfo)
: IPLUG_CTOR(kNumParams, kNumPrograms, instanceInfo)
{
GetParam(kGain)->InitDouble("Gain", 100., 0., 100.0, 0.01, "%");
mMakeGraphicsFunc = [&]() {
return MakeGraphics(*this, PLUG_WIDTH, PLUG_HEIGHT, PLUG_FPS, 1.);
};
mLayoutFunc = [&](IGraphics* pGraphics) {
pGraphics->AttachCornerResizer(kUIResizerScale, false);
pGraphics->AttachPanelBackground(COLOR_GRAY);
pGraphics->LoadFont(ROBOTTO_FN);
pGraphics->AttachControl(new ILambdaControl(pGraphics->GetBounds(),
[&](ILambdaControl* pCaller, IGraphics& g, IRECT& r) {
if(mCount != mPrevCount) {
mStr.SetFormatted(32, "%i", mCount);
dynamic_cast<IPanelControl*>(g.GetBackgroundControl())->SetPattern(IColor::GetRandomColor());
}
g.DrawText(IText(100), mStr.Get(), r);
mPrevCount = mCount;
}, 100, true, true));
};
mSyncThingy.setBeatScalar(4.);
mSyncThingy.setFunc([&]() {
mCount++;
});
}
void IPlugBeatSyncColors::OnReset()
{
mSyncThingy.setSampleRate(GetSampleRate());
}
void IPlugBeatSyncColors::ProcessBlock(sample** inputs, sample** outputs, int nFrames)
{
const double gain = GetParam(kGain)->Value() / 100.;
const int nChans = NOutChansConnected();
mSyncThingy.process(mTimeInfo.mPPQPos);
for (int s = 0; s < nFrames; s++) {
for (int c = 0; c < nChans; c++) {
outputs[c][s] = inputs[c][s] * gain;
}
}
}
#pragma once
#include "IPlug_include_in_plug_hdr.h"
#include <functional>
const int kNumPrograms = 1;
enum EParams
{
kGain = 0,
kNumParams
};
class SyncThingy
{
private:
double mPhaseStep; // (1./sr);
double mPhase = 0.; // float phase (goes between 0. and 1.)
double mPrevPhase = 1.; // for delta calc
double mBeatScalar = 1.;
std::function<void()> mFunc = nullptr;
public:
void setSampleRate(double sr) { mPhaseStep = 1./sr; }
inline void setBeatScalar(double scalar) { mBeatScalar = scalar; }
void setFunc(std::function<void()> func) { mFunc = func; }
void process(double ppqPos) {
if(mFunc) {
auto wrap = [](double x, double lo = 0., double hi = 1.) {
while (x >= hi) x -= hi;
while (x < lo) x += hi - lo;
return x;
};
double oneOverBS = 1./mBeatScalar;
// set phase
double beatPhase = std::fmod(ppqPos, oneOverBS) / oneOverBS;
mPhase = wrap(beatPhase);
if ((mPhase - mPrevPhase) < -0.5)
mFunc();
mPrevPhase = mPhase;
}
}
};
class IPlugBeatSyncColors : public IPlug
{
public:
IPlugBeatSyncColors(IPlugInstanceInfo instanceInfo);
void ProcessBlock(sample** inputs, sample** outputs, int nFrames) override;
void OnReset() override;
private:
SyncThingy mSyncThingy;
int mCount = 0;
int mPrevCount = 0;
WDL_String mStr;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment