diff options
Diffstat (limited to 'quantum/audio.c')
| -rw-r--r-- | quantum/audio.c | 59 |
1 files changed, 45 insertions, 14 deletions
diff --git a/quantum/audio.c b/quantum/audio.c index 6bd6532a3..e0413051a 100644 --- a/quantum/audio.c +++ b/quantum/audio.c | |||
| @@ -4,7 +4,7 @@ | |||
| 4 | #include <avr/pgmspace.h> | 4 | #include <avr/pgmspace.h> |
| 5 | #include <avr/interrupt.h> | 5 | #include <avr/interrupt.h> |
| 6 | #include <avr/io.h> | 6 | #include <avr/io.h> |
| 7 | 7 | #include "print.h" | |
| 8 | #include "audio.h" | 8 | #include "audio.h" |
| 9 | #include "keymap_common.h" | 9 | #include "keymap_common.h" |
| 10 | 10 | ||
| @@ -57,9 +57,11 @@ bool notes = false; | |||
| 57 | bool note = false; | 57 | bool note = false; |
| 58 | float note_frequency = 0; | 58 | float note_frequency = 0; |
| 59 | float note_length = 0; | 59 | float note_length = 0; |
| 60 | float note_tempo = TEMPO_DEFAULT; | ||
| 61 | float note_timbre = TIMBRE_DEFAULT; | ||
| 60 | uint16_t note_position = 0; | 62 | uint16_t note_position = 0; |
| 61 | float (* notes_pointer)[][2]; | 63 | float (* notes_pointer)[][2]; |
| 62 | uint8_t notes_length; | 64 | uint8_t notes_count; |
| 63 | bool notes_repeat; | 65 | bool notes_repeat; |
| 64 | float notes_rest; | 66 | float notes_rest; |
| 65 | bool note_resting = false; | 67 | bool note_resting = false; |
| @@ -255,7 +257,8 @@ ISR(TIMER3_COMPA_vect) { | |||
| 255 | place = 0.0; | 257 | place = 0.0; |
| 256 | } | 258 | } |
| 257 | ICR3 = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)); // Set max to the period | 259 | ICR3 = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)); // Set max to the period |
| 258 | OCR3A = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)) >> 1 * duty_place; // Set compare to half the period | 260 | OCR3A = (int)((((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)) * note_timbre); // Set compare to half the period |
| 261 | //OCR3A = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)) >> 1 * duty_place; // Set compare to half the period | ||
| 259 | place++; | 262 | place++; |
| 260 | // if (duty_counter > (frequencies[voice_place] / 500)) { | 263 | // if (duty_counter > (frequencies[voice_place] / 500)) { |
| 261 | // duty_place = (duty_place % 3) + 1; | 264 | // duty_place = (duty_place % 3) + 1; |
| @@ -288,7 +291,7 @@ ISR(TIMER3_COMPA_vect) { | |||
| 288 | #else | 291 | #else |
| 289 | if (note_frequency > 0) { | 292 | if (note_frequency > 0) { |
| 290 | ICR3 = (int)(((double)F_CPU) / (note_frequency * CPU_PRESCALER)); // Set max to the period | 293 | ICR3 = (int)(((double)F_CPU) / (note_frequency * CPU_PRESCALER)); // Set max to the period |
| 291 | OCR3A = (int)(((double)F_CPU) / (note_frequency * CPU_PRESCALER)) >> 1; // Set compare to half the period | 294 | OCR3A = (int)((((double)F_CPU) / (note_frequency * CPU_PRESCALER)) * note_timbre); // Set compare to half the period |
| 292 | } else { | 295 | } else { |
| 293 | ICR3 = 0; | 296 | ICR3 = 0; |
| 294 | OCR3A = 0; | 297 | OCR3A = 0; |
| @@ -304,7 +307,7 @@ ISR(TIMER3_COMPA_vect) { | |||
| 304 | end_of_note = (note_position >= (note_length * 0x7FF)); | 307 | end_of_note = (note_position >= (note_length * 0x7FF)); |
| 305 | if (end_of_note) { | 308 | if (end_of_note) { |
| 306 | current_note++; | 309 | current_note++; |
| 307 | if (current_note >= notes_length) { | 310 | if (current_note >= notes_count) { |
| 308 | if (notes_repeat) { | 311 | if (notes_repeat) { |
| 309 | current_note = 0; | 312 | current_note = 0; |
| 310 | } else { | 313 | } else { |
| @@ -327,10 +330,10 @@ ISR(TIMER3_COMPA_vect) { | |||
| 327 | note_resting = false; | 330 | note_resting = false; |
| 328 | #ifdef PWM_AUDIO | 331 | #ifdef PWM_AUDIO |
| 329 | note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE; | 332 | note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE; |
| 330 | note_length = (*notes_pointer)[current_note][1]; | 333 | note_length = (*notes_pointer)[current_note][1] * (note_tempo / 100); |
| 331 | #else | 334 | #else |
| 332 | note_frequency = (*notes_pointer)[current_note][0]; | 335 | note_frequency = (*notes_pointer)[current_note][0]; |
| 333 | note_length = (*notes_pointer)[current_note][1] / 4; | 336 | note_length = ((*notes_pointer)[current_note][1] / 4) * (note_tempo / 100); |
| 334 | #endif | 337 | #endif |
| 335 | } | 338 | } |
| 336 | note_position = 0; | 339 | note_position = 0; |
| @@ -344,15 +347,16 @@ ISR(TIMER3_COMPA_vect) { | |||
| 344 | } | 347 | } |
| 345 | } | 348 | } |
| 346 | 349 | ||
| 347 | void play_notes(float (*np)[][2], uint8_t n_length, bool n_repeat, float n_rest) { | 350 | void play_notes(float (*np)[][2], uint8_t n_count, bool n_repeat, float n_rest) { |
| 348 | 351 | ||
| 349 | if (audio_config.enable) { | 352 | if (audio_config.enable) { |
| 350 | 353 | ||
| 351 | if (note) | 354 | if (note) |
| 352 | stop_all_notes(); | 355 | stop_all_notes(); |
| 356 | notes = true; | ||
| 353 | 357 | ||
| 354 | notes_pointer = np; | 358 | notes_pointer = np; |
| 355 | notes_length = n_length; | 359 | notes_count = n_count; |
| 356 | notes_repeat = n_repeat; | 360 | notes_repeat = n_repeat; |
| 357 | notes_rest = n_rest; | 361 | notes_rest = n_rest; |
| 358 | 362 | ||
| @@ -360,10 +364,10 @@ if (audio_config.enable) { | |||
| 360 | current_note = 0; | 364 | current_note = 0; |
| 361 | #ifdef PWM_AUDIO | 365 | #ifdef PWM_AUDIO |
| 362 | note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE; | 366 | note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE; |
| 363 | note_length = (*notes_pointer)[current_note][1]; | 367 | note_length = (*notes_pointer)[current_note][1] * (note_tempo / 100); |
| 364 | #else | 368 | #else |
| 365 | note_frequency = (*notes_pointer)[current_note][0]; | 369 | note_frequency = (*notes_pointer)[current_note][0]; |
| 366 | note_length = (*notes_pointer)[current_note][1] / 4; | 370 | note_length = ((*notes_pointer)[current_note][1] / 4) * (note_tempo / 100); |
| 367 | #endif | 371 | #endif |
| 368 | note_position = 0; | 372 | note_position = 0; |
| 369 | 373 | ||
| @@ -375,7 +379,6 @@ if (audio_config.enable) { | |||
| 375 | TCCR3A |= _BV(COM3A1); | 379 | TCCR3A |= _BV(COM3A1); |
| 376 | #endif | 380 | #endif |
| 377 | 381 | ||
| 378 | notes = true; | ||
| 379 | } | 382 | } |
| 380 | 383 | ||
| 381 | } | 384 | } |
| @@ -405,6 +408,7 @@ if (audio_config.enable && voices < 8) { | |||
| 405 | 408 | ||
| 406 | if (notes) | 409 | if (notes) |
| 407 | stop_all_notes(); | 410 | stop_all_notes(); |
| 411 | note = true; | ||
| 408 | #ifdef PWM_AUDIO | 412 | #ifdef PWM_AUDIO |
| 409 | freq = freq / SAMPLE_RATE; | 413 | freq = freq / SAMPLE_RATE; |
| 410 | #endif | 414 | #endif |
| @@ -436,7 +440,34 @@ if (audio_config.enable && voices < 8) { | |||
| 436 | TCCR3A |= _BV(COM3A1); | 440 | TCCR3A |= _BV(COM3A1); |
| 437 | #endif | 441 | #endif |
| 438 | 442 | ||
| 439 | note = true; | ||
| 440 | } | 443 | } |
| 441 | 444 | ||
| 442 | } \ No newline at end of file | 445 | } |
| 446 | |||
| 447 | void set_timbre(float timbre) | ||
| 448 | { | ||
| 449 | note_timbre = timbre; | ||
| 450 | } | ||
| 451 | |||
| 452 | void set_tempo(float tempo) | ||
| 453 | { | ||
| 454 | note_tempo = tempo; | ||
| 455 | } | ||
| 456 | |||
| 457 | void decrease_tempo(uint8_t tempo_change) | ||
| 458 | { | ||
| 459 | note_tempo += (float) tempo_change; | ||
| 460 | } | ||
| 461 | |||
| 462 | void increase_tempo(uint8_t tempo_change) | ||
| 463 | { | ||
| 464 | if (note_tempo - (float) tempo_change < 10) | ||
| 465 | { | ||
| 466 | note_tempo = 10; | ||
| 467 | } | ||
| 468 | else | ||
| 469 | { | ||
| 470 | note_tempo -= (float) tempo_change; | ||
| 471 | } | ||
| 472 | } | ||
| 473 | |||
