]> code.delx.au - pulseaudio/blob - src/pulse/volume.c
feb33f078b9340b584bdbe28dd2ea5e63be9b741
[pulseaudio] / 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 <assert.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #include "volume.h"
33
34 int pa_cvolume_equal(const pa_cvolume *a, const pa_cvolume *b) {
35 int i;
36 assert(a);
37 assert(b);
38
39 if (a->channels != b->channels)
40 return 0;
41
42 for (i = 0; i < a->channels; i++)
43 if (a->values[i] != b->values[i])
44 return 0;
45
46 return 1;
47 }
48
49 pa_cvolume* pa_cvolume_set(pa_cvolume *a, unsigned channels, pa_volume_t v) {
50 int i;
51
52 assert(a);
53 assert(channels > 0);
54 assert(channels <= PA_CHANNELS_MAX);
55
56 a->channels = channels;
57
58 for (i = 0; i < a->channels; i++)
59 a->values[i] = v;
60
61 return a;
62 }
63
64 pa_volume_t pa_cvolume_avg(const pa_cvolume *a) {
65 uint64_t sum = 0;
66 int i;
67 assert(a);
68
69 for (i = 0; i < a->channels; i++)
70 sum += a->values[i];
71
72 sum /= a->channels;
73
74 return (pa_volume_t) sum;
75 }
76
77 pa_volume_t pa_sw_volume_multiply(pa_volume_t a, pa_volume_t b) {
78 return pa_sw_volume_from_linear(pa_sw_volume_to_linear(a)* pa_sw_volume_to_linear(b));
79 }
80
81 #define USER_DECIBEL_RANGE 30
82
83 pa_volume_t pa_sw_volume_from_dB(double dB) {
84 if (dB <= -USER_DECIBEL_RANGE)
85 return PA_VOLUME_MUTED;
86
87 return (pa_volume_t) ((dB/USER_DECIBEL_RANGE+1)*PA_VOLUME_NORM);
88 }
89
90 double pa_sw_volume_to_dB(pa_volume_t v) {
91 if (v == PA_VOLUME_MUTED)
92 return PA_DECIBEL_MININFTY;
93
94 return ((double) v/PA_VOLUME_NORM-1)*USER_DECIBEL_RANGE;
95 }
96
97 pa_volume_t pa_sw_volume_from_linear(double v) {
98
99 if (v <= 0)
100 return PA_VOLUME_MUTED;
101
102 if (v > .999 && v < 1.001)
103 return PA_VOLUME_NORM;
104
105 return pa_sw_volume_from_dB(20*log10(v));
106 }
107
108 double pa_sw_volume_to_linear(pa_volume_t v) {
109
110 if (v == PA_VOLUME_MUTED)
111 return 0;
112
113 return pow(10, pa_sw_volume_to_dB(v)/20);
114 }
115
116 char *pa_cvolume_snprint(char *s, size_t l, const pa_cvolume *c) {
117 unsigned channel;
118 int first = 1;
119 char *e;
120
121 assert(s);
122 assert(l > 0);
123 assert(c);
124
125 *(e = s) = 0;
126
127 for (channel = 0; channel < c->channels && l > 1; channel++) {
128 l -= snprintf(e, l, "%s%u: %3u%%",
129 first ? "" : " ",
130 channel,
131 (c->values[channel]*100)/PA_VOLUME_NORM);
132
133 e = strchr(e, 0);
134 first = 0;
135 }
136
137 return s;
138 }
139
140 /** Return non-zero if the volume of all channels is equal to the specified value */
141 int pa_cvolume_channels_equal_to(const pa_cvolume *a, pa_volume_t v) {
142 unsigned c;
143 assert(a);
144
145 for (c = 0; c < a->channels; c++)
146 if (a->values[c] != v)
147 return 0;
148
149 return 1;
150 }
151
152 pa_cvolume *pa_sw_cvolume_multiply(pa_cvolume *dest, const pa_cvolume *a, const pa_cvolume *b) {
153 unsigned i;
154
155 assert(dest);
156 assert(a);
157 assert(b);
158
159 for (i = 0; i < a->channels && i < b->channels && i < PA_CHANNELS_MAX; i++) {
160
161 dest->values[i] = pa_sw_volume_multiply(
162 i < a->channels ? a->values[i] : PA_VOLUME_NORM,
163 i < b->channels ? b->values[i] : PA_VOLUME_NORM);
164 }
165
166 dest->channels = i;
167
168 return dest;
169 }
170
171 int pa_cvolume_valid(const pa_cvolume *v) {
172 assert(v);
173
174 if (v->channels <= 0 || v->channels > PA_CHANNELS_MAX)
175 return 0;
176
177 return 1;
178 }