logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

test_audio.cpp (3949B)


  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. #include <cmath>
  16. #include <random>
  17. #include "gtest/gtest.h"
  18. #include "keyboard_report_util.hpp"
  19. #include "test_common.hpp"
  20. namespace {
  21. class AudioTest : public TestFixture {
  22. public:
  23. uint16_t infer_tempo() {
  24. return audio_ms_to_duration(1875) / 2;
  25. }
  26. };
  27. TEST_F(AudioTest, OnOffToggle) {
  28. audio_on();
  29. EXPECT_TRUE(audio_is_on());
  30. audio_off();
  31. EXPECT_FALSE(audio_is_on());
  32. audio_toggle();
  33. EXPECT_TRUE(audio_is_on());
  34. audio_toggle();
  35. EXPECT_FALSE(audio_is_on());
  36. }
  37. TEST_F(AudioTest, ChangeTempo) {
  38. for (int tempo = 50; tempo <= 250; tempo += 50) {
  39. audio_set_tempo(tempo);
  40. EXPECT_EQ(infer_tempo(), tempo);
  41. }
  42. audio_set_tempo(10);
  43. audio_increase_tempo(25);
  44. EXPECT_EQ(infer_tempo(), 35);
  45. audio_decrease_tempo(4);
  46. EXPECT_EQ(infer_tempo(), 31);
  47. audio_increase_tempo(250);
  48. EXPECT_EQ(infer_tempo(), 255);
  49. audio_set_tempo(9);
  50. EXPECT_EQ(infer_tempo(), 10);
  51. audio_decrease_tempo(100);
  52. EXPECT_EQ(infer_tempo(), 10);
  53. }
  54. TEST_F(AudioTest, BpmConversion) {
  55. const int tol = 1;
  56. audio_set_tempo(120);
  57. // At 120 bpm, there are 2 beats per second, and a whole note is 500 ms.
  58. EXPECT_NEAR(audio_duration_to_ms(64 /* whole note */), 500, tol);
  59. EXPECT_NEAR(audio_ms_to_duration(500), 64, tol);
  60. EXPECT_EQ(audio_duration_to_ms(0), 0);
  61. EXPECT_EQ(audio_ms_to_duration(0), 0);
  62. audio_set_tempo(10);
  63. // At 10 bpm, UINT16_MAX ms corresponds to 699/64 beats and is the longest
  64. // duration that can be converted without overflow.
  65. EXPECT_NEAR(audio_ms_to_duration(UINT16_MAX), 699, tol);
  66. EXPECT_NEAR(audio_duration_to_ms(699), 65531, tol);
  67. audio_set_tempo(255);
  68. // At 255 bpm, UINT16_MAX ms corresponds to 17825/64 beats and is the longest
  69. // duration that can be converted without overflow.
  70. EXPECT_NEAR(audio_ms_to_duration(UINT16_MAX), 17825, tol);
  71. EXPECT_NEAR(audio_duration_to_ms(17825), 65533, tol);
  72. std::mt19937 rng(0 /*seed*/);
  73. std::uniform_int_distribution<int> dist_tempo(10, 255);
  74. std::uniform_int_distribution<int> dist_ms(0, UINT16_MAX);
  75. // Test bpm <-> ms conversions for random tempos and durations.
  76. for (int trial = 0; trial < 50; ++trial) {
  77. const int tempo = dist_tempo(rng);
  78. const int duration_ms = dist_ms(rng);
  79. SCOPED_TRACE("tempo " + testing::PrintToString(tempo) + ", duration " + testing::PrintToString(duration_ms) + " ms");
  80. audio_set_tempo(tempo);
  81. int duration_bpm = std::round((64.0f / (60.0f * 1000.0f)) * duration_ms * tempo);
  82. ASSERT_NEAR(audio_ms_to_duration(duration_ms), duration_bpm, tol);
  83. int roundtrip_ms = std::round((60.0f * 1000.0f / 64.0f) * duration_bpm / tempo);
  84. // Because of round-off error, duration_ms and roundtrip_ms may differ by
  85. // about (60 * 1000 / 64) / tempo.
  86. int roundtrip_tol = tol * (60.0f * 1000.0f / 64.0f) / tempo;
  87. ASSERT_NEAR(roundtrip_ms, duration_ms, roundtrip_tol);
  88. // Only test converting back to ms if the result would be in uint16_t range.
  89. if (roundtrip_ms <= UINT16_MAX) {
  90. ASSERT_NEAR(audio_duration_to_ms(duration_bpm), roundtrip_ms, tol);
  91. }
  92. }
  93. }
  94. } // namespace