alphaosc/plugin/alphaosc.cpp

159 lines
5.0 KiB
C++
Raw Normal View History

2025-01-06 21:33:28 +00:00
/*
Alpha Juno Oscillator POC
Copyright 2024 Gordon JC Pearce <gordonjcp@gjcp.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "alphaosc.hpp"
START_NAMESPACE_DISTRHO
AlphaOsc::AlphaOsc() : Plugin(parameterCount, 0, 0), sampleRate(getSampleRate()) {
// initial code here
}
// Initialisation functions
void AlphaOsc::initAudioPort(bool input, uint32_t index, AudioPort &port) {
// port.groupId = kPortGroupStereo;
Plugin::initAudioPort(input, index, port);
if (!input && index == 0) port.name = "Osc Out";
}
// Processing functions
void AlphaOsc::activate() {
// calculate filter coefficients and stuff
printf("called activate()\n");
omega = (1 << 31) / sampleRate * 130;
lfoOmega = (1 << 31) / sampleRate * 3.51;
omega = (130 / sampleRate) * (1 << 23);
2025-01-06 21:33:28 +00:00
}
void AlphaOsc::deactivate() {
printf("called deactivate()\n");
}
void AlphaOsc::run(const float **, float **outputs, uint32_t frames, const MidiEvent *midiEvents, uint32_t midiEventCount) {
bzero(outputs[0], sizeof(float) * frames);
// cast unused parameters to void for now to stop the compiler complaining
(void)midiEventCount;
(void)midiEvents;
uint16_t i;
uint32_t osc;
uint8_t lfo;
float saw, sqr, sub, wave;
float oct1, oct3, pwg;
float out, in;
// calculate an entire block of samples
for (i = 0; i < frames; i++) {
// increment phase of saw counter
// phase is a 32-bit counter because we're on a PC and we can afford to be profligate with silicon
// the actual Alpha Juno oscillators might well have been 8-bit for reasons loosely explained in the README
phase += omega;
lfoPhase += lfoOmega;
// now osc is a ten-bit counter, to give room for the sub osc outputs
// square output will be bit 7 of osc, 25% will be bit 7 & bit 6
// PWM square will be bit 7 & comparator, with the PWM being compared against bits 0-6
osc = phase >> 14;
// LFO is 7-bit triangle
lfo = (lfoPhase >> 24) & 0x7f;
lfo = (lfoPhase & 0x80000000) ? lfo : 127 - lfo;
pw = 0;
// the oscillator outputs in the chip are probably digital signals
// with the saw being the 8-bit outputs of the counter
// the square and sub signals picked off the counter bits
// and a couple of flipflops to generate the sub osc signals
// 8-bit saw scaled
saw = (osc & 0xff) / 256.0f;
// square scaled to 0-1, along with one and three octaves up
sqr = (float)(osc & 0x80) != 0;
oct1 = (float)(osc & 0x40) != 0;
oct3 = (float)(osc & 0x10) != 0;
// pulse width gate
// lower seven bits of the saw osc, compared with PW setting
pw = lfo*0.5;
pwg = (float)((osc & 0x7f) >= pw) != 0;
// calculate the oscillator output
switch (sqrmode) {
case 0:
case 4:
2025-01-07 12:25:10 +00:00
sqr = 0; // oscillator is off
break;
2025-01-07 12:25:10 +00:00
case 1: // do nothing, sqr is fine
break;
case 2:
2025-01-07 12:25:10 +00:00
sqr *= oct1; // 25% pulse
break;
case 3:
2025-01-07 12:25:10 +00:00
sqr *= pwg; // pwm
break;
}
switch (sawmode) {
case 0:
2025-01-07 12:25:10 +00:00
saw = 0;
break; // oscillator is off
case 1:
2025-01-07 12:25:10 +00:00
break; // saw is fine, do nothing
case 2:
2025-01-07 12:25:10 +00:00
saw *= oct1; // pulsed
break;
case 3:
2025-01-07 12:25:10 +00:00
saw *= pwg; // pwm
break;
case 4:
2025-01-07 12:25:10 +00:00
saw *= oct3; // oct3 pulse
break;
case 5:
2025-01-07 12:25:10 +00:00
saw *= oct1 * oct3; // both pulse
break;
}
2025-01-07 12:25:10 +00:00
// mix the signals, probably done with some resistors in the chip
in = (saw * 0.8) + (sqr * 0.63); // scaled similarly to Juno 106
// DC removal highpass filter
// this is very approximately 6Hz at 44.1kHz and 48kHz
// honestly it doesn't matter all that much if it's wrong at higher sample rates
out = in - hpfx + .99915 * hpfy;
hpfx = in;
hpfy = out;
outputs[0][i] = out;
}
// printf("%f %f\n", sqr, saw);
2025-01-06 21:33:28 +00:00
}
// create the plugin
Plugin *createPlugin() { return new AlphaOsc(); }
END_NAMESPACE_DISTRHO