aboutsummaryrefslogtreecommitdiff
path: root/quantum/audio/audio.c
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/audio/audio.c')
-rw-r--r--quantum/audio/audio.c740
1 files changed, 342 insertions, 398 deletions
diff --git a/quantum/audio/audio.c b/quantum/audio/audio.c
index 3da164c73..781378788 100644
--- a/quantum/audio/audio.c
+++ b/quantum/audio/audio.c
@@ -18,9 +18,9 @@
18#include <string.h> 18#include <string.h>
19//#include <math.h> 19//#include <math.h>
20#if defined(__AVR__) 20#if defined(__AVR__)
21 #include <avr/pgmspace.h> 21# include <avr/pgmspace.h>
22 #include <avr/interrupt.h> 22# include <avr/interrupt.h>
23 #include <avr/io.h> 23# include <avr/io.h>
24#endif 24#endif
25#include "print.h" 25#include "print.h"
26#include "audio.h" 26#include "audio.h"
@@ -35,119 +35,118 @@
35// Timer Abstractions 35// Timer Abstractions
36// ----------------------------------------------------------------------------- 36// -----------------------------------------------------------------------------
37 37
38//Currently we support timers 1 and 3 used at the sime time, channels A-C, 38// Currently we support timers 1 and 3 used at the sime time, channels A-C,
39//pins PB5, PB6, PB7, PC4, PC5, and PC6 39// pins PB5, PB6, PB7, PC4, PC5, and PC6
40#if defined(C6_AUDIO) 40#if defined(C6_AUDIO)
41 #define CPIN_AUDIO 41# define CPIN_AUDIO
42 #define CPIN_SET_DIRECTION DDRC |= _BV(PORTC6); 42# define CPIN_SET_DIRECTION DDRC |= _BV(PORTC6);
43 #define INIT_AUDIO_COUNTER_3 TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30); 43# define INIT_AUDIO_COUNTER_3 TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
44 #define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3A) 44# define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3A)
45 #define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3A) 45# define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3A)
46 #define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3A1); 46# define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3A1);
47 #define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3A1) | _BV(COM3A0)); 47# define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3A1) | _BV(COM3A0));
48 #define TIMER_3_PERIOD ICR3 48# define TIMER_3_PERIOD ICR3
49 #define TIMER_3_DUTY_CYCLE OCR3A 49# define TIMER_3_DUTY_CYCLE OCR3A
50 #define TIMER3_AUDIO_vect TIMER3_COMPA_vect 50# define TIMER3_AUDIO_vect TIMER3_COMPA_vect
51#endif 51#endif
52#if defined(C5_AUDIO) 52#if defined(C5_AUDIO)
53 #define CPIN_AUDIO 53# define CPIN_AUDIO
54 #define CPIN_SET_DIRECTION DDRC |= _BV(PORTC5); 54# define CPIN_SET_DIRECTION DDRC |= _BV(PORTC5);
55 #define INIT_AUDIO_COUNTER_3 TCCR3A = (0 << COM3B1) | (0 << COM3B0) | (1 << WGM31) | (0 << WGM30); 55# define INIT_AUDIO_COUNTER_3 TCCR3A = (0 << COM3B1) | (0 << COM3B0) | (1 << WGM31) | (0 << WGM30);
56 #define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3B) 56# define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3B)
57 #define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3B) 57# define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3B)
58 #define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3B1); 58# define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3B1);
59 #define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3B1) | _BV(COM3B0)); 59# define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3B1) | _BV(COM3B0));
60 #define TIMER_3_PERIOD ICR3 60# define TIMER_3_PERIOD ICR3
61 #define TIMER_3_DUTY_CYCLE OCR3B 61# define TIMER_3_DUTY_CYCLE OCR3B
62 #define TIMER3_AUDIO_vect TIMER3_COMPB_vect 62# define TIMER3_AUDIO_vect TIMER3_COMPB_vect
63#endif 63#endif
64#if defined(C4_AUDIO) 64#if defined(C4_AUDIO)
65 #define CPIN_AUDIO 65# define CPIN_AUDIO
66 #define CPIN_SET_DIRECTION DDRC |= _BV(PORTC4); 66# define CPIN_SET_DIRECTION DDRC |= _BV(PORTC4);
67 #define INIT_AUDIO_COUNTER_3 TCCR3A = (0 << COM3C1) | (0 << COM3C0) | (1 << WGM31) | (0 << WGM30); 67# define INIT_AUDIO_COUNTER_3 TCCR3A = (0 << COM3C1) | (0 << COM3C0) | (1 << WGM31) | (0 << WGM30);
68 #define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3C) 68# define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3C)
69 #define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3C) 69# define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3C)
70 #define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3C1); 70# define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3C1);
71 #define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3C1) | _BV(COM3C0)); 71# define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3C1) | _BV(COM3C0));
72 #define TIMER_3_PERIOD ICR3 72# define TIMER_3_PERIOD ICR3
73 #define TIMER_3_DUTY_CYCLE OCR3C 73# define TIMER_3_DUTY_CYCLE OCR3C
74 #define TIMER3_AUDIO_vect TIMER3_COMPC_vect 74# define TIMER3_AUDIO_vect TIMER3_COMPC_vect
75#endif 75#endif
76 76
77#if defined(B5_AUDIO) 77#if defined(B5_AUDIO)
78 #define BPIN_AUDIO 78# define BPIN_AUDIO
79 #define BPIN_SET_DIRECTION DDRB |= _BV(PORTB5); 79# define BPIN_SET_DIRECTION DDRB |= _BV(PORTB5);
80 #define INIT_AUDIO_COUNTER_1 TCCR1A = (0 << COM1A1) | (0 << COM1A0) | (1 << WGM11) | (0 << WGM10); 80# define INIT_AUDIO_COUNTER_1 TCCR1A = (0 << COM1A1) | (0 << COM1A0) | (1 << WGM11) | (0 << WGM10);
81 #define ENABLE_AUDIO_COUNTER_1_ISR TIMSK1 |= _BV(OCIE1A) 81# define ENABLE_AUDIO_COUNTER_1_ISR TIMSK1 |= _BV(OCIE1A)
82 #define DISABLE_AUDIO_COUNTER_1_ISR TIMSK1 &= ~_BV(OCIE1A) 82# define DISABLE_AUDIO_COUNTER_1_ISR TIMSK1 &= ~_BV(OCIE1A)
83 #define ENABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A |= _BV(COM1A1); 83# define ENABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A |= _BV(COM1A1);
84 #define DISABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A &= ~(_BV(COM1A1) | _BV(COM1A0)); 84# define DISABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A &= ~(_BV(COM1A1) | _BV(COM1A0));
85 #define TIMER_1_PERIOD ICR1 85# define TIMER_1_PERIOD ICR1
86 #define TIMER_1_DUTY_CYCLE OCR1A 86# define TIMER_1_DUTY_CYCLE OCR1A
87 #define TIMER1_AUDIO_vect TIMER1_COMPA_vect 87# define TIMER1_AUDIO_vect TIMER1_COMPA_vect
88#endif 88#endif
89#if defined(B6_AUDIO) 89#if defined(B6_AUDIO)
90 #define BPIN_AUDIO 90# define BPIN_AUDIO
91 #define BPIN_SET_DIRECTION DDRB |= _BV(PORTB6); 91# define BPIN_SET_DIRECTION DDRB |= _BV(PORTB6);
92 #define INIT_AUDIO_COUNTER_1 TCCR1A = (0 << COM1B1) | (0 << COM1B0) | (1 << WGM11) | (0 << WGM10); 92# define INIT_AUDIO_COUNTER_1 TCCR1A = (0 << COM1B1) | (0 << COM1B0) | (1 << WGM11) | (0 << WGM10);
93 #define ENABLE_AUDIO_COUNTER_1_ISR TIMSK1 |= _BV(OCIE1B) 93# define ENABLE_AUDIO_COUNTER_1_ISR TIMSK1 |= _BV(OCIE1B)
94 #define DISABLE_AUDIO_COUNTER_1_ISR TIMSK1 &= ~_BV(OCIE1B) 94# define DISABLE_AUDIO_COUNTER_1_ISR TIMSK1 &= ~_BV(OCIE1B)
95 #define ENABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A |= _BV(COM1B1); 95# define ENABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A |= _BV(COM1B1);
96 #define DISABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A &= ~(_BV(COM1B1) | _BV(COM1B0)); 96# define DISABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A &= ~(_BV(COM1B1) | _BV(COM1B0));
97 #define TIMER_1_PERIOD ICR1 97# define TIMER_1_PERIOD ICR1
98 #define TIMER_1_DUTY_CYCLE OCR1B 98# define TIMER_1_DUTY_CYCLE OCR1B
99 #define TIMER1_AUDIO_vect TIMER1_COMPB_vect 99# define TIMER1_AUDIO_vect TIMER1_COMPB_vect
100#endif 100#endif
101#if defined(B7_AUDIO) 101#if defined(B7_AUDIO)
102 #define BPIN_AUDIO 102# define BPIN_AUDIO
103 #define BPIN_SET_DIRECTION DDRB |= _BV(PORTB7); 103# define BPIN_SET_DIRECTION DDRB |= _BV(PORTB7);
104 #define INIT_AUDIO_COUNTER_1 TCCR1A = (0 << COM1C1) | (0 << COM1C0) | (1 << WGM11) | (0 << WGM10); 104# define INIT_AUDIO_COUNTER_1 TCCR1A = (0 << COM1C1) | (0 << COM1C0) | (1 << WGM11) | (0 << WGM10);
105 #define ENABLE_AUDIO_COUNTER_1_ISR TIMSK1 |= _BV(OCIE1C) 105# define ENABLE_AUDIO_COUNTER_1_ISR TIMSK1 |= _BV(OCIE1C)
106 #define DISABLE_AUDIO_COUNTER_1_ISR TIMSK1 &= ~_BV(OCIE1C) 106# define DISABLE_AUDIO_COUNTER_1_ISR TIMSK1 &= ~_BV(OCIE1C)
107 #define ENABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A |= _BV(COM1C1); 107# define ENABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A |= _BV(COM1C1);
108 #define DISABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A &= ~(_BV(COM1C1) | _BV(COM1C0)); 108# define DISABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A &= ~(_BV(COM1C1) | _BV(COM1C0));
109 #define TIMER_1_PERIOD ICR1 109# define TIMER_1_PERIOD ICR1
110 #define TIMER_1_DUTY_CYCLE OCR1C 110# define TIMER_1_DUTY_CYCLE OCR1C
111 #define TIMER1_AUDIO_vect TIMER1_COMPC_vect 111# define TIMER1_AUDIO_vect TIMER1_COMPC_vect
112#endif 112#endif
113// ----------------------------------------------------------------------------- 113// -----------------------------------------------------------------------------
114 114
115 115int voices = 0;
116int voices = 0; 116int voice_place = 0;
117int voice_place = 0; 117float frequency = 0;
118float frequency = 0;
119float frequency_alt = 0; 118float frequency_alt = 0;
120int volume = 0; 119int volume = 0;
121long position = 0; 120long position = 0;
122 121
123float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0}; 122float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
124int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0}; 123int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
125bool sliding = false; 124bool sliding = false;
126 125
127float place = 0; 126float place = 0;
128 127
129uint8_t * sample; 128uint8_t* sample;
130uint16_t sample_length = 0; 129uint16_t sample_length = 0;
131 130
132bool playing_notes = false; 131bool playing_notes = false;
133bool playing_note = false; 132bool playing_note = false;
134float note_frequency = 0; 133float note_frequency = 0;
135float note_length = 0; 134float note_length = 0;
136uint8_t note_tempo = TEMPO_DEFAULT; 135uint8_t note_tempo = TEMPO_DEFAULT;
137float note_timbre = TIMBRE_DEFAULT; 136float note_timbre = TIMBRE_DEFAULT;
138uint16_t note_position = 0; 137uint16_t note_position = 0;
139float (* notes_pointer)[][2]; 138float (*notes_pointer)[][2];
140uint16_t notes_count; 139uint16_t notes_count;
141bool notes_repeat; 140bool notes_repeat;
142bool note_resting = false; 141bool note_resting = false;
143 142
144uint16_t current_note = 0; 143uint16_t current_note = 0;
145uint8_t rest_counter = 0; 144uint8_t rest_counter = 0;
146 145
147#ifdef VIBRATO_ENABLE 146#ifdef VIBRATO_ENABLE
148float vibrato_counter = 0; 147float vibrato_counter = 0;
149float vibrato_strength = .5; 148float vibrato_strength = .5;
150float vibrato_rate = 0.125; 149float vibrato_rate = 0.125;
151#endif 150#endif
152 151
153float polyphony_rate = 0; 152float polyphony_rate = 0;
@@ -157,73 +156,69 @@ static bool audio_initialized = false;
157audio_config_t audio_config; 156audio_config_t audio_config;
158 157
159uint16_t envelope_index = 0; 158uint16_t envelope_index = 0;
160bool glissando = true; 159bool glissando = true;
161 160
162#ifndef STARTUP_SONG 161#ifndef STARTUP_SONG
163 #define STARTUP_SONG SONG(STARTUP_SOUND) 162# define STARTUP_SONG SONG(STARTUP_SOUND)
164#endif 163#endif
165#ifndef AUDIO_ON_SONG 164#ifndef AUDIO_ON_SONG
166 #define AUDIO_ON_SONG SONG(AUDIO_ON_SOUND) 165# define AUDIO_ON_SONG SONG(AUDIO_ON_SOUND)
167#endif 166#endif
168#ifndef AUDIO_OFF_SONG 167#ifndef AUDIO_OFF_SONG
169 #define AUDIO_OFF_SONG SONG(AUDIO_OFF_SOUND) 168# define AUDIO_OFF_SONG SONG(AUDIO_OFF_SOUND)
170#endif 169#endif
171float startup_song[][2] = STARTUP_SONG; 170float startup_song[][2] = STARTUP_SONG;
172float audio_on_song[][2] = AUDIO_ON_SONG; 171float audio_on_song[][2] = AUDIO_ON_SONG;
173float audio_off_song[][2] = AUDIO_OFF_SONG; 172float audio_off_song[][2] = AUDIO_OFF_SONG;
174 173
175void audio_init() 174void audio_init() {
176{
177
178 // Check EEPROM 175 // Check EEPROM
179 if (!eeconfig_is_enabled()) 176 if (!eeconfig_is_enabled()) {
180 {
181 eeconfig_init(); 177 eeconfig_init();
182 } 178 }
183 audio_config.raw = eeconfig_read_audio(); 179 audio_config.raw = eeconfig_read_audio();
184 180
185 if (!audio_initialized) { 181 if (!audio_initialized) {
182// Set audio ports as output
183#ifdef CPIN_AUDIO
184 CPIN_SET_DIRECTION
185 DISABLE_AUDIO_COUNTER_3_ISR;
186#endif
187#ifdef BPIN_AUDIO
188 BPIN_SET_DIRECTION
189 DISABLE_AUDIO_COUNTER_1_ISR;
190#endif
186 191
187 // Set audio ports as output 192// TCCR3A / TCCR3B: Timer/Counter #3 Control Registers TCCR3A/TCCR3B, TCCR1A/TCCR1B
188 #ifdef CPIN_AUDIO 193// Compare Output Mode (COM3An and COM1An) = 0b00 = Normal port operation
189 CPIN_SET_DIRECTION 194// OC3A -- PC6
190 DISABLE_AUDIO_COUNTER_3_ISR; 195// OC3B -- PC5
191 #endif 196// OC3C -- PC4
192 #ifdef BPIN_AUDIO 197// OC1A -- PB5
193 BPIN_SET_DIRECTION 198// OC1B -- PB6
194 DISABLE_AUDIO_COUNTER_1_ISR; 199// OC1C -- PB7
195 #endif 200
196 201// Waveform Generation Mode (WGM3n) = 0b1110 = Fast PWM Mode 14. Period = ICR3, Duty Cycle OCR3A)
197 // TCCR3A / TCCR3B: Timer/Counter #3 Control Registers TCCR3A/TCCR3B, TCCR1A/TCCR1B 202// OCR3A - PC6
198 // Compare Output Mode (COM3An and COM1An) = 0b00 = Normal port operation 203// OCR3B - PC5
199 // OC3A -- PC6 204// OCR3C - PC4
200 // OC3B -- PC5 205// OCR1A - PB5
201 // OC3C -- PC4 206// OCR1B - PB6
202 // OC1A -- PB5 207// OCR1C - PB7
203 // OC1B -- PB6 208
204 // OC1C -- PB7 209// Clock Select (CS3n) = 0b010 = Clock / 8
205 210#ifdef CPIN_AUDIO
206 // Waveform Generation Mode (WGM3n) = 0b1110 = Fast PWM Mode 14. Period = ICR3, Duty Cycle OCR3A) 211 INIT_AUDIO_COUNTER_3
207 // OCR3A - PC6 212 TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
208 // OCR3B - PC5 213 TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (440 * CPU_PRESCALER));
209 // OCR3C - PC4 214 TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (440 * CPU_PRESCALER)) * note_timbre);
210 // OCR1A - PB5 215#endif
211 // OCR1B - PB6 216#ifdef BPIN_AUDIO
212 // OCR1C - PB7 217 INIT_AUDIO_COUNTER_1
213 218 TCCR1B = (1 << WGM13) | (1 << WGM12) | (0 << CS12) | (1 << CS11) | (0 << CS10);
214 // Clock Select (CS3n) = 0b010 = Clock / 8 219 TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (440 * CPU_PRESCALER));
215 #ifdef CPIN_AUDIO 220 TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (440 * CPU_PRESCALER)) * note_timbre);
216 INIT_AUDIO_COUNTER_3 221#endif
217 TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
218 TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (440 * CPU_PRESCALER));
219 TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (440 * CPU_PRESCALER)) * note_timbre);
220 #endif
221 #ifdef BPIN_AUDIO
222 INIT_AUDIO_COUNTER_1
223 TCCR1B = (1 << WGM13) | (1 << WGM12) | (0 << CS12) | (1 << CS11) | (0 << CS10);
224 TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (440 * CPU_PRESCALER));
225 TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (440 * CPU_PRESCALER)) * note_timbre);
226 #endif
227 222
228 audio_initialized = true; 223 audio_initialized = true;
229 } 224 }
@@ -231,11 +226,9 @@ void audio_init()
231 if (audio_config.enable) { 226 if (audio_config.enable) {
232 PLAY_SONG(startup_song); 227 PLAY_SONG(startup_song);
233 } 228 }
234
235} 229}
236 230
237void stop_all_notes() 231void stop_all_notes() {
238{
239 dprintf("audio stop all notes"); 232 dprintf("audio stop all notes");
240 233
241 if (!audio_initialized) { 234 if (!audio_initialized) {
@@ -243,31 +236,29 @@ void stop_all_notes()
243 } 236 }
244 voices = 0; 237 voices = 0;
245 238
246 #ifdef CPIN_AUDIO 239#ifdef CPIN_AUDIO
247 DISABLE_AUDIO_COUNTER_3_ISR; 240 DISABLE_AUDIO_COUNTER_3_ISR;
248 DISABLE_AUDIO_COUNTER_3_OUTPUT; 241 DISABLE_AUDIO_COUNTER_3_OUTPUT;
249 #endif 242#endif
250 243
251 #ifdef BPIN_AUDIO 244#ifdef BPIN_AUDIO
252 DISABLE_AUDIO_COUNTER_1_ISR; 245 DISABLE_AUDIO_COUNTER_1_ISR;
253 DISABLE_AUDIO_COUNTER_1_OUTPUT; 246 DISABLE_AUDIO_COUNTER_1_OUTPUT;
254 #endif 247#endif
255 248
256 playing_notes = false; 249 playing_notes = false;
257 playing_note = false; 250 playing_note = false;
258 frequency = 0; 251 frequency = 0;
259 frequency_alt = 0; 252 frequency_alt = 0;
260 volume = 0; 253 volume = 0;
261 254
262 for (uint8_t i = 0; i < 8; i++) 255 for (uint8_t i = 0; i < 8; i++) {
263 {
264 frequencies[i] = 0; 256 frequencies[i] = 0;
265 volumes[i] = 0; 257 volumes[i] = 0;
266 } 258 }
267} 259}
268 260
269void stop_note(float freq) 261void stop_note(float freq) {
270{
271 dprintf("audio stop note freq=%d", (int)freq); 262 dprintf("audio stop note freq=%d", (int)freq);
272 263
273 if (playing_note) { 264 if (playing_note) {
@@ -277,133 +268,129 @@ void stop_note(float freq)
277 for (int i = 7; i >= 0; i--) { 268 for (int i = 7; i >= 0; i--) {
278 if (frequencies[i] == freq) { 269 if (frequencies[i] == freq) {
279 frequencies[i] = 0; 270 frequencies[i] = 0;
280 volumes[i] = 0; 271 volumes[i] = 0;
281 for (int j = i; (j < 7); j++) { 272 for (int j = i; (j < 7); j++) {
282 frequencies[j] = frequencies[j+1]; 273 frequencies[j] = frequencies[j + 1];
283 frequencies[j+1] = 0; 274 frequencies[j + 1] = 0;
284 volumes[j] = volumes[j+1]; 275 volumes[j] = volumes[j + 1];
285 volumes[j+1] = 0; 276 volumes[j + 1] = 0;
286 } 277 }
287 break; 278 break;
288 } 279 }
289 } 280 }
290 voices--; 281 voices--;
291 if (voices < 0) 282 if (voices < 0) voices = 0;
292 voices = 0;
293 if (voice_place >= voices) { 283 if (voice_place >= voices) {
294 voice_place = 0; 284 voice_place = 0;
295 } 285 }
296 if (voices == 0) { 286 if (voices == 0) {
297 #ifdef CPIN_AUDIO 287#ifdef CPIN_AUDIO
298 DISABLE_AUDIO_COUNTER_3_ISR; 288 DISABLE_AUDIO_COUNTER_3_ISR;
299 DISABLE_AUDIO_COUNTER_3_OUTPUT; 289 DISABLE_AUDIO_COUNTER_3_OUTPUT;
300 #endif 290#endif
301 #ifdef BPIN_AUDIO 291#ifdef BPIN_AUDIO
302 DISABLE_AUDIO_COUNTER_1_ISR; 292 DISABLE_AUDIO_COUNTER_1_ISR;
303 DISABLE_AUDIO_COUNTER_1_OUTPUT; 293 DISABLE_AUDIO_COUNTER_1_OUTPUT;
304 #endif 294#endif
305 frequency = 0; 295 frequency = 0;
306 frequency_alt = 0; 296 frequency_alt = 0;
307 volume = 0; 297 volume = 0;
308 playing_note = false; 298 playing_note = false;
309 } 299 }
310 } 300 }
311} 301}
312 302
313#ifdef VIBRATO_ENABLE 303#ifdef VIBRATO_ENABLE
314 304
315float mod(float a, int b) 305float mod(float a, int b) {
316{
317 float r = fmod(a, b); 306 float r = fmod(a, b);
318 return r < 0 ? r + b : r; 307 return r < 0 ? r + b : r;
319} 308}
320 309
321float vibrato(float average_freq) { 310float vibrato(float average_freq) {
322 #ifdef VIBRATO_STRENGTH_ENABLE 311# ifdef VIBRATO_STRENGTH_ENABLE
323 float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength); 312 float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
324 #else 313# else
325 float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter]; 314 float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
326 #endif 315# endif
327 vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH); 316 vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0 / average_freq)), VIBRATO_LUT_LENGTH);
328 return vibrated_freq; 317 return vibrated_freq;
329} 318}
330 319
331#endif 320#endif
332 321
333#ifdef CPIN_AUDIO 322#ifdef CPIN_AUDIO
334ISR(TIMER3_AUDIO_vect) 323ISR(TIMER3_AUDIO_vect) {
335{
336 float freq; 324 float freq;
337 325
338 if (playing_note) { 326 if (playing_note) {
339 if (voices > 0) { 327 if (voices > 0) {
340 328# ifdef BPIN_AUDIO
341 #ifdef BPIN_AUDIO
342 float freq_alt = 0; 329 float freq_alt = 0;
343 if (voices > 1) { 330 if (voices > 1) {
344 if (polyphony_rate == 0) { 331 if (polyphony_rate == 0) {
345 if (glissando) { 332 if (glissando) {
346 if (frequency_alt != 0 && frequency_alt < frequencies[voices - 2] && frequency_alt < frequencies[voices - 2] * pow(2, -440/frequencies[voices - 2]/12/2)) { 333 if (frequency_alt != 0 && frequency_alt < frequencies[voices - 2] && frequency_alt < frequencies[voices - 2] * pow(2, -440 / frequencies[voices - 2] / 12 / 2)) {
347 frequency_alt = frequency_alt * pow(2, 440/frequency_alt/12/2); 334 frequency_alt = frequency_alt * pow(2, 440 / frequency_alt / 12 / 2);
348 } else if (frequency_alt != 0 && frequency_alt > frequencies[voices - 2] && frequency_alt > frequencies[voices - 2] * pow(2, 440/frequencies[voices - 2]/12/2)) { 335 } else if (frequency_alt != 0 && frequency_alt > frequencies[voices - 2] && frequency_alt > frequencies[voices - 2] * pow(2, 440 / frequencies[voices - 2] / 12 / 2)) {
349 frequency_alt = frequency_alt * pow(2, -440/frequency_alt/12/2); 336 frequency_alt = frequency_alt * pow(2, -440 / frequency_alt / 12 / 2);
350 } else {
351 frequency_alt = frequencies[voices - 2];
352 }
353 } else { 337 } else {
354 frequency_alt = frequencies[voices - 2]; 338 frequency_alt = frequencies[voices - 2];
355 } 339 }
356 340 } else {
357 #ifdef VIBRATO_ENABLE 341 frequency_alt = frequencies[voices - 2];
358 if (vibrato_strength > 0) {
359 freq_alt = vibrato(frequency_alt);
360 } else {
361 freq_alt = frequency_alt;
362 }
363 #else
364 freq_alt = frequency_alt;
365 #endif
366 } 342 }
367 343
368 if (envelope_index < 65535) { 344# ifdef VIBRATO_ENABLE
369 envelope_index++; 345 if (vibrato_strength > 0) {
346 freq_alt = vibrato(frequency_alt);
347 } else {
348 freq_alt = frequency_alt;
370 } 349 }
350# else
351 freq_alt = frequency_alt;
352# endif
353 }
371 354
372 freq_alt = voice_envelope(freq_alt); 355 if (envelope_index < 65535) {
356 envelope_index++;
357 }
373 358
374 if (freq_alt < 30.517578125) { 359 freq_alt = voice_envelope(freq_alt);
375 freq_alt = 30.52;
376 }
377 360
378 TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq_alt * CPU_PRESCALER)); 361 if (freq_alt < 30.517578125) {
379 TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq_alt * CPU_PRESCALER)) * note_timbre); 362 freq_alt = 30.52;
380 } 363 }
381 #endif 364
365 TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq_alt * CPU_PRESCALER));
366 TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq_alt * CPU_PRESCALER)) * note_timbre);
367 }
368# endif
382 369
383 if (polyphony_rate > 0) { 370 if (polyphony_rate > 0) {
384 if (voices > 1) { 371 if (voices > 1) {
385 voice_place %= voices; 372 voice_place %= voices;
386 if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) { 373 if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
387 voice_place = (voice_place + 1) % voices; 374 voice_place = (voice_place + 1) % voices;
388 place = 0.0; 375 place = 0.0;
389 } 376 }
390 } 377 }
391 378
392 #ifdef VIBRATO_ENABLE 379# ifdef VIBRATO_ENABLE
393 if (vibrato_strength > 0) { 380 if (vibrato_strength > 0) {
394 freq = vibrato(frequencies[voice_place]); 381 freq = vibrato(frequencies[voice_place]);
395 } else { 382 } else {
396 freq = frequencies[voice_place];
397 }
398 #else
399 freq = frequencies[voice_place]; 383 freq = frequencies[voice_place];
400 #endif 384 }
385# else
386 freq = frequencies[voice_place];
387# endif
401 } else { 388 } else {
402 if (glissando) { 389 if (glissando) {
403 if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) { 390 if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440 / frequencies[voices - 1] / 12 / 2)) {
404 frequency = frequency * pow(2, 440/frequency/12/2); 391 frequency = frequency * pow(2, 440 / frequency / 12 / 2);
405 } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) { 392 } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440 / frequencies[voices - 1] / 12 / 2)) {
406 frequency = frequency * pow(2, -440/frequency/12/2); 393 frequency = frequency * pow(2, -440 / frequency / 12 / 2);
407 } else { 394 } else {
408 frequency = frequencies[voices - 1]; 395 frequency = frequencies[voices - 1];
409 } 396 }
@@ -411,15 +398,15 @@ ISR(TIMER3_AUDIO_vect)
411 frequency = frequencies[voices - 1]; 398 frequency = frequencies[voices - 1];
412 } 399 }
413 400
414 #ifdef VIBRATO_ENABLE 401# ifdef VIBRATO_ENABLE
415 if (vibrato_strength > 0) { 402 if (vibrato_strength > 0) {
416 freq = vibrato(frequency); 403 freq = vibrato(frequency);
417 } else { 404 } else {
418 freq = frequency;
419 }
420 #else
421 freq = frequency; 405 freq = frequency;
422 #endif 406 }
407# else
408 freq = frequency;
409# endif
423 } 410 }
424 411
425 if (envelope_index < 65535) { 412 if (envelope_index < 65535) {
@@ -432,32 +419,32 @@ ISR(TIMER3_AUDIO_vect)
432 freq = 30.52; 419 freq = 30.52;
433 } 420 }
434 421
435 TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER)); 422 TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
436 TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); 423 TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
437 } 424 }
438 } 425 }
439 426
440 if (playing_notes) { 427 if (playing_notes) {
441 if (note_frequency > 0) { 428 if (note_frequency > 0) {
442 #ifdef VIBRATO_ENABLE 429# ifdef VIBRATO_ENABLE
443 if (vibrato_strength > 0) { 430 if (vibrato_strength > 0) {
444 freq = vibrato(note_frequency); 431 freq = vibrato(note_frequency);
445 } else { 432 } else {
446 freq = note_frequency; 433 freq = note_frequency;
447 } 434 }
448 #else 435# else
449 freq = note_frequency; 436 freq = note_frequency;
450 #endif 437# endif
451 438
452 if (envelope_index < 65535) { 439 if (envelope_index < 65535) {
453 envelope_index++; 440 envelope_index++;
454 } 441 }
455 freq = voice_envelope(freq); 442 freq = voice_envelope(freq);
456 443
457 TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER)); 444 TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
458 TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); 445 TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
459 } else { 446 } else {
460 TIMER_3_PERIOD = 0; 447 TIMER_3_PERIOD = 0;
461 TIMER_3_DUTY_CYCLE = 0; 448 TIMER_3_DUTY_CYCLE = 0;
462 } 449 }
463 450
@@ -489,16 +476,16 @@ ISR(TIMER3_AUDIO_vect)
489 current_note--; 476 current_note--;
490 if ((*notes_pointer)[current_note][0] == (*notes_pointer)[current_note + 1][0]) { 477 if ((*notes_pointer)[current_note][0] == (*notes_pointer)[current_note + 1][0]) {
491 note_frequency = 0; 478 note_frequency = 0;
492 note_length = 1; 479 note_length = 1;
493 } else { 480 } else {
494 note_frequency = (*notes_pointer)[current_note][0]; 481 note_frequency = (*notes_pointer)[current_note][0];
495 note_length = 1; 482 note_length = 1;
496 } 483 }
497 } else { 484 } else {
498 note_resting = false; 485 note_resting = false;
499 envelope_index = 0; 486 envelope_index = 0;
500 note_frequency = (*notes_pointer)[current_note][0]; 487 note_frequency = (*notes_pointer)[current_note][0];
501 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100); 488 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
502 } 489 }
503 490
504 note_position = 0; 491 note_position = 0;
@@ -507,15 +494,14 @@ ISR(TIMER3_AUDIO_vect)
507 494
508 if (!audio_config.enable) { 495 if (!audio_config.enable) {
509 playing_notes = false; 496 playing_notes = false;
510 playing_note = false; 497 playing_note = false;
511 } 498 }
512} 499}
513#endif 500#endif
514 501
515#ifdef BPIN_AUDIO 502#ifdef BPIN_AUDIO
516ISR(TIMER1_AUDIO_vect) 503ISR(TIMER1_AUDIO_vect) {
517{ 504# if defined(BPIN_AUDIO) && !defined(CPIN_AUDIO)
518 #if defined(BPIN_AUDIO) && !defined(CPIN_AUDIO)
519 float freq = 0; 505 float freq = 0;
520 506
521 if (playing_note) { 507 if (playing_note) {
@@ -525,25 +511,25 @@ ISR(TIMER1_AUDIO_vect)
525 voice_place %= voices; 511 voice_place %= voices;
526 if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) { 512 if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
527 voice_place = (voice_place + 1) % voices; 513 voice_place = (voice_place + 1) % voices;
528 place = 0.0; 514 place = 0.0;
529 } 515 }
530 } 516 }
531 517
532 #ifdef VIBRATO_ENABLE 518# ifdef VIBRATO_ENABLE
533 if (vibrato_strength > 0) { 519 if (vibrato_strength > 0) {
534 freq = vibrato(frequencies[voice_place]); 520 freq = vibrato(frequencies[voice_place]);
535 } else { 521 } else {
536 freq = frequencies[voice_place];
537 }
538 #else
539 freq = frequencies[voice_place]; 522 freq = frequencies[voice_place];
540 #endif 523 }
524# else
525 freq = frequencies[voice_place];
526# endif
541 } else { 527 } else {
542 if (glissando) { 528 if (glissando) {
543 if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) { 529 if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440 / frequencies[voices - 1] / 12 / 2)) {
544 frequency = frequency * pow(2, 440/frequency/12/2); 530 frequency = frequency * pow(2, 440 / frequency / 12 / 2);
545 } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) { 531 } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440 / frequencies[voices - 1] / 12 / 2)) {
546 frequency = frequency * pow(2, -440/frequency/12/2); 532 frequency = frequency * pow(2, -440 / frequency / 12 / 2);
547 } else { 533 } else {
548 frequency = frequencies[voices - 1]; 534 frequency = frequencies[voices - 1];
549 } 535 }
@@ -551,15 +537,15 @@ ISR(TIMER1_AUDIO_vect)
551 frequency = frequencies[voices - 1]; 537 frequency = frequencies[voices - 1];
552 } 538 }
553 539
554 #ifdef VIBRATO_ENABLE 540# ifdef VIBRATO_ENABLE
555 if (vibrato_strength > 0) { 541 if (vibrato_strength > 0) {
556 freq = vibrato(frequency); 542 freq = vibrato(frequency);
557 } else { 543 } else {
558 freq = frequency;
559 }
560 #else
561 freq = frequency; 544 freq = frequency;
562 #endif 545 }
546# else
547 freq = frequency;
548# endif
563 } 549 }
564 550
565 if (envelope_index < 65535) { 551 if (envelope_index < 65535) {
@@ -572,32 +558,32 @@ ISR(TIMER1_AUDIO_vect)
572 freq = 30.52; 558 freq = 30.52;
573 } 559 }
574 560
575 TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER)); 561 TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
576 TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); 562 TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
577 } 563 }
578 } 564 }
579 565
580 if (playing_notes) { 566 if (playing_notes) {
581 if (note_frequency > 0) { 567 if (note_frequency > 0) {
582 #ifdef VIBRATO_ENABLE 568# ifdef VIBRATO_ENABLE
583 if (vibrato_strength > 0) { 569 if (vibrato_strength > 0) {
584 freq = vibrato(note_frequency); 570 freq = vibrato(note_frequency);
585 } else { 571 } else {
586 freq = note_frequency; 572 freq = note_frequency;
587 } 573 }
588 #else 574# else
589 freq = note_frequency; 575 freq = note_frequency;
590 #endif 576# endif
591 577
592 if (envelope_index < 65535) { 578 if (envelope_index < 65535) {
593 envelope_index++; 579 envelope_index++;
594 } 580 }
595 freq = voice_envelope(freq); 581 freq = voice_envelope(freq);
596 582
597 TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER)); 583 TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
598 TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); 584 TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
599 } else { 585 } else {
600 TIMER_1_PERIOD = 0; 586 TIMER_1_PERIOD = 0;
601 TIMER_1_DUTY_CYCLE = 0; 587 TIMER_1_DUTY_CYCLE = 0;
602 } 588 }
603 589
@@ -629,16 +615,16 @@ ISR(TIMER1_AUDIO_vect)
629 current_note--; 615 current_note--;
630 if ((*notes_pointer)[current_note][0] == (*notes_pointer)[current_note + 1][0]) { 616 if ((*notes_pointer)[current_note][0] == (*notes_pointer)[current_note + 1][0]) {
631 note_frequency = 0; 617 note_frequency = 0;
632 note_length = 1; 618 note_length = 1;
633 } else { 619 } else {
634 note_frequency = (*notes_pointer)[current_note][0]; 620 note_frequency = (*notes_pointer)[current_note][0];
635 note_length = 1; 621 note_length = 1;
636 } 622 }
637 } else { 623 } else {
638 note_resting = false; 624 note_resting = false;
639 envelope_index = 0; 625 envelope_index = 0;
640 note_frequency = (*notes_pointer)[current_note][0]; 626 note_frequency = (*notes_pointer)[current_note][0];
641 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100); 627 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
642 } 628 }
643 629
644 note_position = 0; 630 note_position = 0;
@@ -647,14 +633,13 @@ ISR(TIMER1_AUDIO_vect)
647 633
648 if (!audio_config.enable) { 634 if (!audio_config.enable) {
649 playing_notes = false; 635 playing_notes = false;
650 playing_note = false; 636 playing_note = false;
651 } 637 }
652#endif 638# endif
653} 639}
654#endif 640#endif
655 641
656void play_note(float freq, int vol) { 642void play_note(float freq, int vol) {
657
658 dprintf("audio play note freq=%d vol=%d", (int)freq, vol); 643 dprintf("audio play note freq=%d vol=%d", (int)freq, vol);
659 644
660 if (!audio_initialized) { 645 if (!audio_initialized) {
@@ -662,16 +647,15 @@ void play_note(float freq, int vol) {
662 } 647 }
663 648
664 if (audio_config.enable && voices < 8) { 649 if (audio_config.enable && voices < 8) {
665 #ifdef CPIN_AUDIO 650#ifdef CPIN_AUDIO
666 DISABLE_AUDIO_COUNTER_3_ISR; 651 DISABLE_AUDIO_COUNTER_3_ISR;
667 #endif 652#endif
668 #ifdef BPIN_AUDIO 653#ifdef BPIN_AUDIO
669 DISABLE_AUDIO_COUNTER_1_ISR; 654 DISABLE_AUDIO_COUNTER_1_ISR;
670 #endif 655#endif
671 656
672 // Cancel notes if notes are playing 657 // Cancel notes if notes are playing
673 if (playing_notes) 658 if (playing_notes) stop_all_notes();
674 stop_all_notes();
675 659
676 playing_note = true; 660 playing_note = true;
677 661
@@ -679,90 +663,78 @@ void play_note(float freq, int vol) {
679 663
680 if (freq > 0) { 664 if (freq > 0) {
681 frequencies[voices] = freq; 665 frequencies[voices] = freq;
682 volumes[voices] = vol; 666 volumes[voices] = vol;
683 voices++; 667 voices++;
684 } 668 }
685 669
686 #ifdef CPIN_AUDIO 670#ifdef CPIN_AUDIO
687 ENABLE_AUDIO_COUNTER_3_ISR; 671 ENABLE_AUDIO_COUNTER_3_ISR;
688 ENABLE_AUDIO_COUNTER_3_OUTPUT; 672 ENABLE_AUDIO_COUNTER_3_OUTPUT;
689 #endif 673#endif
690 #ifdef BPIN_AUDIO 674#ifdef BPIN_AUDIO
691 #ifdef CPIN_AUDIO 675# ifdef CPIN_AUDIO
692 if (voices > 1) { 676 if (voices > 1) {
693 ENABLE_AUDIO_COUNTER_1_ISR;
694 ENABLE_AUDIO_COUNTER_1_OUTPUT;
695 }
696 #else
697 ENABLE_AUDIO_COUNTER_1_ISR; 677 ENABLE_AUDIO_COUNTER_1_ISR;
698 ENABLE_AUDIO_COUNTER_1_OUTPUT; 678 ENABLE_AUDIO_COUNTER_1_OUTPUT;
699 #endif 679 }
700 #endif 680# else
681 ENABLE_AUDIO_COUNTER_1_ISR;
682 ENABLE_AUDIO_COUNTER_1_OUTPUT;
683# endif
684#endif
701 } 685 }
702
703} 686}
704 687
705void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat) 688void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat) {
706{
707
708 if (!audio_initialized) { 689 if (!audio_initialized) {
709 audio_init(); 690 audio_init();
710 } 691 }
711 692
712 if (audio_config.enable) { 693 if (audio_config.enable) {
713 694#ifdef CPIN_AUDIO
714 #ifdef CPIN_AUDIO 695 DISABLE_AUDIO_COUNTER_3_ISR;
715 DISABLE_AUDIO_COUNTER_3_ISR; 696#endif
716 #endif 697#ifdef BPIN_AUDIO
717 #ifdef BPIN_AUDIO 698 DISABLE_AUDIO_COUNTER_1_ISR;
718 DISABLE_AUDIO_COUNTER_1_ISR; 699#endif
719 #endif
720 700
721 // Cancel note if a note is playing 701 // Cancel note if a note is playing
722 if (playing_note) 702 if (playing_note) stop_all_notes();
723 stop_all_notes();
724 703
725 playing_notes = true; 704 playing_notes = true;
726 705
727 notes_pointer = np; 706 notes_pointer = np;
728 notes_count = n_count; 707 notes_count = n_count;
729 notes_repeat = n_repeat; 708 notes_repeat = n_repeat;
730 709
731 place = 0; 710 place = 0;
732 current_note = 0; 711 current_note = 0;
733 712
734 note_frequency = (*notes_pointer)[current_note][0]; 713 note_frequency = (*notes_pointer)[current_note][0];
735 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100); 714 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
736 note_position = 0; 715 note_position = 0;
737 716
738 717#ifdef CPIN_AUDIO
739 #ifdef CPIN_AUDIO 718 ENABLE_AUDIO_COUNTER_3_ISR;
740 ENABLE_AUDIO_COUNTER_3_ISR; 719 ENABLE_AUDIO_COUNTER_3_OUTPUT;
741 ENABLE_AUDIO_COUNTER_3_OUTPUT; 720#endif
742 #endif 721#ifdef BPIN_AUDIO
743 #ifdef BPIN_AUDIO 722# ifndef CPIN_AUDIO
744 #ifndef CPIN_AUDIO 723 ENABLE_AUDIO_COUNTER_1_ISR;
745 ENABLE_AUDIO_COUNTER_1_ISR; 724 ENABLE_AUDIO_COUNTER_1_OUTPUT;
746 ENABLE_AUDIO_COUNTER_1_OUTPUT; 725# endif
747 #endif 726#endif
748 #endif
749 } 727 }
750
751} 728}
752 729
753bool is_playing_notes(void) { 730bool is_playing_notes(void) { return playing_notes; }
754 return playing_notes;
755}
756 731
757bool is_audio_on(void) { 732bool is_audio_on(void) { return (audio_config.enable != 0); }
758 return (audio_config.enable != 0);
759}
760 733
761void audio_toggle(void) { 734void audio_toggle(void) {
762 audio_config.enable ^= 1; 735 audio_config.enable ^= 1;
763 eeconfig_update_audio(audio_config.raw); 736 eeconfig_update_audio(audio_config.raw);
764 if (audio_config.enable) 737 if (audio_config.enable) audio_on_user();
765 audio_on_user();
766} 738}
767 739
768void audio_on(void) { 740void audio_on(void) {
@@ -784,73 +756,45 @@ void audio_off(void) {
784 756
785// Vibrato rate functions 757// Vibrato rate functions
786 758
787void set_vibrato_rate(float rate) { 759void set_vibrato_rate(float rate) { vibrato_rate = rate; }
788 vibrato_rate = rate;
789}
790 760
791void increase_vibrato_rate(float change) { 761void increase_vibrato_rate(float change) { vibrato_rate *= change; }
792 vibrato_rate *= change;
793}
794 762
795void decrease_vibrato_rate(float change) { 763void decrease_vibrato_rate(float change) { vibrato_rate /= change; }
796 vibrato_rate /= change;
797}
798 764
799#ifdef VIBRATO_STRENGTH_ENABLE 765# ifdef VIBRATO_STRENGTH_ENABLE
800 766
801void set_vibrato_strength(float strength) { 767void set_vibrato_strength(float strength) { vibrato_strength = strength; }
802 vibrato_strength = strength;
803}
804 768
805void increase_vibrato_strength(float change) { 769void increase_vibrato_strength(float change) { vibrato_strength *= change; }
806 vibrato_strength *= change;
807}
808 770
809void decrease_vibrato_strength(float change) { 771void decrease_vibrato_strength(float change) { vibrato_strength /= change; }
810 vibrato_strength /= change;
811}
812 772
813#endif /* VIBRATO_STRENGTH_ENABLE */ 773# endif /* VIBRATO_STRENGTH_ENABLE */
814 774
815#endif /* VIBRATO_ENABLE */ 775#endif /* VIBRATO_ENABLE */
816 776
817// Polyphony functions 777// Polyphony functions
818 778
819void set_polyphony_rate(float rate) { 779void set_polyphony_rate(float rate) { polyphony_rate = rate; }
820 polyphony_rate = rate;
821}
822 780
823void enable_polyphony() { 781void enable_polyphony() { polyphony_rate = 5; }
824 polyphony_rate = 5;
825}
826 782
827void disable_polyphony() { 783void disable_polyphony() { polyphony_rate = 0; }
828 polyphony_rate = 0;
829}
830 784
831void increase_polyphony_rate(float change) { 785void increase_polyphony_rate(float change) { polyphony_rate *= change; }
832 polyphony_rate *= change;
833}
834 786
835void decrease_polyphony_rate(float change) { 787void decrease_polyphony_rate(float change) { polyphony_rate /= change; }
836 polyphony_rate /= change;
837}
838 788
839// Timbre function 789// Timbre function
840 790
841void set_timbre(float timbre) { 791void set_timbre(float timbre) { note_timbre = timbre; }
842 note_timbre = timbre;
843}
844 792
845// Tempo functions 793// Tempo functions
846 794
847void set_tempo(uint8_t tempo) { 795void set_tempo(uint8_t tempo) { note_tempo = tempo; }
848 note_tempo = tempo;
849}
850 796
851void decrease_tempo(uint8_t tempo_change) { 797void decrease_tempo(uint8_t tempo_change) { note_tempo += tempo_change; }
852 note_tempo += tempo_change;
853}
854 798
855void increase_tempo(uint8_t tempo_change) { 799void increase_tempo(uint8_t tempo_change) {
856 if (note_tempo - tempo_change < 10) { 800 if (note_tempo - tempo_change < 10) {