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) {
38 double frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
39 int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
46 float places[8] = {0, 0, 0, 0, 0, 0, 0, 0};
48 uint16_t place_int = 0;
51 uint16_t sample_length = 0;
56 float note_frequency = 0;
57 float note_length = 0;
58 uint16_t note_position = 0;
59 float (* notes_pointer)[][2];
62 uint8_t current_note = 0;
64 audio_config_t audio_config;
67 void audio_toggle(void) {
68 audio_config.enable ^= 1;
69 eeconfig_write_audio(audio_config.raw);
73 audio_config.enable = 1;
74 eeconfig_write_audio(audio_config.raw);
77 void audio_off(void) {
78 audio_config.enable = 0;
79 eeconfig_write_audio(audio_config.raw);
83 void stop_all_notes() {
86 TIMSK3 &= ~_BV(OCIE3A);
88 TIMSK3 &= ~_BV(OCIE3A);
89 TCCR3A &= ~_BV(COM3A1);
96 for (int i = 0; i < 8; i++) {
102 void stop_note(double freq) {
105 freq = freq / SAMPLE_RATE;
107 for (int i = 7; i >= 0; i--) {
108 if (frequencies[i] == freq) {
111 for (int j = i; (j < 7); j++) {
112 frequencies[j] = frequencies[j+1];
113 frequencies[j+1] = 0;
114 volumes[j] = volumes[j+1];
124 TIMSK3 &= ~_BV(OCIE3A);
126 TIMSK3 &= ~_BV(OCIE3A);
127 TCCR3A &= ~_BV(COM3A1);
133 double freq = frequencies[voices - 1];
134 int vol = volumes[voices - 1];
135 double starting_f = frequency;
136 if (frequency < freq) {
138 for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 2000.0)) {
142 } else if (frequency > freq) {
144 for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 2000.0)) {
157 /* check signature */
158 if (!eeconfig_is_enabled()) {
161 audio_config.raw = eeconfig_read_audio();
166 while(!(PLLCSR & _BV(PLOCK)));
167 PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */
169 /* Init a fast PWM on Timer4 */
170 TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */
171 TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */
174 /* Enable the OC4A output */
177 TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs
179 TCCR3A = 0x0; // Options not needed
180 TCCR3B = _BV(CS31) | _BV(CS30) | _BV(WGM32); // 64th prescaling and CTC
181 OCR3A = SAMPLE_DIVIDER - 1; // Correct count/compare, related to sample playback
185 TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs
187 TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
188 TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
193 ISR(TIMER3_COMPA_vect) {
198 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 2;
201 // if (((int)place) >= 1024){
202 // OCR4A = 0xFF >> 2;
208 // OCR4A = (int)place / 4;
211 // if (((int)place) >= 1024) {
212 // OCR4A = (int)place / 2;
214 // OCR4A = 2048 - (int)place / 2;
219 if (place >= SINE_LENGTH)
220 place -= SINE_LENGTH;
224 for (int i = 0; i < voices; i++) {
226 sum += pgm_read_byte(&sinewave[(uint16_t)places[i]]) >> 2;
229 // if (((int)places[i]) >= 1024){
235 places[i] += frequencies[i];
237 if (places[i] >= SINE_LENGTH)
238 places[i] -= SINE_LENGTH;
244 // ICR3 = (int)(((double)F_CPU) / frequency); // Set max to the period
245 // OCR3A = (int)(((double)F_CPU) / frequency) >> 1; // Set compare to half the period
246 voice_place %= voices;
247 if (place > (frequencies[voice_place] / 500)) {
248 voice_place = (voice_place + 1) % voices;
251 ICR3 = (int)(((double)F_CPU) / frequencies[voice_place]); // Set max to the period
252 OCR3A = (int)(((double)F_CPU) / frequencies[voice_place]) >> 1 * duty_place; // Set compare to half the period
254 // if (duty_counter > (frequencies[voice_place] / 500)) {
255 // duty_place = (duty_place % 3) + 1;
264 // OCR4A = pgm_read_byte(&sample[(uint16_t)place_int]);
268 // if (place_int >= sample_length)
270 // place_int -= sample_length;
272 // TIMSK3 &= ~_BV(OCIE3A);
277 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 0;
279 place += note_frequency;
280 if (place >= SINE_LENGTH)
281 place -= SINE_LENGTH;
283 if (note_frequency > 0) {
284 ICR3 = (int)(((double)F_CPU) / note_frequency); // Set max to the period
285 OCR3A = (int)(((double)F_CPU) / note_frequency) >> 1; // Set compare to half the period
294 bool end_of_note = false;
296 end_of_note = (note_position >= (note_length / ICR3 * 0xFFFF));
298 end_of_note = (note_position >= (note_length * 0x7FF));
301 if (current_note >= notes_length) {
306 TIMSK3 &= ~_BV(OCIE3A);
308 TIMSK3 &= ~_BV(OCIE3A);
309 TCCR3A &= ~_BV(COM3A1);
316 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
317 note_length = (*notes_pointer)[current_note][1];
319 note_frequency = (*notes_pointer)[current_note][0];
320 note_length = (*notes_pointer)[current_note][1] / 4;
327 if (!audio_config.enable) {
333 void play_notes(float (*np)[][2], uint8_t n_length, bool n_repeat) {
335 if (audio_config.enable) {
342 notes_length = n_length;
343 notes_repeat = n_repeat;
348 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
349 note_length = (*notes_pointer)[current_note][1];
351 note_frequency = (*notes_pointer)[current_note][0];
352 note_length = (*notes_pointer)[current_note][1] / 4;
358 TIMSK3 |= _BV(OCIE3A);
360 TIMSK3 |= _BV(OCIE3A);
361 TCCR3A |= _BV(COM3A1);
367 void play_sample(uint8_t * s, uint16_t l, bool r) {
369 if (audio_config.enable) {
378 TIMSK3 |= _BV(OCIE3A);
386 void play_note(double freq, int vol) {
388 if (audio_config.enable && voices < 8) {
394 freq = freq / SAMPLE_RATE;
397 if (frequency != 0) {
398 double starting_f = frequency;
399 if (frequency < freq) {
400 for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 2000.0)) {
403 } else if (frequency > freq) {
404 for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 2000.0)) {
412 frequencies[voices] = frequency;
413 volumes[voices] = volume;
418 TIMSK3 |= _BV(OCIE3A);
420 TIMSK3 |= _BV(OCIE3A);
421 TCCR3A |= _BV(COM3A1);