Browse Source

Initial commit

tags/v0.4.0
Andrew Belt 5 years ago
commit
69aaa86ed1
23 changed files with 1394 additions and 0 deletions
  1. +13
    -0
      LICENSE-dist.txt
  2. +7
    -0
      LICENSE.txt
  3. +4
    -0
      Makefile
  4. +5
    -0
      README.md
  5. BIN
      res/ADSR.png
  6. BIN
      res/DejaVuSansMono.ttf
  7. BIN
      res/Delay.png
  8. BIN
      res/SEQ3.png
  9. BIN
      res/Scope.png
  10. BIN
      res/VCA.png
  11. BIN
      res/VCF.png
  12. BIN
      res/VCMixer.png
  13. BIN
      res/VCO.png
  14. +128
    -0
      src/ADSR.cpp
  15. +110
    -0
      src/Delay.cpp
  16. +35
    -0
      src/Fundamental.cpp
  17. +49
    -0
      src/Fundamental.hpp
  18. +182
    -0
      src/SEQ3.cpp
  19. +297
    -0
      src/Scope.cpp
  20. +81
    -0
      src/VCA.cpp
  21. +172
    -0
      src/VCF.cpp
  22. +89
    -0
      src/VCMixer.cpp
  23. +222
    -0
      src/VCO.cpp

+ 13
- 0
LICENSE-dist.txt View File

@@ -0,0 +1,13 @@
# kissfft

Copyright (c) 2003-2010 Mark Borgerding

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

+ 7
- 0
LICENSE.txt View File

@@ -0,0 +1,7 @@
Copyright (c) 2016 Andrew Belt

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

+ 4
- 0
Makefile View File

@@ -0,0 +1,4 @@

SOURCES = $(wildcard src/*.cpp) kissfft/kiss_fft.c

include ../../Makefile-plugin.inc

+ 5
- 0
README.md View File

@@ -0,0 +1,5 @@
# Fundamental

The Fundamental plugin pack gives you a basic foundation to create simple synthesizers, route and analyze signals, complement other more complicated modules, and build some not-so-simple patches using brute force (lots of modules).
They are also a great reference for creating your own plugins in C++.


BIN
res/ADSR.png View File

Before After
Width: 120  |  Height: 380  |  Size: 4.6KB

BIN
res/DejaVuSansMono.ttf View File


BIN
res/Delay.png View File

Before After
Width: 120  |  Height: 380  |  Size: 4.4KB

BIN
res/SEQ3.png View File

Before After
Width: 330  |  Height: 380  |  Size: 6.5KB

BIN
res/Scope.png View File

Before After
Width: 195  |  Height: 380  |  Size: 5.2KB

BIN
res/VCA.png View File

Before After
Width: 90  |  Height: 380  |  Size: 4.2KB

BIN
res/VCF.png View File

Before After
Width: 120  |  Height: 380  |  Size: 5.1KB

BIN
res/VCMixer.png View File

Before After
Width: 150  |  Height: 380  |  Size: 7.9KB

BIN
res/VCO.png View File

Before After
Width: 150  |  Height: 380  |  Size: 6.2KB

+ 128
- 0
src/ADSR.cpp View File

@@ -0,0 +1,128 @@
#include "Fundamental.hpp"


struct ADSR : Module {
enum ParamIds {
ATTACK_PARAM,
DECAY_PARAM,
SUSTAIN_PARAM,
RELEASE_PARAM,
NUM_PARAMS
};
enum InputIds {
ATTACK_INPUT,
DECAY_INPUT,
SUSTAIN_INPUT,
RELEASE_INPUT,
GATE_INPUT,
TRIG_INPUT,
NUM_INPUTS
};
enum OutputIds {
ENVELOPE_OUTPUT,
NUM_OUTPUTS
};

bool decaying = false;
float env = 0.0;
SchmittTrigger trigger;
float lights[4] = {};

ADSR();
void step();
};


ADSR::ADSR() {
params.resize(NUM_PARAMS);
inputs.resize(NUM_INPUTS);
outputs.resize(NUM_OUTPUTS);

trigger.setThresholds(0.0, 1.0);
}

void ADSR::step() {
float attack = clampf(params[ATTACK_INPUT] + getf(inputs[ATTACK_INPUT]) / 10.0, 0.0, 1.0);
float decay = clampf(params[DECAY_PARAM] + getf(inputs[DECAY_INPUT]) / 10.0, 0.0, 1.0);
float sustain = clampf(params[SUSTAIN_PARAM] + getf(inputs[SUSTAIN_INPUT]) / 10.0, 0.0, 1.0);
float release = clampf(params[RELEASE_PARAM] + getf(inputs[RELEASE_PARAM]) / 10.0, 0.0, 1.0);

// Lights
lights[0] = 2.0*attack - 1.0;
lights[1] = 2.0*decay - 1.0;
lights[2] = 2.0*sustain - 1.0;
lights[3] = 2.0*release - 1.0;

// Gate and trigger
bool gated = getf(inputs[GATE_INPUT]) >= 1.0;
if (trigger.process(getf(inputs[TRIG_INPUT])))
decaying = false;

const float base = 20000.0;
const float maxTime = 10.0;
if (gated) {
if (decaying) {
// Decay
env += powf(base, 1 - decay) / maxTime * (sustain - env) / gSampleRate;
}
else {
// Attack
// Skip ahead if attack is all the way down (infinitely fast)
if (attack < 1e-4) {
env = 1.0;
}
else {
env += powf(base, 1 - attack) / maxTime * (1.001 - env) / gSampleRate;
}
if (env >= 1.0) {
env = 1.0;
decaying = true;
}
}
}
else {
// Release
env += powf(base, 1 - release) / maxTime * (0.0 - env) / gSampleRate;
decaying = false;
}

setf(outputs[ENVELOPE_OUTPUT], 10.0 * env);
}


ADSRWidget::ADSRWidget() {
ADSR *module = new ADSR();
setModule(module);
box.size = Vec(15*8, 380);

{
Panel *panel = new LightPanel();
panel->box.size = box.size;
panel->backgroundImage = Image::load("plugins/Fundamental/res/ADSR.png");
addChild(panel);
}

addChild(createScrew<ScrewSilver>(Vec(15, 0)));
addChild(createScrew<ScrewSilver>(Vec(box.size.x-30, 0)));
addChild(createScrew<ScrewSilver>(Vec(15, 365)));
addChild(createScrew<ScrewSilver>(Vec(box.size.x-30, 365)));

addParam(createParam<Davies1900hBlackKnob>(Vec(62, 57), module, ADSR::ATTACK_PARAM, 0.0, 1.0, 0.5));
addParam(createParam<Davies1900hBlackKnob>(Vec(62, 124), module, ADSR::DECAY_PARAM, 0.0, 1.0, 0.5));
addParam(createParam<Davies1900hBlackKnob>(Vec(62, 191), module, ADSR::SUSTAIN_PARAM, 0.0, 1.0, 0.5));
addParam(createParam<Davies1900hBlackKnob>(Vec(62, 257), module, ADSR::RELEASE_PARAM, 0.0, 1.0, 0.5));

addInput(createInput<PJ301MPort>(Vec(9, 63), module, ADSR::ATTACK_INPUT));
addInput(createInput<PJ301MPort>(Vec(9, 129), module, ADSR::DECAY_INPUT));
addInput(createInput<PJ301MPort>(Vec(9, 196), module, ADSR::SUSTAIN_INPUT));
addInput(createInput<PJ301MPort>(Vec(9, 263), module, ADSR::RELEASE_INPUT));

addInput(createInput<PJ301MPort>(Vec(9, 320), module, ADSR::GATE_INPUT));
addInput(createInput<PJ301MPort>(Vec(48, 320), module, ADSR::TRIG_INPUT));
addOutput(createOutput<PJ301MPort>(Vec(87, 320), module, ADSR::ENVELOPE_OUTPUT));

addChild(createValueLight<SmallLight<GreenRedPolarityLight>>(Vec(94, 41), &module->lights[0]));
addChild(createValueLight<SmallLight<GreenRedPolarityLight>>(Vec(94, 108), &module->lights[1]));
addChild(createValueLight<SmallLight<GreenRedPolarityLight>>(Vec(94, 175), &module->lights[2]));
addChild(createValueLight<SmallLight<GreenRedPolarityLight>>(Vec(94, 241), &module->lights[3]));
}

+ 110
- 0
src/Delay.cpp View File

@@ -0,0 +1,110 @@
#include "Fundamental.hpp"


#define HISTORY_SIZE (1<<21)

struct Delay : Module {
enum ParamIds {
TIME_PARAM,
FEEDBACK_PARAM,
COLOR_PARAM,
MIX_PARAM,
NUM_PARAMS
};
enum InputIds {
TIME_INPUT,
FEEDBACK_INPUT,
COLOR_INPUT,
MIX_INPUT,
IN_INPUT,
NUM_INPUTS
};
enum OutputIds {
OUT_OUTPUT,
NUM_OUTPUTS
};

DoubleRingBuffer<float, HISTORY_SIZE> historyBuffer;
DoubleRingBuffer<float, 16> outBuffer;
SampleRateConverter<1> src;
float lastIndex = 0.0;

Delay();

void step();
};


Delay::Delay() {
params.resize(NUM_PARAMS);
inputs.resize(NUM_INPUTS);
outputs.resize(NUM_OUTPUTS);
}

void Delay::step() {
// Compute delay time
float delay = 1e-3 * powf(10.0 / 1e-3, clampf(params[TIME_PARAM] + getf(inputs[TIME_INPUT]) / 10.0, 0.0, 1.0));
float index = delay * gSampleRate;
// lastIndex = crossf(lastIndex, index, 0.001);

// Read the history
int consume = (int)(historyBuffer.size() - index);
// printf("wanted: %f\tactual: %d\tdiff: %d\tratio: %f\n", index, historyBuffer.size(), consume, index / historyBuffer.size());
if (outBuffer.empty()) {
if (consume > 0) {
int inFrames = consume;
int outFrames = outBuffer.capacity();
// printf("\t%d\t%d\n", inFrames, outFrames);
src.setRatioSmooth(index / historyBuffer.size());
src.process((const Frame<1>*)historyBuffer.startData(), &inFrames, (Frame<1>*)outBuffer.endData(), &outFrames);
historyBuffer.startIncr(inFrames);
outBuffer.endIncr(outFrames);
// printf("\t%d\t%d\n", inFrames, outFrames);
// if (historyBuffer.size() >= index)
// outBuffer.push(historyBuffer.shift());
}
}

float out = 0.0;
if (!outBuffer.empty()) {
out = outBuffer.shift();
}

// Write the history
float in = getf(inputs[IN_INPUT]);
historyBuffer.push(in + out * clampf(params[FEEDBACK_PARAM] + getf(inputs[FEEDBACK_INPUT]) / 10.0, 0.0, 1.0));
out = crossf(in, out, clampf(params[MIX_PARAM] + getf(inputs[MIX_INPUT]) / 10.0, 0.0, 1.0));

setf(outputs[OUT_OUTPUT], out);
}


DelayWidget::DelayWidget() {
Delay *module = new Delay();
setModule(module);
box.size = Vec(15*8, 380);

{
Panel *panel = new LightPanel();
panel->box.size = box.size;
panel->backgroundImage = Image::load("plugins/Fundamental/res/Delay.png");
addChild(panel);
}

addChild(createScrew<ScrewSilver>(Vec(15, 0)));
addChild(createScrew<ScrewSilver>(Vec(box.size.x-30, 0)));
addChild(createScrew<ScrewSilver>(Vec(15, 365)));
addChild(createScrew<ScrewSilver>(Vec(box.size.x-30, 365)));

addParam(createParam<Davies1900hBlackKnob>(Vec(67, 57), module, Delay::TIME_PARAM, 0.0, 1.0, 0.5));
addParam(createParam<Davies1900hBlackKnob>(Vec(67, 123), module, Delay::FEEDBACK_PARAM, 0.0, 1.0, 0.5));
addParam(createParam<Davies1900hBlackKnob>(Vec(67, 190), module, Delay::COLOR_PARAM, -1.0, 1.0, 0.0));
addParam(createParam<Davies1900hBlackKnob>(Vec(67, 257), module, Delay::MIX_PARAM, 0.0, 1.0, 0.5));

addInput(createInput<PJ301MPort>(Vec(14, 63), module, Delay::TIME_INPUT));
addInput(createInput<PJ301MPort>(Vec(14, 129), module, Delay::FEEDBACK_INPUT));
addInput(createInput<PJ301MPort>(Vec(14, 196), module, Delay::COLOR_INPUT));
addInput(createInput<PJ301MPort>(Vec(14, 263), module, Delay::MIX_INPUT));
addInput(createInput<PJ301MPort>(Vec(14, 320), module, Delay::IN_INPUT));
addOutput(createOutput<PJ301MPort>(Vec(73, 320), module, Delay::OUT_OUTPUT));
}

+ 35
- 0
src/Fundamental.cpp
File diff suppressed because it is too large
View File


+ 49
- 0
src/Fundamental.hpp View File

@@ -0,0 +1,49 @@
#include "rack.hpp"


using namespace rack;


extern float sawTable[2048];
extern float triTable[2048];


////////////////////
// module widgets
////////////////////

struct VCOWidget : ModuleWidget {
VCOWidget();
};

struct VCFWidget : ModuleWidget {
VCFWidget();
};

struct VCAWidget : ModuleWidget {
VCAWidget();
};

struct DelayWidget : ModuleWidget {
DelayWidget();
};

struct ADSRWidget : ModuleWidget {
ADSRWidget();
};

struct VCMixerWidget : ModuleWidget {
VCMixerWidget();
};

// struct MultWidget : ModuleWidget {
// MultWidget();
// };

struct ScopeWidget : ModuleWidget {
ScopeWidget();
};

struct SEQ3Widget : ModuleWidget {
SEQ3Widget();
};

+ 182
- 0
src/SEQ3.cpp View File

@@ -0,0 +1,182 @@
#include "Fundamental.hpp"


struct SEQ3 : Module {
enum ParamIds {
CLOCK_PARAM,
RUN_PARAM,
RESET_PARAM,
STEPS_PARAM,
ROW1_PARAM,
ROW2_PARAM = ROW1_PARAM + 8,
ROW3_PARAM = ROW2_PARAM + 8,
GATE_PARAM = ROW3_PARAM + 8,
NUM_PARAMS = GATE_PARAM + 8
};
enum InputIds {
CLOCK_INPUT,
EXT_CLOCK_INPUT,
RESET_INPUT,
STEPS_INPUT,
NUM_INPUTS
};
enum OutputIds {
GATES_OUTPUT,
ROW1_OUTPUT,
ROW2_OUTPUT,
ROW3_OUTPUT,
GATE_OUTPUT,
NUM_OUTPUTS = GATE_OUTPUT + 8
};

bool running = true;
SchmittTrigger clockTrigger; // for external clock
SchmittTrigger runningTrigger;
SchmittTrigger resetTrigger;
float phase = 0.0;
int index = 0;
SchmittTrigger gateTriggers[8];
bool gateState[8] = {};
float stepLights[8] = {};

// Lights
float runningLight = 0.0;
float resetLight = 0.0;
float gatesLight = 0.0;
float rowLights[3] = {};
float gateLights[8] = {};

SEQ3();
void step();
};


SEQ3::SEQ3() {
params.resize(NUM_PARAMS);
inputs.resize(NUM_INPUTS);
outputs.resize(NUM_OUTPUTS);
}

void SEQ3::step() {
const float lightLambda = 0.075;
// Run
if (runningTrigger.process(params[RUN_PARAM])) {
running = !running;
}
runningLight = running ? 1.0 : 0.0;

bool nextStep = false;

if (running) {
if (inputs[EXT_CLOCK_INPUT]) {
// External clock
if (clockTrigger.process(*inputs[EXT_CLOCK_INPUT])) {
phase = 0.0;
nextStep = true;
}
}
else {
// Internal clock
float clockTime = powf(1/2.0, params[CLOCK_PARAM] + getf(inputs[CLOCK_INPUT]));
phase += clockTime / gSampleRate;
if (phase >= 1.0) {
phase -= 1.0;
nextStep = true;
}
}
}

// Reset
if (resetTrigger.process(params[RESET_PARAM] + getf(inputs[RESET_INPUT]))) {
phase = 0.0;
index = 999;
nextStep = true;
resetLight = 1.0;
}

if (nextStep) {
// Advance step
int numSteps = clampi(roundf(params[STEPS_PARAM] + getf(inputs[STEPS_INPUT])), 1, 8);
index += 1;
if (index >= numSteps) {
index = 0;
}
stepLights[index] = 1.0;
}

resetLight -= resetLight / lightLambda / gSampleRate;

// Gate buttons
for (int i = 0; i < 8; i++) {
if (gateTriggers[i].process(params[GATE_PARAM + i])) {
gateState[i] = !gateState[i];
}
float gate = (i == index && gateState[i] >= 1.0) ? 10.0 : 0.0;
setf(outputs[GATE_OUTPUT + i], gate);
stepLights[i] -= stepLights[i] / lightLambda / gSampleRate;
gateLights[i] = (gateState[i] >= 1.0) ? 1.0 - stepLights[i] : stepLights[i];
}

// Rows
float row1 = params[ROW1_PARAM + index];
float row2 = params[ROW2_PARAM + index];
float row3 = params[ROW3_PARAM + index];
float gates = (gateState[index] >= 1.0) ? 10.0 : 0.0;
setf(outputs[ROW1_OUTPUT], row1);
setf(outputs[ROW2_OUTPUT], row2);
setf(outputs[ROW3_OUTPUT], row3);
setf(outputs[GATES_OUTPUT], gates);
gatesLight = (gateState[index] >= 1.0) ? 1.0 : 0.0;
rowLights[0] = row1;
rowLights[1] = row2;
rowLights[2] = row3;
}


SEQ3Widget::SEQ3Widget() {
SEQ3 *module = new SEQ3();
setModule(module);
box.size = Vec(15*22, 380);

{
Panel *panel = new LightPanel();
panel->box.size = box.size;
panel->backgroundImage = Image::load("plugins/Fundamental/res/SEQ3.png");
addChild(panel);
}

addChild(createScrew<ScrewSilver>(Vec(15, 0)));
addChild(createScrew<ScrewSilver>(Vec(box.size.x-30, 0)));
addChild(createScrew<ScrewSilver>(Vec(15, 365)));
addChild(createScrew<ScrewSilver>(Vec(box.size.x-30, 365)));

addParam(createParam<Davies1900hSmallBlackKnob>(Vec(17, 56), module, SEQ3::CLOCK_PARAM, -6.0, 2.0, -2.0));
addParam(createParam<LEDButton>(Vec(60, 61-1), module, SEQ3::RUN_PARAM, 0.0, 1.0, 0.0));
addChild(createValueLight<SmallLight<GreenValueLight>>(Vec(60+5, 61+4), &module->runningLight));
addParam(createParam<LEDButton>(Vec(98, 61-1), module, SEQ3::RESET_PARAM, 0.0, 1.0, 0.0));
addChild(createValueLight<SmallLight<GreenValueLight>>(Vec(98+5, 61+4), &module->resetLight));
addParam(createParam<Davies1900hSmallBlackSnapKnob>(Vec(132, 56), module, SEQ3::STEPS_PARAM, 1.0, 8.0, 8.0));
addChild(createValueLight<SmallLight<GreenValueLight>>(Vec(180.5, 65), &module->gatesLight));
addChild(createValueLight<SmallLight<GreenValueLight>>(Vec(218.5, 65), &module->rowLights[0]));
addChild(createValueLight<SmallLight<GreenValueLight>>(Vec(257, 65), &module->rowLights[1]));
addChild(createValueLight<SmallLight<GreenValueLight>>(Vec(295.5, 65), &module->rowLights[2]));

static const float portX[8] = {19, 57, 96, 134, 173, 211, 250, 288};
addInput(createInput<PJ301MPort>(Vec(portX[0]-1, 99-1), module, SEQ3::CLOCK_INPUT));
addInput(createInput<PJ301MPort>(Vec(portX[1]-1, 99-1), module, SEQ3::EXT_CLOCK_INPUT));
addInput(createInput<PJ301MPort>(Vec(portX[2]-1, 99-1), module, SEQ3::RESET_INPUT));
addInput(createInput<PJ301MPort>(Vec(portX[3]-1, 99-1), module, SEQ3::STEPS_INPUT));
addOutput(createOutput<PJ301MPort>(Vec(portX[4]-1, 99-1), module, SEQ3::GATES_OUTPUT));
addOutput(createOutput<PJ301MPort>(Vec(portX[5]-1, 99-1), module, SEQ3::ROW1_OUTPUT));
addOutput(createOutput<PJ301MPort>(Vec(portX[6]-1, 99-1), module, SEQ3::ROW2_OUTPUT));
addOutput(createOutput<PJ301MPort>(Vec(portX[7]-1, 99-1), module, SEQ3::ROW3_OUTPUT));

for (int i = 0; i < 8; i++) {
addParam(createParam<Davies1900hSmallBlackKnob>(Vec(portX[i]-2, 157), module, SEQ3::ROW1_PARAM + i, 0.0, 6.0, 0.0));
addParam(createParam<Davies1900hSmallBlackKnob>(Vec(portX[i]-2, 198), module, SEQ3::ROW2_PARAM + i, 0.0, 6.0, 0.0));
addParam(createParam<Davies1900hSmallBlackKnob>(Vec(portX[i]-2, 240), module, SEQ3::ROW3_PARAM + i, 0.0, 6.0, 0.0));
addParam(createParam<LEDButton>(Vec(portX[i]+2, 278-1), module, SEQ3::GATE_PARAM + i, 0.0, 1.0, 0.0));
addChild(createValueLight<SmallLight<GreenValueLight>>(Vec(portX[i]+7, 278+4), &module->gateLights[i]));
addOutput(createOutput<PJ301MPort>(Vec(portX[i]-1, 308-1), module, SEQ3::GATE_OUTPUT + i));
}
}

+ 297
- 0
src/Scope.cpp View File

@@ -0,0 +1,297 @@
#include <string.h>
#include "Fundamental.hpp"


#define BUFFER_SIZE 512

struct Scope : Module {
enum ParamIds {
X_SCALE_PARAM,
X_POS_PARAM,
Y_SCALE_PARAM,
Y_POS_PARAM,
TIME_PARAM,
MODE_PARAM,
TRIG_PARAM,
EXT_PARAM,
NUM_PARAMS
};
enum InputIds {
X_INPUT,
Y_INPUT,
TRIG_INPUT,
NUM_INPUTS
};
enum OutputIds {
NUM_OUTPUTS
};

float bufferX[BUFFER_SIZE] = {};
float bufferY[BUFFER_SIZE] = {};
int bufferIndex = 0;
float frameIndex = 0;

SchmittTrigger sumTrigger;
SchmittTrigger extTrigger;
bool sum = false;
bool ext = false;
float lights[4] = {};
SchmittTrigger resetTrigger;

Scope();
void step();
};


Scope::Scope() {
params.resize(NUM_PARAMS);
inputs.resize(NUM_INPUTS);
outputs.resize(NUM_OUTPUTS);
}

void Scope::step() {
// Modes
if (sumTrigger.process(params[MODE_PARAM])) {
sum = !sum;
}
lights[0] = sum ? 0.0 : 1.0;
lights[1] = sum ? 1.0 : 0.0;

if (extTrigger.process(params[EXT_PARAM])) {
ext = !ext;
}
lights[2] = ext ? 0.0 : 1.0;
lights[3] = ext ? 1.0 : 0.0;

// Compute time
float deltaTime = powf(2.0, params[TIME_PARAM]);
int frameCount = (int)ceilf(deltaTime * gSampleRate);

// Add frame to buffer
if (bufferIndex < BUFFER_SIZE) {
if (++frameIndex > frameCount) {
frameIndex = 0;
bufferX[bufferIndex] = getf(inputs[X_INPUT]);
bufferY[bufferIndex] = getf(inputs[Y_INPUT]);
bufferIndex++;
}
}

// Are we waiting on the next trigger?
if (bufferIndex >= BUFFER_SIZE) {
// Trigger immediately if external but nothing plugged in
if (ext && !inputs[TRIG_INPUT]) {
bufferIndex = 0; frameIndex = 0; return;
}

// Reset the Schmitt trigger so we don't trigger immediately if the input is high
if (frameIndex == 0) {
resetTrigger.reset();
}
frameIndex++;

// Must go below 0.1V to trigger
resetTrigger.setThresholds(params[TRIG_PARAM] - 0.1, params[TRIG_PARAM]);
float gate = ext ? getf(inputs[TRIG_INPUT]) : getf(inputs[X_INPUT]);

// Reset if triggered
float holdTime = 0.1;
if (resetTrigger.process(gate) || (frameIndex >= gSampleRate * holdTime)) {
bufferIndex = 0; frameIndex = 0; return;
}

// Reset if we've waited too long
if (frameIndex >= gSampleRate * holdTime) {
bufferIndex = 0; frameIndex = 0; return;
}
}
}


struct ScopeDisplay : TransparentWidget {
Scope *module;
int frame = 0;
std::shared_ptr<Font> font;

struct Stats {
float vrms, vpp, vmin, vmax;
void calculate(float *values) {
vrms = 0.0;
vmax = -INFINITY;
vmin = INFINITY;
for (int i = 0; i < BUFFER_SIZE; i++) {
float v = values[i];
vrms += v*v;
vmax = fmaxf(vmax, v);
vmin = fminf(vmin, v);
}
vrms = sqrtf(vrms / BUFFER_SIZE);
vpp = vmax - vmin;
}
};
Stats statsX, statsY;

ScopeDisplay() {
font = Font::load("plugins/Fundamental/res/DejaVuSansMono.ttf");
}

void drawWaveform(NVGcontext *vg, float *values, float gain, float offset) {
nvgSave(vg);
Rect b = Rect(Vec(0, 15), box.size.minus(Vec(0, 15*2)));
nvgScissor(vg, b.pos.x, b.pos.y, b.size.x, b.size.y);
nvgBeginPath(vg);
// Draw maximum display left to right
for (int i = 0; i < BUFFER_SIZE; i++) {
float value = values[i] * gain + offset;
Vec p = Vec(b.pos.x + i * b.size.x / (BUFFER_SIZE-1), b.pos.y + b.size.y * (1 - value) / 2);
if (i == 0)
nvgMoveTo(vg, p.x, p.y);
else
nvgLineTo(vg, p.x, p.y);
}
nvgLineCap(vg, NVG_ROUND);
nvgMiterLimit(vg, 2.0);
nvgStrokeWidth(vg, 1.75);
nvgGlobalCompositeOperation(vg, NVG_LIGHTER);
nvgStroke(vg);
nvgResetScissor(vg);
nvgRestore(vg);
}

void drawTrig(NVGcontext *vg, float value, float gain, float offset) {
Rect b = Rect(Vec(0, 15), box.size.minus(Vec(0, 15*2)));
nvgScissor(vg, b.pos.x, b.pos.y, b.size.x, b.size.y);

value = value * gain + offset;
Vec p = Vec(box.size.x, b.pos.y + b.size.y * (1 - value) / 2);

// Draw line
nvgStrokeColor(vg, nvgRGBA(0xff, 0xff, 0xff, 0x10));
{
nvgBeginPath(vg);
nvgMoveTo(vg, p.x - 13, p.y);
nvgLineTo(vg, 0, p.y);
nvgClosePath(vg);
}
nvgStroke(vg);

// Draw indicator
nvgFillColor(vg, nvgRGBA(0xff, 0xff, 0xff, 0x60));
{
nvgBeginPath(vg);
nvgMoveTo(vg, p.x - 2, p.y - 4);
nvgLineTo(vg, p.x - 9, p.y - 4);
nvgLineTo(vg, p.x - 13, p.y);
nvgLineTo(vg, p.x - 9, p.y + 4);
nvgLineTo(vg, p.x - 2, p.y + 4);
nvgClosePath(vg);
}
nvgFill(vg);

nvgFontSize(vg, 8);
nvgFontFaceId(vg, font->handle);
nvgFillColor(vg, nvgRGBA(0x1e, 0x28, 0x2b, 0xff));
nvgText(vg, p.x - 8, p.y + 3, "T", NULL);
nvgResetScissor(vg);
}

void drawStats(NVGcontext *vg, Vec pos, const char *title, Stats *stats) {
nvgFontSize(vg, 10);
nvgFontFaceId(vg, font->handle);
nvgTextLetterSpacing(vg, -2);

nvgFillColor(vg, nvgRGBA(0xff, 0xff, 0xff, 0xff));
nvgText(vg, pos.x + 5, pos.y + 10, title, NULL);

nvgFillColor(vg, nvgRGBA(0xff, 0xff, 0xff, 0x80));
char text[128];
snprintf(text, sizeof(text), "rms %5.2f pp %5.2f max % 6.2f min % 6.2f", stats->vrms, stats->vpp, stats->vmax, stats->vmin);
nvgText(vg, pos.x + 17, pos.y + 10, text, NULL);
}

void draw(NVGcontext *vg) {
float gainX = powf(2.0, roundf(module->params[Scope::X_SCALE_PARAM])) / 12.0;
float gainY = powf(2.0, roundf(module->params[Scope::Y_SCALE_PARAM])) / 12.0;
float posX = module->params[Scope::X_POS_PARAM];
float posY = module->params[Scope::Y_POS_PARAM];

// Draw waveforms
if (module->sum) {
float sumBuffer[BUFFER_SIZE];
for (int i = 0; i < BUFFER_SIZE; i++) {
sumBuffer[i] = module->bufferX[i] + module->bufferY[i];
}
// X + Y
nvgStrokeColor(vg, nvgRGBA(0x9f, 0xe4, 0x36, 0xc0));
drawWaveform(vg, sumBuffer, gainX, posX);
}
else {
// Y
if (module->inputs[Scope::Y_INPUT]) {
nvgStrokeColor(vg, nvgRGBA(0xe1, 0x02, 0x78, 0xc0));
drawWaveform(vg, module->bufferY, gainY, posY);
}

// X
if (module->inputs[Scope::X_INPUT]) {
nvgStrokeColor(vg, nvgRGBA(0x28, 0xb0, 0xf3, 0xc0));
drawWaveform(vg, module->bufferX, gainX, posX);
}
}
drawTrig(vg, module->params[Scope::TRIG_PARAM], gainX, posX);

// Calculate and draw stats
if (++frame >= 4) {
frame = 0;
statsX.calculate(module->bufferX);
statsY.calculate(module->bufferY);
}
drawStats(vg, Vec(0, 0), "X", &statsX);
drawStats(vg, Vec(0, box.size.y - 15), "Y", &statsY);
}
};


ScopeWidget::ScopeWidget() {
Scope *module = new Scope();
setModule(module);
box.size = Vec(15*13, 380);

{
Panel *panel = new LightPanel();
panel->box.size = box.size;
panel->backgroundImage = Image::load("plugins/Fundamental/res/Scope.png");
addChild(panel);
}

addChild(createScrew<ScrewSilver>(Vec(15, 0)));
addChild(createScrew<ScrewSilver>(Vec(box.size.x-30, 0)));
addChild(createScrew<ScrewSilver>(Vec(15, 365)));
addChild(createScrew<ScrewSilver>(Vec(box.size.x-30, 365)));

{
ScopeDisplay *display = new ScopeDisplay();
display->module = module;
display->box.pos = Vec(0, 44);
display->box.size = Vec(box.size.x, 140);
addChild(display);
}

addParam(createParam<Davies1900hSmallBlackSnapKnob>(Vec(15, 209), module, Scope::X_SCALE_PARAM, -1.0, 9.0, 1.0));
addParam(createParam<Davies1900hSmallBlackKnob>(Vec(15, 263), module, Scope::X_POS_PARAM, -1.0, 1.0, 0.0));
addParam(createParam<Davies1900hSmallBlackSnapKnob>(Vec(61, 209), module, Scope::Y_SCALE_PARAM, -1.0, 9.0, 1.0));
addParam(createParam<Davies1900hSmallBlackKnob>(Vec(61, 263), module, Scope::Y_POS_PARAM, -1.0, 1.0, 0.0));
addParam(createParam<Davies1900hSmallBlackKnob>(Vec(107, 209), module, Scope::TIME_PARAM, -6.0, -16.0, -14.0));
addParam(createParam<CKD6>(Vec(106, 262), module, Scope::MODE_PARAM, 0.0, 1.0, 0.0));
addParam(createParam<Davies1900hSmallBlackKnob>(Vec(153, 209), module, Scope::TRIG_PARAM, -12.0, 12.0, 0.0));
addParam(createParam<CKD6>(Vec(152, 262), module, Scope::EXT_PARAM, 0.0, 1.0, 0.0));

addInput(createInput<PJ301MPort>(Vec(17, 319), module, Scope::X_INPUT));
addInput(createInput<PJ301MPort>(Vec(63, 319), module, Scope::Y_INPUT));
addInput(createInput<PJ301MPort>(Vec(154, 319), module, Scope::TRIG_INPUT));

addChild(createValueLight<TinyLight<GreenValueLight>>(Vec(104, 251), &module->lights[0]));
addChild(createValueLight<TinyLight<GreenValueLight>>(Vec(104, 296), &module->lights[1]));
addChild(createValueLight<TinyLight<GreenValueLight>>(Vec(150, 251), &module->lights[2]));
addChild(createValueLight<TinyLight<GreenValueLight>>(Vec(150, 296), &module->lights[3]));
}

+ 81
- 0
src/VCA.cpp View File

@@ -0,0 +1,81 @@
#include "Fundamental.hpp"


struct VCA : Module {
enum ParamIds {
LEVEL1_PARAM,
LEVEL2_PARAM,
NUM_PARAMS
};
enum InputIds {
EXP1_INPUT,
LIN1_INPUT,
IN1_INPUT,
EXP2_INPUT,
LIN2_INPUT,
IN2_INPUT,
NUM_INPUTS
};
enum OutputIds {
OUT1_OUTPUT,
OUT2_OUTPUT,
NUM_OUTPUTS
};

VCA();
void step();
};


VCA::VCA() {
params.resize(NUM_PARAMS);
inputs.resize(NUM_INPUTS);
outputs.resize(NUM_OUTPUTS);
}

static void stepChannel(const float *in, float level, const float *lin, const float *exp, float *out) {
float v = getf(in);
if (lin)
v *= clampf(*lin / 10.0, 0.0, 1.0);
const float expBase = 50.0;
if (exp)
v *= rescalef(powf(expBase, clampf(*exp / 10.0, 0.0, 1.0)), 1.0, expBase, 0.0, 1.0);
setf(out, v);
}

void VCA::step() {
stepChannel(inputs[IN1_INPUT], params[LEVEL1_PARAM], inputs[LIN1_INPUT], inputs[EXP1_INPUT], outputs[OUT1_OUTPUT]);
stepChannel(inputs[IN2_INPUT], params[LEVEL2_PARAM], inputs[LIN2_INPUT], inputs[EXP2_INPUT], outputs[OUT2_OUTPUT]);
}


VCAWidget::VCAWidget() {
VCA *module = new VCA();
setModule(module);
box.size = Vec(15*6, 380);

{
Panel *panel = new LightPanel();
panel->box.size = box.size;
panel->backgroundImage = Image::load("plugins/Fundamental/res/VCA.png");
addChild(panel);
}

addChild(createScrew<ScrewSilver>(Vec(15, 0)));
addChild(createScrew<ScrewSilver>(Vec(box.size.x-30, 0)));
addChild(createScrew<ScrewSilver>(Vec(15, 365)));
addChild(createScrew<ScrewSilver>(Vec(box.size.x-30, 365)));

addParam(createParam<Davies1900hBlackKnob>(Vec(27, 57), module, VCA::LEVEL1_PARAM, 0.0, 1.0, 0.5));
addParam(createParam<Davies1900hBlackKnob>(Vec(27, 222), module, VCA::LEVEL2_PARAM, 0.0, 1.0, 0.5));

addInput(createInput<PJ301MPort>(Vec(11, 113), module, VCA::EXP1_INPUT));
addInput(createInput<PJ301MPort>(Vec(54, 113), module, VCA::LIN1_INPUT));
addInput(createInput<PJ301MPort>(Vec(11, 156), module, VCA::IN1_INPUT));
addInput(createInput<PJ301MPort>(Vec(11, 276), module, VCA::EXP2_INPUT));
addInput(createInput<PJ301MPort>(Vec(54, 276), module, VCA::LIN2_INPUT));
addInput(createInput<PJ301MPort>(Vec(11, 320), module, VCA::IN2_INPUT));

addOutput(createOutput<PJ301MPort>(Vec(54, 156), module, VCA::OUT1_OUTPUT));
addOutput(createOutput<PJ301MPort>(Vec(54, 320), module, VCA::OUT2_OUTPUT));
}

+ 172
- 0
src/VCF.cpp View File

@@ -0,0 +1,172 @@
/*
The filter DSP code has been derived from
Miller Puckette's code hosted at
https://github.com/ddiakopoulos/MoogLadders/blob/master/src/RKSimulationModel.h
which is licensed for use under the following terms (MIT license):


Copyright (c) 2015, Miller Puckette. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#include "Fundamental.hpp"


// The clipping function of a transistor pair is approximately tanh(x)
// TODO: Put this in a lookup table. 5th order approx doesn't seem to cut it
inline float clip(float x) {
return tanhf(x);
}

struct LadderFilter {
float cutoff = 1000.0;
float resonance = 1.0;
float state[4] = {};

void calculateDerivatives(float input, float *dstate, const float *state) {
float cutoff2Pi = 2*M_PI * cutoff;

float satstate0 = clip(state[0]);
float satstate1 = clip(state[1]);
float satstate2 = clip(state[2]);

dstate[0] = cutoff2Pi * (clip(input - resonance * state[3]) - satstate0);
dstate[1] = cutoff2Pi * (satstate0 - satstate1);
dstate[2] = cutoff2Pi * (satstate1 - satstate2);
dstate[3] = cutoff2Pi * (satstate2 - clip(state[3]));
}

void process(float input, float dt) {
float deriv1[4], deriv2[4], deriv3[4], deriv4[4], tempState[4];

calculateDerivatives(input, deriv1, state);
for (int i = 0; i < 4; i++)
tempState[i] = state[i] + 0.5 * dt * deriv1[i];

calculateDerivatives(input, deriv2, tempState);
for (int i = 0; i < 4; i++)
tempState[i] = state[i] + 0.5 * dt * deriv2[i];

calculateDerivatives(input, deriv3, tempState);
for (int i = 0; i < 4; i++)
tempState[i] = state[i] + dt * deriv3[i];

calculateDerivatives(input, deriv4, tempState);
for (int i = 0; i < 4; i++)
state[i] += (1.0 / 6.0) * dt * (deriv1[i] + 2.0 * deriv2[i] + 2.0 * deriv3[i] + deriv4[i]);
}
};


struct VCF : Module {
enum ParamIds {
FREQ_PARAM,
FINE_PARAM,
RES_PARAM,
FREQ_CV_PARAM,
DRIVE_PARAM,
NUM_PARAMS
};
enum InputIds {
FREQ_INPUT,
RES_INPUT,
DRIVE_INPUT,
IN_INPUT,
NUM_INPUTS
};
enum OutputIds {
LPF_OUTPUT,
HPF_OUTPUT,
NUM_OUTPUTS
};

LadderFilter filter;

VCF();
void step();
};


VCF::VCF() {
params.resize(NUM_PARAMS);
inputs.resize(NUM_INPUTS);
outputs.resize(NUM_OUTPUTS);
}

void VCF::step() {
float input = getf(inputs[IN_INPUT]) / 5.0;
float drive = powf(100.0, params[DRIVE_PARAM]);
input *= drive;
// Add -60dB noise to bootstrap self-oscillation
input += 1.0e-6 * (2.0*randomf() - 1.0);

// Set resonance
float res = params[RES_PARAM] + getf(inputs[RES_INPUT]) / 5.0;
res = 5.5 * clampf(res, 0.0, 1.0);
filter.resonance = res;

// Set cutoff frequency
float cutoffExp = params[FREQ_PARAM] + params[FREQ_CV_PARAM] * getf(inputs[FREQ_INPUT]) / 5.0;
cutoffExp = clampf(cutoffExp, 0.0, 1.0);
filter.cutoff = 200.0 * powf(8400.0/200.0, cutoffExp);

// Push a sample to the state filter
filter.process(input, 1.0/gSampleRate);

// Extract outputs
setf(outputs[LPF_OUTPUT], 5.0 * filter.state[3]);
setf(outputs[HPF_OUTPUT], 5.0 * (input - filter.state[3]));
}


VCFWidget::VCFWidget() {
VCF *module = new VCF();
setModule(module);
box.size = Vec(15*8, 380);

{
Panel *panel = new LightPanel();
panel->box.size = box.size;
panel->backgroundImage = Image::load("plugins/Fundamental/res/VCF.png");
addChild(panel);
}

addChild(createScrew<ScrewSilver>(Vec(15, 0)));
addChild(createScrew<ScrewSilver>(Vec(box.size.x-30, 0)));
addChild(createScrew<ScrewSilver>(Vec(15, 365)));
addChild(createScrew<ScrewSilver>(Vec(box.size.x-30, 365)));

addParam(createParam<Davies1900hLargeBlackKnob>(Vec(33, 61), module, VCF::FREQ_PARAM, 0.0, 1.0, 0.5));
addParam(createParam<Davies1900hBlackKnob>(Vec(12, 143), module, VCF::FINE_PARAM, 0.0, 1.0, 0.5));
addParam(createParam<Davies1900hBlackKnob>(Vec(71, 143), module, VCF::RES_PARAM, 0.0, 1.0, 0.0));
addParam(createParam<Davies1900hBlackKnob>(Vec(12, 208), module, VCF::FREQ_CV_PARAM, -1.0, 1.0, 0.0));
addParam(createParam<Davies1900hBlackKnob>(Vec(71, 208), module, VCF::DRIVE_PARAM, 0.0, 1.0, 0.0));

addInput(createInput<PJ301MPort>(Vec(10, 276), module, VCF::FREQ_INPUT));
addInput(createInput<PJ301MPort>(Vec(48, 276), module, VCF::RES_INPUT));
addInput(createInput<PJ301MPort>(Vec(85, 276), module, VCF::DRIVE_INPUT));
addInput(createInput<PJ301MPort>(Vec(10, 320), module, VCF::IN_INPUT));

addOutput(createOutput<PJ301MPort>(Vec(48, 320), module, VCF::LPF_OUTPUT));
addOutput(createOutput<PJ301MPort>(Vec(85, 320), module, VCF::HPF_OUTPUT));
}

+ 89
- 0
src/VCMixer.cpp View File

@@ -0,0 +1,89 @@
#include "Fundamental.hpp"


struct VCMixer : Module {
enum ParamIds {
MIX_PARAM,
CH1_PARAM,
CH2_PARAM,
CH3_PARAM,
NUM_PARAMS
};
enum InputIds {
MIX_CV_INPUT,
CH1_INPUT,
CH1_CV_INPUT,
CH2_INPUT,
CH2_CV_INPUT,
CH3_INPUT,
CH3_CV_INPUT,
NUM_INPUTS
};
enum OutputIds {
MIX_OUTPUT,
CH1_OUTPUT,
CH2_OUTPUT,
CH3_OUTPUT,
NUM_OUTPUTS
};

VCMixer();
void step();
};


VCMixer::VCMixer() {
params.resize(NUM_PARAMS);
inputs.resize(NUM_INPUTS);
outputs.resize(NUM_OUTPUTS);
}


void VCMixer::step() {
float ch1 = getf(inputs[CH1_INPUT]) * params[CH1_PARAM] * clampf(getf(inputs[CH1_CV_INPUT], 10.0) / 10.0, 0.0, 1.0);
float ch2 = getf(inputs[CH2_INPUT]) * params[CH2_PARAM] * clampf(getf(inputs[CH2_CV_INPUT], 10.0) / 10.0, 0.0, 1.0);
float ch3 = getf(inputs[CH3_INPUT]) * params[CH3_PARAM] * clampf(getf(inputs[CH3_CV_INPUT], 10.0) / 10.0, 0.0, 1.0);
float mix = (ch1 + ch2 + ch3) * params[MIX_PARAM] * getf(inputs[MIX_CV_INPUT], 10.0) / 10.0;

setf(outputs[CH1_OUTPUT], ch1);
setf(outputs[CH2_OUTPUT], ch2);
setf(outputs[CH3_OUTPUT], ch3);
setf(outputs[MIX_OUTPUT], mix);
}


VCMixerWidget::VCMixerWidget() {
VCMixer *module = new VCMixer();
setModule(module);
box.size = Vec(15*10, 380);

{
Panel *panel = new LightPanel();
panel->box.size = box.size;
panel->backgroundImage = Image::load("plugins/Fundamental/res/VCMixer.png");
addChild(panel);
}

addChild(createScrew<ScrewSilver>(Vec(15, 0)));
addChild(createScrew<ScrewSilver>(Vec(box.size.x-30, 0)));
addChild(createScrew<ScrewSilver>(Vec(15, 365)));
addChild(createScrew<ScrewSilver>(Vec(box.size.x-30, 365)));

addParam(createParam<Davies1900hLargeBlackKnob>(Vec(48, 55), module, VCMixer::MIX_PARAM, 0.0, 1.0, 0.5));
addParam(createParam<Davies1900hBlackKnob>(Vec(57, 139), module, VCMixer::CH1_PARAM, 0.0, 1.0, 0.5));
addParam(createParam<Davies1900hBlackKnob>(Vec(57, 219), module, VCMixer::CH2_PARAM, 0.0, 1.0, 0.5));
addParam(createParam<Davies1900hBlackKnob>(Vec(57, 300), module, VCMixer::CH3_PARAM, 0.0, 1.0, 0.5));

addInput(createInput<PJ301MPort>(Vec(16, 69), module, VCMixer::MIX_CV_INPUT));
addInput(createInput<PJ301MPort>(Vec(22, 129), module, VCMixer::CH1_INPUT));
addInput(createInput<PJ301MPort>(Vec(22, 160), module, VCMixer::CH1_CV_INPUT));
addInput(createInput<PJ301MPort>(Vec(22, 209), module, VCMixer::CH2_INPUT));
addInput(createInput<PJ301MPort>(Vec(22, 241), module, VCMixer::CH2_CV_INPUT));
addInput(createInput<PJ301MPort>(Vec(22, 290), module, VCMixer::CH3_INPUT));
addInput(createInput<PJ301MPort>(Vec(22, 322), module, VCMixer::CH3_CV_INPUT));

addOutput(createOutput<PJ301MPort>(Vec(110, 69), module, VCMixer::MIX_OUTPUT));
addOutput(createOutput<PJ301MPort>(Vec(110, 145), module, VCMixer::CH1_OUTPUT));
addOutput(createOutput<PJ301MPort>(Vec(110, 225), module, VCMixer::CH2_OUTPUT));
addOutput(createOutput<PJ301MPort>(Vec(110, 306), module, VCMixer::CH3_OUTPUT));
}

+ 222
- 0
src/VCO.cpp View File

@@ -0,0 +1,222 @@
#include "Fundamental.hpp"
#include "dsp.hpp"


#define OVERSAMPLE 16
#define QUALITY 16


struct VCO : Module {
enum ParamIds {
MODE_PARAM,
SYNC_PARAM,
FREQ_PARAM,
FINE_PARAM,
FM_PARAM,
PW_PARAM,
PW_CV_PARAM,
NUM_PARAMS
};
enum InputIds {
PITCH_INPUT,
FM_INPUT,
PW_INPUT,
SYNC_INPUT,
NUM_INPUTS
};
enum OutputIds {
SIN_OUTPUT,
TRI_OUTPUT,
SAW_OUTPUT,
SQR_OUTPUT,
NUM_OUTPUTS
};

float lastSync = 0.0;
float phase = 0.0;
bool syncDirection = false;

Decimator<OVERSAMPLE, QUALITY> sinDecimator;
Decimator<OVERSAMPLE, QUALITY> triDecimator;
Decimator<OVERSAMPLE, QUALITY> sawDecimator;
Decimator<OVERSAMPLE, QUALITY> sqrDecimator;
RCFilter sqrFilter;

float lights[1] = {};

// For analog detuning effect
float pitchSlew = 0.0;
int pitchSlewIndex = 0;

VCO();
void step();
};


VCO::VCO() {
params.resize(NUM_PARAMS);
inputs.resize(NUM_INPUTS);
outputs.resize(NUM_OUTPUTS);
}

void VCO::step() {
bool analog = params[MODE_PARAM] < 1.0;
// TODO Soft sync features
bool soft = params[SYNC_PARAM] < 1.0;

if (analog) {
// Adjust pitch slew
if (++pitchSlewIndex > 32) {
const float pitchSlewTau = 100.0; // Time constant for leaky integrator in seconds
pitchSlew += (randomNormal() - pitchSlew / pitchSlewTau) / gSampleRate;
pitchSlewIndex = 0;
}
}

// Compute frequency
float pitch = params[FREQ_PARAM];
if (analog) {
// Apply pitch slew
const float pitchSlewAmount = 3.0;
pitch += pitchSlew * pitchSlewAmount;
}
else {
// Quantize coarse knob if digital mode
pitch = roundf(pitch);
}
pitch += 12.0 * getf(inputs[PITCH_INPUT]);
pitch += 3.0 * quadraticBipolar(params[FINE_PARAM]);
if (inputs[FM_INPUT]) {
pitch += quadraticBipolar(params[FM_PARAM]) * 12.0 * *inputs[FM_INPUT];
}
float freq = 261.626 * powf(2.0, pitch / 12.0);

// Pulse width
const float pwMin = 0.01;
float pw = clampf(params[PW_PARAM] + params[PW_CV_PARAM] * getf(inputs[PW_INPUT]) / 10.0, pwMin, 1.0 - pwMin);

// Advance phase
float deltaPhase = clampf(freq / gSampleRate, 1e-6, 0.5);

// Detect sync
int syncIndex = -1; // Index in the oversample loop where sync occurs [0, OVERSAMPLE)
float syncCrossing = 0.0; // Offset that sync occurs [0.0, 1.0)
if (inputs[SYNC_INPUT]) {
float sync = *inputs[SYNC_INPUT] - 0.01;
if (sync > 0.0 && lastSync <= 0.0) {
float deltaSync = sync - lastSync;
syncCrossing = 1.0 - sync / deltaSync;
syncCrossing *= OVERSAMPLE;
syncIndex = (int)syncCrossing;
syncCrossing -= syncIndex;
}
lastSync = sync;
}

if (syncDirection)
deltaPhase *= -1.0;

// Oversample loop
float sin[OVERSAMPLE];
float tri[OVERSAMPLE];
float saw[OVERSAMPLE];
float sqr[OVERSAMPLE];
sqrFilter.setCutoff(40.0 / gSampleRate);

for (int i = 0; i < OVERSAMPLE; i++) {
if (syncIndex == i) {
if (soft) {
syncDirection = !syncDirection;
deltaPhase *= -1.0;
}
else {
// phase = syncCrossing * deltaPhase / OVERSAMPLE;
phase = 0.0;
}
}

if (outputs[SIN_OUTPUT]) {
if (analog)
// Quadratic approximation of sine, slightly richer harmonics
sin[i] = 1.08 * ((phase < 0.25) ? (-1.0 + (4*phase)*(4*phase)) : (phase < 0.75) ? (1.0 - (4*phase-2)*(4*phase-2)) : (-1.0 + (4*phase-4)*(4*phase-4)));
else
sin[i] = -cosf(2*M_PI * phase);
}
if (outputs[TRI_OUTPUT]) {
if (analog)
tri[i] = 1.35 * interpf(triTable, phase * 2047.0);
else
tri[i] = (phase < 0.5) ? (-1.0 + 4.0*phase) : (1.0 - 4.0*(phase - 0.5));
}
if (outputs[SAW_OUTPUT]) {
if (analog)
saw[i] = 1.5 * interpf(sawTable, phase * 2047.0);
else
saw[i] = -1.0 + 2.0*phase;
}
if (outputs[SQR_OUTPUT]) {
sqr[i] = (phase < 1.0 - pw) ? -1.0 : 1.0;
if (analog) {
// Simply filter here
sqrFilter.process(sqr[i]);
sqr[i] = sqrFilter.highpass() / 2.0;
}
}

// Advance phase
phase += deltaPhase / OVERSAMPLE;
phase = eucmodf(phase, 1.0);
}

// Set output
if (outputs[SIN_OUTPUT])
*outputs[SIN_OUTPUT] = 5.0 * sinDecimator.process(sin);
if (outputs[TRI_OUTPUT])
*outputs[TRI_OUTPUT] = 5.0 * triDecimator.process(tri);
if (outputs[SAW_OUTPUT])
*outputs[SAW_OUTPUT] = 5.0 * sawDecimator.process(saw);
if (outputs[SQR_OUTPUT])
*outputs[SQR_OUTPUT] = 5.0 * sqrDecimator.process(sqr);

lights[0] = rescalef(pitch, -48.0, 48.0, -1.0, 1.0);
}


VCOWidget::VCOWidget() {
VCO *module = new VCO();
setModule(module);
box.size = Vec(15*10, 380);

{
Panel *panel = new LightPanel();
panel->box.size = box.size;
panel->backgroundImage = Image::load("plugins/Fundamental/res/VCO.png");
addChild(panel);
}

addChild(createScrew<ScrewSilver>(Vec(15, 0)));
addChild(createScrew<ScrewSilver>(Vec(box.size.x-30, 0)));
addChild(createScrew<ScrewSilver>(Vec(15, 365)));
addChild(createScrew<ScrewSilver>(Vec(box.size.x-30, 365)));

addParam(createParam<CKSS>(Vec(15, 77), module, VCO::MODE_PARAM, 0.0, 1.0, 1.0));
addParam(createParam<CKSS>(Vec(120, 77), module, VCO::SYNC_PARAM, 0.0, 1.0, 1.0));

addParam(createParam<Davies1900hLargeBlackKnob>(Vec(48, 61), module, VCO::FREQ_PARAM, -54.0, 54.0, 0.0));
addParam(createParam<Davies1900hBlackKnob>(Vec(23, 143), module, VCO::FINE_PARAM, -1.0, 1.0, 0.0));
addParam(createParam<Davies1900hBlackKnob>(Vec(91, 143), module, VCO::PW_PARAM, 0.0, 1.0, 0.5));
addParam(createParam<Davies1900hBlackKnob>(Vec(23, 208), module, VCO::FM_PARAM, 0.0, 1.0, 0.0));
addParam(createParam<Davies1900hBlackKnob>(Vec(91, 208), module, VCO::PW_CV_PARAM, 0.0, 1.0, 0.0));

addInput(createInput<PJ301MPort>(Vec(11, 276), module, VCO::PITCH_INPUT));
addInput(createInput<PJ301MPort>(Vec(45, 276), module, VCO::FM_INPUT));
addInput(createInput<PJ301MPort>(Vec(80, 276), module, VCO::SYNC_INPUT));
addInput(createInput<PJ301MPort>(Vec(114, 276), module, VCO::PW_INPUT));

addOutput(createOutput<PJ301MPort>(Vec(11, 320), module, VCO::SIN_OUTPUT));
addOutput(createOutput<PJ301MPort>(Vec(45, 320), module, VCO::TRI_OUTPUT));
addOutput(createOutput<PJ301MPort>(Vec(80, 320), module, VCO::SAW_OUTPUT));
addOutput(createOutput<PJ301MPort>(Vec(114, 320), module, VCO::SQR_OUTPUT));

addChild(createValueLight<SmallLight<GreenRedPolarityLight>>(Vec(99, 41), &module->lights[0]));
}

Loading…
Cancel
Save