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 dprintf("audio stop all notes");
111 if (!audio_initialized) {
116 DISABLE_AUDIO_COUNTER_3_ISR;
117 DISABLE_AUDIO_COUNTER_3_OUTPUT;
119 playing_notes = false;
120 playing_note = false;
124 for (uint8_t i = 0; i < 8; i++)
131 void stop_note(float freq)
133 dprintf("audio stop note freq=%d", (int)freq);
136 if (!audio_initialized) {
139 for (int i = 7; i >= 0; i--) {
140 if (frequencies[i] == freq) {
143 for (int j = i; (j < 7); j++) {
144 frequencies[j] = frequencies[j+1];
145 frequencies[j+1] = 0;
146 volumes[j] = volumes[j+1];
155 if (voice_place >= voices) {
159 DISABLE_AUDIO_COUNTER_3_ISR;
160 DISABLE_AUDIO_COUNTER_3_OUTPUT;
163 playing_note = false;
168 #ifdef VIBRATO_ENABLE
170 float mod(float a, int b)
172 float r = fmod(a, b);
173 return r < 0 ? r + b : r;
176 float vibrato(float average_freq) {
177 #ifdef VIBRATO_STRENGTH_ENABLE
178 float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
180 float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
182 vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
183 return vibrated_freq;
188 ISR(TIMER3_COMPA_vect)
194 if (polyphony_rate > 0) {
196 voice_place %= voices;
197 if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
198 voice_place = (voice_place + 1) % voices;
203 #ifdef VIBRATO_ENABLE
204 if (vibrato_strength > 0) {
205 freq = vibrato(frequencies[voice_place]);
207 freq = frequencies[voice_place];
210 freq = frequencies[voice_place];
214 if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
215 frequency = frequency * pow(2, 440/frequency/12/2);
216 } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
217 frequency = frequency * pow(2, -440/frequency/12/2);
219 frequency = frequencies[voices - 1];
222 frequency = frequencies[voices - 1];
225 #ifdef VIBRATO_ENABLE
226 if (vibrato_strength > 0) {
227 freq = vibrato(frequency);
236 if (envelope_index < 65535) {
240 freq = voice_envelope(freq);
242 if (freq < 30.517578125) {
246 TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
247 TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
252 if (note_frequency > 0) {
253 #ifdef VIBRATO_ENABLE
254 if (vibrato_strength > 0) {
255 freq = vibrato(note_frequency);
257 freq = note_frequency;
260 freq = note_frequency;
263 if (envelope_index < 65535) {
266 freq = voice_envelope(freq);
268 TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
269 TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
272 TIMER_3_DUTY_CYCLE = 0;
276 bool end_of_note = false;
277 if (TIMER_3_PERIOD > 0) {
278 end_of_note = (note_position >= (note_length / TIMER_3_PERIOD * 0xFFFF));
280 end_of_note = (note_position >= (note_length * 0x7FF));
285 if (current_note >= notes_count) {
289 DISABLE_AUDIO_COUNTER_3_ISR;
290 DISABLE_AUDIO_COUNTER_3_OUTPUT;
291 playing_notes = false;
295 if (!note_resting && (notes_rest > 0)) {
298 note_length = notes_rest;
301 note_resting = false;
303 note_frequency = (*notes_pointer)[current_note][0];
304 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
311 if (!audio_config.enable) {
312 playing_notes = false;
313 playing_note = false;
317 void play_note(float freq, int vol) {
319 dprintf("audio play note freq=%d vol=%d", (int)freq, vol);
321 if (!audio_initialized) {
325 if (audio_config.enable && voices < 8) {
326 DISABLE_AUDIO_COUNTER_3_ISR;
328 // Cancel notes if notes are playing
337 frequencies[voices] = freq;
338 volumes[voices] = vol;
342 ENABLE_AUDIO_COUNTER_3_ISR;
343 ENABLE_AUDIO_COUNTER_3_OUTPUT;
348 void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest)
351 if (!audio_initialized) {
355 if (audio_config.enable) {
357 DISABLE_AUDIO_COUNTER_3_ISR;
359 // Cancel note if a note is playing
363 playing_notes = true;
366 notes_count = n_count;
367 notes_repeat = n_repeat;
373 note_frequency = (*notes_pointer)[current_note][0];
374 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
378 ENABLE_AUDIO_COUNTER_3_ISR;
379 ENABLE_AUDIO_COUNTER_3_OUTPUT;
384 bool is_playing_notes(void) {
385 return playing_notes;
388 bool is_audio_on(void) {
389 return (audio_config.enable != 0);
392 void audio_toggle(void) {
393 audio_config.enable ^= 1;
394 eeconfig_update_audio(audio_config.raw);
395 if (audio_config.enable)
399 void audio_on(void) {
400 audio_config.enable = 1;
401 eeconfig_update_audio(audio_config.raw);
405 void audio_off(void) {
406 audio_config.enable = 0;
407 eeconfig_update_audio(audio_config.raw);
410 #ifdef VIBRATO_ENABLE
412 // Vibrato rate functions
414 void set_vibrato_rate(float rate) {
418 void increase_vibrato_rate(float change) {
419 vibrato_rate *= change;
422 void decrease_vibrato_rate(float change) {
423 vibrato_rate /= change;
426 #ifdef VIBRATO_STRENGTH_ENABLE
428 void set_vibrato_strength(float strength) {
429 vibrato_strength = strength;
432 void increase_vibrato_strength(float change) {
433 vibrato_strength *= change;
436 void decrease_vibrato_strength(float change) {
437 vibrato_strength /= change;
440 #endif /* VIBRATO_STRENGTH_ENABLE */
442 #endif /* VIBRATO_ENABLE */
444 // Polyphony functions
446 void set_polyphony_rate(float rate) {
447 polyphony_rate = rate;
450 void enable_polyphony() {
454 void disable_polyphony() {
458 void increase_polyphony_rate(float change) {
459 polyphony_rate *= change;
462 void decrease_polyphony_rate(float change) {
463 polyphony_rate /= change;
468 void set_timbre(float timbre) {
469 note_timbre = timbre;
474 void set_tempo(uint8_t tempo) {
478 void decrease_tempo(uint8_t tempo_change) {
479 note_tempo += tempo_change;
482 void increase_tempo(uint8_t tempo_change) {
483 if (note_tempo - tempo_change < 10) {
486 note_tempo -= tempo_change;