4 #include <avr/pgmspace.h>
5 #include <avr/interrupt.h>
9 #include "keymap_common.h"
14 #define SAMPLE_DIVIDER 39
15 #define SAMPLE_RATE (2000000.0/SAMPLE_DIVIDER/2048)
16 // Resistor value of 1/ (2 * PI * 10nF * (2000000 hertz / SAMPLE_DIVIDER / 10)) for 10nF cap
18 void delay_us(int count) {
29 double frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
30 int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
33 volatile int i=0; //elements of the wave
40 uint16_t place_int = 0;
43 uint16_t sample_length = 0;
47 float note_frequency = 0;
48 float note_length = 0;
49 uint16_t note_position = 0;
50 float (* notes_pointer)[][2];
53 uint8_t current_note = 0;
55 void stop_all_notes() {
57 TIMSK3 &= ~_BV(OCIE3A);
59 playing_notes = false;
63 for (int i = 0; i < 8; i++) {
69 void stop_note(double freq) {
70 freq = freq / SAMPLE_RATE;
71 for (int i = 7; i >= 0; i--) {
72 if (frequencies[i] == freq) {
75 for (int j = i; (j < 7); j++) {
76 frequencies[j] = frequencies[j+1];
78 volumes[j] = volumes[j+1];
87 TIMSK3 &= ~_BV(OCIE3A);
91 double freq = frequencies[voices - 1];
92 int vol = volumes[voices - 1];
93 double starting_f = frequency;
94 if (frequency < freq) {
96 for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 500.0)) {
100 } else if (frequency > freq) {
102 for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 500.0)) {
116 while(!(PLLCSR & _BV(PLOCK)));
117 PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */
119 /* Init a fast PWM on Timer4 */
120 TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */
121 TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */
124 /* Enable the OC4A output */
127 TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs
129 TCCR3A = 0x0; // Options not needed
130 TCCR3B = _BV(CS31) | _BV(CS30) | _BV(WGM32); // 64th prescaling and CTC
131 OCR3A = SAMPLE_DIVIDER - 1; // Correct count/compare, related to sample playback
133 playing_notes = false;
138 ISR(TIMER3_COMPA_vect) {
142 // OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]);
145 // if (((int)place) >= 1024){
152 // OCR4A = (int)place / 4;
155 // if (((int)place) >= 1024) {
156 // OCR4A = (int)place / 2;
158 // OCR4A = 2048 - (int)place / 2;
161 // place += frequency;
163 // if (place >= SINE_LENGTH)
165 // place -= SINE_LENGTH;
167 // TIMSK3 &= ~_BV(OCIE3A);
170 // OCR4A = pgm_read_byte(&sample[(uint16_t)place_int]);
174 // if (place_int >= sample_length)
176 // place_int -= sample_length;
178 // TIMSK3 &= ~_BV(OCIE3A);
182 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 0;
184 place += note_frequency;
185 if (place >= SINE_LENGTH)
186 place -= SINE_LENGTH;
188 if (note_position >= note_length) {
190 if (current_note >= notes_length) {
194 TIMSK3 &= ~_BV(OCIE3A);
196 playing_notes = false;
200 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
201 note_length = (*notes_pointer)[current_note][1];
209 void play_notes(float (*np)[][2], uint8_t n_length, bool n_repeat) {
213 notes_length = n_length;
214 notes_repeat = n_repeat;
218 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
219 note_length = (*notes_pointer)[current_note][1];
220 // note_frequency = 880.0 / SAMPLE_RATE;
221 // note_length = 1000;
225 TIMSK3 |= _BV(OCIE3A);
226 playing_notes = true;
229 void play_sample(uint8_t * s, uint16_t l, bool r) {
235 TIMSK3 |= _BV(OCIE3A);
236 playing_notes = true;
239 void play_note(double freq, int vol) {
241 freq = freq / SAMPLE_RATE;
243 if (frequency != 0) {
244 double starting_f = frequency;
245 if (frequency < freq) {
246 for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 500.0)) {
249 } else if (frequency > freq) {
250 for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 500.0)) {
258 frequencies[voices] = frequency;
259 volumes[voices] = volume;
263 TIMSK3 |= _BV(OCIE3A);