TAISAKU NISHIJIMA
/
penkich_percussion_2_NUCLEO
penkich_percussion_2_NUCLEO
Fork of penkich_test_percussion by
main.cpp@0:e363f64b5744, 2015-08-18 (annotated)
- Committer:
- penkich
- Date:
- Tue Aug 18 12:26:25 2015 +0000
- Revision:
- 0:e363f64b5744
- Child:
- 1:b102b9476594
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
penkich | 0:e363f64b5744 | 1 | #include "mbed.h" |
penkich | 0:e363f64b5744 | 2 | |
penkich | 0:e363f64b5744 | 3 | DigitalIn sw(dp9); |
penkich | 0:e363f64b5744 | 4 | Ticker timer; |
penkich | 0:e363f64b5744 | 5 | PwmOut speaker(dp18); |
penkich | 0:e363f64b5744 | 6 | Ticker envelope; |
penkich | 0:e363f64b5744 | 7 | PwmOut env(dp24); |
penkich | 0:e363f64b5744 | 8 | static int tms_per10=0; |
penkich | 0:e363f64b5744 | 9 | |
penkich | 0:e363f64b5744 | 10 | void env_out(void){ |
penkich | 0:e363f64b5744 | 11 | float attack_ms =10; |
penkich | 0:e363f64b5744 | 12 | float decay_ms = 10; |
penkich | 0:e363f64b5744 | 13 | float sustain_ms = 200; |
penkich | 0:e363f64b5744 | 14 | float sustain = 0.5; |
penkich | 0:e363f64b5744 | 15 | float release_ms = 400; |
penkich | 0:e363f64b5744 | 16 | |
penkich | 0:e363f64b5744 | 17 | float out_level; |
penkich | 0:e363f64b5744 | 18 | tms_per10++; |
penkich | 0:e363f64b5744 | 19 | if (tms_per10 > (attack_ms + decay_ms + sustain_ms + release_ms) * 10){ |
penkich | 0:e363f64b5744 | 20 | env.write(0.0f); |
penkich | 0:e363f64b5744 | 21 | }else if(tms_per10 > (attack_ms + decay_ms + sustain_ms) * 10){ |
penkich | 0:e363f64b5744 | 22 | out_level = sustain/(tms_per10 - (attack_ms + decay_ms + sustain_ms)) * release_ms; |
penkich | 0:e363f64b5744 | 23 | env.write(out_level); |
penkich | 0:e363f64b5744 | 24 | }else if(tms_per10 > (attack_ms + decay_ms) * 10){ |
penkich | 0:e363f64b5744 | 25 | env.write(sustain); |
penkich | 0:e363f64b5744 | 26 | }else if(tms_per10 > attack_ms * 10){ |
penkich | 0:e363f64b5744 | 27 | env.write(1.0f); |
penkich | 0:e363f64b5744 | 28 | }else{ |
penkich | 0:e363f64b5744 | 29 | out_level = 1.0/attack_ms * tms_per10; |
penkich | 0:e363f64b5744 | 30 | env.write(out_level); |
penkich | 0:e363f64b5744 | 31 | } |
penkich | 0:e363f64b5744 | 32 | } |
penkich | 0:e363f64b5744 | 33 | |
penkich | 0:e363f64b5744 | 34 | void sound_out(void){ |
penkich | 0:e363f64b5744 | 35 | speaker.write(((float)rand()/RAND_MAX)); |
penkich | 0:e363f64b5744 | 36 | } |
penkich | 0:e363f64b5744 | 37 | |
penkich | 0:e363f64b5744 | 38 | |
penkich | 0:e363f64b5744 | 39 | int main() |
penkich | 0:e363f64b5744 | 40 | { |
penkich | 0:e363f64b5744 | 41 | while(1){ |
penkich | 0:e363f64b5744 | 42 | if (sw ==0){ |
penkich | 0:e363f64b5744 | 43 | tms_per10=0; |
penkich | 0:e363f64b5744 | 44 | speaker.period_us(10); |
penkich | 0:e363f64b5744 | 45 | env.period_us(10); |
penkich | 0:e363f64b5744 | 46 | timer.attach_us(&sound_out,100); //10kHz |
penkich | 0:e363f64b5744 | 47 | envelope.attach_us(&env_out,100); //10kHz |
penkich | 0:e363f64b5744 | 48 | } |
penkich | 0:e363f64b5744 | 49 | wait(0.01f); |
penkich | 0:e363f64b5744 | 50 | } |
penkich | 0:e363f64b5744 | 51 | } |