Luke Cartwright / Mbed 2 deprecated ELEC2645_Project_el18loc_nearlythere

Dependencies:   mbed

Committer:
lukeocarwright
Date:
Tue May 26 14:21:36 2020 +0000
Revision:
31:cfdb014ff086
Parent:
30:08cc4ec58d07
Final Pad edit (-API check)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lukeocarwright 18:204cd747b54a 1 #include "mbed.h"
lukeocarwright 18:204cd747b54a 2 #include "Envelope.h"
lukeocarwright 19:08862f49cd9e 3 //Global var.
lukeocarwright 31:cfdb014ff086 4 volatile int silent_flag; //used to trigger note off
lukeocarwright 18:204cd747b54a 5
lukeocarwright 18:204cd747b54a 6 //constructor/destructor
lukeocarwright 18:204cd747b54a 7 Envelope::Envelope()
lukeocarwright 18:204cd747b54a 8 {
lukeocarwright 18:204cd747b54a 9 }
lukeocarwright 18:204cd747b54a 10 Envelope::~Envelope()
lukeocarwright 18:204cd747b54a 11 {
lukeocarwright 18:204cd747b54a 12 }
lukeocarwright 18:204cd747b54a 13 //PUBLIC------------------------------------------------------------------------
lukeocarwright 18:204cd747b54a 14 uint16_t Envelope::env_in(int a, int d, int s, int r, int in, bool init)
lukeocarwright 18:204cd747b54a 15 {
lukeocarwright 31:cfdb014ff086 16 if(init==true) { //initial value set
lukeocarwright 31:cfdb014ff086 17 //printf("INITIATING ENVELOPE\n");
lukeocarwright 19:08862f49cd9e 18 av=a*a*52;// max 63700 = 4 seconds (ish)
lukeocarwright 19:08862f49cd9e 19 dv=d*d*52;// max 63700 = 4 seconds
lukeocarwright 19:08862f49cd9e 20 sv=s*s*53;// max 64925 = Max Volume (ish)
lukeocarwright 19:08862f49cd9e 21 rv=r*r*52;// max 63700 = 4 seconds
lukeocarwright 19:08862f49cd9e 22 samples=0; //initialise sample counter
lukeocarwright 18:204cd747b54a 23 a_vec=a_vector_calc(av); //initialise a value for a dif
lukeocarwright 30:08cc4ec58d07 24 d_vec=d_vector_calc(dv,sv); //initialise a value for d dif
lukeocarwright 18:204cd747b54a 25 at=0;//initialises
lukeocarwright 31:cfdb014ff086 26 #ifdef SLOW_TIME //SLOW_TIME print variables
lukeocarwright 19:08862f49cd9e 27 printf("INITIATING ENVELOPE\n");
lukeocarwright 19:08862f49cd9e 28 printf("a=%d,av= %d,a vector= %d\n",a,av,a_vec);
lukeocarwright 19:08862f49cd9e 29 printf("d=%d,dv= %d,d vector= %d\n",d,dv,d_vec);
lukeocarwright 19:08862f49cd9e 30 printf("s=%d,sv= %d\n",s,sv);
lukeocarwright 19:08862f49cd9e 31 #endif
lukeocarwright 18:204cd747b54a 32 }
lukeocarwright 31:cfdb014ff086 33 if (samples<=av) { //Attack transient
lukeocarwright 31:cfdb014ff086 34 at=at+a_vec; //itterates multiplier
lukeocarwright 19:08862f49cd9e 35 out=(in*at/63980)+32767;
lukeocarwright 19:08862f49cd9e 36 //printf("ATTACK");
lukeocarwright 31:cfdb014ff086 37 } else if(samples>av && samples<=av+dv) { //Decay Transient
lukeocarwright 31:cfdb014ff086 38 at=at-d_vec; //iterates multiplier
lukeocarwright 19:08862f49cd9e 39 out=(in*at/63980)+32767;
lukeocarwright 19:08862f49cd9e 40 //printf("DECAY");
lukeocarwright 31:cfdb014ff086 41 } else if (samples>av+dv) { //Sustain
lukeocarwright 19:08862f49cd9e 42 out=(in*sv/65520)+32767;
lukeocarwright 19:08862f49cd9e 43 }
lukeocarwright 31:cfdb014ff086 44 #ifdef SLOW_TIME //SLOW_TIME print statements
lukeocarwright 19:08862f49cd9e 45 printf("FILTER:\nIN= %d,\nOUT (+offs) = %d\n",in,out);
lukeocarwright 19:08862f49cd9e 46 printf("MULTIPLIER = %d\n",at);
lukeocarwright 18:204cd747b54a 47 printf("SAMPLE_%d\n",samples);
lukeocarwright 19:08862f49cd9e 48 #endif
lukeocarwright 31:cfdb014ff086 49 samples=samples++; //itterates samples
lukeocarwright 18:204cd747b54a 50 return (out);
lukeocarwright 18:204cd747b54a 51 }
lukeocarwright 18:204cd747b54a 52
lukeocarwright 19:08862f49cd9e 53 uint16_t Envelope::release(int s, int r, int in, bool init)
lukeocarwright 19:08862f49cd9e 54 {
lukeocarwright 31:cfdb014ff086 55 if (init==true) { //Initial Value Set
lukeocarwright 19:08862f49cd9e 56 sv=s*s*53;// max 64925 = Max Volume (ish)
lukeocarwright 19:08862f49cd9e 57 rv=r*r*52;// max 63700 = 4 seconds
lukeocarwright 19:08862f49cd9e 58 r_vec=r_vector_calc(sv,rv);
lukeocarwright 31:cfdb014ff086 59 at=sv; //Initial Value
lukeocarwright 19:08862f49cd9e 60 samples_r=0;
lukeocarwright 19:08862f49cd9e 61 silent_flag=0;
lukeocarwright 19:08862f49cd9e 62 return(0);
lukeocarwright 31:cfdb014ff086 63 } else if (samples_r<=rv) { //upto end of decay transient
lukeocarwright 31:cfdb014ff086 64 at=at-r_vec; //itterates Multiplier
lukeocarwright 19:08862f49cd9e 65 out=(in*at/63980)+32767;
lukeocarwright 19:08862f49cd9e 66 }
lukeocarwright 31:cfdb014ff086 67 if(samples_r>rv) { //past note end
lukeocarwright 31:cfdb014ff086 68 silent_flag=1; //Triggers Note off
lukeocarwright 19:08862f49cd9e 69 }
lukeocarwright 31:cfdb014ff086 70 samples_r++; //itterates samples
lukeocarwright 19:08862f49cd9e 71 return(out);
lukeocarwright 19:08862f49cd9e 72 }
lukeocarwright 18:204cd747b54a 73 //PRIVATE:----------------------------------------------------------------------
lukeocarwright 19:08862f49cd9e 74 int Envelope::a_vector_calc(int av)
lukeocarwright 19:08862f49cd9e 75 {
lukeocarwright 19:08862f49cd9e 76 a_vec=64925/av;
lukeocarwright 18:204cd747b54a 77 return(a_vec);
lukeocarwright 19:08862f49cd9e 78 }
lukeocarwright 19:08862f49cd9e 79
lukeocarwright 19:08862f49cd9e 80 int Envelope::d_vector_calc(int dv, int sv)
lukeocarwright 19:08862f49cd9e 81 {
lukeocarwright 19:08862f49cd9e 82 d_vec=(64925-sv)/dv;
lukeocarwright 19:08862f49cd9e 83 return(d_vec);
lukeocarwright 19:08862f49cd9e 84 }
lukeocarwright 19:08862f49cd9e 85 int Envelope::r_vector_calc(int sv,int rv)
lukeocarwright 19:08862f49cd9e 86 {
lukeocarwright 19:08862f49cd9e 87 return(sv/rv);
lukeocarwright 19:08862f49cd9e 88 }