4 #include <avr/pgmspace.h>
5 #include <avr/interrupt.h>
13 #define CPU_PRESCALER 8
15 // -----------------------------------------------------------------------------
17 // -----------------------------------------------------------------------------
19 // TIMSK3 - Timer/Counter #3 Interrupt Mask Register
20 // Turn on/off 3A interputs, stopping/enabling the ISR calls
21 #define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3A)
22 #define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3A)
24 // TCCR3A: Timer/Counter #3 Control Register
25 // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
26 #define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3A1);
27 #define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3A1) | _BV(COM3A0));
29 // Fast PWM Mode Controls
30 #define TIMER_3_PERIOD ICR3
31 #define TIMER_3_DUTY_CYCLE OCR3A
33 // -----------------------------------------------------------------------------
42 float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
43 int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
49 uint16_t sample_length = 0;
51 bool playing_notes = false;
52 bool playing_note = false;
53 float note_frequency = 0;
54 float note_length = 0;
55 uint8_t note_tempo = TEMPO_DEFAULT;
56 float note_timbre = TIMBRE_DEFAULT;
57 uint16_t note_position = 0;
58 float (* notes_pointer)[][2];
62 bool note_resting = false;
64 uint8_t current_note = 0;
65 uint8_t rest_counter = 0;
68 float vibrato_counter = 0;
69 float vibrato_strength = .5;
70 float vibrato_rate = 0.125;
73 float polyphony_rate = 0;
75 static bool audio_initialized = false;
77 audio_config_t audio_config;
79 uint16_t envelope_index = 0;
85 if (!eeconfig_is_enabled())
89 audio_config.raw = eeconfig_read_audio();
91 // Set port PC6 (OC3A and /OC4A) as output
94 DISABLE_AUDIO_COUNTER_3_ISR;
96 // TCCR3A / TCCR3B: Timer/Counter #3 Control Registers
97 // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
98 // Waveform Generation Mode (WGM3n) = 0b1110 = Fast PWM Mode 14 (Period = ICR3, Duty Cycle = OCR3A)
99 // Clock Select (CS3n) = 0b010 = Clock / 8
100 TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
101 TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
103 audio_initialized = true;
106 void stop_all_notes()
108 if (!audio_initialized) {
113 DISABLE_AUDIO_COUNTER_3_ISR;
114 DISABLE_AUDIO_COUNTER_3_OUTPUT;
116 playing_notes = false;
117 playing_note = false;
121 for (uint8_t i = 0; i < 8; i++)
128 void stop_note(float freq)
131 if (!audio_initialized) {
134 for (int i = 7; i >= 0; i--) {
135 if (frequencies[i] == freq) {
138 for (int j = i; (j < 7); j++) {
139 frequencies[j] = frequencies[j+1];
140 frequencies[j+1] = 0;
141 volumes[j] = volumes[j+1];
150 if (voice_place >= voices) {
154 DISABLE_AUDIO_COUNTER_3_ISR;
155 DISABLE_AUDIO_COUNTER_3_OUTPUT;
158 playing_note = false;
163 #ifdef VIBRATO_ENABLE
165 float mod(float a, int b)
167 float r = fmod(a, b);
168 return r < 0 ? r + b : r;
171 float vibrato(float average_freq) {
172 #ifdef VIBRATO_STRENGTH_ENABLE
173 float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
175 float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
177 vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
178 return vibrated_freq;
183 ISR(TIMER3_COMPA_vect)
189 if (polyphony_rate > 0) {
191 voice_place %= voices;
192 if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
193 voice_place = (voice_place + 1) % voices;
198 #ifdef VIBRATO_ENABLE
199 if (vibrato_strength > 0) {
200 freq = vibrato(frequencies[voice_place]);
202 freq = frequencies[voice_place];
205 freq = frequencies[voice_place];
208 if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
209 frequency = frequency * pow(2, 440/frequency/12/2);
210 } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
211 frequency = frequency * pow(2, -440/frequency/12/2);
213 frequency = frequencies[voices - 1];
216 #ifdef VIBRATO_ENABLE
217 if (vibrato_strength > 0) {
218 freq = vibrato(frequency);
227 if (envelope_index < 65535) {
231 freq = voice_envelope(freq);
233 if (freq < 30.517578125) {
237 TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
238 TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
243 if (note_frequency > 0) {
244 #ifdef VIBRATO_ENABLE
245 if (vibrato_strength > 0) {
246 freq = vibrato(note_frequency);
248 freq = note_frequency;
251 freq = note_frequency;
254 if (envelope_index < 65535) {
257 freq = voice_envelope(freq);
259 TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
260 TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
263 TIMER_3_DUTY_CYCLE = 0;
267 bool end_of_note = false;
268 if (TIMER_3_PERIOD > 0) {
269 end_of_note = (note_position >= (note_length / TIMER_3_PERIOD * 0xFFFF));
271 end_of_note = (note_position >= (note_length * 0x7FF));
276 if (current_note >= notes_count) {
280 DISABLE_AUDIO_COUNTER_3_ISR;
281 DISABLE_AUDIO_COUNTER_3_OUTPUT;
282 playing_notes = false;
286 if (!note_resting && (notes_rest > 0)) {
289 note_length = notes_rest;
292 note_resting = false;
294 note_frequency = (*notes_pointer)[current_note][0];
295 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
302 if (!audio_config.enable) {
303 playing_notes = false;
304 playing_note = false;
308 void play_note(float freq, int vol) {
310 if (!audio_initialized) {
314 if (audio_config.enable && voices < 8) {
315 DISABLE_AUDIO_COUNTER_3_ISR;
317 // Cancel notes if notes are playing
326 frequencies[voices] = freq;
327 volumes[voices] = vol;
331 ENABLE_AUDIO_COUNTER_3_ISR;
332 ENABLE_AUDIO_COUNTER_3_OUTPUT;
337 void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest)
340 if (!audio_initialized) {
344 if (audio_config.enable) {
346 DISABLE_AUDIO_COUNTER_3_ISR;
348 // Cancel note if a note is playing
352 playing_notes = true;
355 notes_count = n_count;
356 notes_repeat = n_repeat;
362 note_frequency = (*notes_pointer)[current_note][0];
363 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
367 ENABLE_AUDIO_COUNTER_3_ISR;
368 ENABLE_AUDIO_COUNTER_3_OUTPUT;
373 bool is_playing_notes(void) {
374 return playing_notes;
377 bool is_audio_on(void) {
378 return (audio_config.enable != 0);
381 void audio_toggle(void) {
382 audio_config.enable ^= 1;
383 eeconfig_update_audio(audio_config.raw);
384 if (audio_config.enable)
388 void audio_on(void) {
389 audio_config.enable = 1;
390 eeconfig_update_audio(audio_config.raw);
394 void audio_off(void) {
395 audio_config.enable = 0;
396 eeconfig_update_audio(audio_config.raw);
399 #ifdef VIBRATO_ENABLE
401 // Vibrato rate functions
403 void set_vibrato_rate(float rate) {
407 void increase_vibrato_rate(float change) {
408 vibrato_rate *= change;
411 void decrease_vibrato_rate(float change) {
412 vibrato_rate /= change;
415 #ifdef VIBRATO_STRENGTH_ENABLE
417 void set_vibrato_strength(float strength) {
418 vibrato_strength = strength;
421 void increase_vibrato_strength(float change) {
422 vibrato_strength *= change;
425 void decrease_vibrato_strength(float change) {
426 vibrato_strength /= change;
429 #endif /* VIBRATO_STRENGTH_ENABLE */
431 #endif /* VIBRATO_ENABLE */
433 // Polyphony functions
435 void set_polyphony_rate(float rate) {
436 polyphony_rate = rate;
439 void enable_polyphony() {
443 void disable_polyphony() {
447 void increase_polyphony_rate(float change) {
448 polyphony_rate *= change;
451 void decrease_polyphony_rate(float change) {
452 polyphony_rate /= change;
457 void set_timbre(float timbre) {
458 note_timbre = timbre;
463 void set_tempo(uint8_t tempo) {
467 void decrease_tempo(uint8_t tempo_change) {
468 note_tempo += tempo_change;
471 void increase_tempo(uint8_t tempo_change) {
472 if (note_tempo - tempo_change < 10) {
475 note_tempo -= tempo_change;