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;
80 bool glissando = true;
86 if (!eeconfig_is_enabled())
90 audio_config.raw = eeconfig_read_audio();
92 // Set port PC6 (OC3A and /OC4A) as output
95 DISABLE_AUDIO_COUNTER_3_ISR;
97 // TCCR3A / TCCR3B: Timer/Counter #3 Control Registers
98 // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
99 // Waveform Generation Mode (WGM3n) = 0b1110 = Fast PWM Mode 14 (Period = ICR3, Duty Cycle = OCR3A)
100 // Clock Select (CS3n) = 0b010 = Clock / 8
101 TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
102 TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
104 audio_initialized = true;
107 void stop_all_notes()
109 if (!audio_initialized) {
114 DISABLE_AUDIO_COUNTER_3_ISR;
115 DISABLE_AUDIO_COUNTER_3_OUTPUT;
117 playing_notes = false;
118 playing_note = false;
122 for (uint8_t i = 0; i < 8; i++)
129 void stop_note(float freq)
132 if (!audio_initialized) {
135 for (int i = 7; i >= 0; i--) {
136 if (frequencies[i] == freq) {
139 for (int j = i; (j < 7); j++) {
140 frequencies[j] = frequencies[j+1];
141 frequencies[j+1] = 0;
142 volumes[j] = volumes[j+1];
151 if (voice_place >= voices) {
155 DISABLE_AUDIO_COUNTER_3_ISR;
156 DISABLE_AUDIO_COUNTER_3_OUTPUT;
159 playing_note = false;
164 #ifdef VIBRATO_ENABLE
166 float mod(float a, int b)
168 float r = fmod(a, b);
169 return r < 0 ? r + b : r;
172 float vibrato(float average_freq) {
173 #ifdef VIBRATO_STRENGTH_ENABLE
174 float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
176 float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
178 vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
179 return vibrated_freq;
184 ISR(TIMER3_COMPA_vect)
190 if (polyphony_rate > 0) {
192 voice_place %= voices;
193 if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
194 voice_place = (voice_place + 1) % voices;
199 #ifdef VIBRATO_ENABLE
200 if (vibrato_strength > 0) {
201 freq = vibrato(frequencies[voice_place]);
203 freq = frequencies[voice_place];
206 freq = frequencies[voice_place];
210 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);
212 } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
213 frequency = frequency * pow(2, -440/frequency/12/2);
215 frequency = frequencies[voices - 1];
218 frequency = frequencies[voices - 1];
221 #ifdef VIBRATO_ENABLE
222 if (vibrato_strength > 0) {
223 freq = vibrato(frequency);
232 if (envelope_index < 65535) {
236 freq = voice_envelope(freq);
238 if (freq < 30.517578125) {
242 TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
243 TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
248 if (note_frequency > 0) {
249 #ifdef VIBRATO_ENABLE
250 if (vibrato_strength > 0) {
251 freq = vibrato(note_frequency);
253 freq = note_frequency;
256 freq = note_frequency;
259 if (envelope_index < 65535) {
262 freq = voice_envelope(freq);
264 TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
265 TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
268 TIMER_3_DUTY_CYCLE = 0;
272 bool end_of_note = false;
273 if (TIMER_3_PERIOD > 0) {
274 end_of_note = (note_position >= (note_length / TIMER_3_PERIOD * 0xFFFF));
276 end_of_note = (note_position >= (note_length * 0x7FF));
281 if (current_note >= notes_count) {
285 DISABLE_AUDIO_COUNTER_3_ISR;
286 DISABLE_AUDIO_COUNTER_3_OUTPUT;
287 playing_notes = false;
291 if (!note_resting && (notes_rest > 0)) {
294 note_length = notes_rest;
297 note_resting = false;
299 note_frequency = (*notes_pointer)[current_note][0];
300 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
307 if (!audio_config.enable) {
308 playing_notes = false;
309 playing_note = false;
313 void play_note(float freq, int vol) {
315 if (!audio_initialized) {
319 if (audio_config.enable && voices < 8) {
320 DISABLE_AUDIO_COUNTER_3_ISR;
322 // Cancel notes if notes are playing
331 frequencies[voices] = freq;
332 volumes[voices] = vol;
336 ENABLE_AUDIO_COUNTER_3_ISR;
337 ENABLE_AUDIO_COUNTER_3_OUTPUT;
342 void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest)
345 if (!audio_initialized) {
349 if (audio_config.enable) {
351 DISABLE_AUDIO_COUNTER_3_ISR;
353 // Cancel note if a note is playing
357 playing_notes = true;
360 notes_count = n_count;
361 notes_repeat = n_repeat;
367 note_frequency = (*notes_pointer)[current_note][0];
368 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
372 ENABLE_AUDIO_COUNTER_3_ISR;
373 ENABLE_AUDIO_COUNTER_3_OUTPUT;
378 bool is_playing_notes(void) {
379 return playing_notes;
382 bool is_audio_on(void) {
383 return (audio_config.enable != 0);
386 void audio_toggle(void) {
387 audio_config.enable ^= 1;
388 eeconfig_update_audio(audio_config.raw);
389 if (audio_config.enable)
393 void audio_on(void) {
394 audio_config.enable = 1;
395 eeconfig_update_audio(audio_config.raw);
399 void audio_off(void) {
400 audio_config.enable = 0;
401 eeconfig_update_audio(audio_config.raw);
404 #ifdef VIBRATO_ENABLE
406 // Vibrato rate functions
408 void set_vibrato_rate(float rate) {
412 void increase_vibrato_rate(float change) {
413 vibrato_rate *= change;
416 void decrease_vibrato_rate(float change) {
417 vibrato_rate /= change;
420 #ifdef VIBRATO_STRENGTH_ENABLE
422 void set_vibrato_strength(float strength) {
423 vibrato_strength = strength;
426 void increase_vibrato_strength(float change) {
427 vibrato_strength *= change;
430 void decrease_vibrato_strength(float change) {
431 vibrato_strength /= change;
434 #endif /* VIBRATO_STRENGTH_ENABLE */
436 #endif /* VIBRATO_ENABLE */
438 // Polyphony functions
440 void set_polyphony_rate(float rate) {
441 polyphony_rate = rate;
444 void enable_polyphony() {
448 void disable_polyphony() {
452 void increase_polyphony_rate(float change) {
453 polyphony_rate *= change;
456 void decrease_polyphony_rate(float change) {
457 polyphony_rate /= change;
462 void set_timbre(float timbre) {
463 note_timbre = timbre;
468 void set_tempo(uint8_t tempo) {
472 void decrease_tempo(uint8_t tempo_change) {
473 note_tempo += tempo_change;
476 void increase_tempo(uint8_t tempo_change) {
477 if (note_tempo - tempo_change < 10) {
480 note_tempo -= tempo_change;