4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
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.
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.
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
31 #include <pulsecore/core-util.h>
32 #include <pulsecore/macro.h>
36 int pa_cvolume_equal(const pa_cvolume *a, const pa_cvolume *b) {
41 if (a->channels != b->channels)
44 for (i = 0; i < a->channels; i++)
45 if (a->values[i] != b->values[i])
51 pa_cvolume* pa_cvolume_set(pa_cvolume *a, unsigned channels, pa_volume_t v) {
55 pa_assert(channels > 0);
56 pa_assert(channels <= PA_CHANNELS_MAX);
58 a->channels = channels;
60 for (i = 0; i < a->channels; i++)
66 pa_volume_t pa_cvolume_avg(const pa_cvolume *a) {
71 for (i = 0; i < a->channels; i++)
76 return (pa_volume_t) sum;
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));
83 #define USER_DECIBEL_RANGE 60
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;
89 return (pa_volume_t) ((dB/USER_DECIBEL_RANGE+1)*PA_VOLUME_NORM);
92 double pa_sw_volume_to_dB(pa_volume_t v) {
93 if (v == PA_VOLUME_MUTED)
94 return PA_DECIBEL_MININFTY;
96 return ((double) v/PA_VOLUME_NORM-1)*USER_DECIBEL_RANGE;
99 pa_volume_t pa_sw_volume_from_linear(double v) {
102 return PA_VOLUME_MUTED;
104 if (v > .999 && v < 1.001)
105 return PA_VOLUME_NORM;
107 return pa_sw_volume_from_dB(20*log10(v));
110 double pa_sw_volume_to_linear(pa_volume_t v) {
112 if (v == PA_VOLUME_MUTED)
115 return pow(10, pa_sw_volume_to_dB(v)/20);
118 char *pa_cvolume_snprint(char *s, size_t l, const pa_cvolume *c) {
129 for (channel = 0; channel < c->channels && l > 1; channel++) {
130 l -= pa_snprintf(e, l, "%s%u: %3u%%",
133 (c->values[channel]*100)/PA_VOLUME_NORM);
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) {
147 for (c = 0; c < a->channels; c++)
148 if (a->values[c] != v)
154 pa_cvolume *pa_sw_cvolume_multiply(pa_cvolume *dest, const pa_cvolume *a, const pa_cvolume *b) {
161 for (i = 0; i < a->channels && i < b->channels && i < PA_CHANNELS_MAX; i++) {
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);
173 int pa_cvolume_valid(const pa_cvolume *v) {
176 if (v->channels <= 0 || v->channels > PA_CHANNELS_MAX)