merge glitch-free branch back into trunk
[profile/ivi/pulseaudio.git] / src / pulse / volume.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2004-2006 Lennart Poettering
7
8   PulseAudio is free software; you can redistribute it and/or modify
9   it under the terms of the GNU Lesser General Public License as published
10   by the Free Software Foundation; either version 2 of the License,
11   or (at your option) any later version.
12
13   PulseAudio is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with PulseAudio; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21   USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <string.h>
30
31 #include <pulsecore/core-util.h>
32 #include <pulsecore/macro.h>
33
34 #include "volume.h"
35
36 int pa_cvolume_equal(const pa_cvolume *a, const pa_cvolume *b) {
37     int i;
38     pa_assert(a);
39     pa_assert(b);
40
41     if (a->channels != b->channels)
42         return 0;
43
44     for (i = 0; i < a->channels; i++)
45         if (a->values[i] != b->values[i])
46             return 0;
47
48     return 1;
49 }
50
51 pa_cvolume* pa_cvolume_set(pa_cvolume *a, unsigned channels, pa_volume_t v) {
52     int i;
53
54     pa_assert(a);
55     pa_assert(channels > 0);
56     pa_assert(channels <= PA_CHANNELS_MAX);
57
58     a->channels = channels;
59
60     for (i = 0; i < a->channels; i++)
61         a->values[i] = v;
62
63     return a;
64 }
65
66 pa_volume_t pa_cvolume_avg(const pa_cvolume *a) {
67     uint64_t sum = 0;
68     int i;
69     pa_assert(a);
70
71     for (i = 0; i < a->channels; i++)
72         sum += a->values[i];
73
74     sum /= a->channels;
75
76     return (pa_volume_t) sum;
77 }
78
79 pa_volume_t pa_sw_volume_multiply(pa_volume_t a, pa_volume_t b) {
80     return pa_sw_volume_from_linear(pa_sw_volume_to_linear(a)* pa_sw_volume_to_linear(b));
81 }
82
83 #define USER_DECIBEL_RANGE 60
84
85 pa_volume_t pa_sw_volume_from_dB(double dB) {
86     if (isinf(dB) < 0 || dB <= -USER_DECIBEL_RANGE)
87         return PA_VOLUME_MUTED;
88
89     return (pa_volume_t) ((dB/USER_DECIBEL_RANGE+1)*PA_VOLUME_NORM);
90 }
91
92 double pa_sw_volume_to_dB(pa_volume_t v) {
93     if (v == PA_VOLUME_MUTED)
94         return PA_DECIBEL_MININFTY;
95
96     return ((double) v/PA_VOLUME_NORM-1)*USER_DECIBEL_RANGE;
97 }
98
99 pa_volume_t pa_sw_volume_from_linear(double v) {
100
101     if (v <= 0)
102         return PA_VOLUME_MUTED;
103
104     if (v > .999 && v < 1.001)
105         return PA_VOLUME_NORM;
106
107     return pa_sw_volume_from_dB(20*log10(v));
108 }
109
110 double pa_sw_volume_to_linear(pa_volume_t v) {
111
112     if (v == PA_VOLUME_MUTED)
113         return 0;
114
115     return pow(10, pa_sw_volume_to_dB(v)/20);
116 }
117
118 char *pa_cvolume_snprint(char *s, size_t l, const pa_cvolume *c) {
119     unsigned channel;
120     int first = 1;
121     char *e;
122
123     pa_assert(s);
124     pa_assert(l > 0);
125     pa_assert(c);
126
127     *(e = s) = 0;
128
129     for (channel = 0; channel < c->channels && l > 1; channel++) {
130         l -= pa_snprintf(e, l, "%s%u: %3u%%",
131                       first ? "" : " ",
132                       channel,
133                       (c->values[channel]*100)/PA_VOLUME_NORM);
134
135         e = strchr(e, 0);
136         first = 0;
137     }
138
139     return s;
140 }
141
142 /** Return non-zero if the volume of all channels is equal to the specified value */
143 int pa_cvolume_channels_equal_to(const pa_cvolume *a, pa_volume_t v) {
144     unsigned c;
145     pa_assert(a);
146
147     for (c = 0; c < a->channels; c++)
148         if (a->values[c] != v)
149             return 0;
150
151     return 1;
152 }
153
154 pa_cvolume *pa_sw_cvolume_multiply(pa_cvolume *dest, const pa_cvolume *a, const pa_cvolume *b) {
155     unsigned i;
156
157     pa_assert(dest);
158     pa_assert(a);
159     pa_assert(b);
160
161     for (i = 0; i < a->channels && i < b->channels && i < PA_CHANNELS_MAX; i++) {
162
163         dest->values[i] = pa_sw_volume_multiply(
164             i < a->channels ? a->values[i] : PA_VOLUME_NORM,
165             i < b->channels ? b->values[i] : PA_VOLUME_NORM);
166     }
167
168     dest->channels = i;
169
170     return dest;
171 }
172
173 int pa_cvolume_valid(const pa_cvolume *v) {
174     pa_assert(v);
175
176     if (v->channels <= 0 || v->channels > PA_CHANNELS_MAX)
177         return 0;
178
179     return 1;
180 }