4 #include <avr/pgmspace.h>
5 #include <avr/interrupt.h>
9 #include "keymap_common.h"
15 #define CPU_PRESCALER 8
21 #define SAMPLE_DIVIDER 39
22 #define SAMPLE_RATE (2000000.0/SAMPLE_DIVIDER/2048)
23 // Resistor value of 1/ (2 * PI * 10nF * (2000000 hertz / SAMPLE_DIVIDER / 10)) for 10nF cap
26 void delay_us(int count) {
40 double frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
41 int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
48 float places[8] = {0, 0, 0, 0, 0, 0, 0, 0};
50 uint16_t place_int = 0;
53 uint16_t sample_length = 0;
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];
67 bool note_resting = false;
69 uint8_t current_note = 0;
70 uint8_t rest_counter = 0;
72 audio_config_t audio_config;
75 void audio_toggle(void) {
76 audio_config.enable ^= 1;
77 eeconfig_write_audio(audio_config.raw);
81 audio_config.enable = 1;
82 eeconfig_write_audio(audio_config.raw);
85 void audio_off(void) {
86 audio_config.enable = 0;
87 eeconfig_write_audio(audio_config.raw);
91 void stop_all_notes() {
94 TIMSK3 &= ~_BV(OCIE3A);
96 TIMSK3 &= ~_BV(OCIE3A);
97 TCCR3A &= ~_BV(COM3A1);
104 for (int i = 0; i < 8; i++) {
110 void stop_note(double freq) {
113 freq = freq / SAMPLE_RATE;
115 for (int i = 7; i >= 0; i--) {
116 if (frequencies[i] == freq) {
119 for (int j = i; (j < 7); j++) {
120 frequencies[j] = frequencies[j+1];
121 frequencies[j+1] = 0;
122 volumes[j] = volumes[j+1];
132 TIMSK3 &= ~_BV(OCIE3A);
134 TIMSK3 &= ~_BV(OCIE3A);
135 TCCR3A &= ~_BV(COM3A1);
141 double freq = frequencies[voices - 1];
142 int vol = volumes[voices - 1];
143 double starting_f = frequency;
144 if (frequency < freq) {
146 for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 2000.0)) {
150 } else if (frequency > freq) {
152 for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 2000.0)) {
165 /* check signature */
166 if (!eeconfig_is_enabled()) {
169 audio_config.raw = eeconfig_read_audio();
174 while(!(PLLCSR & _BV(PLOCK)));
175 PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */
177 /* Init a fast PWM on Timer4 */
178 TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */
179 TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */
182 /* Enable the OC4A output */
185 TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs
187 TCCR3A = 0x0; // Options not needed
188 TCCR3B = _BV(CS31) | _BV(CS30) | _BV(WGM32); // 64th prescaling and CTC
189 OCR3A = SAMPLE_DIVIDER - 1; // Correct count/compare, related to sample playback
193 TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs
195 TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
196 TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
201 ISR(TIMER3_COMPA_vect) {
206 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 2;
209 // if (((int)place) >= 1024){
210 // OCR4A = 0xFF >> 2;
216 // OCR4A = (int)place / 4;
219 // if (((int)place) >= 1024) {
220 // OCR4A = (int)place / 2;
222 // OCR4A = 2048 - (int)place / 2;
227 if (place >= SINE_LENGTH)
228 place -= SINE_LENGTH;
232 for (int i = 0; i < voices; i++) {
234 sum += pgm_read_byte(&sinewave[(uint16_t)places[i]]) >> 2;
237 // if (((int)places[i]) >= 1024){
243 places[i] += frequencies[i];
245 if (places[i] >= SINE_LENGTH)
246 places[i] -= SINE_LENGTH;
252 // ICR3 = (int)(((double)F_CPU) / frequency); // Set max to the period
253 // OCR3A = (int)(((double)F_CPU) / frequency) >> 1; // Set compare to half the period
254 voice_place %= voices;
255 if (place > (frequencies[voice_place] / 50)) {
256 voice_place = (voice_place + 1) % voices;
259 ICR3 = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)); // Set max to the period
260 OCR3A = (int)((((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
261 //OCR3A = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)) >> 1 * duty_place; // Set compare to half the period
263 // if (duty_counter > (frequencies[voice_place] / 500)) {
264 // duty_place = (duty_place % 3) + 1;
273 // OCR4A = pgm_read_byte(&sample[(uint16_t)place_int]);
277 // if (place_int >= sample_length)
279 // place_int -= sample_length;
281 // TIMSK3 &= ~_BV(OCIE3A);
286 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 0;
288 place += note_frequency;
289 if (place >= SINE_LENGTH)
290 place -= SINE_LENGTH;
292 if (note_frequency > 0) {
293 ICR3 = (int)(((double)F_CPU) / (note_frequency * CPU_PRESCALER)); // Set max to the period
294 OCR3A = (int)((((double)F_CPU) / (note_frequency * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
303 bool end_of_note = false;
305 end_of_note = (note_position >= (note_length / ICR3 * 0xFFFF));
307 end_of_note = (note_position >= (note_length * 0x7FF));
310 if (current_note >= notes_count) {
315 TIMSK3 &= ~_BV(OCIE3A);
317 TIMSK3 &= ~_BV(OCIE3A);
318 TCCR3A &= ~_BV(COM3A1);
324 if (!note_resting && (notes_rest > 0)) {
327 note_length = notes_rest;
330 note_resting = false;
332 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
333 note_length = (*notes_pointer)[current_note][1] * (note_tempo / 100);
335 note_frequency = (*notes_pointer)[current_note][0];
336 note_length = ((*notes_pointer)[current_note][1] / 4) * (note_tempo / 100);
344 if (!audio_config.enable) {
350 void play_notes(float (*np)[][2], uint8_t n_count, bool n_repeat, float n_rest) {
352 if (audio_config.enable) {
359 notes_count = n_count;
360 notes_repeat = n_repeat;
366 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
367 note_length = (*notes_pointer)[current_note][1] * (note_tempo / 100);
369 note_frequency = (*notes_pointer)[current_note][0];
370 note_length = ((*notes_pointer)[current_note][1] / 4) * (note_tempo / 100);
376 TIMSK3 |= _BV(OCIE3A);
378 TIMSK3 |= _BV(OCIE3A);
379 TCCR3A |= _BV(COM3A1);
386 void play_sample(uint8_t * s, uint16_t l, bool r) {
388 if (audio_config.enable) {
397 TIMSK3 |= _BV(OCIE3A);
405 void play_note(double freq, int vol) {
407 if (audio_config.enable && voices < 8) {
413 freq = freq / SAMPLE_RATE;
416 if (frequency != 0) {
417 double starting_f = frequency;
418 if (frequency < freq) {
419 for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 2000.0)) {
422 } else if (frequency > freq) {
423 for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 2000.0)) {
431 frequencies[voices] = frequency;
432 volumes[voices] = volume;
437 TIMSK3 |= _BV(OCIE3A);
439 TIMSK3 |= _BV(OCIE3A);
440 TCCR3A |= _BV(COM3A1);
447 void set_timbre(float timbre)
449 note_timbre = timbre;
452 void set_tempo(float tempo)
457 void decrease_tempo(uint8_t tempo_change)
459 note_tempo += (float) tempo_change;
462 void increase_tempo(uint8_t tempo_change)
464 if (note_tempo - (float) tempo_change < 10)
470 note_tempo -= (float) tempo_change;