Luke O. Cartwright 201225242

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Envelope.cpp Source File

Envelope.cpp

00001 #include "mbed.h"
00002 #include "Envelope.h"
00003 //Global var.
00004 volatile int silent_flag; //used to trigger note off
00005 
00006 //constructor/destructor
00007 Envelope::Envelope()
00008 {
00009 }
00010 Envelope::~Envelope()
00011 {
00012 }
00013 //PUBLIC------------------------------------------------------------------------
00014 uint16_t Envelope::env_in(int a, int d, int s, int r, int in, bool init)
00015 {
00016     if(init==true) { //initial value set
00017         //printf("INITIATING ENVELOPE\n");
00018         av=a*a*52;// max 63700 = 4 seconds (ish)
00019         dv=d*d*52;// max 63700 = 4 seconds
00020         sv=s*s*53;// max 64925 =  Max Volume (ish)
00021         rv=r*r*52;// max 63700 = 4 seconds
00022         samples=0; //initialise sample counter
00023         a_vec=a_vector_calc(av); //initialise a value for a dif
00024         d_vec=d_vector_calc(dv,sv); //initialise a value for d dif
00025         at=0;//initialises
00026 #ifdef SLOW_TIME //SLOW_TIME print variables
00027         printf("INITIATING ENVELOPE\n");
00028         printf("a=%d,av= %d,a vector= %d\n",a,av,a_vec);
00029         printf("d=%d,dv= %d,d vector= %d\n",d,dv,d_vec);
00030         printf("s=%d,sv= %d\n",s,sv);
00031 #endif
00032     }
00033     if (samples<=av) { //Attack transient
00034         at=at+a_vec; //itterates multiplier
00035         out=(in*at/63980)+32767;
00036         //printf("ATTACK");
00037     } else if(samples>av && samples<=av+dv) { //Decay Transient
00038         at=at-d_vec; //iterates multiplier
00039         out=(in*at/63980)+32767;
00040         //printf("DECAY");
00041     } else if (samples>av+dv) { //Sustain
00042         out=(in*sv/65520)+32767;
00043     }
00044  #ifdef SLOW_TIME //SLOW_TIME print statements
00045     printf("FILTER:\nIN= %d,\nOUT (+offs) = %d\n",in,out);
00046     printf("MULTIPLIER = %d\n",at);
00047     printf("SAMPLE_%d\n",samples);
00048 #endif
00049     samples=samples++; //itterates samples
00050     return (out);
00051 }
00052 
00053 uint16_t Envelope::release(int s, int r, int in, bool init)
00054 {
00055     if (init==true) { //Initial Value Set
00056         sv=s*s*53;// max 64925 =  Max Volume (ish)
00057         rv=r*r*52;// max 63700 = 4 seconds
00058         r_vec=r_vector_calc(sv,rv);
00059         at=sv; //Initial Value
00060         samples_r=0;
00061         silent_flag=0;
00062         return(0);
00063     } else if (samples_r<=rv) { //upto end of decay transient
00064         at=at-r_vec; //itterates Multiplier
00065         out=(in*at/63980)+32767;
00066     }
00067     if(samples_r>rv) { //past note end
00068         silent_flag=1; //Triggers Note off
00069     }
00070     samples_r++; //itterates samples
00071     return(out);
00072 }
00073 //PRIVATE:----------------------------------------------------------------------
00074 int Envelope::a_vector_calc(int av)
00075 {
00076     a_vec=64925/av;
00077     return(a_vec);
00078 }
00079 
00080 int Envelope::d_vector_calc(int dv, int sv)
00081 {
00082     d_vec=(64925-sv)/dv;
00083     return(d_vec);
00084 }
00085 int Envelope::r_vector_calc(int sv,int rv)
00086 {
00087     return(sv/rv);
00088 }