summaryrefslogtreecommitdiff
path: root/tests/audio
diff options
context:
space:
mode:
authorPascal Getreuer <50221757+getreuer@users.noreply.github.com>2023-07-07 08:47:16 -0600
committerGitHub <noreply@github.com>2023-07-08 00:47:16 +1000
commita8a87a0922a33d38ed80165ee62f9dc35bd08a3d (patch)
treeb590d46212c06e92f1683e08d9ebf12784eabd32 /tests/audio
parente648b84da3db022ba308163f21c3cf8165c45e09 (diff)
[Core] Simplify audio_duration_to_ms() and audio_ms_to_duration(), reduce firmware size by a few bytes. (#21427)
Diffstat (limited to 'tests/audio')
-rw-r--r--tests/audio/config.h18
-rw-r--r--tests/audio/test.mk16
-rw-r--r--tests/audio/test_audio.cpp118
3 files changed, 152 insertions, 0 deletions
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
16AUDIO_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
23namespace {
24
25class AudioTest : public TestFixture {
26 public:
27 uint16_t infer_tempo() {
28 return audio_ms_to_duration(1875) / 2;
29 }
30};
31
32TEST_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
46TEST_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
69TEST_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