added glide, but still needs debugging

This commit is contained in:
Oleksiy
2026-09-13 18:57:20 +03:00
parent 86535fa998
commit 245982c575
2 changed files with 74 additions and 59 deletions

View File

@ -7,18 +7,17 @@
// This code is licenced under GPL v3 or later // This code is licenced under GPL v3 or later
// todo: // todo:
// add glide handling // debug glide (slides down from lower notes?)
// make a glide on/off swithc instead of drone, both for MIDI and CV (sample pitch on gate)
// add accent handling // add accent handling
// bring back manual triggering (test conflicts with midi)
#include <Mozzi.h> #include <Mozzi.h>
#include <MIDI.h> #include <MIDI.h>
#include <Oscil.h> #include <Oscil.h>
#include <MetaOscil.h> #include <MetaOscil.h>
//#include <StateVariable.h>
#include <ResonantFilter.h> #include <ResonantFilter.h>
//#include <ADSR.h>
#include <Ead.h> #include <Ead.h>
#include <Portamento.h>
#include <mozzi_midi.h> #include <mozzi_midi.h>
#include <IntMap.h> #include <IntMap.h>
#include <FixMath.h> #include <FixMath.h>
@ -101,42 +100,57 @@ MetaOscil<SAW_MAX_138_AT_16384_512_NUM_CELLS, MOZZI_AUDIO_RATE, 16> aSaw {&aSaw1
IntMap kMapNote( 0, 4095, 24 * pitchSubSteps, 84 * pitchSubSteps ); IntMap kMapNote( 0, 4095, 24 * pitchSubSteps, 84 * pitchSubSteps );
IntMap kMapResonance( 0, 1023, 0, 255 ); IntMap kMapResonance( 0, 1023, 0, 255 );
IntMap kMapCutoff( 0, 1023, 20, 127 ); IntMap kMapCutoff( 0, 1023, 26, 100 );
IntMap kMapAttack( 0, 1023, 3, 100 ); IntMap kMapAttack( 0, 1023, 3, 100 );
IntMap kMapDecay( 0, 1023, 200, 2000 ); IntMap kMapDecay( 0, 1023, 200, 2000 );
IntMap kMapEnvMod( 0, 1023, 0, 255 ); IntMap kMapEnvMod( 0, 1023, 0, 160 );
IntMap kMapDrive( 0, 1023, 0, 400 );
//ADSR <MOZZI_CONTROL_RATE, MOZZI_CONTROL_RATE> envelope;
Ead kEnvelope(MOZZI_CONTROL_RATE); Ead kEnvelope(MOZZI_CONTROL_RATE);
//StateVariable <LOWPASS> lpf;
LowPassFilter lpfr; LowPassFilter lpfr;
Portamento <MOZZI_CONTROL_RATE> glide;
//Global variables //Global variables
byte gain; byte gain;
bool MIDINotePlaying; bool MIDINotePlaying;
bool gateIsHigh = false; bool gateIsHigh = false;
float noteFreq; Q16n16 noteFreq;
bool envSwitchVal; bool envSwitchVal;
bool droneSwitchVal;
byte MIDIChannel; byte MIDIChannel;
byte memCode = "a"; //a = MIDI Channel stored in 1023 byte memCode = "a"; //a = MIDI Channel stored in 1023
int driveAmount;
byte notePlaying;
// long debugMaxSignal = 0;
// long debugMinSignal = 0;
void MIDINoteOn(byte channel, byte note, byte velocity) { void MIDINoteOn(byte channel, byte note, byte velocity) {
noteFreq = mtof((int) note); notePlaying = note;
noteFreq = Q16n16_mtof((int) note);
if (!MIDINotePlaying || !droneSwitchVal) {
glide.setTime(0);
glide.start(note);
kEnvelope.start(); kEnvelope.start();
//envelope.noteOn(); } else {
glide.setTime(GLIDE_TIME);
glide.start(note);
}
MIDINotePlaying = true; MIDINotePlaying = true;
digitalWrite(LED, LOW); digitalWrite(LED, LOW);
} }
void MIDINoteOff(byte channel, byte note, byte velocity) { void MIDINoteOff(byte channel, byte note, byte velocity) {
//envelope.noteOff(); if (note == notePlaying || !droneSwitchVal) {
MIDINotePlaying = false; MIDINotePlaying = false;
digitalWrite(LED, HIGH); digitalWrite(LED, HIGH);
glide.setTime(0);
}
} }
long softClip(long input) { long softClip(long input) {
int threshold = 2048; int threshold = 24000;
if (input < -threshold) { if (input < -threshold) {
return -threshold + (input + threshold) / 4; return -threshold + (input + threshold) / 4;
} else if (input > threshold) { } else if (input > threshold) {
@ -153,7 +167,7 @@ void setup(){
pinMode(DroneSwitch, INPUT_PULLUP); pinMode(DroneSwitch, INPUT_PULLUP);
//MIDI Channel setup //MIDI Channel setup
if(!digitalRead(GateIn)) { //&& !digitalRead(DroneSwitch) && !digitalRead(EnvSwitch)) { //MIDI setup mode, first knob you rotate = MIDI channel if(!digitalRead(GateIn)) { //MIDI setup mode, first knob you rotate = MIDI channel
int knob1Orig = analogRead(Knob1); int knob1Orig = analogRead(Knob1);
int knob2Orig = analogRead(Knob2); int knob2Orig = analogRead(Knob2);
int knob3Orig = analogRead(Knob3); int knob3Orig = analogRead(Knob3);
@ -161,7 +175,7 @@ void setup(){
int knobAOrig = analogRead(KnobA); int knobAOrig = analogRead(KnobA);
int knobDROrig = analogRead(KnobDR); int knobDROrig = analogRead(KnobDR);
int knobSOrig = analogRead(KnobS); int knobSOrig = analogRead(KnobS);
int threshhold = 50; //To reduce noise from the input, needs testing and fine-tuning int threshhold = 50; //To reduce noise from the input
while(true) { while(true) {
if(abs(analogRead(Knob1) - knob1Orig) > threshhold) { if(abs(analogRead(Knob1) - knob1Orig) > threshhold) {
MIDIChannel = 1; MIDIChannel = 1;
@ -227,9 +241,6 @@ void setup(){
startMozzi(); startMozzi();
//envelope.setAttackLevel(255);
kEnvelope.setAttack(3);
digitalWrite(LED, HIGH); digitalWrite(LED, HIGH);
} }
@ -247,74 +258,79 @@ void updateControl(){
int knobAVal = mozziAnalogRead(KnobA); int knobAVal = mozziAnalogRead(KnobA);
int knobDRVal = mozziAnalogRead(KnobDR); int knobDRVal = mozziAnalogRead(KnobDR);
int knobSVal = mozziAnalogRead(KnobS); int knobSVal = mozziAnalogRead(KnobS);
bool droneSwitchVal = digitalRead(DroneSwitch); droneSwitchVal = digitalRead(DroneSwitch);
envSwitchVal = digitalRead(EnvSwitch); envSwitchVal = digitalRead(EnvSwitch);
bool gateInVal = !digitalRead(GateIn); bool gateInVal = !digitalRead(GateIn);
int CVInVal = mozziAnalogRead(CVIn); int CVInVal = mozziAnalogRead(CVIn);
//Remap the values and assign to parameter //Remap the values and assign to parameters
int cutoff = kMapCutoff(knob1Val); int cutoff = kMapCutoff(knob1Val);
int resonance = kMapResonance(knob2Val); int resonance = kMapResonance(knob2Val);
int envMod = kMapEnvMod(knob3Val); int envMod = kMapEnvMod(knob3Val);
driveAmount = kMapDrive(knob4Val);
//Set pitch and play notes on trigger //Set pitch and play notes on trigger
if (!MIDINotePlaying) { // if (!MIDINotePlaying) {
noteFreq = mtof((float) kMapNote(CVInVal << 2) / pitchSubSteps); // noteFreq = mtof((float) kMapNote(CVInVal << 2) / pitchSubSteps);
digitalWrite(LED, !gateInVal); // digitalWrite(LED, !gateInVal);
if (gateInVal && !gateIsHigh) { // if (gateInVal && !gateIsHigh) {
gateIsHigh = true; // gateIsHigh = true;
kEnvelope.start(); // kEnvelope.start();
//envelope.noteOn(); // } else if (!gateInVal && gateIsHigh) {
} else if (!gateInVal && gateIsHigh) { // gateIsHigh = false;
gateIsHigh = false; // }
//envelope.noteOff(); // }
}
} /*else if (MIDINotePlaying && !envelope.playing()) {
MIDINotePlaying = false;
}*/
//Update Filter settings
//lpf.setResonance(resonance);
//Update Envelope Settings //Update Envelope Settings
int attackTime = kMapAttack(knobAVal); int attackTime = kMapAttack(knobAVal);
int decayReleaseTime = kMapDecay(knobDRVal); int decayReleaseTime = kMapDecay(knobDRVal);
//int sustainLevel = kMapSustain(knobSVal);
kEnvelope.setDecay(decayReleaseTime); kEnvelope.setDecay(decayReleaseTime);
kEnvelope.setAttack(attackTime); kEnvelope.setAttack(attackTime);
//envelope.setDecayLevel(sustainLevel);
//envelope.setTimes(attackTime, decayReleaseTime, 30000, decayReleaseTime); //30000 is so the note will sustain 30 seconds unless a noteOff comes //Calculate makeup Gain
//makeupGain = (256L * 127) / (127 + driveAmount); // Q8 fixed-point compensation factor
//Set Oscilator Frequency //Set Oscilator Frequency
float oscFreq = noteFreq; Q16n16 oscFreq;
aSquare.setFreq(oscFreq); oscFreq = glide.next();
aSaw.setFreq(oscFreq); aSquare.setFreq_Q16n16(oscFreq);
aSaw.setFreq_Q16n16(oscFreq);
//envelope.update();
//int env = envelope.next();
int env = kEnvelope.next(); int env = kEnvelope.next();
if(!droneSwitchVal) { //if(!droneSwitchVal) {
gain = env; gain = env;
} else { //} else {
gain = 255; // gain = 255;
} //}
//lpf.setCentreFreq(constrain(cutoff + (((long) env * envMod) >> 8), 20, 4096));
lpfr.setCutoffFreqAndResonance(constrain(cutoff + (((long)env * envMod * cutoff)>>16), 0, 255), resonance); lpfr.setCutoffFreqAndResonance(constrain(cutoff + (((long)env * envMod * cutoff)>>16), 0, 255), resonance);
// static int debugCounter = 0;
// if (++debugCounter >= 100) { // adjust to taste - print every ~100 control cycles
// Serial.print("min: "); Serial.print(debugMinSignal);
// Serial.print(" max: "); Serial.println(debugMaxSignal);
// debugMaxSignal = 0;
// debugMinSignal = 0;
// debugCounter = 0;
// }
} }
AudioOutput updateAudio(){ AudioOutput updateAudio(){
long signal; long signal;
if(envSwitchVal) { if(envSwitchVal) {
signal = (aSquare.next() * 3) >> 2;// 0.75 gain to ~ match saw level signal = (aSquare.next() * 3) >> 2; // tricky way to set 0.75 gain to ~ match saw level
} else { } else {
signal = aSaw.next(); signal = aSaw.next();
} }
signal = lpfr.next(signal); //filter signal = lpfr.next(signal); //filter
signal = (signal * gain); //envelope signal = (signal * gain); //envelope
//signal = softClip((signal * (127 + driveAmount)) >> 6); //overdrive //signal = softClip((signal * (127 + driveAmount)) >> 9); //overdrive
//signal = (signal * makeupGain) >> 7; //makeup gain compensation
// if (signal > debugMaxSignal) debugMaxSignal = signal;
// if (signal < debugMinSignal) debugMinSignal = signal;
return MonoOutput::fromNBit(17, signal).clip(); return MonoOutput::fromNBit(17, signal).clip();
} }

View File

@ -1,8 +1,7 @@
//Settings //Settings
const int pitchSubSteps = 16; //set how many steps are there between semitones. set to 1 to quantize to semitones const int pitchSubSteps = 16; //set how many steps are there between semitones. set to 1 to quantize to semitones
const int driveAmount = 250;
const bool oversamplingEnabled = false; //makes V/OCT tracking more precise, but adds a little portamento
#define MOZZI_CONTROL_RATE 256 #define MOZZI_CONTROL_RATE 256
#define GLIDE_TIME 150
//Hardware Definitions //Hardware Definitions
#define Knob1 A6 //Intensity #define Knob1 A6 //Intensity