Page 1 of 1

audio codec ADSR is it posible?

PostPosted: Fri Mar 22, 2013 12:25 am
by dsese1234
Hi guys,

I am wondering how to code an adsr with the audio codec. i am using arduino on it. is it posible? i have an video about the adsr. here is the video. nice sound! :)

any idea?


Re: audio codec ADSR is it posible?

PostPosted: Fri Mar 22, 2013 2:33 pm
by guest
it should be possible to do something like that.

the first thing to accomplish is getting some sort of triggering based on input threshold. so maybe make a simple noise gate first:

if audio_in < value -> then audio_out = 0; else audio_out = audio_in;

you can then play around with the value to see where a good trigger level is.
the next thing to accomplish is varying the amplitude with time. so set up a variable which is a counter. this will count the amount of times you have fallen through the ISR (put this in the ISR loop). you can multiply your input value by this counter to create an envelope.

here is some pseudocode

Code: Select all
at beginning

unsigned int env = 0;
byte trig = 0;
int threshold = some value;
byte decay = 0;

in ISR()

// check if doing envelope or not
if (trig = 0) {
  if (audio_in > threshold) { // if not check if trigger threshold has been reached
    trig = 1;
  audio_out = 0;

else {
  if (decay = 0) { // if not decaying, upcount your envelope
    if (env > 0xfffe) {
    decay = 1;
  else { // otherwise downcount
    if (env == 0) {
      decay = 0;
      trig = 0;
audio_out = audio_in * env; // multiply audio by envelope - be sure to use special math functions here

after you have something like this working, you can change how the env value gets incremented and decremented for seperate attack and decay values. be sure to look at some of the examples of doing the special math multiplies, these take way less time.

Re: audio codec ADSR is it posible?

PostPosted: Fri Mar 22, 2013 6:13 pm
by dsese1234
Thank you so much for replying, i am analyzing your code. can you make it full code? this is nice actually but not sure yet how to apply the code on arduino. i am new about it., thanks in advance! =)