2 This file is part of PulseAudio.
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.
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.
14 You should have received a copy of the GNU Lesser General Public License
15 along with PulseAudio; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <pulse/volume.h>
31 #include <pulsecore/log.h>
32 #include <pulsecore/macro.h>
34 START_TEST (volume_test) {
42 if (!getenv("MAKE_CHECK"))
43 pa_log_set_level(PA_LOG_DEBUG);
45 pa_log("Attenuation of sample 1 against 32767: %g dB", 20.0*log10(1.0/32767.0));
46 pa_log("Smallest possible attenuation > 0 applied to 32767: %li", lrint(32767.0*pa_sw_volume_to_linear(1)));
48 for (v = PA_VOLUME_MUTED; v <= PA_VOLUME_NORM*2; v += 256) {
50 double dB = pa_sw_volume_to_dB(v);
51 double f = pa_sw_volume_to_linear(v);
53 pa_log_debug("Volume: %3i; percent: %i%%; decibel %0.2f; linear = %0.2f; volume(decibel): %3i; volume(linear): %3i",
54 v, (v*100)/PA_VOLUME_NORM, dB, f, pa_sw_volume_from_dB(dB), pa_sw_volume_from_linear(f));
57 for (v = PA_VOLUME_MUTED; v <= PA_VOLUME_NORM*2; v += 256) {
58 char s[PA_CVOLUME_SNPRINT_MAX], t[PA_SW_CVOLUME_SNPRINT_DB_MAX];
60 pa_cvolume_set(&cv, 2, v);
62 pa_log_debug("Volume: %3i [%s] [%s]", v, pa_cvolume_snprint(s, sizeof(s), &cv), pa_sw_cvolume_snprint_dB(t, sizeof(t), &cv));
65 map.channels = cv.channels = 2;
66 map.map[0] = PA_CHANNEL_POSITION_LEFT;
67 map.map[1] = PA_CHANNEL_POSITION_RIGHT;
69 for (cv.values[0] = PA_VOLUME_MUTED; cv.values[0] <= PA_VOLUME_NORM*2; cv.values[0] += 4096)
70 for (cv.values[1] = PA_VOLUME_MUTED; cv.values[1] <= PA_VOLUME_NORM*2; cv.values[1] += 4096) {
71 char s[PA_CVOLUME_SNPRINT_MAX];
73 pa_log_debug("Volume: [%s]; balance: %2.1f", pa_cvolume_snprint(s, sizeof(s), &cv), pa_cvolume_get_balance(&cv, &map));
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_MAX];
83 pa_log_debug("Before: volume: [%s]; balance: %2.1f", pa_cvolume_snprint(s, sizeof(s), &cv), pa_cvolume_get_balance(&cv, &map));
86 pa_cvolume_set_balance(&r, &map,b);
88 k = pa_cvolume_get_balance(&r, &map);
89 pa_log_debug("After: volume: [%s]; balance: %2.1f (intended: %2.1f) %s", pa_cvolume_snprint(s, sizeof(s), &r), k, b, k < b-.05 || k > b+.5 ? "MISMATCH" : "");
92 for (v = PA_VOLUME_MUTED; v <= PA_VOLUME_NORM*2; v += 51) {
94 double l = pa_sw_volume_to_linear(v);
95 pa_volume_t k = pa_sw_volume_from_linear(l);
96 double db = pa_sw_volume_to_dB(v);
97 pa_volume_t r = pa_sw_volume_from_dB(db);
103 for (w = PA_VOLUME_MUTED; w < PA_VOLUME_NORM*2; w += 37) {
105 double t = pa_sw_volume_to_linear(w);
106 double db2 = pa_sw_volume_to_dB(w);
107 pa_volume_t p, p1, p2;
110 p = pa_sw_volume_multiply(v, w);
112 p2 = pa_sw_volume_from_dB(qq);
114 p1 = pa_sw_volume_from_linear(q);
116 if (p2 > p && p2 - p > md)
118 if (p2 < p && p - p2 > md)
120 if (p1 > p && p1 - p > md)
122 if (p1 < p && p - p1 > md)
125 if (p1 != p || p2 != p)
130 pa_log("max deviation: %lu n=%lu", (unsigned long) md, (unsigned long) mdn);
132 fail_unless(md <= 1);
133 fail_unless(mdn <= 251);
137 int main(int argc, char *argv[]) {
143 s = suite_create("Volume");
144 tc = tcase_create("volume");
145 tcase_add_test(tc, volume_test);
146 tcase_set_timeout(tc, 120);
147 suite_add_tcase(s, tc);
149 sr = srunner_create(s);
150 srunner_run_all(sr, CK_NORMAL);
151 failed = srunner_ntests_failed(sr);
154 return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;