TAISAKU NISHIJIMA
/
penkich_percussion_2_NUCLEO
penkich_percussion_2_NUCLEO
Fork of penkich_test_percussion by
main.cpp@1:b102b9476594, 2015-08-30 (annotated)
- Committer:
- penkich
- Date:
- Sun Aug 30 07:31:36 2015 +0000
- Revision:
- 1:b102b9476594
- Parent:
- 0:e363f64b5744
penkich_percussion_2_NUCLEO
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 | 1:b102b9476594 | 3 | AnalogIn sw(A0); |
penkich | 0:e363f64b5744 | 4 | Ticker timer; |
penkich | 1:b102b9476594 | 5 | PwmOut speaker(D5); |
penkich | 0:e363f64b5744 | 6 | Ticker envelope; |
penkich | 1:b102b9476594 | 7 | PwmOut env(D3); |
penkich | 0:e363f64b5744 | 8 | static int tms_per10=0; |
penkich | 0:e363f64b5744 | 9 | |
penkich | 0:e363f64b5744 | 10 | void env_out(void){ |
penkich | 1:b102b9476594 | 11 | float attack_ms =5; |
penkich | 1:b102b9476594 | 12 | float decay_ms = 50; |
penkich | 1:b102b9476594 | 13 | float sustain_ms = 20; |
penkich | 0:e363f64b5744 | 14 | float sustain = 0.5; |
penkich | 1:b102b9476594 | 15 | float release_ms = 0; |
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 | 1:b102b9476594 | 29 | out_level = 1.0f/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 | 1:b102b9476594 | 42 | if (sw >=0.3f){ |
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 | 1:b102b9476594 | 49 | wait(0.01f); |
penkich | 1:b102b9476594 | 50 | //envelope.detach(); |
penkich | 0:e363f64b5744 | 51 | } |
penkich | 0:e363f64b5744 | 52 | } |