1 /* Copyright 2016 Jack Humbert
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <avr/pgmspace.h>
20 #include <avr/interrupt.h>
28 #define CPU_PRESCALER 8
30 // -----------------------------------------------------------------------------
32 // -----------------------------------------------------------------------------
34 // TIMSK3 - Timer/Counter #3 Interrupt Mask Register
35 // Turn on/off 3A interputs, stopping/enabling the ISR calls
36 #define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3A)
37 #define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3A)
39 // TCCR3A: Timer/Counter #3 Control Register
40 // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
41 #define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3A1);
42 #define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3A1) | _BV(COM3A0));
44 // Fast PWM Mode Controls
45 #define TIMER_3_PERIOD ICR3
46 #define TIMER_3_DUTY_CYCLE OCR3A
48 // -----------------------------------------------------------------------------
57 float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
58 int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
64 uint16_t sample_length = 0;
66 bool playing_notes = false;
67 bool playing_note = false;
68 float note_frequency = 0;
69 float note_length = 0;
70 uint8_t note_tempo = TEMPO_DEFAULT;
71 float note_timbre = TIMBRE_DEFAULT;
72 uint16_t note_position = 0;
73 float (* notes_pointer)[][2];
77 bool note_resting = false;
79 uint8_t current_note = 0;
80 uint8_t rest_counter = 0;
83 float vibrato_counter = 0;
84 float vibrato_strength = .5;
85 float vibrato_rate = 0.125;
88 float polyphony_rate = 0;
90 static bool audio_initialized = false;
92 audio_config_t audio_config;
94 uint16_t envelope_index = 0;
95 bool glissando = true;
101 if (!eeconfig_is_enabled())
105 audio_config.raw = eeconfig_read_audio();
107 // Set port PC6 (OC3A and /OC4A) as output
110 DISABLE_AUDIO_COUNTER_3_ISR;
112 // TCCR3A / TCCR3B: Timer/Counter #3 Control Registers
113 // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
114 // Waveform Generation Mode (WGM3n) = 0b1110 = Fast PWM Mode 14 (Period = ICR3, Duty Cycle = OCR3A)
115 // Clock Select (CS3n) = 0b010 = Clock / 8
116 TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
117 TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
119 audio_initialized = true;
122 void stop_all_notes()
124 dprintf("audio stop all notes");
126 if (!audio_initialized) {
131 DISABLE_AUDIO_COUNTER_3_ISR;
132 DISABLE_AUDIO_COUNTER_3_OUTPUT;
134 playing_notes = false;
135 playing_note = false;
139 for (uint8_t i = 0; i < 8; i++)
146 void stop_note(float freq)
148 dprintf("audio stop note freq=%d", (int)freq);
151 if (!audio_initialized) {
154 for (int i = 7; i >= 0; i--) {
155 if (frequencies[i] == freq) {
158 for (int j = i; (j < 7); j++) {
159 frequencies[j] = frequencies[j+1];
160 frequencies[j+1] = 0;
161 volumes[j] = volumes[j+1];
170 if (voice_place >= voices) {
174 DISABLE_AUDIO_COUNTER_3_ISR;
175 DISABLE_AUDIO_COUNTER_3_OUTPUT;
178 playing_note = false;
183 #ifdef VIBRATO_ENABLE
185 float mod(float a, int b)
187 float r = fmod(a, b);
188 return r < 0 ? r + b : r;
191 float vibrato(float average_freq) {
192 #ifdef VIBRATO_STRENGTH_ENABLE
193 float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
195 float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
197 vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
198 return vibrated_freq;
203 ISR(TIMER3_COMPA_vect)
209 if (polyphony_rate > 0) {
211 voice_place %= voices;
212 if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
213 voice_place = (voice_place + 1) % voices;
218 #ifdef VIBRATO_ENABLE
219 if (vibrato_strength > 0) {
220 freq = vibrato(frequencies[voice_place]);
222 freq = frequencies[voice_place];
225 freq = frequencies[voice_place];
229 if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
230 frequency = frequency * pow(2, 440/frequency/12/2);
231 } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
232 frequency = frequency * pow(2, -440/frequency/12/2);
234 frequency = frequencies[voices - 1];
237 frequency = frequencies[voices - 1];
240 #ifdef VIBRATO_ENABLE
241 if (vibrato_strength > 0) {
242 freq = vibrato(frequency);
251 if (envelope_index < 65535) {
255 freq = voice_envelope(freq);
257 if (freq < 30.517578125) {
261 TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
262 TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
267 if (note_frequency > 0) {
268 #ifdef VIBRATO_ENABLE
269 if (vibrato_strength > 0) {
270 freq = vibrato(note_frequency);
272 freq = note_frequency;
275 freq = note_frequency;
278 if (envelope_index < 65535) {
281 freq = voice_envelope(freq);
283 TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
284 TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
287 TIMER_3_DUTY_CYCLE = 0;
291 bool end_of_note = false;
292 if (TIMER_3_PERIOD > 0) {
293 end_of_note = (note_position >= (note_length / TIMER_3_PERIOD * 0xFFFF));
295 end_of_note = (note_position >= (note_length * 0x7FF));
300 if (current_note >= notes_count) {
304 DISABLE_AUDIO_COUNTER_3_ISR;
305 DISABLE_AUDIO_COUNTER_3_OUTPUT;
306 playing_notes = false;
310 if (!note_resting && (notes_rest > 0)) {
313 note_length = notes_rest;
316 note_resting = false;
318 note_frequency = (*notes_pointer)[current_note][0];
319 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
326 if (!audio_config.enable) {
327 playing_notes = false;
328 playing_note = false;
332 void play_note(float freq, int vol) {
334 dprintf("audio play note freq=%d vol=%d", (int)freq, vol);
336 if (!audio_initialized) {
340 if (audio_config.enable && voices < 8) {
341 DISABLE_AUDIO_COUNTER_3_ISR;
343 // Cancel notes if notes are playing
352 frequencies[voices] = freq;
353 volumes[voices] = vol;
357 ENABLE_AUDIO_COUNTER_3_ISR;
358 ENABLE_AUDIO_COUNTER_3_OUTPUT;
363 void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest)
366 if (!audio_initialized) {
370 if (audio_config.enable) {
372 DISABLE_AUDIO_COUNTER_3_ISR;
374 // Cancel note if a note is playing
378 playing_notes = true;
381 notes_count = n_count;
382 notes_repeat = n_repeat;
388 note_frequency = (*notes_pointer)[current_note][0];
389 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
393 ENABLE_AUDIO_COUNTER_3_ISR;
394 ENABLE_AUDIO_COUNTER_3_OUTPUT;
399 bool is_playing_notes(void) {
400 return playing_notes;
403 bool is_audio_on(void) {
404 return (audio_config.enable != 0);
407 void audio_toggle(void) {
408 audio_config.enable ^= 1;
409 eeconfig_update_audio(audio_config.raw);
410 if (audio_config.enable)
414 void audio_on(void) {
415 audio_config.enable = 1;
416 eeconfig_update_audio(audio_config.raw);
420 void audio_off(void) {
421 audio_config.enable = 0;
422 eeconfig_update_audio(audio_config.raw);
425 #ifdef VIBRATO_ENABLE
427 // Vibrato rate functions
429 void set_vibrato_rate(float rate) {
433 void increase_vibrato_rate(float change) {
434 vibrato_rate *= change;
437 void decrease_vibrato_rate(float change) {
438 vibrato_rate /= change;
441 #ifdef VIBRATO_STRENGTH_ENABLE
443 void set_vibrato_strength(float strength) {
444 vibrato_strength = strength;
447 void increase_vibrato_strength(float change) {
448 vibrato_strength *= change;
451 void decrease_vibrato_strength(float change) {
452 vibrato_strength /= change;
455 #endif /* VIBRATO_STRENGTH_ENABLE */
457 #endif /* VIBRATO_ENABLE */
459 // Polyphony functions
461 void set_polyphony_rate(float rate) {
462 polyphony_rate = rate;
465 void enable_polyphony() {
469 void disable_polyphony() {
473 void increase_polyphony_rate(float change) {
474 polyphony_rate *= change;
477 void decrease_polyphony_rate(float change) {
478 polyphony_rate /= change;
483 void set_timbre(float timbre) {
484 note_timbre = timbre;
489 void set_tempo(uint8_t tempo) {
493 void decrease_tempo(uint8_t tempo_change) {
494 note_tempo += tempo_change;
497 void increase_tempo(uint8_t tempo_change) {
498 if (note_tempo - tempo_change < 10) {
501 note_tempo -= tempo_change;