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