for percussion1

Dependencies:   mbed

Fork of penkich_test_percussion by kitac_mbed_WS

Committer:
penkich
Date:
Sun Aug 30 07:28:07 2015 +0000
Revision:
3:396b2628e3d7
Parent:
2:269fadd5d1f2
penkich_percussion_1_LPC1114

Who changed what in which revision?

UserRevisionLine numberNew contents of line
penkich 0:e363f64b5744 1 #include "mbed.h"
penkich 0:e363f64b5744 2
penkich 3:396b2628e3d7 3 AnalogIn sw(dp9);
penkich 2:269fadd5d1f2 4
penkich 0:e363f64b5744 5 Ticker timer;
penkich 3:396b2628e3d7 6 PwmOut speaker(dp1);
penkich 0:e363f64b5744 7 Ticker envelope;
penkich 3:396b2628e3d7 8 PwmOut env(dp2);
penkich 0:e363f64b5744 9 static int tms_per10=0;
penkich 0:e363f64b5744 10
penkich 0:e363f64b5744 11 void env_out(void){
penkich 0:e363f64b5744 12 float attack_ms =10;
penkich 0:e363f64b5744 13 float decay_ms = 10;
penkich 1:3252e4fbccab 14 float sustain_ms = 20;
penkich 1:3252e4fbccab 15 float sustain = 0.2;
penkich 1:3252e4fbccab 16 float release_ms = 5000;
penkich 0:e363f64b5744 17
penkich 0:e363f64b5744 18 float out_level;
penkich 0:e363f64b5744 19 tms_per10++;
penkich 0:e363f64b5744 20 if (tms_per10 > (attack_ms + decay_ms + sustain_ms + release_ms) * 10){
penkich 0:e363f64b5744 21 env.write(0.0f);
penkich 0:e363f64b5744 22 }else if(tms_per10 > (attack_ms + decay_ms + sustain_ms) * 10){
penkich 0:e363f64b5744 23 out_level = sustain/(tms_per10 - (attack_ms + decay_ms + sustain_ms)) * release_ms;
penkich 0:e363f64b5744 24 env.write(out_level);
penkich 0:e363f64b5744 25 }else if(tms_per10 > (attack_ms + decay_ms) * 10){
penkich 0:e363f64b5744 26 env.write(sustain);
penkich 0:e363f64b5744 27 }else if(tms_per10 > attack_ms * 10){
penkich 0:e363f64b5744 28 env.write(1.0f);
penkich 0:e363f64b5744 29 }else{
penkich 1:3252e4fbccab 30 out_level = 1.0f/attack_ms * tms_per10;
penkich 0:e363f64b5744 31 env.write(out_level);
penkich 0:e363f64b5744 32 }
penkich 0:e363f64b5744 33 }
penkich 0:e363f64b5744 34
penkich 0:e363f64b5744 35 void sound_out(void){
penkich 0:e363f64b5744 36 speaker.write(((float)rand()/RAND_MAX));
penkich 0:e363f64b5744 37 }
penkich 0:e363f64b5744 38
penkich 0:e363f64b5744 39
penkich 0:e363f64b5744 40 int main()
penkich 0:e363f64b5744 41 {
penkich 3:396b2628e3d7 42 // sw.mode(PullUp);
penkich 0:e363f64b5744 43 while(1){
penkich 3:396b2628e3d7 44 if (sw >= 0.3f){
penkich 0:e363f64b5744 45 tms_per10=0;
penkich 0:e363f64b5744 46 speaker.period_us(10);
penkich 0:e363f64b5744 47 env.period_us(10);
penkich 0:e363f64b5744 48 timer.attach_us(&sound_out,100); //10kHz
penkich 0:e363f64b5744 49 envelope.attach_us(&env_out,100); //10kHz
penkich 3:396b2628e3d7 50 // wait(1.0f);
penkich 0:e363f64b5744 51 }
penkich 0:e363f64b5744 52 wait(0.01f);
penkich 0:e363f64b5744 53 }
penkich 0:e363f64b5744 54 }