1 /* Copyright 2016 Jack Humbert
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <avr/pgmspace.h>
20 #include <avr/interrupt.h>
30 #define CPU_PRESCALER 8
35 // TIMSK3 - Timer/Counter #3 Interrupt Mask Register
36 // Turn on/off 3A interputs, stopping/enabling the ISR calls
37 #define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3A)
38 #define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3A)
41 // TCCR3A: Timer/Counter #3 Control Register
42 // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
43 #define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3A1);
44 #define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3A1) | _BV(COM3A0));
47 #define NOTE_PERIOD ICR3
48 #define NOTE_DUTY_CYCLE OCR3A
53 #define SAMPLE_DIVIDER 39
54 #define SAMPLE_RATE (2000000.0/SAMPLE_DIVIDER/2048)
55 // Resistor value of 1/ (2 * PI * 10nF * (2000000 hertz / SAMPLE_DIVIDER / 10)) for 10nF cap
57 float places[8] = {0, 0, 0, 0, 0, 0, 0, 0};
58 uint16_t place_int = 0;
62 void delay_us(int count) {
74 float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
75 int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
81 uint16_t sample_length = 0;
84 bool playing_notes = false;
85 bool playing_note = false;
86 float note_frequency = 0;
87 float note_length = 0;
88 uint8_t note_tempo = TEMPO_DEFAULT;
89 float note_timbre = TIMBRE_DEFAULT;
90 uint16_t note_position = 0;
91 float (* notes_pointer)[][2];
95 bool note_resting = false;
97 uint16_t current_note = 0;
98 uint8_t rest_counter = 0;
100 #ifdef VIBRATO_ENABLE
101 float vibrato_counter = 0;
102 float vibrato_strength = .5;
103 float vibrato_rate = 0.125;
106 float polyphony_rate = 0;
108 static bool audio_initialized = false;
110 audio_config_t audio_config;
112 uint16_t envelope_index = 0;
117 if (!eeconfig_is_enabled())
121 audio_config.raw = eeconfig_read_audio();
127 while(!(PLLCSR & _BV(PLOCK)));
128 PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */
130 /* Init a fast PWM on Timer4 */
131 TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */
132 TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */
135 /* Enable the OC4A output */
138 DISABLE_AUDIO_COUNTER_3_ISR; // Turn off 3A interputs
140 TCCR3A = 0x0; // Options not needed
141 TCCR3B = _BV(CS31) | _BV(CS30) | _BV(WGM32); // 64th prescaling and CTC
142 OCR3A = SAMPLE_DIVIDER - 1; // Correct count/compare, related to sample playback
146 // Set port PC6 (OC3A and /OC4A) as output
149 DISABLE_AUDIO_COUNTER_3_ISR;
151 // TCCR3A / TCCR3B: Timer/Counter #3 Control Registers
152 // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
153 // Waveform Generation Mode (WGM3n) = 0b1110 = Fast PWM Mode 14 (Period = ICR3, Duty Cycle = OCR3A)
154 // Clock Select (CS3n) = 0b010 = Clock / 8
155 TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
156 TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
160 audio_initialized = true;
163 void stop_all_notes() {
164 if (!audio_initialized) {
169 DISABLE_AUDIO_COUNTER_3_ISR;
171 DISABLE_AUDIO_COUNTER_3_ISR;
172 DISABLE_AUDIO_COUNTER_3_OUTPUT;
175 playing_notes = false;
176 playing_note = false;
180 for (uint8_t i = 0; i < 8; i++)
187 void stop_note(float freq)
190 if (!audio_initialized) {
194 freq = freq / SAMPLE_RATE;
196 for (int i = 7; i >= 0; i--) {
197 if (frequencies[i] == freq) {
200 for (int j = i; (j < 7); j++) {
201 frequencies[j] = frequencies[j+1];
202 frequencies[j+1] = 0;
203 volumes[j] = volumes[j+1];
212 if (voice_place >= voices) {
217 DISABLE_AUDIO_COUNTER_3_ISR;
219 DISABLE_AUDIO_COUNTER_3_ISR;
220 DISABLE_AUDIO_COUNTER_3_OUTPUT;
224 playing_note = false;
229 #ifdef VIBRATO_ENABLE
231 float mod(float a, int b)
233 float r = fmod(a, b);
234 return r < 0 ? r + b : r;
237 float vibrato(float average_freq) {
238 #ifdef VIBRATO_STRENGTH_ENABLE
239 float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
241 float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
243 vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
244 return vibrated_freq;
249 ISR(TIMER3_COMPA_vect)
255 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 2;
258 // if (((int)place) >= 1024){
259 // OCR4A = 0xFF >> 2;
265 // OCR4A = (int)place / 4;
268 // if (((int)place) >= 1024) {
269 // OCR4A = (int)place / 2;
271 // OCR4A = 2048 - (int)place / 2;
276 if (place >= SINE_LENGTH)
277 place -= SINE_LENGTH;
281 for (int i = 0; i < voices; i++) {
283 sum += pgm_read_byte(&sinewave[(uint16_t)places[i]]) >> 2;
286 // if (((int)places[i]) >= 1024){
292 places[i] += frequencies[i];
294 if (places[i] >= SINE_LENGTH)
295 places[i] -= SINE_LENGTH;
302 if (polyphony_rate > 0) {
304 voice_place %= voices;
305 if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
306 voice_place = (voice_place + 1) % voices;
310 #ifdef VIBRATO_ENABLE
311 if (vibrato_strength > 0) {
312 freq = vibrato(frequencies[voice_place]);
317 freq = frequencies[voice_place];
320 if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
321 frequency = frequency * pow(2, 440/frequency/12/2);
322 } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
323 frequency = frequency * pow(2, -440/frequency/12/2);
325 frequency = frequencies[voices - 1];
329 #ifdef VIBRATO_ENABLE
330 if (vibrato_strength > 0) {
331 freq = vibrato(frequency);
340 if (envelope_index < 65535) {
343 freq = voice_envelope(freq);
345 if (freq < 30.517578125)
347 NOTE_PERIOD = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
348 NOTE_DUTY_CYCLE = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
354 // OCR4A = pgm_read_byte(&sample[(uint16_t)place_int]);
358 // if (place_int >= sample_length)
360 // place_int -= sample_length;
362 // DISABLE_AUDIO_COUNTER_3_ISR;
367 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 0;
369 place += note_frequency;
370 if (place >= SINE_LENGTH)
371 place -= SINE_LENGTH;
373 if (note_frequency > 0) {
376 #ifdef VIBRATO_ENABLE
377 if (vibrato_strength > 0) {
378 freq = vibrato(note_frequency);
383 freq = note_frequency;
386 if (envelope_index < 65535) {
389 freq = voice_envelope(freq);
391 NOTE_PERIOD = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
392 NOTE_DUTY_CYCLE = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
401 bool end_of_note = false;
403 end_of_note = (note_position >= (note_length / NOTE_PERIOD * 0xFFFF));
405 end_of_note = (note_position >= (note_length * 0x7FF));
408 if (current_note >= notes_count) {
413 DISABLE_AUDIO_COUNTER_3_ISR;
415 DISABLE_AUDIO_COUNTER_3_ISR;
416 DISABLE_AUDIO_COUNTER_3_OUTPUT;
418 playing_notes = false;
422 if (!note_resting && (notes_rest > 0)) {
425 note_length = notes_rest;
428 note_resting = false;
430 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
431 note_length = (*notes_pointer)[current_note][1] * (((float)note_tempo) / 100);
434 note_frequency = (*notes_pointer)[current_note][0];
435 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
443 if (!audio_config.enable) {
444 playing_notes = false;
445 playing_note = false;
449 void play_note(float freq, int vol) {
451 if (!audio_initialized) {
455 if (audio_config.enable && voices < 8) {
456 DISABLE_AUDIO_COUNTER_3_ISR;
458 // Cancel notes if notes are playing
467 freq = freq / SAMPLE_RATE;
470 frequencies[voices] = freq;
471 volumes[voices] = vol;
476 ENABLE_AUDIO_COUNTER_3_ISR;
478 ENABLE_AUDIO_COUNTER_3_ISR;
479 ENABLE_AUDIO_COUNTER_3_OUTPUT;
485 void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest)
488 if (!audio_initialized) {
492 if (audio_config.enable) {
494 DISABLE_AUDIO_COUNTER_3_ISR;
496 // Cancel note if a note is playing
500 playing_notes = true;
503 notes_count = n_count;
504 notes_repeat = n_repeat;
511 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
512 note_length = (*notes_pointer)[current_note][1] * (((float)note_tempo) / 100);
514 note_frequency = (*notes_pointer)[current_note][0];
515 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
521 ENABLE_AUDIO_COUNTER_3_ISR;
523 ENABLE_AUDIO_COUNTER_3_ISR;
524 ENABLE_AUDIO_COUNTER_3_OUTPUT;
531 void play_sample(uint8_t * s, uint16_t l, bool r) {
532 if (!audio_initialized) {
536 if (audio_config.enable) {
537 DISABLE_AUDIO_COUNTER_3_ISR;
544 ENABLE_AUDIO_COUNTER_3_ISR;
550 void audio_toggle(void) {
551 audio_config.enable ^= 1;
552 eeconfig_update_audio(audio_config.raw);
555 void audio_on(void) {
556 audio_config.enable = 1;
557 eeconfig_update_audio(audio_config.raw);
560 void audio_off(void) {
561 audio_config.enable = 0;
562 eeconfig_update_audio(audio_config.raw);
565 #ifdef VIBRATO_ENABLE
567 // Vibrato rate functions
569 void set_vibrato_rate(float rate) {
573 void increase_vibrato_rate(float change) {
574 vibrato_rate *= change;
577 void decrease_vibrato_rate(float change) {
578 vibrato_rate /= change;
581 #ifdef VIBRATO_STRENGTH_ENABLE
583 void set_vibrato_strength(float strength) {
584 vibrato_strength = strength;
587 void increase_vibrato_strength(float change) {
588 vibrato_strength *= change;
591 void decrease_vibrato_strength(float change) {
592 vibrato_strength /= change;
595 #endif /* VIBRATO_STRENGTH_ENABLE */
597 #endif /* VIBRATO_ENABLE */
599 // Polyphony functions
601 void set_polyphony_rate(float rate) {
602 polyphony_rate = rate;
605 void enable_polyphony() {
609 void disable_polyphony() {
613 void increase_polyphony_rate(float change) {
614 polyphony_rate *= change;
617 void decrease_polyphony_rate(float change) {
618 polyphony_rate /= change;
623 void set_timbre(float timbre) {
624 note_timbre = timbre;
629 void set_tempo(uint8_t tempo) {
633 void decrease_tempo(uint8_t tempo_change) {
634 note_tempo += tempo_change;
637 void increase_tempo(uint8_t tempo_change) {
638 if (note_tempo - tempo_change < 10) {
641 note_tempo -= tempo_change;
646 //------------------------------------------------------------------------------
647 // Override these functions in your keymap file to play different tunes on
648 // startup and bootloader jump
649 __attribute__ ((weak))
650 void play_startup_tone()
654 __attribute__ ((weak))
655 void play_goodbye_tone()
658 //------------------------------------------------------------------------------