]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/audio.c
fade envelope
[qmk_firmware.git] / quantum / audio.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <math.h>
4 #include <avr/pgmspace.h>
5 #include <avr/interrupt.h>
6 #include <avr/io.h>
7 #include "print.h"
8 #include "audio.h"
9 #include "keymap_common.h"
10
11 #include "eeconfig.h"
12
13 #ifdef VIBRATO_ENABLE
14     #include "vibrato_lut.h"
15 #endif
16
17 #define PI 3.14159265
18
19 #define CPU_PRESCALER 8
20
21 #ifdef PWM_AUDIO
22     #include "wave.h"
23     #define SAMPLE_DIVIDER 39
24     #define SAMPLE_RATE (2000000.0/SAMPLE_DIVIDER/2048)
25     // Resistor value of 1/ (2 * PI * 10nF * (2000000 hertz / SAMPLE_DIVIDER / 10)) for 10nF cap
26
27     float places[8] = {0, 0, 0, 0, 0, 0, 0, 0};
28     uint16_t place_int = 0;
29     bool repeat = true;
30 #endif
31
32 void delay_us(int count) {
33   while(count--) {
34     _delay_us(1);
35   }
36 }
37
38 int voices = 0;
39 int voice_place = 0;
40 float frequency = 0;
41 int volume = 0;
42 long position = 0;
43
44 float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
45 int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
46 bool sliding = false;
47
48 int max = 0xFF;
49 float sum = 0;
50 float place = 0;
51
52 uint8_t * sample;
53 uint16_t sample_length = 0;
54 // float freq = 0;
55
56 bool notes = false;
57 bool note = false;
58 float note_frequency = 0;
59 float note_length = 0;
60 float note_tempo = TEMPO_DEFAULT;
61 float note_timbre = TIMBRE_DEFAULT;
62 uint16_t note_position = 0;
63 float (* notes_pointer)[][2];
64 uint16_t notes_count;
65 bool notes_repeat;
66 float notes_rest;
67 bool note_resting = false;
68
69 uint8_t current_note = 0;
70 uint8_t rest_counter = 0;
71
72 #ifdef VIBRATO_ENABLE
73 float vibrato_counter = 0;
74 float vibrato_strength = .5;
75 float vibrato_rate = 0.125;
76 #endif
77
78 float polyphony_rate = 0;
79
80 bool inited = false;
81
82 audio_config_t audio_config;
83
84 uint16_t envelope_index = 0;
85
86 void audio_toggle(void) {
87     audio_config.enable ^= 1;
88     eeconfig_write_audio(audio_config.raw);
89 }
90
91 void audio_on(void) {
92     audio_config.enable = 1;
93     eeconfig_write_audio(audio_config.raw);
94 }
95
96 void audio_off(void) {
97     audio_config.enable = 0;
98     eeconfig_write_audio(audio_config.raw);
99 }
100
101 #ifdef VIBRATO_ENABLE
102 // Vibrato rate functions
103
104 void set_vibrato_rate(float rate) {
105     vibrato_rate = rate;
106 }
107
108 void increase_vibrato_rate(float change) {
109     vibrato_rate *= change;
110 }
111
112 void decrease_vibrato_rate(float change) {
113     vibrato_rate /= change;
114 }
115
116 #ifdef VIBRATO_STRENGTH_ENABLE
117
118 void set_vibrato_strength(float strength) {
119     vibrato_strength = strength;
120 }
121
122 void increase_vibrato_strength(float change) {
123     vibrato_strength *= change;
124 }
125
126 void decrease_vibrato_strength(float change) {
127     vibrato_strength /= change;
128 }
129
130 #endif
131
132 #endif
133
134 // Polyphony functions
135
136 void set_polyphony_rate(float rate) {
137     polyphony_rate = rate;
138 }
139
140 void enable_polyphony() {
141     polyphony_rate = 5;
142 }
143
144 void disable_polyphony() {
145     polyphony_rate = 0;
146 }
147
148 void increase_polyphony_rate(float change) {
149     polyphony_rate *= change;
150 }
151
152 void decrease_polyphony_rate(float change) {
153     polyphony_rate /= change;
154 }
155
156 // Timbre function
157
158 void set_timbre(float timbre) {
159     note_timbre = timbre;
160 }
161
162 // Tempo functions
163
164 void set_tempo(float tempo) {
165     note_tempo = tempo;
166 }
167
168 void decrease_tempo(uint8_t tempo_change) {
169     note_tempo += (float) tempo_change;
170 }
171
172 void increase_tempo(uint8_t tempo_change) {
173     if (note_tempo - (float) tempo_change < 10) {
174         note_tempo = 10;
175     } else {
176         note_tempo -= (float) tempo_change;
177     }
178 }
179
180 void audio_init() {
181
182     /* check signature */
183     if (!eeconfig_is_enabled()) {
184         eeconfig_init();
185     }
186     audio_config.raw = eeconfig_read_audio();
187
188     #ifdef PWM_AUDIO
189         PLLFRQ = _BV(PDIV2);
190         PLLCSR = _BV(PLLE);
191         while(!(PLLCSR & _BV(PLOCK)));
192         PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */
193
194         /* Init a fast PWM on Timer4 */
195         TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */
196         TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */
197         OCR4A = 0;
198
199         /* Enable the OC4A output */
200         DDRC |= _BV(PORTC6);
201
202         TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs
203
204         TCCR3A = 0x0; // Options not needed
205         TCCR3B = _BV(CS31) | _BV(CS30) | _BV(WGM32); // 64th prescaling and CTC
206         OCR3A = SAMPLE_DIVIDER - 1; // Correct count/compare, related to sample playback
207     #else
208         DDRC |= _BV(PORTC6);
209
210         TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs
211
212         TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
213         TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
214     #endif
215
216     inited = true;
217 }
218
219 void stop_all_notes() {
220     if (!inited) {
221         audio_init();
222     }
223     voices = 0;
224     #ifdef PWM_AUDIO
225         TIMSK3 &= ~_BV(OCIE3A);
226     #else
227         TIMSK3 &= ~_BV(OCIE3A);
228         TCCR3A &= ~_BV(COM3A1);
229     #endif
230     notes = false;
231     note = false;
232     frequency = 0;
233     volume = 0;
234
235     for (int i = 0; i < 8; i++) {
236         frequencies[i] = 0;
237         volumes[i] = 0;
238     }
239 }
240
241 void stop_note(float freq) {
242     if (note) {
243         if (!inited) {
244             audio_init();
245         }
246         #ifdef PWM_AUDIO
247             freq = freq / SAMPLE_RATE;
248         #endif
249         for (int i = 7; i >= 0; i--) {
250             if (frequencies[i] == freq) {
251                 frequencies[i] = 0;
252                 volumes[i] = 0;
253                 for (int j = i; (j < 7); j++) {
254                     frequencies[j] = frequencies[j+1];
255                     frequencies[j+1] = 0;
256                     volumes[j] = volumes[j+1];
257                     volumes[j+1] = 0;
258                 }
259                 break;
260             }
261         }
262         voices--;
263         if (voices < 0)
264             voices = 0;
265         if (voice_place >= voices) {
266             voice_place = 0;
267         }
268         if (voices == 0) {
269             #ifdef PWM_AUDIO
270                 TIMSK3 &= ~_BV(OCIE3A);
271             #else
272                 TIMSK3 &= ~_BV(OCIE3A);
273                 TCCR3A &= ~_BV(COM3A1);
274             #endif
275             frequency = 0;
276             volume = 0;
277             note = false;
278         }
279     }
280 }
281
282 #ifdef VIBRATO_ENABLE
283
284 float mod(float a, int b)
285 {
286     float r = fmod(a, b);
287     return r < 0 ? r + b : r;
288 }
289
290 float vibrato(float average_freq) {
291     #ifdef VIBRATO_STRENGTH_ENABLE
292         float vibrated_freq = average_freq * pow(VIBRATO_LUT[(int)vibrato_counter], vibrato_strength);
293     #else
294         float vibrated_freq = average_freq * VIBRATO_LUT[(int)vibrato_counter];
295     #endif
296     vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
297     return vibrated_freq;
298 }
299
300 #endif
301
302 float envelope(float f) {
303     uint16_t compensated_index = (uint16_t)((float)envelope_index * (880.0 / f));
304     switch (compensated_index) {
305         case 0 ... 9:
306             f = f / 4;
307             note_timbre = TIMBRE_12;
308         break;
309         case 10 ... 19:
310             f = f / 2;
311             note_timbre = TIMBRE_12;
312         break;
313         case 20 ... 200:
314             note_timbre = .125 - pow(((float)compensated_index - 20) / (200 - 20), 2)*.125;
315         break;
316         default:
317             note_timbre = 0;
318         break;
319     }
320     return f;
321 }
322
323 ISR(TIMER3_COMPA_vect) {
324     if (note) {
325         #ifdef PWM_AUDIO
326             if (voices == 1) {
327                 // SINE
328                 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 2;
329
330                 // SQUARE
331                 // if (((int)place) >= 1024){
332                 //     OCR4A = 0xFF >> 2;
333                 // } else {
334                 //     OCR4A = 0x00;
335                 // }
336
337                 // SAWTOOTH
338                 // OCR4A = (int)place / 4;
339
340                 // TRIANGLE
341                 // if (((int)place) >= 1024) {
342                 //     OCR4A = (int)place / 2;
343                 // } else {
344                 //     OCR4A = 2048 - (int)place / 2;
345                 // }
346
347                 place += frequency;
348
349                 if (place >= SINE_LENGTH)
350                     place -= SINE_LENGTH;
351
352             } else {
353                 int sum = 0;
354                 for (int i = 0; i < voices; i++) {
355                     // SINE
356                     sum += pgm_read_byte(&sinewave[(uint16_t)places[i]]) >> 2;
357
358                     // SQUARE
359                     // if (((int)places[i]) >= 1024){
360                     //     sum += 0xFF >> 2;
361                     // } else {
362                     //     sum += 0x00;
363                     // }
364
365                     places[i] += frequencies[i];
366
367                     if (places[i] >= SINE_LENGTH)
368                         places[i] -= SINE_LENGTH;
369                 }
370                 OCR4A = sum;
371             }
372         #else
373             if (voices > 0) {
374                 float freq;
375                 if (polyphony_rate > 0) {                
376                     if (voices > 1) {
377                         voice_place %= voices;
378                         if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
379                             voice_place = (voice_place + 1) % voices;
380                             place = 0.0;
381                         }
382                     }
383                     #ifdef VIBRATO_ENABLE
384                     if (vibrato_strength > 0) {
385                         freq = vibrato(frequencies[voice_place]);
386                     } else {
387                     #else
388                     {
389                     #endif
390                         freq = frequencies[voice_place];
391                     } 
392                 } else {
393                     if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
394                         frequency = frequency * pow(2, 440/frequency/12/2);
395                     } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
396                         frequency = frequency * pow(2, -440/frequency/12/2);
397                     } else {
398                         frequency = frequencies[voices - 1];
399                     }
400
401
402                     #ifdef VIBRATO_ENABLE
403                     if (vibrato_strength > 0) {
404                         freq = vibrato(frequency);
405                     } else {
406                     #else
407                     {
408                     #endif
409                         freq = frequency;
410                     } 
411                 }
412
413                 if (envelope_index < 65535) {
414                     envelope_index++;
415                 }
416                 freq = envelope(freq);
417
418                 if (freq < 30.517578125)
419                     freq = 30.52;
420                 ICR3 = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
421                 OCR3A = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
422             }
423         #endif
424     }
425
426     // SAMPLE
427     // OCR4A = pgm_read_byte(&sample[(uint16_t)place_int]);
428
429     // place_int++;
430
431     // if (place_int >= sample_length)
432     //     if (repeat)
433     //         place_int -= sample_length;
434     //     else
435     //         TIMSK3 &= ~_BV(OCIE3A);
436
437
438     if (notes) {
439         #ifdef PWM_AUDIO
440             OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 0;
441
442             place += note_frequency;
443             if (place >= SINE_LENGTH)
444                 place -= SINE_LENGTH;
445         #else
446             if (note_frequency > 0) {
447                 float freq;
448
449                 #ifdef VIBRATO_ENABLE
450                 if (vibrato_strength > 0) {
451                     freq = vibrato(note_frequency);
452                 } else {
453                 #else
454                 {
455                 #endif
456                     freq = note_frequency;
457                 }
458
459                 ICR3 = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
460                 OCR3A = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
461             } else {
462                 ICR3 = 0;
463                 OCR3A = 0;
464             }
465         #endif
466
467
468         note_position++;
469         bool end_of_note = false;
470         if (ICR3 > 0)
471             end_of_note = (note_position >= (note_length / ICR3 * 0xFFFF));
472         else
473             end_of_note = (note_position >= (note_length * 0x7FF));
474         if (end_of_note) {
475             current_note++;
476             if (current_note >= notes_count) {
477                 if (notes_repeat) {
478                     current_note = 0;
479                 } else {
480                     #ifdef PWM_AUDIO
481                         TIMSK3 &= ~_BV(OCIE3A);
482                     #else
483                         TIMSK3 &= ~_BV(OCIE3A);
484                         TCCR3A &= ~_BV(COM3A1);
485                     #endif
486                     notes = false;
487                     return;
488                 }
489             }
490             if (!note_resting && (notes_rest > 0)) {
491                 note_resting = true;
492                 note_frequency = 0;
493                 note_length = notes_rest;
494                 current_note--;
495             } else {
496                 note_resting = false;
497                 #ifdef PWM_AUDIO
498                     note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
499                     note_length = (*notes_pointer)[current_note][1] * (note_tempo / 100);
500                 #else
501                     note_frequency = (*notes_pointer)[current_note][0];
502                     note_length = ((*notes_pointer)[current_note][1] / 4) * (note_tempo / 100);
503                 #endif
504             }
505             note_position = 0;
506         }
507
508     }
509
510     if (!audio_config.enable) {
511         notes = false;
512         note = false;
513     }
514 }
515
516 void play_note(float freq, int vol) {
517
518     if (!inited) {
519         audio_init();
520     }
521
522 if (audio_config.enable && voices < 8) {
523     TIMSK3 &= ~_BV(OCIE3A);
524     // Cancel notes if notes are playing
525     if (notes)
526         stop_all_notes();
527     note = true;
528     envelope_index = 0;
529     #ifdef PWM_AUDIO
530         freq = freq / SAMPLE_RATE;
531     #endif
532     if (freq > 0) {
533         frequencies[voices] = freq;
534         volumes[voices] = vol;
535         voices++;
536     }
537
538     #ifdef PWM_AUDIO
539         TIMSK3 |= _BV(OCIE3A);
540     #else
541         TIMSK3 |= _BV(OCIE3A);
542         TCCR3A |= _BV(COM3A1);
543     #endif
544 }
545
546 }
547
548 void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest) {
549
550     if (!inited) {
551         audio_init();
552     }
553
554 if (audio_config.enable) {
555     TIMSK3 &= ~_BV(OCIE3A);
556         // Cancel note if a note is playing
557     if (note)
558         stop_all_notes();
559     notes = true;
560
561     notes_pointer = np;
562     notes_count = n_count;
563     notes_repeat = n_repeat;
564     notes_rest = n_rest;
565
566     place = 0;
567     current_note = 0;
568     #ifdef PWM_AUDIO
569         note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
570         note_length = (*notes_pointer)[current_note][1] * (note_tempo / 100);
571     #else
572         note_frequency = (*notes_pointer)[current_note][0];
573         note_length = ((*notes_pointer)[current_note][1] / 4) * (note_tempo / 100);
574     #endif
575     note_position = 0;
576
577
578     #ifdef PWM_AUDIO
579         TIMSK3 |= _BV(OCIE3A);
580     #else
581         TIMSK3 |= _BV(OCIE3A);
582         TCCR3A |= _BV(COM3A1);
583     #endif
584 }
585
586 }
587
588 #ifdef PWM_AUDIO
589 void play_sample(uint8_t * s, uint16_t l, bool r) {
590     if (!inited) {
591         audio_init();
592     }
593
594     if (audio_config.enable) {
595         TIMSK3 &= ~_BV(OCIE3A);
596         stop_all_notes();
597         place_int = 0;
598         sample = s;
599         sample_length = l;
600         repeat = r;
601
602         TIMSK3 |= _BV(OCIE3A);
603     }
604 }
605 #endif
606
607 //------------------------------------------------------------------------------
608 // Override these functions in your keymap file to play different tunes on
609 // startup and bootloader jump
610 __attribute__ ((weak))
611 void play_startup_tone()
612 {
613 }
614
615
616
617 __attribute__ ((weak))
618 void play_goodbye_tone()
619 {
620
621 }
622 //------------------------------------------------------------------------------