Compare commits

..

10 Commits

2 changed files with 86 additions and 67 deletions

View File

@ -7,18 +7,17 @@
// This code is licenced under GPL v3 or later
// todo:
// add glide handling
// make a glide on/off swithc instead of drone, both for MIDI and CV (sample pitch on gate)
// add accent handling
// debug accent handling that overloads the filter, also accent can be too short or too long if it's relying on midi note
// add overdrive
// frequency from CV (noteFreq) could be Q16n16 for consistency
#include <Mozzi.h>
#include <MIDI.h>
#include <Oscil.h>
#include <MetaOscil.h>
//#include <StateVariable.h>
#include <ResonantFilter.h>
//#include <ADSR.h>
#include <Ead.h>
#include <Portamento.h>
#include <mozzi_midi.h>
#include <IntMap.h>
#include <FixMath.h>
@ -100,43 +99,63 @@ Oscil <SAW_MAX_8192_AT_16384_512_NUM_CELLS, MOZZI_AUDIO_RATE> aSaw8192(SAW_MAX_8
MetaOscil<SAW_MAX_138_AT_16384_512_NUM_CELLS, MOZZI_AUDIO_RATE, 16> aSaw {&aSaw138, &aSaw154, &aSaw182, &aSaw240, &aSaw292, &aSaw341, &aSaw390, &aSaw431, &aSaw512, &aSaw630, &aSaw744, &aSaw1170, &aSaw1638, &aSaw2730, &aSaw4096, &aSaw8192};
IntMap kMapNote( 0, 4095, 24 * pitchSubSteps, 84 * pitchSubSteps );
IntMap kMapCutoff( 0, 1023, 26, 100 );
IntMap kMapEnvMod( 0, 1023, 0, 140 );
IntMap kMapResonance( 0, 1023, 0, 255 );
IntMap kMapCutoff( 0, 1023, 20, 127 );
IntMap kMapAttack( 0, 1023, 3, 100 );
IntMap kMapDecay( 0, 1023, 200, 2000 );
IntMap kMapEnvMod( 0, 1023, 0, 255 );
IntMap kMapDrive( 0, 1023, 0, 400 );
IntMap kMapAttack( 0, 1023, 1, 100 );
IntMap kMapDecay( 0, 1023, 100, 1500 );
IntMap kMapAccentLevel( 0, 1023, 0, 128 );
//ADSR <MOZZI_CONTROL_RATE, MOZZI_CONTROL_RATE> envelope;
Ead kEnvelope(MOZZI_CONTROL_RATE);
//StateVariable <LOWPASS> lpf;
LowPassFilter lpfr;
Portamento <MOZZI_CONTROL_RATE> kGlide;
//Global variables
byte gain;
bool MIDINotePlaying;
bool MIDINoteFaded = true;
bool gateIsHigh = false;
float noteFreq;
bool envSwitchVal;
bool droneSwitchVal;
byte MIDIChannel;
byte memCode = "a"; //a = MIDI Channel stored in 1023
int driveAmount;
byte notePlaying;
bool accent = false;
void MIDINoteOn(byte channel, byte note, byte velocity) {
noteFreq = mtof((int) note);
notePlaying = note;
accent = false; //reset if the previous note was accented
if (velocity >= 127) {
accent = true;
}
if (!MIDINotePlaying || !droneSwitchVal) {
kGlide.setTime(2); //4 at 256, 2 at 512 control rate to avoid portamento glitches caused by 0
} else {
kGlide.setTime(GLIDE_TIME);
}
kGlide.start(note);
if (!MIDINotePlaying || !droneSwitchVal) {
kEnvelope.start();
//envelope.noteOn();
}
MIDINotePlaying = true;
MIDINoteFaded = false;
digitalWrite(LED, LOW);
}
void MIDINoteOff(byte channel, byte note, byte velocity) {
//envelope.noteOff();
if (note == notePlaying || !droneSwitchVal) {
MIDINotePlaying = false;
digitalWrite(LED, HIGH);
}
}
long softClip(long input) {
int threshold = 2048;
int threshold = 24000;
if (input < -threshold) {
return -threshold + (input + threshold) / 4;
} else if (input > threshold) {
@ -153,7 +172,7 @@ void setup(){
pinMode(DroneSwitch, INPUT_PULLUP);
//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 knob2Orig = analogRead(Knob2);
int knob3Orig = analogRead(Knob3);
@ -161,7 +180,7 @@ void setup(){
int knobAOrig = analogRead(KnobA);
int knobDROrig = analogRead(KnobDR);
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) {
if(abs(analogRead(Knob1) - knob1Orig) > threshhold) {
MIDIChannel = 1;
@ -227,9 +246,6 @@ void setup(){
startMozzi();
//envelope.setAttackLevel(255);
kEnvelope.setAttack(3);
digitalWrite(LED, HIGH);
}
@ -247,74 +263,78 @@ void updateControl(){
int knobAVal = mozziAnalogRead(KnobA);
int knobDRVal = mozziAnalogRead(KnobDR);
int knobSVal = mozziAnalogRead(KnobS);
bool droneSwitchVal = digitalRead(DroneSwitch);
droneSwitchVal = digitalRead(DroneSwitch);
envSwitchVal = digitalRead(EnvSwitch);
bool gateInVal = !digitalRead(GateIn);
int CVInVal = mozziAnalogRead(CVIn);
//Remap the values and assign to parameter
int cutoff = kMapCutoff(knob1Val);
int resonance = kMapResonance(knob2Val);
//Remap the values and assign to parameters
int accentLevel = kMapAccentLevel(knobSVal);
int baseCutoff = kMapCutoff(knob1Val);
int cutoff = accent? (baseCutoff + (((long)accentLevel * (100 - baseCutoff)) >> 7)) : baseCutoff;
int resonance = accent? (255 - (((long)(128 - accentLevel) * (255 - kMapResonance(knob2Val))) >> 7)) : kMapResonance(knob2Val);
int envMod = kMapEnvMod(knob3Val);
driveAmount = kMapDrive(knob4Val);
//Set pitch and play notes on trigger
if (!MIDINotePlaying) {
if (MIDINoteFaded) {
noteFreq = mtof((float) kMapNote(CVInVal << 2) / pitchSubSteps);
digitalWrite(LED, !gateInVal);
if (gateInVal && !gateIsHigh) {
gateIsHigh = true;
kEnvelope.start();
//envelope.noteOn();
} else if (!gateInVal && gateIsHigh) {
gateIsHigh = false;
//envelope.noteOff();
}
} /*else if (MIDINotePlaying && !envelope.playing()) {
MIDINotePlaying = false;
}*/
//Update Filter settings
//lpf.setResonance(resonance);
}
//Update Envelope Settings
int attackTime = kMapAttack(knobAVal);
int decayReleaseTime = kMapDecay(knobDRVal);
//int sustainLevel = kMapSustain(knobSVal);
kEnvelope.setDecay(decayReleaseTime);
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
int baseDecay = kMapDecay(knobDRVal);
int decayTime = accent? (100 + (((long)(128 - accentLevel) * (baseDecay - 100)) >> 7)) : baseDecay;
kEnvelope.setDecay(decayTime);
kEnvelope.setAttack(accent? 1 : attackTime);
//Calculate makeup Gain
//makeupGain = ((long)256 * 127) / (127 + driveAmount);
//Set Oscilator Frequency
float oscFreq = noteFreq;
aSquare.setFreq(oscFreq);
aSaw.setFreq(oscFreq);
Q16n16 oscFreq;
if (!MIDINoteFaded) {
oscFreq = kGlide.next();
aSquare.setFreq_Q16n16(oscFreq);
aSaw.setFreq_Q16n16(oscFreq);
} else {
aSquare.setFreq(noteFreq);
aSaw.setFreq(noteFreq);
}
//envelope.update();
//int env = envelope.next();
int env = kEnvelope.next();
if(!droneSwitchVal) {
gain = env;
} else {
gain = 255;
if (!MIDINotePlaying && !MIDINoteFaded && env == 0) {
MIDINoteFaded = true;
accent = false;
}
//lpf.setCentreFreq(constrain(cutoff + (((long) env * envMod) >> 8), 20, 4096));
lpfr.setCutoffFreqAndResonance(constrain(cutoff + (((long)env * envMod * cutoff)>>16), 0, 255), resonance);
gain = accent? constrain(env + (((long)env * accentLevel) >> 7), 0, 255) : env; //needs finetuning
lpfr.setCutoffFreqAndResonance(constrain(cutoff + (((long)env * envMod * cutoff)>>16), 0, 255), resonance);
}
AudioOutput updateAudio(){
long signal;
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 {
signal = aSaw.next();
}
signal = signal >> 2; //lowering the gain before filter to avoid filter overflow bug
signal = lpfr.next(signal); //filter
signal = (signal * gain); //envelope
//signal = softClip((signal * (127 + driveAmount)) >> 6); //overdrive
signal = (signal * gain) << 2; //envelope
//signal = softClip((signal * (127 + driveAmount)) >> 9); //overdrive
//signal = (signal * makeupGain) >> 7; //makeup gain compensation
return MonoOutput::fromNBit(17, signal).clip();
}

View File

@ -1,17 +1,16 @@
//Settings
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 512
#define GLIDE_TIME 60
//Hardware Definitions
#define Knob1 A6 //Intensity
#define Knob2 A4 //Modulator frequency ratio/Harmonics
#define Knob3 A2 //LFO Frequency
#define Knob4 A0 //LFO Shape
#define Knob1 A6 //Cutoff
#define Knob2 A4 //Resonance
#define Knob3 A2 //Env.Mod
#define Knob4 A0 //Overdrive
#define KnobA A5 //Attack
#define KnobDR A3 //Decay and Release
#define KnobS A1 //Sustain
#define KnobDR A3 //Decay
#define KnobS A1 //Accent
#define CVIn A7 //CV input and Pitch knob
#define GateIn 10
#define EnvSwitch 11