I'm working on a synthesiser with mozzi, I have some button (soon to be replaces by a proper keyboard) to generate the tones but I can't seem to figure out how to make an ADSR envelope working.
This is the code (I have the values for the Attack, Decay, Sustain and Release fixed with the value to test it but it doesn't work, any suggestion?
/*
* my Synth - Bruno Di Micco
*/
#include <MozziGuts.h>
#include <Oscil.h> // oscillator template
#include <tables/square_no_alias512_int8.h>
#include <tables/sin512_int8.h>
#include <tables/triangle512_int8.h>
#include <tables/saw512_int8.h>
#include <ADSR.h>
#define CONTROL_RATE 128
#define ATTACK_LEVEL 255
#define DECAY_LEVEL 255
unsigned char gain;
unsigned char ATTACK;
unsigned char DECAY;
unsigned char SUSTAIN;
unsigned char RELEASE;
ADSR <CONTROL_RATE, CONTROL_RATE> envelope;
// use: Oscil <table_size, update_rate> oscilName (wavetable), look in .h file of table #included above
Oscil <TRIANGLE512_NUM_CELLS, AUDIO_RATE> aOscil(TRIANGLE512_DATA);
Oscil <TRIANGLE512_NUM_CELLS, AUDIO_RATE> aSOscil(TRIANGLE512_DATA);
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int val = 0;
float sensor_value = 0;
int sensor_wave = 0;
float sensor_valuesOsc = 0;
int sensor_wavesOsc = 0;
int tones[] = { 261, 294, 329, 349, 392, 440, 493 };
void setup()
{
startMozzi(); // uses the default control rate of 64, defined in mozzi_config.h
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void choosetable(int waveNumber)
{
switch(waveNumber)
{
case 0:
aOscil.setTable(SIN512_DATA);
break;
case 1:
aOscil.setTable(SQUARE_NO_ALIAS512_DATA);
break;
case 2:
aOscil.setTable(TRIANGLE512_DATA);
break;
case 3:
aOscil.setTable(SAW512_DATA);
break;
}
}
void choosetablesOsc(int waveNumber)
{
switch(waveNumber)
{
case 0:
aSOscil.setTable(SIN512_DATA);
break;
case 1:
aSOscil.setTable(SQUARE_NO_ALIAS512_DATA);
break;
case 2:
aSOscil.setTable(TRIANGLE512_DATA);
break;
case 3:
aSOscil.setTable(SAW512_DATA);
break;
}
}
void playTone() {
digitalWrite(ledPin, LOW);
sensor_value = 0;
// sensor_wave = 0;
sensor_valuesOsc = 0;
//sensor_wavesOsc = 0;
for (int i = 0; i <5; i++) {
// play the tone corresponding to the note name
if ( digitalRead(i+2) == HIGH) {
// turn LED on:
}
else {
// turn LED off:
digitalWrite(ledPin, HIGH);
sensor_value = tones[i];
// sensor_wave = 400;
sensor_valuesOsc = tones[i];
//sensor_wavesOsc = 400;
}
}
}
void updateControl()
{
//ATTACK = map(mozziAnalogRead(0), 0, 1023, 0, 50);
// DECAY = map(mozziAnalogRead(0), 0, 1023, 0, 50);
//SUSTAIN = map(mozziAnalogRead(0), 0, 1023, 0, 60000);
//RELEASE = map(mozziAnalogRead(0), 0, 1023, 0, 2000);
envelope.setADLevels(200,200);
envelope.setTimes(200,100,100,100);
envelope.update();
gain = envelope.next();
sensor_value = 0;//map(mozziAnalogRead(0),1023,0,1,1000); // value is 0-1023
sensor_wave = map(mozziAnalogRead(1),1020,0,0,3);
int octave = map(mozziAnalogRead(3),1020,0,1,4);
if (octave == 3) octave = 4;
sensor_valuesOsc = 0;//map(mozziAnalogRead(0),1023,0,1,1000); // value is 0-1023
sensor_wavesOsc =map(mozziAnalogRead(2),1020,0,0,3);
choosetablesOsc(sensor_wavesOsc);
playTone();
choosetable(sensor_wave);
aOscil.setFreq(sensor_value/octave); // set the frequency straight from the sensor reading
aSOscil.setFreq(sensor_valuesOsc/8); // set the frequency straight from the sensor reading
}
int updateAudio()
{
return gain *(aOscil.next()+aSOscil.next()))>>8; // 8 bits scaled up to 14 bits for Hi-Fi
}
void loop()
{
audioHook(); // required here
}