4 #include <avr/pgmspace.h>
5 #include <avr/interrupt.h>
9 #include "keymap_common.h"
19 #define SAMPLE_DIVIDER 39
20 #define SAMPLE_RATE (2000000.0/SAMPLE_DIVIDER/2048)
21 // Resistor value of 1/ (2 * PI * 10nF * (2000000 hertz / SAMPLE_DIVIDER / 10)) for 10nF cap
24 void delay_us(int count) {
36 double frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
37 int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
44 float places[8] = {0, 0, 0, 0, 0, 0, 0, 0};
46 uint16_t place_int = 0;
49 uint16_t sample_length = 0;
54 float note_frequency = 0;
55 float note_length = 0;
56 uint16_t note_position = 0;
57 float (* notes_pointer)[][2];
60 uint8_t current_note = 0;
62 audio_config_t audio_config;
65 void audio_toggle(void) {
66 audio_config.enable ^= 1;
67 eeconfig_write_audio(audio_config.raw);
71 audio_config.enable = 1;
72 eeconfig_write_audio(audio_config.raw);
75 void audio_off(void) {
76 audio_config.enable = 0;
77 eeconfig_write_audio(audio_config.raw);
81 void stop_all_notes() {
84 TIMSK3 &= ~_BV(OCIE3A);
86 TIMSK3 &= ~_BV(OCIE3A);
87 TCCR3A &= ~_BV(COM3A1);
94 for (int i = 0; i < 8; i++) {
100 void stop_note(double freq) {
102 freq = freq / SAMPLE_RATE;
104 for (int i = 7; i >= 0; i--) {
105 if (frequencies[i] == freq) {
108 for (int j = i; (j < 7); j++) {
109 frequencies[j] = frequencies[j+1];
110 frequencies[j+1] = 0;
111 volumes[j] = volumes[j+1];
121 TIMSK3 &= ~_BV(OCIE3A);
123 TIMSK3 &= ~_BV(OCIE3A);
124 TCCR3A &= ~_BV(COM3A1);
130 double freq = frequencies[voices - 1];
131 int vol = volumes[voices - 1];
132 double starting_f = frequency;
133 if (frequency < freq) {
135 for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 2000.0)) {
139 } else if (frequency > freq) {
141 for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 2000.0)) {
153 /* check signature */
154 if (!eeconfig_is_enabled()) {
157 audio_config.raw = eeconfig_read_audio();
162 while(!(PLLCSR & _BV(PLOCK)));
163 PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */
165 /* Init a fast PWM on Timer4 */
166 TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */
167 TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */
170 /* Enable the OC4A output */
173 TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs
175 TCCR3A = 0x0; // Options not needed
176 TCCR3B = _BV(CS31) | _BV(CS30) | _BV(WGM32); // 64th prescaling and CTC
177 OCR3A = SAMPLE_DIVIDER - 1; // Correct count/compare, related to sample playback
181 TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs
183 TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
184 TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
189 ISR(TIMER3_COMPA_vect) {
194 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 2;
197 // if (((int)place) >= 1024){
198 // OCR4A = 0xFF >> 2;
204 // OCR4A = (int)place / 4;
207 // if (((int)place) >= 1024) {
208 // OCR4A = (int)place / 2;
210 // OCR4A = 2048 - (int)place / 2;
215 if (place >= SINE_LENGTH)
216 place -= SINE_LENGTH;
220 for (int i = 0; i < voices; i++) {
222 sum += pgm_read_byte(&sinewave[(uint16_t)places[i]]) >> 2;
225 // if (((int)places[i]) >= 1024){
231 places[i] += frequencies[i];
233 if (places[i] >= SINE_LENGTH)
234 places[i] -= SINE_LENGTH;
240 // ICR3 = (int)(((double)F_CPU) / frequency); // Set max to the period
241 // OCR3A = (int)(((double)F_CPU) / frequency) >> 1; // Set compare to half the period
243 voice_place = (voice_place + 1) % voices;
246 ICR3 = (int)(((double)F_CPU) / frequencies[voice_place]); // Set max to the period
247 OCR3A = (int)(((double)F_CPU) / frequencies[voice_place]) >> 1; // Set compare to half the period
254 // OCR4A = pgm_read_byte(&sample[(uint16_t)place_int]);
258 // if (place_int >= sample_length)
260 // place_int -= sample_length;
262 // TIMSK3 &= ~_BV(OCIE3A);
267 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 0;
269 place += note_frequency;
270 if (place >= SINE_LENGTH)
271 place -= SINE_LENGTH;
273 if (note_frequency > 0) {
274 ICR3 = (int)(((double)F_CPU) / note_frequency); // Set max to the period
275 OCR3A = (int)(((double)F_CPU) / note_frequency) >> 1; // Set compare to half the period
284 bool end_of_note = false;
286 end_of_note = (note_position >= (note_length / ICR3 * 0xFFFF));
288 end_of_note = (note_position >= (note_length * 0x7FF));
291 if (current_note >= notes_length) {
296 TIMSK3 &= ~_BV(OCIE3A);
298 TIMSK3 &= ~_BV(OCIE3A);
299 TCCR3A &= ~_BV(COM3A1);
306 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
307 note_length = (*notes_pointer)[current_note][1];
309 note_frequency = (*notes_pointer)[current_note][0];
310 note_length = (*notes_pointer)[current_note][1] / 4;
317 if (!audio_config.enable) {
323 void play_notes(float (*np)[][2], uint8_t n_length, bool n_repeat) {
325 if (audio_config.enable) {
332 notes_length = n_length;
333 notes_repeat = n_repeat;
338 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
339 note_length = (*notes_pointer)[current_note][1];
341 note_frequency = (*notes_pointer)[current_note][0];
342 note_length = (*notes_pointer)[current_note][1] / 4;
348 TIMSK3 |= _BV(OCIE3A);
350 TIMSK3 |= _BV(OCIE3A);
351 TCCR3A |= _BV(COM3A1);
357 void play_sample(uint8_t * s, uint16_t l, bool r) {
359 if (audio_config.enable) {
368 TIMSK3 |= _BV(OCIE3A);
376 void play_note(double freq, int vol) {
378 if (audio_config.enable) {
384 freq = freq / SAMPLE_RATE;
387 if (frequency != 0) {
388 double starting_f = frequency;
389 if (frequency < freq) {
390 for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 2000.0)) {
393 } else if (frequency > freq) {
394 for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 2000.0)) {
402 frequencies[voices] = frequency;
403 volumes[voices] = volume;
408 TIMSK3 |= _BV(OCIE3A);
410 TIMSK3 |= _BV(OCIE3A);
411 TCCR3A |= _BV(COM3A1);