volume: simplify volume multiplifactions, do them in integer only
[pulseaudio-mirror.git] / src / pulse / volume.c
blob0d402371f5dfc6eb8477fa2bf584e43b36499b81
1 /***
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
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <stdio.h>
27 #include <string.h>
29 #include <pulse/i18n.h>
31 #include <pulsecore/core-util.h>
32 #include <pulsecore/macro.h>
33 #include <pulsecore/sample-util.h>
35 #include "volume.h"
37 int pa_cvolume_equal(const pa_cvolume *a, const pa_cvolume *b) {
38 int i;
39 pa_assert(a);
40 pa_assert(b);
42 pa_return_val_if_fail(pa_cvolume_valid(a), 0);
44 if (PA_UNLIKELY(a == b))
45 return 1;
47 pa_return_val_if_fail(pa_cvolume_valid(b), 0);
49 if (a->channels != b->channels)
50 return 0;
52 for (i = 0; i < a->channels; i++)
53 if (a->values[i] != b->values[i])
54 return 0;
56 return 1;
59 pa_cvolume* pa_cvolume_init(pa_cvolume *a) {
60 unsigned c;
62 pa_assert(a);
64 a->channels = 0;
66 for (c = 0; c < PA_CHANNELS_MAX; c++)
67 a->values[c] = (pa_volume_t) -1;
69 return a;
72 pa_cvolume* pa_cvolume_set(pa_cvolume *a, unsigned channels, pa_volume_t v) {
73 int i;
75 pa_assert(a);
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++)
82 a->values[i] = v;
84 return a;
87 pa_volume_t pa_cvolume_avg(const pa_cvolume *a) {
88 uint64_t sum = 0;
89 unsigned c;
91 pa_assert(a);
92 pa_return_val_if_fail(pa_cvolume_valid(a), PA_VOLUME_MUTED);
94 for (c = 0; c < a->channels; c++)
95 sum += a->values[c];
97 sum /= a->channels;
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) {
103 uint64_t sum = 0;
104 unsigned c, n;
106 pa_assert(a);
108 if (!cm)
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))
116 continue;
118 sum += a->values[c];
119 n ++;
122 if (n > 0)
123 sum /= n;
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;
130 unsigned c;
132 pa_assert(a);
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)
137 m = a->values[c];
139 return m;
142 pa_volume_t pa_cvolume_min(const pa_cvolume *a) {
143 pa_volume_t m = (pa_volume_t) -1;
144 unsigned c;
146 pa_assert(a);
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)
151 m = a->values[c];
153 return 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;
158 unsigned c, n;
160 pa_assert(a);
162 if (!cm)
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))
170 continue;
172 if (a->values[c] > m)
173 m = a->values[c];
176 return 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;
181 unsigned c, n;
183 pa_assert(a);
185 if (!cm)
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))
193 continue;
195 if (m == (pa_volume_t) -1 || a->values[c] < m)
196 m = a->values[c];
199 return 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)
212 return 0;
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) {
243 if (v <= 0.0)
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) {
260 double f;
262 if (v <= PA_VOLUME_MUTED)
263 return 0.0;
265 if (v == PA_VOLUME_NORM)
266 return 1.0;
268 f = ((double) v / PA_VOLUME_NORM);
270 return f*f*f;
273 char *pa_cvolume_snprint(char *s, size_t l, const pa_cvolume *c) {
274 unsigned channel;
275 pa_bool_t first = TRUE;
276 char *e;
278 pa_assert(s);
279 pa_assert(l > 0);
280 pa_assert(c);
282 pa_init_i18n();
284 if (!pa_cvolume_valid(c)) {
285 pa_snprintf(s, l, _("(invalid)"));
286 return s;
289 *(e = s) = 0;
291 for (channel = 0; channel < c->channels && l > 1; channel++) {
292 l -= pa_snprintf(e, l, "%s%u: %3u%%",
293 first ? "" : " ",
294 channel,
295 (c->values[channel]*100)/PA_VOLUME_NORM);
297 e = strchr(e, 0);
298 first = FALSE;
301 return s;
304 char *pa_volume_snprint(char *s, size_t l, pa_volume_t v) {
305 pa_assert(s);
306 pa_assert(l > 0);
308 pa_init_i18n();
310 if (v == (pa_volume_t) -1) {
311 pa_snprintf(s, l, _("(invalid)"));
312 return s;
315 pa_snprintf(s, l, "%3u%%", (v*100)/PA_VOLUME_NORM);
316 return s;
319 char *pa_sw_cvolume_snprint_dB(char *s, size_t l, const pa_cvolume *c) {
320 unsigned channel;
321 pa_bool_t first = TRUE;
322 char *e;
324 pa_assert(s);
325 pa_assert(l > 0);
326 pa_assert(c);
328 pa_init_i18n();
330 if (!pa_cvolume_valid(c)) {
331 pa_snprintf(s, l, _("(invalid)"));
332 return s;
335 *(e = s) = 0;
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",
341 first ? "" : " ",
342 channel,
343 isinf(f) < 0 || f <= PA_DECIBEL_MININFTY ? -INFINITY : f);
345 e = strchr(e, 0);
346 first = FALSE;
349 return s;
352 char *pa_sw_volume_snprint_dB(char *s, size_t l, pa_volume_t v) {
353 double f;
355 pa_assert(s);
356 pa_assert(l > 0);
358 pa_init_i18n();
360 if (v == (pa_volume_t) -1) {
361 pa_snprintf(s, l, _("(invalid)"));
362 return s;
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);
369 return s;
372 int pa_cvolume_channels_equal_to(const pa_cvolume *a, pa_volume_t v) {
373 unsigned c;
374 pa_assert(a);
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)
380 return 0;
382 return 1;
385 pa_cvolume *pa_sw_cvolume_multiply(pa_cvolume *dest, const pa_cvolume *a, const pa_cvolume *b) {
386 unsigned i;
388 pa_assert(dest);
389 pa_assert(a);
390 pa_assert(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;
400 return dest;
403 pa_cvolume *pa_sw_cvolume_multiply_scalar(pa_cvolume *dest, const pa_cvolume *a, pa_volume_t b) {
404 unsigned i;
406 pa_assert(dest);
407 pa_assert(a);
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;
416 return dest;
419 pa_cvolume *pa_sw_cvolume_divide(pa_cvolume *dest, const pa_cvolume *a, const pa_cvolume *b) {
420 unsigned i;
422 pa_assert(dest);
423 pa_assert(a);
424 pa_assert(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;
434 return dest;
437 pa_cvolume *pa_sw_cvolume_divide_scalar(pa_cvolume *dest, const pa_cvolume *a, pa_volume_t b) {
438 unsigned i;
440 pa_assert(dest);
441 pa_assert(a);
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;
450 return dest;
453 int pa_cvolume_valid(const pa_cvolume *v) {
454 unsigned c;
456 pa_assert(v);
458 if (v->channels <= 0 || v->channels > PA_CHANNELS_MAX)
459 return 0;
461 for (c = 0; c < v->channels; c++)
462 if (v->values[c] == (pa_volume_t) -1)
463 return 0;
465 return 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) {
493 int a, b;
494 pa_cvolume result;
496 pa_assert(v);
497 pa_assert(from);
498 pa_assert(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))
506 return v;
508 result.channels = to->channels;
510 for (b = 0; b < to->channels; b++) {
511 pa_volume_t k = 0;
512 int n = 0;
514 for (a = 0; a < from->channels; a++)
515 if (from->map[a] == to->map[b]) {
516 k += v->values[a];
517 n ++;
520 if (n <= 0) {
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]))) {
527 k += v->values[a];
528 n ++;
532 if (n <= 0)
533 k = pa_cvolume_avg(v);
534 else
535 k /= n;
537 result.values[b] = k;
540 *v = result;
541 return v;
544 int pa_cvolume_compatible(const pa_cvolume *v, const pa_sample_spec *ss) {
546 pa_assert(v);
547 pa_assert(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) {
556 pa_assert(v);
557 pa_assert(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) {
566 int c;
567 pa_volume_t left = 0, right = 0;
568 unsigned n_left = 0, n_right = 0;
570 pa_assert(v);
571 pa_assert(map);
572 pa_assert(map->channels == v->channels);
573 pa_assert(l);
574 pa_assert(r);
576 for (c = 0; c < map->channels; c++) {
577 if (on_left(map->map[c])) {
578 left += v->values[c];
579 n_left++;
580 } else if (on_right(map->map[c])) {
581 right += v->values[c];
582 n_right++;
586 if (n_left <= 0)
587 *l = PA_VOLUME_NORM;
588 else
589 *l = left / n_left;
591 if (n_right <= 0)
592 *r = PA_VOLUME_NORM;
593 else
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;
600 pa_assert(v);
601 pa_assert(map);
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))
608 return 0.0f;
610 get_avg_lr(map, v, &left, &right);
612 if (left == right)
613 return 0.0f;
615 /* 1.0, 0.0 => -1.0
616 0.0, 1.0 => 1.0
617 0.0, 0.0 => 0.0
618 0.5, 0.5 => 0.0
619 1.0, 0.5 => -0.5
620 1.0, 0.25 => -0.75
621 0.75, 0.25 => -0.66
622 0.5, 0.25 => -0.5 */
624 if (left > right)
625 return -1.0f + ((float) right / (float) left);
626 else
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;
632 unsigned c;
634 pa_assert(map);
635 pa_assert(v);
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))
644 return v;
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;
652 nleft = m;
653 } else {
654 nleft = (1.0f - new_balance) * m;
655 nright = m;
658 for (c = 0; c < map->channels; c++) {
659 if (on_left(map->map[c])) {
660 if (left == 0)
661 v->values[c] = nleft;
662 else
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])) {
665 if (right == 0)
666 v->values[c] = nright;
667 else
668 v->values[c] = (pa_volume_t) (((uint64_t) v->values[c] * (uint64_t) nright) / (uint64_t) right);
672 return v;
675 pa_cvolume* pa_cvolume_scale(pa_cvolume *v, pa_volume_t max) {
676 unsigned c;
677 pa_volume_t t = 0;
679 pa_assert(v);
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);
692 return v;
695 pa_cvolume* pa_cvolume_scale_mask(pa_cvolume *v, pa_volume_t max, pa_channel_map *cm, pa_channel_position_mask_t mask) {
696 unsigned c;
697 pa_volume_t t = 0;
699 pa_assert(v);
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);
712 return v;
715 static void get_avg_fr(const pa_channel_map *map, const pa_cvolume *v, pa_volume_t *f, pa_volume_t *r) {
716 int c;
717 pa_volume_t front = 0, rear = 0;
718 unsigned n_front = 0, n_rear = 0;
720 pa_assert(v);
721 pa_assert(map);
722 pa_assert(map->channels == v->channels);
723 pa_assert(f);
724 pa_assert(r);
726 for (c = 0; c < map->channels; c++) {
727 if (on_front(map->map[c])) {
728 front += v->values[c];
729 n_front++;
730 } else if (on_rear(map->map[c])) {
731 rear += v->values[c];
732 n_rear++;
736 if (n_front <= 0)
737 *f = PA_VOLUME_NORM;
738 else
739 *f = front / n_front;
741 if (n_rear <= 0)
742 *r = PA_VOLUME_NORM;
743 else
744 *r = rear / n_rear;
747 float pa_cvolume_get_fade(const pa_cvolume *v, const pa_channel_map *map) {
748 pa_volume_t front, rear;
750 pa_assert(v);
751 pa_assert(map);
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))
758 return 0.0f;
760 get_avg_fr(map, v, &front, &rear);
762 if (front == rear)
763 return 0.0f;
765 if (rear > front)
766 return -1.0f + ((float) front / (float) rear);
767 else
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;
773 unsigned c;
775 pa_assert(map);
776 pa_assert(v);
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))
785 return v;
787 get_avg_fr(map, v, &front, &rear);
789 m = PA_MAX(front, rear);
791 if (new_fade <= 0) {
792 nfront = (new_fade + 1.0f) * m;
793 nrear = m;
794 } else {
795 nrear = (1.0f - new_fade) * m;
796 nfront = m;
799 for (c = 0; c < map->channels; c++) {
800 if (on_front(map->map[c])) {
801 if (front == 0)
802 v->values[c] = nfront;
803 else
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])) {
806 if (rear == 0)
807 v->values[c] = nrear;
808 else
809 v->values[c] = (pa_volume_t) (((uint64_t) v->values[c] * (uint64_t) nrear) / (uint64_t) rear);
813 return v;
816 pa_cvolume* pa_cvolume_set_position(
817 pa_cvolume *cv,
818 const pa_channel_map *map,
819 pa_channel_position_t t,
820 pa_volume_t v) {
822 unsigned c;
823 pa_bool_t good = FALSE;
825 pa_assert(cv);
826 pa_assert(map);
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) {
833 cv->values[c] = v;
834 good = TRUE;
837 return good ? cv : NULL;
840 pa_volume_t pa_cvolume_get_position(
841 pa_cvolume *cv,
842 const pa_channel_map *map,
843 pa_channel_position_t t) {
845 unsigned c;
846 pa_volume_t v = PA_VOLUME_MUTED;
848 pa_assert(cv);
849 pa_assert(map);
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)
857 v = cv->values[c];
859 return v;
862 pa_cvolume* pa_cvolume_merge(pa_cvolume *dest, const pa_cvolume *a, const pa_cvolume *b) {
863 unsigned i;
865 pa_assert(dest);
866 pa_assert(a);
867 pa_assert(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;
877 return dest;