2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
6 PulseAudio 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.1 of the License,
9 or (at your option) any later version.
11 PulseAudio 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.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <pulse/i18n.h>
31 #include <pulsecore/core-util.h>
32 #include <pulsecore/macro.h>
33 #include <pulsecore/sample-util.h>
37 int pa_cvolume_equal(const pa_cvolume
*a
, const pa_cvolume
*b
) {
42 pa_return_val_if_fail(pa_cvolume_valid(a
), 0);
44 if (PA_UNLIKELY(a
== b
))
47 pa_return_val_if_fail(pa_cvolume_valid(b
), 0);
49 if (a
->channels
!= b
->channels
)
52 for (i
= 0; i
< a
->channels
; i
++)
53 if (a
->values
[i
] != b
->values
[i
])
59 pa_cvolume
* pa_cvolume_init(pa_cvolume
*a
) {
66 for (c
= 0; c
< PA_CHANNELS_MAX
; c
++)
67 a
->values
[c
] = (pa_volume_t
) -1;
72 pa_cvolume
* pa_cvolume_set(pa_cvolume
*a
, unsigned channels
, pa_volume_t v
) {
76 pa_assert(channels
> 0);
77 pa_assert(channels
<= PA_CHANNELS_MAX
);
79 a
->channels
= (uint8_t) channels
;
81 for (i
= 0; i
< a
->channels
; i
++)
87 pa_volume_t
pa_cvolume_avg(const pa_cvolume
*a
) {
92 pa_return_val_if_fail(pa_cvolume_valid(a
), PA_VOLUME_MUTED
);
94 for (c
= 0; c
< a
->channels
; c
++)
99 return (pa_volume_t
) sum
;
102 pa_volume_t
pa_cvolume_avg_mask(const pa_cvolume
*a
, const pa_channel_map
*cm
, pa_channel_position_mask_t mask
) {
109 return pa_cvolume_avg(a
);
111 pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(a
, cm
), PA_VOLUME_MUTED
);
113 for (c
= n
= 0; c
< a
->channels
; c
++) {
115 if (!(PA_CHANNEL_POSITION_MASK(cm
->map
[c
]) & mask
))
125 return (pa_volume_t
) sum
;
128 pa_volume_t
pa_cvolume_max(const pa_cvolume
*a
) {
129 pa_volume_t m
= PA_VOLUME_MUTED
;
133 pa_return_val_if_fail(pa_cvolume_valid(a
), PA_VOLUME_MUTED
);
135 for (c
= 0; c
< a
->channels
; c
++)
136 if (a
->values
[c
] > m
)
142 pa_volume_t
pa_cvolume_min(const pa_cvolume
*a
) {
143 pa_volume_t m
= (pa_volume_t
) -1;
147 pa_return_val_if_fail(pa_cvolume_valid(a
), PA_VOLUME_MUTED
);
149 for (c
= 0; c
< a
->channels
; c
++)
150 if (m
== (pa_volume_t
) -1 || a
->values
[c
] < m
)
156 pa_volume_t
pa_cvolume_max_mask(const pa_cvolume
*a
, const pa_channel_map
*cm
, pa_channel_position_mask_t mask
) {
157 pa_volume_t m
= PA_VOLUME_MUTED
;
163 return pa_cvolume_max(a
);
165 pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(a
, cm
), PA_VOLUME_MUTED
);
167 for (c
= n
= 0; c
< a
->channels
; c
++) {
169 if (!(PA_CHANNEL_POSITION_MASK(cm
->map
[c
]) & mask
))
172 if (a
->values
[c
] > m
)
179 pa_volume_t
pa_cvolume_min_mask(const pa_cvolume
*a
, const pa_channel_map
*cm
, pa_channel_position_mask_t mask
) {
180 pa_volume_t m
= (pa_volume_t
) -1;
186 return pa_cvolume_min(a
);
188 pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(a
, cm
), PA_VOLUME_MUTED
);
190 for (c
= n
= 0; c
< a
->channels
; c
++) {
192 if (!(PA_CHANNEL_POSITION_MASK(cm
->map
[c
]) & mask
))
195 if (m
== (pa_volume_t
) -1 || a
->values
[c
] < m
)
202 pa_volume_t
pa_sw_volume_multiply(pa_volume_t a
, pa_volume_t b
) {
204 /* cbrt((a/PA_VOLUME_NORM)^3*(b/PA_VOLUME_NORM)^3)*PA_VOLUME_NORM = a*b/PA_VOLUME_NORM */
206 return (pa_volume_t
) (((uint64_t) a
* (uint64_t) b
+ (uint64_t) PA_VOLUME_NORM
/ 2ULL) / (uint64_t) PA_VOLUME_NORM
);
209 pa_volume_t
pa_sw_volume_divide(pa_volume_t a
, pa_volume_t b
) {
211 if (b
<= PA_VOLUME_MUTED
)
214 return (pa_volume_t
) (((uint64_t) a
* (uint64_t) PA_VOLUME_NORM
+ (uint64_t) b
/ 2ULL) / (uint64_t) b
);
217 /* Amplitude, not power */
218 static double linear_to_dB(double v
) {
219 return 20.0 * log10(v
);
222 static double dB_to_linear(double v
) {
223 return pow(10.0, v
/ 20.0);
226 pa_volume_t
pa_sw_volume_from_dB(double dB
) {
227 if (isinf(dB
) < 0 || dB
<= PA_DECIBEL_MININFTY
)
228 return PA_VOLUME_MUTED
;
230 return pa_sw_volume_from_linear(dB_to_linear(dB
));
233 double pa_sw_volume_to_dB(pa_volume_t v
) {
235 if (v
<= PA_VOLUME_MUTED
)
236 return PA_DECIBEL_MININFTY
;
238 return linear_to_dB(pa_sw_volume_to_linear(v
));
241 pa_volume_t
pa_sw_volume_from_linear(double v
) {
244 return PA_VOLUME_MUTED
;
247 * We use a cubic mapping here, as suggested and discussed here:
249 * http://www.robotplanet.dk/audio/audio_gui_design/
250 * http://lists.linuxaudio.org/pipermail/linux-audio-dev/2009-May/thread.html#23151
252 * We make sure that the conversion to linear and back yields the
253 * same volume value! That's why we need the lround() below!
256 return (pa_volume_t
) lround(cbrt(v
) * PA_VOLUME_NORM
);
259 double pa_sw_volume_to_linear(pa_volume_t v
) {
262 if (v
<= PA_VOLUME_MUTED
)
265 if (v
== PA_VOLUME_NORM
)
268 f
= ((double) v
/ PA_VOLUME_NORM
);
273 char *pa_cvolume_snprint(char *s
, size_t l
, const pa_cvolume
*c
) {
275 pa_bool_t first
= TRUE
;
284 if (!pa_cvolume_valid(c
)) {
285 pa_snprintf(s
, l
, _("(invalid)"));
291 for (channel
= 0; channel
< c
->channels
&& l
> 1; channel
++) {
292 l
-= pa_snprintf(e
, l
, "%s%u: %3u%%",
295 (c
->values
[channel
]*100)/PA_VOLUME_NORM
);
304 char *pa_volume_snprint(char *s
, size_t l
, pa_volume_t v
) {
310 if (v
== (pa_volume_t
) -1) {
311 pa_snprintf(s
, l
, _("(invalid)"));
315 pa_snprintf(s
, l
, "%3u%%", (v
*100)/PA_VOLUME_NORM
);
319 char *pa_sw_cvolume_snprint_dB(char *s
, size_t l
, const pa_cvolume
*c
) {
321 pa_bool_t first
= TRUE
;
330 if (!pa_cvolume_valid(c
)) {
331 pa_snprintf(s
, l
, _("(invalid)"));
337 for (channel
= 0; channel
< c
->channels
&& l
> 1; channel
++) {
338 double f
= pa_sw_volume_to_dB(c
->values
[channel
]);
340 l
-= pa_snprintf(e
, l
, "%s%u: %0.2f dB",
343 isinf(f
) < 0 || f
<= PA_DECIBEL_MININFTY
? -INFINITY
: f
);
352 char *pa_sw_volume_snprint_dB(char *s
, size_t l
, pa_volume_t v
) {
360 if (v
== (pa_volume_t
) -1) {
361 pa_snprintf(s
, l
, _("(invalid)"));
365 f
= pa_sw_volume_to_dB(v
);
366 pa_snprintf(s
, l
, "%0.2f dB",
367 isinf(f
) < 0 || f
<= PA_DECIBEL_MININFTY
? -INFINITY
: f
);
372 int pa_cvolume_channels_equal_to(const pa_cvolume
*a
, pa_volume_t v
) {
376 pa_return_val_if_fail(pa_cvolume_valid(a
), 0);
378 for (c
= 0; c
< a
->channels
; c
++)
379 if (a
->values
[c
] != v
)
385 pa_cvolume
*pa_sw_cvolume_multiply(pa_cvolume
*dest
, const pa_cvolume
*a
, const pa_cvolume
*b
) {
392 pa_return_val_if_fail(pa_cvolume_valid(a
), NULL
);
393 pa_return_val_if_fail(pa_cvolume_valid(b
), NULL
);
395 for (i
= 0; i
< a
->channels
&& i
< b
->channels
; i
++)
396 dest
->values
[i
] = pa_sw_volume_multiply(a
->values
[i
], b
->values
[i
]);
398 dest
->channels
= (uint8_t) i
;
403 pa_cvolume
*pa_sw_cvolume_multiply_scalar(pa_cvolume
*dest
, const pa_cvolume
*a
, pa_volume_t b
) {
409 pa_return_val_if_fail(pa_cvolume_valid(a
), NULL
);
411 for (i
= 0; i
< a
->channels
; i
++)
412 dest
->values
[i
] = pa_sw_volume_multiply(a
->values
[i
], b
);
414 dest
->channels
= (uint8_t) i
;
419 pa_cvolume
*pa_sw_cvolume_divide(pa_cvolume
*dest
, const pa_cvolume
*a
, const pa_cvolume
*b
) {
426 pa_return_val_if_fail(pa_cvolume_valid(a
), NULL
);
427 pa_return_val_if_fail(pa_cvolume_valid(b
), NULL
);
429 for (i
= 0; i
< a
->channels
&& i
< b
->channels
; i
++)
430 dest
->values
[i
] = pa_sw_volume_divide(a
->values
[i
], b
->values
[i
]);
432 dest
->channels
= (uint8_t) i
;
437 pa_cvolume
*pa_sw_cvolume_divide_scalar(pa_cvolume
*dest
, const pa_cvolume
*a
, pa_volume_t b
) {
443 pa_return_val_if_fail(pa_cvolume_valid(a
), NULL
);
445 for (i
= 0; i
< a
->channels
; i
++)
446 dest
->values
[i
] = pa_sw_volume_divide(a
->values
[i
], b
);
448 dest
->channels
= (uint8_t) i
;
453 int pa_cvolume_valid(const pa_cvolume
*v
) {
458 if (v
->channels
<= 0 || v
->channels
> PA_CHANNELS_MAX
)
461 for (c
= 0; c
< v
->channels
; c
++)
462 if (v
->values
[c
] == (pa_volume_t
) -1)
468 static pa_bool_t
on_left(pa_channel_position_t p
) {
469 return !!(PA_CHANNEL_POSITION_MASK(p
) & PA_CHANNEL_POSITION_MASK_LEFT
);
472 static pa_bool_t
on_right(pa_channel_position_t p
) {
473 return !!(PA_CHANNEL_POSITION_MASK(p
) & PA_CHANNEL_POSITION_MASK_RIGHT
);
476 static pa_bool_t
on_center(pa_channel_position_t p
) {
477 return !!(PA_CHANNEL_POSITION_MASK(p
) & PA_CHANNEL_POSITION_MASK_CENTER
);
480 static pa_bool_t
on_lfe(pa_channel_position_t p
) {
481 return p
== PA_CHANNEL_POSITION_LFE
;
484 static pa_bool_t
on_front(pa_channel_position_t p
) {
485 return !!(PA_CHANNEL_POSITION_MASK(p
) & PA_CHANNEL_POSITION_MASK_FRONT
);
488 static pa_bool_t
on_rear(pa_channel_position_t p
) {
489 return !!(PA_CHANNEL_POSITION_MASK(p
) & PA_CHANNEL_POSITION_MASK_REAR
);
492 pa_cvolume
*pa_cvolume_remap(pa_cvolume
*v
, const pa_channel_map
*from
, const pa_channel_map
*to
) {
500 pa_return_val_if_fail(pa_cvolume_valid(v
), NULL
);
501 pa_return_val_if_fail(pa_channel_map_valid(from
), NULL
);
502 pa_return_val_if_fail(pa_channel_map_valid(to
), NULL
);
503 pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(v
, from
), NULL
);
505 if (pa_channel_map_equal(from
, to
))
508 result
.channels
= to
->channels
;
510 for (b
= 0; b
< to
->channels
; b
++) {
514 for (a
= 0; a
< from
->channels
; a
++)
515 if (from
->map
[a
] == to
->map
[b
]) {
521 for (a
= 0; a
< from
->channels
; a
++)
522 if ((on_left(from
->map
[a
]) && on_left(to
->map
[b
])) ||
523 (on_right(from
->map
[a
]) && on_right(to
->map
[b
])) ||
524 (on_center(from
->map
[a
]) && on_center(to
->map
[b
])) ||
525 (on_lfe(from
->map
[a
]) && on_lfe(to
->map
[b
]))) {
533 k
= pa_cvolume_avg(v
);
537 result
.values
[b
] = k
;
544 int pa_cvolume_compatible(const pa_cvolume
*v
, const pa_sample_spec
*ss
) {
549 pa_return_val_if_fail(pa_cvolume_valid(v
), 0);
550 pa_return_val_if_fail(pa_sample_spec_valid(ss
), 0);
552 return v
->channels
== ss
->channels
;
555 int pa_cvolume_compatible_with_channel_map(const pa_cvolume
*v
, const pa_channel_map
*cm
) {
559 pa_return_val_if_fail(pa_cvolume_valid(v
), 0);
560 pa_return_val_if_fail(pa_channel_map_valid(cm
), 0);
562 return v
->channels
== cm
->channels
;
565 static void get_avg_lr(const pa_channel_map
*map
, const pa_cvolume
*v
, pa_volume_t
*l
, pa_volume_t
*r
) {
567 pa_volume_t left
= 0, right
= 0;
568 unsigned n_left
= 0, n_right
= 0;
572 pa_assert(map
->channels
== v
->channels
);
576 for (c
= 0; c
< map
->channels
; c
++) {
577 if (on_left(map
->map
[c
])) {
578 left
+= v
->values
[c
];
580 } else if (on_right(map
->map
[c
])) {
581 right
+= v
->values
[c
];
594 *r
= right
/ n_right
;
597 float pa_cvolume_get_balance(const pa_cvolume
*v
, const pa_channel_map
*map
) {
598 pa_volume_t left
, right
;
603 pa_return_val_if_fail(pa_cvolume_valid(v
), 0.0f
);
604 pa_return_val_if_fail(pa_channel_map_valid(map
), 0.0f
);
605 pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(v
, map
), 0.0f
);
607 if (!pa_channel_map_can_balance(map
))
610 get_avg_lr(map
, v
, &left
, &right
);
625 return -1.0f
+ ((float) right
/ (float) left
);
627 return 1.0f
- ((float) left
/ (float) right
);
630 pa_cvolume
* pa_cvolume_set_balance(pa_cvolume
*v
, const pa_channel_map
*map
, float new_balance
) {
631 pa_volume_t left
, nleft
, right
, nright
, m
;
636 pa_assert(new_balance
>= -1.0f
);
637 pa_assert(new_balance
<= 1.0f
);
639 pa_return_val_if_fail(pa_cvolume_valid(v
), NULL
);
640 pa_return_val_if_fail(pa_channel_map_valid(map
), NULL
);
641 pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(v
, map
), NULL
);
643 if (!pa_channel_map_can_balance(map
))
646 get_avg_lr(map
, v
, &left
, &right
);
648 m
= PA_MAX(left
, right
);
650 if (new_balance
<= 0) {
651 nright
= (new_balance
+ 1.0f
) * m
;
654 nleft
= (1.0f
- new_balance
) * m
;
658 for (c
= 0; c
< map
->channels
; c
++) {
659 if (on_left(map
->map
[c
])) {
661 v
->values
[c
] = nleft
;
663 v
->values
[c
] = (pa_volume_t
) (((uint64_t) v
->values
[c
] * (uint64_t) nleft
) / (uint64_t) left
);
664 } else if (on_right(map
->map
[c
])) {
666 v
->values
[c
] = nright
;
668 v
->values
[c
] = (pa_volume_t
) (((uint64_t) v
->values
[c
] * (uint64_t) nright
) / (uint64_t) right
);
675 pa_cvolume
* pa_cvolume_scale(pa_cvolume
*v
, pa_volume_t max
) {
681 pa_return_val_if_fail(pa_cvolume_valid(v
), NULL
);
682 pa_return_val_if_fail(max
!= (pa_volume_t
) -1, NULL
);
684 t
= pa_cvolume_max(v
);
686 if (t
<= PA_VOLUME_MUTED
)
687 return pa_cvolume_set(v
, v
->channels
, max
);
689 for (c
= 0; c
< v
->channels
; c
++)
690 v
->values
[c
] = (pa_volume_t
) (((uint64_t) v
->values
[c
] * (uint64_t) max
) / (uint64_t) t
);
695 pa_cvolume
* pa_cvolume_scale_mask(pa_cvolume
*v
, pa_volume_t max
, pa_channel_map
*cm
, pa_channel_position_mask_t mask
) {
701 pa_return_val_if_fail(pa_cvolume_valid(v
), NULL
);
702 pa_return_val_if_fail(max
!= (pa_volume_t
) -1, NULL
);
704 t
= pa_cvolume_max_mask(v
, cm
, mask
);
706 if (t
<= PA_VOLUME_MUTED
)
707 return pa_cvolume_set(v
, v
->channels
, max
);
709 for (c
= 0; c
< v
->channels
; c
++)
710 v
->values
[c
] = (pa_volume_t
) (((uint64_t) v
->values
[c
] * (uint64_t) max
) / (uint64_t) t
);
715 static void get_avg_fr(const pa_channel_map
*map
, const pa_cvolume
*v
, pa_volume_t
*f
, pa_volume_t
*r
) {
717 pa_volume_t front
= 0, rear
= 0;
718 unsigned n_front
= 0, n_rear
= 0;
722 pa_assert(map
->channels
== v
->channels
);
726 for (c
= 0; c
< map
->channels
; c
++) {
727 if (on_front(map
->map
[c
])) {
728 front
+= v
->values
[c
];
730 } else if (on_rear(map
->map
[c
])) {
731 rear
+= v
->values
[c
];
739 *f
= front
/ n_front
;
747 float pa_cvolume_get_fade(const pa_cvolume
*v
, const pa_channel_map
*map
) {
748 pa_volume_t front
, rear
;
753 pa_return_val_if_fail(pa_cvolume_valid(v
), 0.0f
);
754 pa_return_val_if_fail(pa_channel_map_valid(map
), 0.0f
);
755 pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(v
, map
), 0.0f
);
757 if (!pa_channel_map_can_fade(map
))
760 get_avg_fr(map
, v
, &front
, &rear
);
766 return -1.0f
+ ((float) front
/ (float) rear
);
768 return 1.0f
- ((float) rear
/ (float) front
);
771 pa_cvolume
* pa_cvolume_set_fade(pa_cvolume
*v
, const pa_channel_map
*map
, float new_fade
) {
772 pa_volume_t front
, nfront
, rear
, nrear
, m
;
777 pa_assert(new_fade
>= -1.0f
);
778 pa_assert(new_fade
<= 1.0f
);
780 pa_return_val_if_fail(pa_cvolume_valid(v
), NULL
);
781 pa_return_val_if_fail(pa_channel_map_valid(map
), NULL
);
782 pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(v
, map
), NULL
);
784 if (!pa_channel_map_can_fade(map
))
787 get_avg_fr(map
, v
, &front
, &rear
);
789 m
= PA_MAX(front
, rear
);
792 nfront
= (new_fade
+ 1.0f
) * m
;
795 nrear
= (1.0f
- new_fade
) * m
;
799 for (c
= 0; c
< map
->channels
; c
++) {
800 if (on_front(map
->map
[c
])) {
802 v
->values
[c
] = nfront
;
804 v
->values
[c
] = (pa_volume_t
) (((uint64_t) v
->values
[c
] * (uint64_t) nfront
) / (uint64_t) front
);
805 } else if (on_rear(map
->map
[c
])) {
807 v
->values
[c
] = nrear
;
809 v
->values
[c
] = (pa_volume_t
) (((uint64_t) v
->values
[c
] * (uint64_t) nrear
) / (uint64_t) rear
);
816 pa_cvolume
* pa_cvolume_set_position(
818 const pa_channel_map
*map
,
819 pa_channel_position_t t
,
823 pa_bool_t good
= FALSE
;
828 pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(cv
, map
), NULL
);
829 pa_return_val_if_fail(t
< PA_CHANNEL_POSITION_MAX
, NULL
);
831 for (c
= 0; c
< map
->channels
; c
++)
832 if (map
->map
[c
] == t
) {
837 return good
? cv
: NULL
;
840 pa_volume_t
pa_cvolume_get_position(
842 const pa_channel_map
*map
,
843 pa_channel_position_t t
) {
846 pa_volume_t v
= PA_VOLUME_MUTED
;
851 pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(cv
, map
), PA_VOLUME_MUTED
);
852 pa_return_val_if_fail(t
< PA_CHANNEL_POSITION_MAX
, PA_VOLUME_MUTED
);
854 for (c
= 0; c
< map
->channels
; c
++)
855 if (map
->map
[c
] == t
)
856 if (cv
->values
[c
] > v
)
862 pa_cvolume
* pa_cvolume_merge(pa_cvolume
*dest
, const pa_cvolume
*a
, const pa_cvolume
*b
) {
869 pa_return_val_if_fail(pa_cvolume_valid(a
), NULL
);
870 pa_return_val_if_fail(pa_cvolume_valid(b
), NULL
);
872 for (i
= 0; i
< a
->channels
&& i
< b
->channels
; i
++)
873 dest
->values
[i
] = PA_MAX(a
->values
[i
], b
->values
[i
]);
875 dest
->channels
= (uint8_t) i
;