aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Humbert <jack.humb@gmail.com>2016-04-20 22:29:01 -0400
committerJack Humbert <jack.humb@gmail.com>2016-04-20 22:29:01 -0400
commit2e303b40aed372ea69b79850dae41e4f8ea457f4 (patch)
treef1f874c48d593489bcf37671353cf843b21fee14
parent7d1a683607a95aa9715b5decb1013f3e644b96c9 (diff)
downloadqmk_firmware-2e303b40aed372ea69b79850dae41e4f8ea457f4.tar.gz
qmk_firmware-2e303b40aed372ea69b79850dae41e4f8ea457f4.zip
start of envelope function
-rw-r--r--quantum/audio.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/quantum/audio.c b/quantum/audio.c
index e4f0bf30e..1327887d9 100644
--- a/quantum/audio.c
+++ b/quantum/audio.c
@@ -81,6 +81,7 @@ bool inited = false;
81 81
82audio_config_t audio_config; 82audio_config_t audio_config;
83 83
84uint16_t envelope_index = 0;
84 85
85void audio_toggle(void) { 86void audio_toggle(void) {
86 audio_config.enable ^= 1; 87 audio_config.enable ^= 1;
@@ -298,6 +299,26 @@ float vibrato(float average_freq) {
298 299
299#endif 300#endif
300 301
302float envelope(float f) {
303 uint16_t compensated_index = (uint16_t)((float)envelope_index * (880.0 / f));
304 switch (compensated_index) {
305 case 0:
306 note_timbre = TIMBRE_50;
307 break;
308 case 20:
309 note_timbre = TIMBRE_25;
310 break;
311 case 32:
312 note_timbre = TIMBRE_12;
313 break;
314 case 40 ... 60:
315 f = f / 2;
316 note_timbre = TIMBRE_50;
317 break;
318 }
319 return f;
320}
321
301ISR(TIMER3_COMPA_vect) { 322ISR(TIMER3_COMPA_vect) {
302 if (note) { 323 if (note) {
303 #ifdef PWM_AUDIO 324 #ifdef PWM_AUDIO
@@ -387,6 +408,12 @@ ISR(TIMER3_COMPA_vect) {
387 freq = frequency; 408 freq = frequency;
388 } 409 }
389 } 410 }
411
412 if (envelope_index < 65535) {
413 envelope_index++;
414 }
415 freq = envelope(freq);
416
390 ICR3 = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period 417 ICR3 = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
391 OCR3A = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period 418 OCR3A = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
392 } 419 }
@@ -495,6 +522,7 @@ if (audio_config.enable && voices < 8) {
495 if (notes) 522 if (notes)
496 stop_all_notes(); 523 stop_all_notes();
497 note = true; 524 note = true;
525 envelope_index = 0;
498 #ifdef PWM_AUDIO 526 #ifdef PWM_AUDIO
499 freq = freq / SAMPLE_RATE; 527 freq = freq / SAMPLE_RATE;
500 #endif 528 #endif