diff options
| author | Pascal Getreuer <50221757+getreuer@users.noreply.github.com> | 2023-07-07 08:47:16 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-08 00:47:16 +1000 |
| commit | a8a87a0922a33d38ed80165ee62f9dc35bd08a3d (patch) | |
| tree | b590d46212c06e92f1683e08d9ebf12784eabd32 | |
| parent | e648b84da3db022ba308163f21c3cf8165c45e09 (diff) | |
[Core] Simplify audio_duration_to_ms() and audio_ms_to_duration(), reduce firmware size by a few bytes. (#21427)
| -rw-r--r-- | platforms/test/drivers/audio_pwm.h | 16 | ||||
| -rw-r--r-- | platforms/test/drivers/audio_pwm_hardware.c | 20 | ||||
| -rw-r--r-- | quantum/audio/audio.c | 45 | ||||
| -rw-r--r-- | tests/audio/config.h | 18 | ||||
| -rw-r--r-- | tests/audio/test.mk | 16 | ||||
| -rw-r--r-- | tests/audio/test_audio.cpp | 118 |
6 files changed, 220 insertions, 13 deletions
diff --git a/platforms/test/drivers/audio_pwm.h b/platforms/test/drivers/audio_pwm.h new file mode 100644 index 0000000000..9a3fa88cec --- /dev/null +++ b/platforms/test/drivers/audio_pwm.h | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | // Copyright 2023 Google LLC | ||
| 2 | // | ||
| 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. | ||
| 7 | // | ||
| 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. | ||
| 12 | // | ||
| 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/>. | ||
| 15 | |||
| 16 | #pragma once | ||
diff --git a/platforms/test/drivers/audio_pwm_hardware.c b/platforms/test/drivers/audio_pwm_hardware.c new file mode 100644 index 0000000000..336e4f5844 --- /dev/null +++ b/platforms/test/drivers/audio_pwm_hardware.c | |||
| @@ -0,0 +1,20 @@ | |||
| 1 | // Copyright 2023 Google LLC | ||
| 2 | // | ||
| 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. | ||
| 7 | // | ||
| 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. | ||
| 12 | // | ||
| 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/>. | ||
| 15 | |||
| 16 | #include "audio.h" | ||
| 17 | |||
| 18 | void audio_driver_initialize(void) {} | ||
| 19 | void audio_driver_start() {} | ||
| 20 | void audio_driver_stop() {} | ||
diff --git a/quantum/audio/audio.c b/quantum/audio/audio.c index 2570ad9cd1..0300483a93 100644 --- a/quantum/audio/audio.c +++ b/quantum/audio/audio.c | |||
| @@ -547,20 +547,39 @@ void audio_decrease_tempo(uint8_t tempo_change) { | |||
| 547 | note_tempo -= tempo_change; | 547 | note_tempo -= tempo_change; |
| 548 | } | 548 | } |
| 549 | 549 | ||
| 550 | // TODO in the int-math version are some bugs; songs sometimes abruptly end - maybe an issue with the timer/system-tick wrapping around? | 550 | /** |
| 551 | * Converts from units of 1/64ths of a beat to milliseconds. | ||
| 552 | * | ||
| 553 | * Round-off error is at most 1 millisecond. | ||
| 554 | * | ||
| 555 | * Conversion will never overflow for duration_bpm <= 699, provided that | ||
| 556 | * note_tempo is at least 10. This is quite a long duration, over ten beats. | ||
| 557 | * | ||
| 558 | * Beware that for duration_bpm > 699, the result may overflow uint16_t range | ||
| 559 | * when duration_bpm is large compared to note_tempo: | ||
| 560 | * | ||
| 561 | * duration_bpm * 60 * 1000 / (64 * note_tempo) > UINT16_MAX | ||
| 562 | * | ||
| 563 | * duration_bpm > (2 * 65535 / 1875) * note_tempo | ||
| 564 | * = 69.904 * note_tempo. | ||
| 565 | */ | ||
| 551 | uint16_t audio_duration_to_ms(uint16_t duration_bpm) { | 566 | uint16_t audio_duration_to_ms(uint16_t duration_bpm) { |
| 552 | #if defined(__AVR__) | 567 | return ((uint32_t)duration_bpm * 1875) / ((uint_fast16_t)note_tempo * 2); |
| 553 | // doing int-math saves us some bytes in the overall firmware size, but the intermediate result is less accurate before being cast to/returned as uint | ||
| 554 | return ((uint32_t)duration_bpm * 60 * 1000) / (64 * note_tempo); | ||
| 555 | // NOTE: beware of uint16_t overflows when note_tempo is low and/or the duration is long | ||
| 556 | #else | ||
| 557 | return ((float)duration_bpm * 60) / (64 * note_tempo) * 1000; | ||
| 558 | #endif | ||
| 559 | } | 568 | } |
| 569 | |||
| 570 | /** | ||
| 571 | * Converts from units of milliseconds to 1/64ths of a beat. | ||
| 572 | * | ||
| 573 | * Round-off error is at most 1/64th of a beat. | ||
| 574 | * | ||
| 575 | * This conversion never overflows: since duration_ms <= UINT16_MAX = 65535 | ||
| 576 | * and note_tempo <= 255, the result is always in uint16_t range: | ||
| 577 | * | ||
| 578 | * duration_ms * 64 * note_tempo / 60 / 1000 | ||
| 579 | * <= 65535 * 2 * 255 / 1875 | ||
| 580 | * = 17825.52 | ||
| 581 | * <= UINT16_MAX. | ||
| 582 | */ | ||
| 560 | uint16_t audio_ms_to_duration(uint16_t duration_ms) { | 583 | uint16_t audio_ms_to_duration(uint16_t duration_ms) { |
| 561 | #if defined(__AVR__) | 584 | return ((uint32_t)duration_ms * 2 * note_tempo) / 1875; |
| 562 | return ((uint32_t)duration_ms * 64 * note_tempo) / 60 / 1000; | ||
| 563 | #else | ||
| 564 | return ((float)duration_ms * 64 * note_tempo) / 60 / 1000; | ||
| 565 | #endif | ||
| 566 | } | 585 | } |
diff --git a/tests/audio/config.h b/tests/audio/config.h new file mode 100644 index 0000000000..d0c4ddadbd --- /dev/null +++ b/tests/audio/config.h | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | // Copyright 2023 Google LLC | ||
| 2 | // | ||
| 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. | ||
| 7 | // | ||
| 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. | ||
| 12 | // | ||
| 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/>. | ||
| 15 | |||
| 16 | #pragma once | ||
| 17 | |||
| 18 | #include "test_common.h" | ||
diff --git a/tests/audio/test.mk b/tests/audio/test.mk new file mode 100644 index 0000000000..a2c71d9587 --- /dev/null +++ b/tests/audio/test.mk | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | # Copyright 2023 Google LLC | ||
| 2 | # | ||
| 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. | ||
| 7 | # | ||
| 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. | ||
| 12 | # | ||
| 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/>. | ||
| 15 | |||
| 16 | AUDIO_ENABLE = yes | ||
diff --git a/tests/audio/test_audio.cpp b/tests/audio/test_audio.cpp new file mode 100644 index 0000000000..ef34748b06 --- /dev/null +++ b/tests/audio/test_audio.cpp | |||
| @@ -0,0 +1,118 @@ | |||
| 1 | // Copyright 2023 Google LLC | ||
| 2 | // | ||
| 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. | ||
| 7 | // | ||
| 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. | ||
| 12 | // | ||
| 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/>. | ||
| 15 | |||
| 16 | #include <cmath> | ||
| 17 | #include <random> | ||
| 18 | |||
| 19 | #include "gtest/gtest.h" | ||
| 20 | #include "keyboard_report_util.hpp" | ||
| 21 | #include "test_common.hpp" | ||
| 22 | |||
| 23 | namespace { | ||
| 24 | |||
| 25 | class AudioTest : public TestFixture { | ||
| 26 | public: | ||
| 27 | uint16_t infer_tempo() { | ||
| 28 | return audio_ms_to_duration(1875) / 2; | ||
| 29 | } | ||
| 30 | }; | ||
| 31 | |||
| 32 | TEST_F(AudioTest, OnOffToggle) { | ||
| 33 | audio_on(); | ||
| 34 | EXPECT_TRUE(audio_is_on()); | ||
| 35 | |||
| 36 | audio_off(); | ||
| 37 | EXPECT_FALSE(audio_is_on()); | ||
| 38 | |||
| 39 | audio_toggle(); | ||
| 40 | EXPECT_TRUE(audio_is_on()); | ||
| 41 | |||
| 42 | audio_toggle(); | ||
| 43 | EXPECT_FALSE(audio_is_on()); | ||
| 44 | } | ||
| 45 | |||
| 46 | TEST_F(AudioTest, ChangeTempo) { | ||
| 47 | for (int tempo = 50; tempo <= 250; tempo += 50) { | ||
| 48 | audio_set_tempo(tempo); | ||
| 49 | EXPECT_EQ(infer_tempo(), tempo); | ||
| 50 | } | ||
| 51 | |||
| 52 | audio_set_tempo(10); | ||
| 53 | audio_increase_tempo(25); | ||
| 54 | EXPECT_EQ(infer_tempo(), 35); | ||
| 55 | |||
| 56 | audio_decrease_tempo(4); | ||
| 57 | EXPECT_EQ(infer_tempo(), 31); | ||
| 58 | |||
| 59 | audio_increase_tempo(250); | ||
| 60 | EXPECT_EQ(infer_tempo(), 255); | ||
| 61 | |||
| 62 | audio_set_tempo(9); | ||
| 63 | EXPECT_EQ(infer_tempo(), 10); | ||
| 64 | |||
| 65 | audio_decrease_tempo(100); | ||
| 66 | EXPECT_EQ(infer_tempo(), 10); | ||
| 67 | } | ||
| 68 | |||
| 69 | TEST_F(AudioTest, BpmConversion) { | ||
| 70 | const int tol = 1; | ||
| 71 | |||
| 72 | audio_set_tempo(120); | ||
| 73 | // At 120 bpm, there are 2 beats per second, and a whole note is 500 ms. | ||
| 74 | EXPECT_NEAR(audio_duration_to_ms(64 /* whole note */), 500, tol); | ||
| 75 | EXPECT_NEAR(audio_ms_to_duration(500), 64, tol); | ||
| 76 | EXPECT_EQ(audio_duration_to_ms(0), 0); | ||
| 77 | EXPECT_EQ(audio_ms_to_duration(0), 0); | ||
| 78 | |||
| 79 | audio_set_tempo(10); | ||
| 80 | // At 10 bpm, UINT16_MAX ms corresponds to 699/64 beats and is the longest | ||
| 81 | // duration that can be converted without overflow. | ||
| 82 | EXPECT_NEAR(audio_ms_to_duration(UINT16_MAX), 699, tol); | ||
| 83 | EXPECT_NEAR(audio_duration_to_ms(699), 65531, tol); | ||
| 84 | |||
| 85 | audio_set_tempo(255); | ||
| 86 | // At 255 bpm, UINT16_MAX ms corresponds to 17825/64 beats and is the longest | ||
| 87 | // duration that can be converted without overflow. | ||
| 88 | EXPECT_NEAR(audio_ms_to_duration(UINT16_MAX), 17825, tol); | ||
| 89 | EXPECT_NEAR(audio_duration_to_ms(17825), 65533, tol); | ||
| 90 | |||
| 91 | std::mt19937 rng(0 /*seed*/); | ||
| 92 | std::uniform_int_distribution<int> dist_tempo(10, 255); | ||
| 93 | std::uniform_int_distribution<int> dist_ms(0, UINT16_MAX); | ||
| 94 | |||
| 95 | // Test bpm <-> ms conversions for random tempos and durations. | ||
| 96 | for (int trial = 0; trial < 50; ++trial) { | ||
| 97 | const int tempo = dist_tempo(rng); | ||
| 98 | const int duration_ms = dist_ms(rng); | ||
| 99 | SCOPED_TRACE("tempo " + testing::PrintToString(tempo) + ", duration " + testing::PrintToString(duration_ms) + " ms"); | ||
| 100 | |||
| 101 | audio_set_tempo(tempo); | ||
| 102 | int duration_bpm = std::round((64.0f / (60.0f * 1000.0f)) * duration_ms * tempo); | ||
| 103 | ASSERT_NEAR(audio_ms_to_duration(duration_ms), duration_bpm, tol); | ||
| 104 | |||
| 105 | int roundtrip_ms = std::round((60.0f * 1000.0f / 64.0f) * duration_bpm / tempo); | ||
| 106 | // Because of round-off error, duration_ms and roundtrip_ms may differ by | ||
| 107 | // about (60 * 1000 / 64) / tempo. | ||
| 108 | int roundtrip_tol = tol * (60.0f * 1000.0f / 64.0f) / tempo; | ||
| 109 | ASSERT_NEAR(roundtrip_ms, duration_ms, roundtrip_tol); | ||
| 110 | |||
| 111 | // Only test converting back to ms if the result would be in uint16_t range. | ||
| 112 | if (roundtrip_ms <= UINT16_MAX) { | ||
| 113 | ASSERT_NEAR(audio_duration_to_ms(duration_bpm), roundtrip_ms, tol); | ||
| 114 | } | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | } // namespace | ||
