Problems with ADSR

185 views
Skip to first unread message

Bruno Di Micco

unread,
Mar 18, 2015, 4:33:30 PM3/18/15
to mozzi...@googlegroups.com
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
}

Bruno Di Micco

unread,
Mar 23, 2015, 7:32:44 AM3/23/15
to mozzi...@googlegroups.com
No clues? :)

Tim Barrass

unread,
Mar 23, 2015, 7:57:15 AM3/23/15
to mozzi...@googlegroups.com
Hi Bruno,
sorry for missing your question, I am very busy at the moment.  I won't be able to test your code for a few weeks, but can you describe what it is doing/what is going wrong? 

One thing I notice is this line:

   return   gain *(aOscil.next()+aSOscil.next()))>>8; // 8 bits scaled up to 14 bits for Hi-Fi

you need to cast to long to give room for your multiplication, since you have declared gain as a uchar, and you probably need to also give room for your addition, and if you are using HIFI you would >> 3, since
8bits * (8bits+8bits=9bits) = 17bits

   return ((long) gain*((int)aOscil.next()+aSOscil.next()))>>3;

I'm not sure what your playTone() is meant to do, but it might not be good to use it this way in updateControl(), as it runs a loop which might block the processor for long enought to cause glitches.

Not sure, but it looks like there are probably a few other things to iron out.  Are you able to gradually build on one of the working Mozzi examples, testing at each stage that things are working as you expect?

Tim


--
You received this message because you are subscribed to the Google Groups "Mozzi-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mozzi-users...@googlegroups.com.
To post to this group, send email to mozzi...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/mozzi-users/c5a1b0c6-0b1d-4630-a720-31a6aee23265%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Bruno Di Micco

unread,
Mar 23, 2015, 8:09:00 AM3/23/15
to mozzi...@googlegroups.com
Hi Tim,
 
Thanks for your tips, I will check them and let you know.
 
The playTone() simply assignes the tones to be played to each of the buttons I have.
 
Thanks again
Bruno
Reply all
Reply to author
Forward
0 new messages