1 /***
2 This file is part of PulseAudio.
3
4 PulseAudio is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published
6 by the Free Software Foundation; either version 2.1 of the License,
7 or (at your option) any later version.
8
9 PulseAudio is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
16 ***/
17
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21
22 #include <stdio.h>
23 #include <math.h>
24
25 #include <check.h>
26
27 #include <pulse/volume.h>
28
29 #include <pulsecore/log.h>
30 #include <pulsecore/macro.h>
31
START_TEST(volume_test)32 START_TEST (volume_test) {
33 pa_volume_t v;
34 pa_cvolume cv;
35 float b;
36 pa_channel_map map;
37 pa_volume_t md = 0;
38 unsigned mdn = 0;
39
40 if (!getenv("MAKE_CHECK"))
41 pa_log_set_level(PA_LOG_DEBUG);
42
43 pa_log("Attenuation of sample 1 against 32767: %g dB", 20.0*log10(1.0/32767.0));
44 pa_log("Smallest possible attenuation > 0 applied to 32767: %li", lrint(32767.0*pa_sw_volume_to_linear(1)));
45
46 for (v = PA_VOLUME_MUTED; v <= PA_VOLUME_NORM*2; v += 256) {
47
48 double dB = pa_sw_volume_to_dB(v);
49 double f = pa_sw_volume_to_linear(v);
50
51 pa_log_debug("Volume: %3i; percent: %i%%; decibel %0.2f; linear = %0.2f; volume(decibel): %3i; volume(linear): %3i",
52 v, (v*100)/PA_VOLUME_NORM, dB, f, pa_sw_volume_from_dB(dB), pa_sw_volume_from_linear(f));
53 }
54
55 map.channels = cv.channels = 2;
56 map.map[0] = PA_CHANNEL_POSITION_LEFT;
57 map.map[1] = PA_CHANNEL_POSITION_RIGHT;
58
59 for (v = PA_VOLUME_MUTED; v <= PA_VOLUME_NORM*2; v += 256) {
60 char s[PA_CVOLUME_SNPRINT_VERBOSE_MAX];
61
62 pa_cvolume_set(&cv, 2, v);
63
64 pa_log_debug("Volume: %3i [%s]", v, pa_cvolume_snprint_verbose(s, sizeof(s), &cv, &map, true));
65 }
66
67 for (cv.values[0] = PA_VOLUME_MUTED; cv.values[0] <= PA_VOLUME_NORM*2; cv.values[0] += 4096)
68 for (cv.values[1] = PA_VOLUME_MUTED; cv.values[1] <= PA_VOLUME_NORM*2; cv.values[1] += 4096) {
69 char s[PA_CVOLUME_SNPRINT_VERBOSE_MAX];
70
71 pa_log_debug("Volume: [%s]; balance: %2.1f",
72 pa_cvolume_snprint_verbose(s, sizeof(s), &cv, &map, true),
73 pa_cvolume_get_balance(&cv, &map));
74 }
75
76 for (cv.values[0] = PA_VOLUME_MUTED+4096; cv.values[0] <= PA_VOLUME_NORM*2; cv.values[0] += 4096)
77 for (cv.values[1] = PA_VOLUME_MUTED; cv.values[1] <= PA_VOLUME_NORM*2; cv.values[1] += 4096)
78 for (b = -1.0f; b <= 1.0f; b += 0.2f) {
79 char s[PA_CVOLUME_SNPRINT_VERBOSE_MAX];
80 pa_cvolume r;
81 float k;
82
83 pa_log_debug("Before: volume: [%s]; balance: %2.1f",
84 pa_cvolume_snprint_verbose(s, sizeof(s), &cv, &map, true),
85 pa_cvolume_get_balance(&cv, &map));
86
87 r = cv;
88 pa_cvolume_set_balance(&r, &map,b);
89
90 k = pa_cvolume_get_balance(&r, &map);
91 pa_log_debug("After: volume: [%s]; balance: %2.1f (intended: %2.1f) %s",
92 pa_cvolume_snprint_verbose(s, sizeof(s), &r, &map, true),
93 k,
94 b,
95 k < b - .05 || k > b + .5 ? "MISMATCH" : "");
96 }
97
98 for (v = PA_VOLUME_MUTED; v <= PA_VOLUME_NORM*2; v += 51) {
99
100 double l = pa_sw_volume_to_linear(v);
101 pa_volume_t k = pa_sw_volume_from_linear(l);
102 double db = pa_sw_volume_to_dB(v);
103 pa_volume_t r = pa_sw_volume_from_dB(db);
104 pa_volume_t w;
105
106 fail_unless(k == v);
107 fail_unless(r == v);
108
109 for (w = PA_VOLUME_MUTED; w < PA_VOLUME_NORM*2; w += 37) {
110
111 double t = pa_sw_volume_to_linear(w);
112 double db2 = pa_sw_volume_to_dB(w);
113 pa_volume_t p, p1, p2;
114 pa_volume_t md_local = 0;
115 double q, qq;
116
117 p = pa_sw_volume_multiply(v, w);
118 if (isfinite(db) && isfinite(db2))
119 qq = db + db2;
120 else
121 qq = -INFINITY;
122 p2 = pa_sw_volume_from_dB(qq);
123 q = l*t;
124 p1 = pa_sw_volume_from_linear(q);
125
126 if (p2 > p)
127 md_local = p2 - p;
128 else
129 md_local = p - p2;
130
131 if (p1 > p && p1 - p > md_local)
132 md_local = p1 - p;
133 if (p1 < p && p - p1 > md_local)
134 md_local = p - p1;
135
136 /* compute the number of times the deviation is over the acceptable threshold */
137 if (md_local > 1)
138 mdn++;
139
140 if (md_local > md)
141 md = md_local;
142 }
143 }
144
145 /*
146 * As the hardware, the compiler version and the compilation flags may
147 * generate rounding issues, we allow p1 and p2 to have a difference of + or - 1.
148 */
149 pa_log("max deviation: %lu, number of times over 1:%lu", (unsigned long) md, (unsigned long) mdn);
150
151 fail_unless(md <= 1);
152 }
153 END_TEST
154
main(int argc,char * argv[])155 int main(int argc, char *argv[]) {
156 int failed = 0;
157 Suite *s;
158 TCase *tc;
159 SRunner *sr;
160
161 s = suite_create("Volume");
162 tc = tcase_create("volume");
163 tcase_add_test(tc, volume_test);
164 tcase_set_timeout(tc, 120);
165 suite_add_tcase(s, tc);
166
167 sr = srunner_create(s);
168 srunner_run_all(sr, CK_NORMAL);
169 failed = srunner_ntests_failed(sr);
170 srunner_free(sr);
171
172 return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
173 }
174