109 lines
2.7 KiB
C++
109 lines
2.7 KiB
C++
#include "gainbalance.h"
|
|
using Xybrid::Gadgets::GainBalance;
|
|
using namespace Xybrid::Data;
|
|
|
|
#include "nodelib/basics.h"
|
|
using namespace Xybrid::NodeLib;
|
|
|
|
#include "data/porttypes.h"
|
|
|
|
#include "config/pluginregistry.h"
|
|
using namespace Xybrid::Config;
|
|
|
|
#include "audio/audioengine.h"
|
|
using namespace Xybrid::Audio;
|
|
|
|
#include "ui/patchboard/nodeobject.h"
|
|
#include "ui/gadgets/knobgadget.h"
|
|
using namespace Xybrid::UI;
|
|
|
|
#include <cmath>
|
|
|
|
#include <QCborMap>
|
|
|
|
namespace {
|
|
bool _ = PluginRegistry::enqueueRegistration([] {
|
|
auto i = std::make_shared<PluginInfo>();
|
|
i->id = "gadget:gainbalance";
|
|
i->displayName = "Gain/Balance";
|
|
i->category = "Gadget";
|
|
//i->hidden = true;
|
|
i->createInstance = []{ return std::make_shared<GainBalance>(); };
|
|
PluginRegistry::registerPlugin(i);
|
|
//inf = i;
|
|
});
|
|
}
|
|
|
|
GainBalance::GainBalance() {
|
|
|
|
}
|
|
|
|
void GainBalance::init() {
|
|
addPort(Port::Input, Port::Audio, 0);
|
|
addPort(Port::Output, Port::Audio, 0);
|
|
}
|
|
|
|
void GainBalance::process() { // TODO: lerp from tick to tick?
|
|
double g = gain.load();
|
|
double b = balance.load();
|
|
|
|
// calculate multipliers
|
|
double gm = std::pow(10.0, g / 20.0); // dBFS
|
|
double s = (b+1.0) * PI * 0.25;
|
|
double lm = std::cos(s) * PAN_MULT;
|
|
double rm = std::sin(s) * PAN_MULT;
|
|
|
|
auto in = std::static_pointer_cast<AudioPort>(port(Port::Input, Port::Audio, 0));
|
|
auto out = std::static_pointer_cast<AudioPort>(port(Port::Output, Port::Audio, 0));
|
|
in->pull();
|
|
out->pull();
|
|
|
|
size_t ts = audioEngine->curTickSize();
|
|
|
|
for (size_t s = 0; s < ts; s++) {
|
|
out->bufL[s] = static_cast<float>(static_cast<double>(in->bufL[s]) * gm * lm);
|
|
out->bufR[s] = static_cast<float>(static_cast<double>(in->bufR[s]) * gm * rm);
|
|
}
|
|
}
|
|
|
|
void GainBalance::saveData(QCborMap& m) const {
|
|
m.insert(QString("gain"), QCborValue(gain));
|
|
m.insert(QString("balance"), QCborValue(balance));
|
|
}
|
|
|
|
void GainBalance::loadData(const QCborMap& m) {
|
|
gain = m.value("gain").toDouble(0.0);
|
|
balance = m.value("balance").toDouble(0.0);
|
|
}
|
|
|
|
void GainBalance::onGadgetCreated() {
|
|
if (!obj) return;
|
|
|
|
//obj->showName = false;
|
|
//obj->canRename = false;
|
|
obj->showPluginName = false;
|
|
obj->setGadgetSize(8+8*2+32*2, 64);
|
|
|
|
{
|
|
auto k = new KnobGadget(obj->contents);
|
|
k->setPos(8, 16);
|
|
k->min = -60;
|
|
k->max = 6;
|
|
k->step = .1;
|
|
k->bind(gain);
|
|
|
|
k->setLabel("Gain");
|
|
k->fText = [](double d) { return QString("%1dB").arg(d); };
|
|
}
|
|
|
|
{
|
|
auto k = new KnobGadget(obj->contents);
|
|
k->setPos(32+8+8, 16);
|
|
k->min = -1;
|
|
k->max = 1;
|
|
k->bind(balance);
|
|
|
|
k->setLabel("Balance");
|
|
}
|
|
}
|