TAISAKU NISHIJIMA
/
penkich_percussion_1_LPC1114
for percussion1
Fork of penkich_test_percussion by
main.cpp@1:3252e4fbccab, 2015-08-23 (annotated)
- Committer:
- penkich
- Date:
- Sun Aug 23 00:52:12 2015 +0000
- Revision:
- 1:3252e4fbccab
- Parent:
- 0:e363f64b5744
- Child:
- 2:269fadd5d1f2
for 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:3252e4fbccab | 3 | DigitalIn sw(D7); |
penkich | 0:e363f64b5744 | 4 | Ticker timer; |
penkich | 1:3252e4fbccab | 5 | PwmOut speaker(D5); |
penkich | 0:e363f64b5744 | 6 | Ticker envelope; |
penkich | 1:3252e4fbccab | 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 | 0:e363f64b5744 | 11 | float attack_ms =10; |
penkich | 0:e363f64b5744 | 12 | float decay_ms = 10; |
penkich | 1:3252e4fbccab | 13 | float sustain_ms = 20; |
penkich | 1:3252e4fbccab | 14 | float sustain = 0.2; |
penkich | 1:3252e4fbccab | 15 | float release_ms = 5000; |
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:3252e4fbccab | 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 | 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 | } |