diff options
| author | Drashna Jaelre <drashna@live.com> | 2019-02-14 20:12:37 -0800 |
|---|---|---|
| committer | MechMerlin <30334081+mechmerlin@users.noreply.github.com> | 2019-02-14 20:12:37 -0800 |
| commit | 85022f8bb5129f7118b55556c1ce85bc7d721356 (patch) | |
| tree | 07a6af1b959c7647e85fddc254d5d2a9845df9c9 | |
| parent | 9e4ac6cf29e6b2ce950135c8f877c95f2d1f1d55 (diff) | |
| download | qmk_firmware-85022f8bb5129f7118b55556c1ce85bc7d721356.tar.gz qmk_firmware-85022f8bb5129f7118b55556c1ce85bc7d721356.zip | |
Fix ARM Audio issues and its EEPROM persistence (#4936)
* Don't click if turning audio off
On ARM, playing the click when turning off audio causes the audio get stuck and continually play the tone
* Fix Audio EEPROM support for ARM
* Update touched files to conform to QMK Coding Conventions
* Add better check for ARM EEPROM support
| -rw-r--r-- | quantum/audio/audio_arm.c | 612 | ||||
| -rw-r--r-- | quantum/process_keycode/process_clicky.c | 26 |
2 files changed, 322 insertions, 316 deletions
diff --git a/quantum/audio/audio_arm.c b/quantum/audio/audio_arm.c index 989f7a64b..f24ce4bd0 100644 --- a/quantum/audio/audio_arm.c +++ b/quantum/audio/audio_arm.c | |||
| @@ -273,19 +273,24 @@ static const DACConversionGroup dacgrpcfg2 = { | |||
| 273 | .trigger = DAC_TRG(0) | 273 | .trigger = DAC_TRG(0) |
| 274 | }; | 274 | }; |
| 275 | 275 | ||
| 276 | void audio_init() | 276 | void audio_init() { |
| 277 | { | 277 | |
| 278 | 278 | if (audio_initialized) { | |
| 279 | if (audio_initialized) | 279 | return; |
| 280 | return; | 280 | } |
| 281 | 281 | ||
| 282 | // Check EEPROM | 282 | // Check EEPROM |
| 283 | // if (!eeconfig_is_enabled()) | 283 | #if defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE) |
| 284 | // { | 284 | if (!eeconfig_is_enabled()) { |
| 285 | // eeconfig_init(); | 285 | eeconfig_init(); |
| 286 | // } | 286 | } |
| 287 | // audio_config.raw = eeconfig_read_audio(); | 287 | audio_config.raw = eeconfig_read_audio(); |
| 288 | #else // ARM EEPROM | ||
| 288 | audio_config.enable = true; | 289 | audio_config.enable = true; |
| 290 | #ifdef AUDIO_CLICKY_ON | ||
| 291 | audio_config.clicky_enable = true; | ||
| 292 | #endif | ||
| 293 | #endif // ARM EEPROM | ||
| 289 | 294 | ||
| 290 | /* | 295 | /* |
| 291 | * Starting DAC1 driver, setting up the output pin as analog as suggested | 296 | * Starting DAC1 driver, setting up the output pin as analog as suggested |
| @@ -308,16 +313,15 @@ void audio_init() | |||
| 308 | dacStartConversion(&DACD1, &dacgrpcfg1, (dacsample_t *)dac_buffer, DAC_BUFFER_SIZE); | 313 | dacStartConversion(&DACD1, &dacgrpcfg1, (dacsample_t *)dac_buffer, DAC_BUFFER_SIZE); |
| 309 | dacStartConversion(&DACD2, &dacgrpcfg2, (dacsample_t *)dac_buffer_2, DAC_BUFFER_SIZE); | 314 | dacStartConversion(&DACD2, &dacgrpcfg2, (dacsample_t *)dac_buffer_2, DAC_BUFFER_SIZE); |
| 310 | 315 | ||
| 311 | audio_initialized = true; | 316 | audio_initialized = true; |
| 312 | 317 | ||
| 313 | if (audio_config.enable) { | 318 | if (audio_config.enable) { |
| 314 | PLAY_SONG(startup_song); | 319 | PLAY_SONG(startup_song); |
| 315 | } | 320 | } |
| 316 | 321 | ||
| 317 | } | 322 | } |
| 318 | 323 | ||
| 319 | void stop_all_notes() | 324 | void stop_all_notes() { |
| 320 | { | ||
| 321 | dprintf("audio stop all notes"); | 325 | dprintf("audio stop all notes"); |
| 322 | 326 | ||
| 323 | if (!audio_initialized) { | 327 | if (!audio_initialized) { |
| @@ -342,347 +346,347 @@ void stop_all_notes() | |||
| 342 | } | 346 | } |
| 343 | } | 347 | } |
| 344 | 348 | ||
| 345 | void stop_note(float freq) | 349 | void stop_note(float freq) { |
| 346 | { | 350 | dprintf("audio stop note freq=%d", (int)freq); |
| 347 | dprintf("audio stop note freq=%d", (int)freq); | ||
| 348 | 351 | ||
| 349 | if (playing_note) { | 352 | if (playing_note) { |
| 350 | if (!audio_initialized) { | 353 | if (!audio_initialized) { |
| 351 | audio_init(); | 354 | audio_init(); |
| 352 | } | 355 | } |
| 353 | for (int i = 7; i >= 0; i--) { | 356 | for (int i = 7; i >= 0; i--) { |
| 354 | if (frequencies[i] == freq) { | 357 | if (frequencies[i] == freq) { |
| 355 | frequencies[i] = 0; | 358 | frequencies[i] = 0; |
| 356 | volumes[i] = 0; | 359 | volumes[i] = 0; |
| 357 | for (int j = i; (j < 7); j++) { | 360 | for (int j = i; (j < 7); j++) { |
| 358 | frequencies[j] = frequencies[j+1]; | 361 | frequencies[j] = frequencies[j+1]; |
| 359 | frequencies[j+1] = 0; | 362 | frequencies[j+1] = 0; |
| 360 | volumes[j] = volumes[j+1]; | 363 | volumes[j] = volumes[j+1]; |
| 361 | volumes[j+1] = 0; | 364 | volumes[j+1] = 0; |
| 362 | } | ||
| 363 | break; | ||
| 364 | } | ||
| 365 | } | ||
| 366 | voices--; | ||
| 367 | if (voices < 0) | ||
| 368 | voices = 0; | ||
| 369 | if (voice_place >= voices) { | ||
| 370 | voice_place = 0; | ||
| 371 | } | ||
| 372 | if (voices == 0) { | ||
| 373 | STOP_CHANNEL_1(); | ||
| 374 | STOP_CHANNEL_2(); | ||
| 375 | gptStopTimer(&GPTD8); | ||
| 376 | frequency = 0; | ||
| 377 | frequency_alt = 0; | ||
| 378 | volume = 0; | ||
| 379 | playing_note = false; | ||
| 380 | } | 365 | } |
| 366 | break; | ||
| 367 | } | ||
| 368 | } | ||
| 369 | voices--; | ||
| 370 | if (voices < 0) { | ||
| 371 | voices = 0; | ||
| 372 | } | ||
| 373 | if (voice_place >= voices) { | ||
| 374 | voice_place = 0; | ||
| 375 | } | ||
| 376 | if (voices == 0) { | ||
| 377 | STOP_CHANNEL_1(); | ||
| 378 | STOP_CHANNEL_2(); | ||
| 379 | gptStopTimer(&GPTD8); | ||
| 380 | frequency = 0; | ||
| 381 | frequency_alt = 0; | ||
| 382 | volume = 0; | ||
| 383 | playing_note = false; | ||
| 381 | } | 384 | } |
| 385 | } | ||
| 382 | } | 386 | } |
| 383 | 387 | ||
| 384 | #ifdef VIBRATO_ENABLE | 388 | #ifdef VIBRATO_ENABLE |
| 385 | 389 | ||
| 386 | float mod(float a, int b) | 390 | float mod(float a, int b) { |
| 387 | { | 391 | float r = fmod(a, b); |
| 388 | float r = fmod(a, b); | 392 | return r < 0 ? r + b : r; |
| 389 | return r < 0 ? r + b : r; | ||
| 390 | } | 393 | } |
| 391 | 394 | ||
| 392 | float vibrato(float average_freq) { | 395 | float vibrato(float average_freq) { |
| 393 | #ifdef VIBRATO_STRENGTH_ENABLE | 396 | #ifdef VIBRATO_STRENGTH_ENABLE |
| 394 | float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength); | 397 | float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength); |
| 395 | #else | 398 | #else |
| 396 | float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter]; | 399 | float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter]; |
| 397 | #endif | 400 | #endif |
| 398 | vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH); | 401 | vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH); |
| 399 | return vibrated_freq; | 402 | return vibrated_freq; |
| 400 | } | 403 | } |
| 401 | 404 | ||
| 402 | #endif | 405 | #endif |
| 403 | 406 | ||
| 404 | static void gpt_cb8(GPTDriver *gptp) { | 407 | static void gpt_cb8(GPTDriver *gptp) { |
| 405 | float freq; | 408 | float freq; |
| 406 | 409 | ||
| 407 | if (playing_note) { | 410 | if (playing_note) { |
| 408 | if (voices > 0) { | 411 | if (voices > 0) { |
| 409 | 412 | ||
| 410 | float freq_alt = 0; | 413 | float freq_alt = 0; |
| 411 | if (voices > 1) { | 414 | if (voices > 1) { |
| 412 | if (polyphony_rate == 0) { | 415 | if (polyphony_rate == 0) { |
| 413 | if (glissando) { | 416 | if (glissando) { |
| 414 | if (frequency_alt != 0 && frequency_alt < frequencies[voices - 2] && frequency_alt < frequencies[voices - 2] * pow(2, -440/frequencies[voices - 2]/12/2)) { | 417 | if (frequency_alt != 0 && frequency_alt < frequencies[voices - 2] && frequency_alt < frequencies[voices - 2] * pow(2, -440/frequencies[voices - 2]/12/2)) { |
| 415 | frequency_alt = frequency_alt * pow(2, 440/frequency_alt/12/2); | 418 | frequency_alt = frequency_alt * pow(2, 440/frequency_alt/12/2); |
| 416 | } else if (frequency_alt != 0 && frequency_alt > frequencies[voices - 2] && frequency_alt > frequencies[voices - 2] * pow(2, 440/frequencies[voices - 2]/12/2)) { | 419 | } else if (frequency_alt != 0 && frequency_alt > frequencies[voices - 2] && frequency_alt > frequencies[voices - 2] * pow(2, 440/frequencies[voices - 2]/12/2)) { |
| 417 | frequency_alt = frequency_alt * pow(2, -440/frequency_alt/12/2); | 420 | frequency_alt = frequency_alt * pow(2, -440/frequency_alt/12/2); |
| 418 | } else { | ||
| 419 | frequency_alt = frequencies[voices - 2]; | ||
| 420 | } | ||
| 421 | } else { | ||
| 422 | frequency_alt = frequencies[voices - 2]; | ||
| 423 | } | ||
| 424 | |||
| 425 | #ifdef VIBRATO_ENABLE | ||
| 426 | if (vibrato_strength > 0) { | ||
| 427 | freq_alt = vibrato(frequency_alt); | ||
| 428 | } else { | ||
| 429 | freq_alt = frequency_alt; | ||
| 430 | } | ||
| 431 | #else | ||
| 432 | freq_alt = frequency_alt; | ||
| 433 | #endif | ||
| 434 | } | ||
| 435 | |||
| 436 | if (envelope_index < 65535) { | ||
| 437 | envelope_index++; | ||
| 438 | } | ||
| 439 | |||
| 440 | freq_alt = voice_envelope(freq_alt); | ||
| 441 | |||
| 442 | if (freq_alt < 30.517578125) { | ||
| 443 | freq_alt = 30.52; | ||
| 444 | } | ||
| 445 | |||
| 446 | if (GET_CHANNEL_2_FREQ != (uint16_t)freq_alt) { | ||
| 447 | UPDATE_CHANNEL_2_FREQ(freq_alt); | ||
| 448 | } else { | ||
| 449 | RESTART_CHANNEL_2(); | ||
| 450 | } | ||
| 451 | //note_timbre; | ||
| 452 | } | ||
| 453 | |||
| 454 | if (polyphony_rate > 0) { | ||
| 455 | if (voices > 1) { | ||
| 456 | voice_place %= voices; | ||
| 457 | if (place++ > (frequencies[voice_place] / polyphony_rate)) { | ||
| 458 | voice_place = (voice_place + 1) % voices; | ||
| 459 | place = 0.0; | ||
| 460 | } | ||
| 461 | } | ||
| 462 | |||
| 463 | #ifdef VIBRATO_ENABLE | ||
| 464 | if (vibrato_strength > 0) { | ||
| 465 | freq = vibrato(frequencies[voice_place]); | ||
| 466 | } else { | ||
| 467 | freq = frequencies[voice_place]; | ||
| 468 | } | ||
| 469 | #else | ||
| 470 | freq = frequencies[voice_place]; | ||
| 471 | #endif | ||
| 472 | } else { | 421 | } else { |
| 473 | if (glissando) { | 422 | frequency_alt = frequencies[voices - 2]; |
| 474 | if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) { | ||
| 475 | frequency = frequency * pow(2, 440/frequency/12/2); | ||
| 476 | } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) { | ||
| 477 | frequency = frequency * pow(2, -440/frequency/12/2); | ||
| 478 | } else { | ||
| 479 | frequency = frequencies[voices - 1]; | ||
| 480 | } | ||
| 481 | } else { | ||
| 482 | frequency = frequencies[voices - 1]; | ||
| 483 | } | ||
| 484 | |||
| 485 | #ifdef VIBRATO_ENABLE | ||
| 486 | if (vibrato_strength > 0) { | ||
| 487 | freq = vibrato(frequency); | ||
| 488 | } else { | ||
| 489 | freq = frequency; | ||
| 490 | } | ||
| 491 | #else | ||
| 492 | freq = frequency; | ||
| 493 | #endif | ||
| 494 | } | 423 | } |
| 424 | } else { | ||
| 425 | frequency_alt = frequencies[voices - 2]; | ||
| 426 | } | ||
| 495 | 427 | ||
| 496 | if (envelope_index < 65535) { | 428 | #ifdef VIBRATO_ENABLE |
| 497 | envelope_index++; | 429 | if (vibrato_strength > 0) { |
| 498 | } | 430 | freq_alt = vibrato(frequency_alt); |
| 499 | |||
| 500 | freq = voice_envelope(freq); | ||
| 501 | |||
| 502 | if (freq < 30.517578125) { | ||
| 503 | freq = 30.52; | ||
| 504 | } | ||
| 505 | |||
| 506 | |||
| 507 | if (GET_CHANNEL_1_FREQ != (uint16_t)freq) { | ||
| 508 | UPDATE_CHANNEL_1_FREQ(freq); | ||
| 509 | } else { | 431 | } else { |
| 510 | RESTART_CHANNEL_1(); | 432 | freq_alt = frequency_alt; |
| 511 | } | 433 | } |
| 512 | //note_timbre; | 434 | #else |
| 435 | freq_alt = frequency_alt; | ||
| 436 | #endif | ||
| 513 | } | 437 | } |
| 514 | } | ||
| 515 | 438 | ||
| 516 | if (playing_notes) { | 439 | if (envelope_index < 65535) { |
| 517 | if (note_frequency > 0) { | 440 | envelope_index++; |
| 518 | #ifdef VIBRATO_ENABLE | 441 | } |
| 519 | if (vibrato_strength > 0) { | ||
| 520 | freq = vibrato(note_frequency); | ||
| 521 | } else { | ||
| 522 | freq = note_frequency; | ||
| 523 | } | ||
| 524 | #else | ||
| 525 | freq = note_frequency; | ||
| 526 | #endif | ||
| 527 | |||
| 528 | if (envelope_index < 65535) { | ||
| 529 | envelope_index++; | ||
| 530 | } | ||
| 531 | freq = voice_envelope(freq); | ||
| 532 | 442 | ||
| 443 | freq_alt = voice_envelope(freq_alt); | ||
| 533 | 444 | ||
| 534 | if (GET_CHANNEL_1_FREQ != (uint16_t)freq) { | 445 | if (freq_alt < 30.517578125) { |
| 535 | UPDATE_CHANNEL_1_FREQ(freq); | 446 | freq_alt = 30.52; |
| 536 | UPDATE_CHANNEL_2_FREQ(freq); | 447 | } |
| 537 | } | 448 | |
| 538 | //note_timbre; | 449 | if (GET_CHANNEL_2_FREQ != (uint16_t)freq_alt) { |
| 450 | UPDATE_CHANNEL_2_FREQ(freq_alt); | ||
| 539 | } else { | 451 | } else { |
| 540 | // gptStopTimer(&GPTD6); | 452 | RESTART_CHANNEL_2(); |
| 541 | // gptStopTimer(&GPTD7); | 453 | } |
| 454 | //note_timbre; | ||
| 455 | } | ||
| 456 | |||
| 457 | if (polyphony_rate > 0) { | ||
| 458 | if (voices > 1) { | ||
| 459 | voice_place %= voices; | ||
| 460 | if (place++ > (frequencies[voice_place] / polyphony_rate)) { | ||
| 461 | voice_place = (voice_place + 1) % voices; | ||
| 462 | place = 0.0; | ||
| 463 | } | ||
| 542 | } | 464 | } |
| 543 | 465 | ||
| 544 | note_position++; | 466 | #ifdef VIBRATO_ENABLE |
| 545 | bool end_of_note = false; | 467 | if (vibrato_strength > 0) { |
| 546 | if (GET_CHANNEL_1_FREQ > 0) { | 468 | freq = vibrato(frequencies[voice_place]); |
| 547 | if (!note_resting) | 469 | } else { |
| 548 | end_of_note = (note_position >= (note_length*8 - 1)); | 470 | freq = frequencies[voice_place]; |
| 549 | else | 471 | } |
| 550 | end_of_note = (note_position >= (note_length*8)); | 472 | #else |
| 473 | freq = frequencies[voice_place]; | ||
| 474 | #endif | ||
| 475 | } else { | ||
| 476 | if (glissando) { | ||
| 477 | if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) { | ||
| 478 | frequency = frequency * pow(2, 440/frequency/12/2); | ||
| 479 | } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) { | ||
| 480 | frequency = frequency * pow(2, -440/frequency/12/2); | ||
| 481 | } else { | ||
| 482 | frequency = frequencies[voices - 1]; | ||
| 483 | } | ||
| 551 | } else { | 484 | } else { |
| 552 | end_of_note = (note_position >= (note_length*8)); | 485 | frequency = frequencies[voices - 1]; |
| 553 | } | 486 | } |
| 554 | 487 | ||
| 555 | if (end_of_note) { | 488 | #ifdef VIBRATO_ENABLE |
| 556 | current_note++; | 489 | if (vibrato_strength > 0) { |
| 557 | if (current_note >= notes_count) { | 490 | freq = vibrato(frequency); |
| 558 | if (notes_repeat) { | 491 | } else { |
| 559 | current_note = 0; | 492 | freq = frequency; |
| 560 | } else { | 493 | } |
| 561 | STOP_CHANNEL_1(); | 494 | #else |
| 562 | STOP_CHANNEL_2(); | 495 | freq = frequency; |
| 563 | // gptStopTimer(&GPTD8); | 496 | #endif |
| 564 | playing_notes = false; | 497 | } |
| 565 | return; | ||
| 566 | } | ||
| 567 | } | ||
| 568 | if (!note_resting) { | ||
| 569 | note_resting = true; | ||
| 570 | current_note--; | ||
| 571 | if ((*notes_pointer)[current_note][0] == (*notes_pointer)[current_note + 1][0]) { | ||
| 572 | note_frequency = 0; | ||
| 573 | note_length = 1; | ||
| 574 | } else { | ||
| 575 | note_frequency = (*notes_pointer)[current_note][0]; | ||
| 576 | note_length = 1; | ||
| 577 | } | ||
| 578 | } else { | ||
| 579 | note_resting = false; | ||
| 580 | envelope_index = 0; | ||
| 581 | note_frequency = (*notes_pointer)[current_note][0]; | ||
| 582 | note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100); | ||
| 583 | } | ||
| 584 | 498 | ||
| 585 | note_position = 0; | 499 | if (envelope_index < 65535) { |
| 586 | } | 500 | envelope_index++; |
| 587 | } | 501 | } |
| 588 | 502 | ||
| 589 | if (!audio_config.enable) { | 503 | freq = voice_envelope(freq); |
| 590 | playing_notes = false; | ||
| 591 | playing_note = false; | ||
| 592 | } | ||
| 593 | } | ||
| 594 | 504 | ||
| 595 | void play_note(float freq, int vol) { | 505 | if (freq < 30.517578125) { |
| 506 | freq = 30.52; | ||
| 507 | } | ||
| 596 | 508 | ||
| 597 | dprintf("audio play note freq=%d vol=%d", (int)freq, vol); | ||
| 598 | 509 | ||
| 599 | if (!audio_initialized) { | 510 | if (GET_CHANNEL_1_FREQ != (uint16_t)freq) { |
| 600 | audio_init(); | 511 | UPDATE_CHANNEL_1_FREQ(freq); |
| 512 | } else { | ||
| 513 | RESTART_CHANNEL_1(); | ||
| 514 | } | ||
| 515 | //note_timbre; | ||
| 601 | } | 516 | } |
| 517 | } | ||
| 602 | 518 | ||
| 603 | if (audio_config.enable && voices < 8) { | 519 | if (playing_notes) { |
| 520 | if (note_frequency > 0) { | ||
| 521 | #ifdef VIBRATO_ENABLE | ||
| 522 | if (vibrato_strength > 0) { | ||
| 523 | freq = vibrato(note_frequency); | ||
| 524 | } else { | ||
| 525 | freq = note_frequency; | ||
| 526 | } | ||
| 527 | #else | ||
| 528 | freq = note_frequency; | ||
| 529 | #endif | ||
| 604 | 530 | ||
| 531 | if (envelope_index < 65535) { | ||
| 532 | envelope_index++; | ||
| 533 | } | ||
| 534 | freq = voice_envelope(freq); | ||
| 605 | 535 | ||
| 606 | // Cancel notes if notes are playing | ||
| 607 | if (playing_notes) | ||
| 608 | stop_all_notes(); | ||
| 609 | 536 | ||
| 610 | playing_note = true; | 537 | if (GET_CHANNEL_1_FREQ != (uint16_t)freq) { |
| 538 | UPDATE_CHANNEL_1_FREQ(freq); | ||
| 539 | UPDATE_CHANNEL_2_FREQ(freq); | ||
| 540 | } | ||
| 541 | //note_timbre; | ||
| 542 | } else { | ||
| 543 | // gptStopTimer(&GPTD6); | ||
| 544 | // gptStopTimer(&GPTD7); | ||
| 545 | } | ||
| 611 | 546 | ||
| 612 | envelope_index = 0; | 547 | note_position++; |
| 548 | bool end_of_note = false; | ||
| 549 | if (GET_CHANNEL_1_FREQ > 0) { | ||
| 550 | if (!note_resting) | ||
| 551 | end_of_note = (note_position >= (note_length*8 - 1)); | ||
| 552 | else | ||
| 553 | end_of_note = (note_position >= (note_length*8)); | ||
| 554 | } else { | ||
| 555 | end_of_note = (note_position >= (note_length*8)); | ||
| 556 | } | ||
| 613 | 557 | ||
| 614 | if (freq > 0) { | 558 | if (end_of_note) { |
| 615 | frequencies[voices] = freq; | 559 | current_note++; |
| 616 | volumes[voices] = vol; | 560 | if (current_note >= notes_count) { |
| 617 | voices++; | 561 | if (notes_repeat) { |
| 562 | current_note = 0; | ||
| 563 | } else { | ||
| 564 | STOP_CHANNEL_1(); | ||
| 565 | STOP_CHANNEL_2(); | ||
| 566 | // gptStopTimer(&GPTD8); | ||
| 567 | playing_notes = false; | ||
| 568 | return; | ||
| 569 | } | ||
| 570 | } | ||
| 571 | if (!note_resting) { | ||
| 572 | note_resting = true; | ||
| 573 | current_note--; | ||
| 574 | if ((*notes_pointer)[current_note][0] == (*notes_pointer)[current_note + 1][0]) { | ||
| 575 | note_frequency = 0; | ||
| 576 | note_length = 1; | ||
| 577 | } else { | ||
| 578 | note_frequency = (*notes_pointer)[current_note][0]; | ||
| 579 | note_length = 1; | ||
| 618 | } | 580 | } |
| 581 | } else { | ||
| 582 | note_resting = false; | ||
| 583 | envelope_index = 0; | ||
| 584 | note_frequency = (*notes_pointer)[current_note][0]; | ||
| 585 | note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100); | ||
| 586 | } | ||
| 619 | 587 | ||
| 620 | gptStart(&GPTD8, &gpt8cfg1); | 588 | note_position = 0; |
| 621 | gptStartContinuous(&GPTD8, 2U); | ||
| 622 | RESTART_CHANNEL_1(); | ||
| 623 | RESTART_CHANNEL_2(); | ||
| 624 | } | 589 | } |
| 590 | } | ||
| 625 | 591 | ||
| 592 | if (!audio_config.enable) { | ||
| 593 | playing_notes = false; | ||
| 594 | playing_note = false; | ||
| 595 | } | ||
| 626 | } | 596 | } |
| 627 | 597 | ||
| 628 | void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat) | 598 | void play_note(float freq, int vol) { |
| 629 | { | ||
| 630 | 599 | ||
| 631 | if (!audio_initialized) { | 600 | dprintf("audio play note freq=%d vol=%d", (int)freq, vol); |
| 632 | audio_init(); | 601 | |
| 602 | if (!audio_initialized) { | ||
| 603 | audio_init(); | ||
| 604 | } | ||
| 605 | |||
| 606 | if (audio_config.enable && voices < 8) { | ||
| 607 | |||
| 608 | // Cancel notes if notes are playing | ||
| 609 | if (playing_notes) { | ||
| 610 | stop_all_notes(); | ||
| 633 | } | 611 | } |
| 634 | 612 | ||
| 635 | if (audio_config.enable) { | 613 | playing_note = true; |
| 636 | 614 | ||
| 637 | // Cancel note if a note is playing | 615 | envelope_index = 0; |
| 638 | if (playing_note) | ||
| 639 | stop_all_notes(); | ||
| 640 | 616 | ||
| 641 | playing_notes = true; | 617 | if (freq > 0) { |
| 618 | frequencies[voices] = freq; | ||
| 619 | volumes[voices] = vol; | ||
| 620 | voices++; | ||
| 621 | } | ||
| 642 | 622 | ||
| 643 | notes_pointer = np; | 623 | gptStart(&GPTD8, &gpt8cfg1); |
| 644 | notes_count = n_count; | 624 | gptStartContinuous(&GPTD8, 2U); |
| 645 | notes_repeat = n_repeat; | 625 | RESTART_CHANNEL_1(); |
| 626 | RESTART_CHANNEL_2(); | ||
| 627 | } | ||
| 646 | 628 | ||
| 647 | place = 0; | 629 | } |
| 648 | current_note = 0; | ||
| 649 | 630 | ||
| 650 | note_frequency = (*notes_pointer)[current_note][0]; | 631 | void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat) { |
| 651 | note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100); | ||
| 652 | note_position = 0; | ||
| 653 | 632 | ||
| 654 | gptStart(&GPTD8, &gpt8cfg1); | 633 | if (!audio_initialized) { |
| 655 | gptStartContinuous(&GPTD8, 2U); | 634 | audio_init(); |
| 656 | RESTART_CHANNEL_1(); | 635 | } |
| 657 | RESTART_CHANNEL_2(); | 636 | |
| 637 | if (audio_config.enable) { | ||
| 638 | |||
| 639 | // Cancel note if a note is playing | ||
| 640 | if (playing_note) { | ||
| 641 | stop_all_notes(); | ||
| 658 | } | 642 | } |
| 659 | 643 | ||
| 644 | playing_notes = true; | ||
| 645 | |||
| 646 | notes_pointer = np; | ||
| 647 | notes_count = n_count; | ||
| 648 | notes_repeat = n_repeat; | ||
| 649 | |||
| 650 | place = 0; | ||
| 651 | current_note = 0; | ||
| 652 | |||
| 653 | note_frequency = (*notes_pointer)[current_note][0]; | ||
| 654 | note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100); | ||
| 655 | note_position = 0; | ||
| 656 | |||
| 657 | gptStart(&GPTD8, &gpt8cfg1); | ||
| 658 | gptStartContinuous(&GPTD8, 2U); | ||
| 659 | RESTART_CHANNEL_1(); | ||
| 660 | RESTART_CHANNEL_2(); | ||
| 661 | } | ||
| 660 | } | 662 | } |
| 661 | 663 | ||
| 662 | bool is_playing_notes(void) { | 664 | bool is_playing_notes(void) { |
| 663 | return playing_notes; | 665 | return playing_notes; |
| 664 | } | 666 | } |
| 665 | 667 | ||
| 666 | bool is_audio_on(void) { | 668 | bool is_audio_on(void) { |
| 667 | return (audio_config.enable != 0); | 669 | return (audio_config.enable != 0); |
| 668 | } | 670 | } |
| 669 | 671 | ||
| 670 | void audio_toggle(void) { | 672 | void audio_toggle(void) { |
| 671 | audio_config.enable ^= 1; | 673 | audio_config.enable ^= 1; |
| 672 | eeconfig_update_audio(audio_config.raw); | 674 | eeconfig_update_audio(audio_config.raw); |
| 673 | if (audio_config.enable) | 675 | if (audio_config.enable) { |
| 674 | audio_on_user(); | 676 | audio_on_user(); |
| 677 | } | ||
| 675 | } | 678 | } |
| 676 | 679 | ||
| 677 | void audio_on(void) { | 680 | void audio_on(void) { |
| 678 | audio_config.enable = 1; | 681 | audio_config.enable = 1; |
| 679 | eeconfig_update_audio(audio_config.raw); | 682 | eeconfig_update_audio(audio_config.raw); |
| 680 | audio_on_user(); | 683 | audio_on_user(); |
| 681 | } | 684 | } |
| 682 | 685 | ||
| 683 | void audio_off(void) { | 686 | void audio_off(void) { |
| 684 | audio_config.enable = 0; | 687 | stop_all_notes(); |
| 685 | eeconfig_update_audio(audio_config.raw); | 688 | audio_config.enable = 0; |
| 689 | eeconfig_update_audio(audio_config.raw); | ||
| 686 | } | 690 | } |
| 687 | 691 | ||
| 688 | #ifdef VIBRATO_ENABLE | 692 | #ifdef VIBRATO_ENABLE |
| @@ -690,29 +694,29 @@ void audio_off(void) { | |||
| 690 | // Vibrato rate functions | 694 | // Vibrato rate functions |
| 691 | 695 | ||
| 692 | void set_vibrato_rate(float rate) { | 696 | void set_vibrato_rate(float rate) { |
| 693 | vibrato_rate = rate; | 697 | vibrato_rate = rate; |
| 694 | } | 698 | } |
| 695 | 699 | ||
| 696 | void increase_vibrato_rate(float change) { | 700 | void increase_vibrato_rate(float change) { |
| 697 | vibrato_rate *= change; | 701 | vibrato_rate *= change; |
| 698 | } | 702 | } |
| 699 | 703 | ||
| 700 | void decrease_vibrato_rate(float change) { | 704 | void decrease_vibrato_rate(float change) { |
| 701 | vibrato_rate /= change; | 705 | vibrato_rate /= change; |
| 702 | } | 706 | } |
| 703 | 707 | ||
| 704 | #ifdef VIBRATO_STRENGTH_ENABLE | 708 | #ifdef VIBRATO_STRENGTH_ENABLE |
| 705 | 709 | ||
| 706 | void set_vibrato_strength(float strength) { | 710 | void set_vibrato_strength(float strength) { |
| 707 | vibrato_strength = strength; | 711 | vibrato_strength = strength; |
| 708 | } | 712 | } |
| 709 | 713 | ||
| 710 | void increase_vibrato_strength(float change) { | 714 | void increase_vibrato_strength(float change) { |
| 711 | vibrato_strength *= change; | 715 | vibrato_strength *= change; |
| 712 | } | 716 | } |
| 713 | 717 | ||
| 714 | void decrease_vibrato_strength(float change) { | 718 | void decrease_vibrato_strength(float change) { |
| 715 | vibrato_strength /= change; | 719 | vibrato_strength /= change; |
| 716 | } | 720 | } |
| 717 | 721 | ||
| 718 | #endif /* VIBRATO_STRENGTH_ENABLE */ | 722 | #endif /* VIBRATO_STRENGTH_ENABLE */ |
| @@ -722,45 +726,45 @@ void decrease_vibrato_strength(float change) { | |||
| 722 | // Polyphony functions | 726 | // Polyphony functions |
| 723 | 727 | ||
| 724 | void set_polyphony_rate(float rate) { | 728 | void set_polyphony_rate(float rate) { |
| 725 | polyphony_rate = rate; | 729 | polyphony_rate = rate; |
| 726 | } | 730 | } |
| 727 | 731 | ||
| 728 | void enable_polyphony() { | 732 | void enable_polyphony() { |
| 729 | polyphony_rate = 5; | 733 | polyphony_rate = 5; |
| 730 | } | 734 | } |
| 731 | 735 | ||
| 732 | void disable_polyphony() { | 736 | void disable_polyphony() { |
| 733 | polyphony_rate = 0; | 737 | polyphony_rate = 0; |
| 734 | } | 738 | } |
| 735 | 739 | ||
| 736 | void increase_polyphony_rate(float change) { | 740 | void increase_polyphony_rate(float change) { |
| 737 | polyphony_rate *= change; | 741 | polyphony_rate *= change; |
| 738 | } | 742 | } |
| 739 | 743 | ||
| 740 | void decrease_polyphony_rate(float change) { | 744 | void decrease_polyphony_rate(float change) { |
| 741 | polyphony_rate /= change; | 745 | polyphony_rate /= change; |
| 742 | } | 746 | } |
| 743 | 747 | ||
| 744 | // Timbre function | 748 | // Timbre function |
| 745 | 749 | ||
| 746 | void set_timbre(float timbre) { | 750 | void set_timbre(float timbre) { |
| 747 | note_timbre = timbre; | 751 | note_timbre = timbre; |
| 748 | } | 752 | } |
| 749 | 753 | ||
| 750 | // Tempo functions | 754 | // Tempo functions |
| 751 | 755 | ||
| 752 | void set_tempo(uint8_t tempo) { | 756 | void set_tempo(uint8_t tempo) { |
| 753 | note_tempo = tempo; | 757 | note_tempo = tempo; |
| 754 | } | 758 | } |
| 755 | 759 | ||
| 756 | void decrease_tempo(uint8_t tempo_change) { | 760 | void decrease_tempo(uint8_t tempo_change) { |
| 757 | note_tempo += tempo_change; | 761 | note_tempo += tempo_change; |
| 758 | } | 762 | } |
| 759 | 763 | ||
| 760 | void increase_tempo(uint8_t tempo_change) { | 764 | void increase_tempo(uint8_t tempo_change) { |
| 761 | if (note_tempo - tempo_change < 10) { | 765 | if (note_tempo - tempo_change < 10) { |
| 762 | note_tempo = 10; | 766 | note_tempo = 10; |
| 763 | } else { | 767 | } else { |
| 764 | note_tempo -= tempo_change; | 768 | note_tempo -= tempo_change; |
| 765 | } | 769 | } |
| 766 | } | 770 | } |
diff --git a/quantum/process_keycode/process_clicky.c b/quantum/process_keycode/process_clicky.c index 8238c263f..34c3f620b 100644 --- a/quantum/process_keycode/process_clicky.c +++ b/quantum/process_keycode/process_clicky.c | |||
| @@ -32,7 +32,7 @@ extern bool midi_activated; | |||
| 32 | 32 | ||
| 33 | void clicky_play(void) { | 33 | void clicky_play(void) { |
| 34 | #ifndef NO_MUSIC_MODE | 34 | #ifndef NO_MUSIC_MODE |
| 35 | if (music_activated || midi_activated) return; | 35 | if (music_activated || midi_activated || audio_config.enable) return; |
| 36 | #endif // !NO_MUSIC_MODE | 36 | #endif // !NO_MUSIC_MODE |
| 37 | clicky_song[0][0] = 2.0f * clicky_freq * (1.0f + clicky_rand * ( ((float)rand()) / ((float)(RAND_MAX)) ) ); | 37 | clicky_song[0][0] = 2.0f * clicky_freq * (1.0f + clicky_rand * ( ((float)rand()) / ((float)(RAND_MAX)) ) ); |
| 38 | clicky_song[1][0] = clicky_freq * (1.0f + clicky_rand * ( ((float)rand()) / ((float)(RAND_MAX)) ) ); | 38 | clicky_song[1][0] = clicky_freq * (1.0f + clicky_rand * ( ((float)rand()) / ((float)(RAND_MAX)) ) ); |
| @@ -73,27 +73,29 @@ void clicky_off(void) { | |||
| 73 | } | 73 | } |
| 74 | 74 | ||
| 75 | bool is_clicky_on(void) { | 75 | bool is_clicky_on(void) { |
| 76 | return (audio_config.clicky_enable != 0); | 76 | return (audio_config.clicky_enable != 0); |
| 77 | } | 77 | } |
| 78 | 78 | ||
| 79 | bool process_clicky(uint16_t keycode, keyrecord_t *record) { | 79 | bool process_clicky(uint16_t keycode, keyrecord_t *record) { |
| 80 | if (keycode == CLICKY_TOGGLE && record->event.pressed) { clicky_toggle(); } | 80 | if (keycode == CLICKY_TOGGLE && record->event.pressed) { clicky_toggle(); } |
| 81 | 81 | ||
| 82 | if (keycode == CLICKY_ENABLE && record->event.pressed) { clicky_on(); } | 82 | if (keycode == CLICKY_ENABLE && record->event.pressed) { clicky_on(); } |
| 83 | if (keycode == CLICKY_DISABLE && record->event.pressed) { clicky_off(); } | 83 | if (keycode == CLICKY_DISABLE && record->event.pressed) { clicky_off(); } |
| 84 | 84 | ||
| 85 | if (keycode == CLICKY_RESET && record->event.pressed) { clicky_freq_reset(); } | 85 | if (keycode == CLICKY_RESET && record->event.pressed) { clicky_freq_reset(); } |
| 86 | 86 | ||
| 87 | if (keycode == CLICKY_UP && record->event.pressed) { clicky_freq_up(); } | 87 | if (keycode == CLICKY_UP && record->event.pressed) { clicky_freq_up(); } |
| 88 | if (keycode == CLICKY_DOWN && record->event.pressed) { clicky_freq_down(); } | 88 | if (keycode == CLICKY_DOWN && record->event.pressed) { clicky_freq_down(); } |
| 89 | 89 | ||
| 90 | 90 | ||
| 91 | if ( audio_config.clicky_enable ) { | 91 | if (audio_config.enable && audio_config.clicky_enable) { |
| 92 | if (record->event.pressed) { | 92 | if (record->event.pressed) { // Leave this separate so it's easier to add upstroke sound |
| 93 | clicky_play();; | 93 | if (keycode != AU_OFF && keycode != AU_TOG) { // DO NOT PLAY if audio will be disabled, and causes issuse on ARM |
| 94 | clicky_play(); | ||
| 94 | } | 95 | } |
| 95 | } | 96 | } |
| 96 | return true; | 97 | } |
| 98 | return true; | ||
| 97 | } | 99 | } |
| 98 | 100 | ||
| 99 | #endif //AUDIO_CLICKY | 101 | #endif //AUDIO_CLICKY |
