]> code.delx.au - pulseaudio/blob - src/pulse/volume.c
Merge branch 'master' of git://0pointer.de/pulseaudio into dbus-work
[pulseaudio] / src / pulse / volume.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
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.
10
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.
15
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 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <string.h>
28
29 #include <pulse/i18n.h>
30
31 #include <pulsecore/core-util.h>
32 #include <pulsecore/macro.h>
33 #include <pulsecore/sample-util.h>
34
35 #include "volume.h"
36
37 int pa_cvolume_equal(const pa_cvolume *a, const pa_cvolume *b) {
38 int i;
39 pa_assert(a);
40 pa_assert(b);
41
42 pa_return_val_if_fail(pa_cvolume_valid(a), 0);
43 pa_return_val_if_fail(pa_cvolume_valid(b), 0);
44
45 if (a->channels != b->channels)
46 return 0;
47
48 for (i = 0; i < a->channels; i++)
49 if (a->values[i] != b->values[i])
50 return 0;
51
52 return 1;
53 }
54
55 pa_cvolume* pa_cvolume_init(pa_cvolume *a) {
56 unsigned c;
57
58 pa_assert(a);
59
60 a->channels = 0;
61
62 for (c = 0; c < PA_CHANNELS_MAX; c++)
63 a->values[c] = (pa_volume_t) -1;
64
65 return a;
66 }
67
68 pa_cvolume* pa_cvolume_set(pa_cvolume *a, unsigned channels, pa_volume_t v) {
69 int i;
70
71 pa_assert(a);
72 pa_assert(channels > 0);
73 pa_assert(channels <= PA_CHANNELS_MAX);
74
75 a->channels = (uint8_t) channels;
76
77 for (i = 0; i < a->channels; i++)
78 a->values[i] = v;
79
80 return a;
81 }
82
83 pa_volume_t pa_cvolume_avg(const pa_cvolume *a) {
84 uint64_t sum = 0;
85 unsigned c;
86
87 pa_assert(a);
88 pa_return_val_if_fail(pa_cvolume_valid(a), PA_VOLUME_MUTED);
89
90 for (c = 0; c < a->channels; c++)
91 sum += a->values[c];
92
93 sum /= a->channels;
94
95 return (pa_volume_t) sum;
96 }
97
98 pa_volume_t pa_cvolume_avg_mask(const pa_cvolume *a, const pa_channel_map *cm, pa_channel_position_mask_t mask) {
99 uint64_t sum = 0;
100 unsigned c, n;
101
102 pa_assert(a);
103
104 if (!cm)
105 return pa_cvolume_avg(a);
106
107 pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(a, cm), PA_VOLUME_MUTED);
108
109 for (c = n = 0; c < a->channels; c++) {
110
111 if (!(PA_CHANNEL_POSITION_MASK(cm->map[c]) & mask))
112 continue;
113
114 sum += a->values[c];
115 n ++;
116 }
117
118 if (n > 0)
119 sum /= n;
120
121 return (pa_volume_t) sum;
122 }
123
124 pa_volume_t pa_cvolume_max(const pa_cvolume *a) {
125 pa_volume_t m = 0;
126 unsigned c;
127
128 pa_assert(a);
129 pa_return_val_if_fail(pa_cvolume_valid(a), PA_VOLUME_MUTED);
130
131 for (c = 0; c < a->channels; c++)
132 if (a->values[c] > m)
133 m = a->values[c];
134
135 return m;
136 }
137
138 pa_volume_t pa_cvolume_max_mask(const pa_cvolume *a, const pa_channel_map *cm, pa_channel_position_mask_t mask) {
139 pa_volume_t m = 0;
140 unsigned c, n;
141
142 pa_assert(a);
143
144 if (!cm)
145 return pa_cvolume_max(a);
146
147 pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(a, cm), PA_VOLUME_MUTED);
148
149 for (c = n = 0; c < a->channels; c++) {
150
151 if (!(PA_CHANNEL_POSITION_MASK(cm->map[c]) & mask))
152 continue;
153
154 if (a->values[c] > m)
155 m = a->values[c];
156 }
157
158 return m;
159 }
160
161 pa_volume_t pa_sw_volume_multiply(pa_volume_t a, pa_volume_t b) {
162 return pa_sw_volume_from_linear(pa_sw_volume_to_linear(a) * pa_sw_volume_to_linear(b));
163 }
164
165 pa_volume_t pa_sw_volume_divide(pa_volume_t a, pa_volume_t b) {
166 double v = pa_sw_volume_to_linear(b);
167
168 if (v <= 0)
169 return 0;
170
171 return pa_sw_volume_from_linear(pa_sw_volume_to_linear(a) / v);
172 }
173
174 /* Amplitude, not power */
175 static double linear_to_dB(double v) {
176 return 20.0 * log10(v);
177 }
178
179 static double dB_to_linear(double v) {
180 return pow(10.0, v / 20.0);
181 }
182
183 pa_volume_t pa_sw_volume_from_dB(double dB) {
184 if (isinf(dB) < 0 || dB <= PA_DECIBEL_MININFTY)
185 return PA_VOLUME_MUTED;
186
187 return pa_sw_volume_from_linear(dB_to_linear(dB));
188 }
189
190 double pa_sw_volume_to_dB(pa_volume_t v) {
191
192 if (v <= PA_VOLUME_MUTED)
193 return PA_DECIBEL_MININFTY;
194
195 return linear_to_dB(pa_sw_volume_to_linear(v));
196 }
197
198 pa_volume_t pa_sw_volume_from_linear(double v) {
199
200 if (v <= 0.0)
201 return PA_VOLUME_MUTED;
202
203 /*
204 * We use a cubic mapping here, as suggested and discussed here:
205 *
206 * http://www.robotplanet.dk/audio/audio_gui_design/
207 * http://lists.linuxaudio.org/pipermail/linux-audio-dev/2009-May/thread.html#23151
208 *
209 * We make sure that the conversion to linear and back yields the
210 * same volume value! That's why we need the lround() below!
211 */
212
213 return (pa_volume_t) lround(cbrt(v) * PA_VOLUME_NORM);
214 }
215
216 double pa_sw_volume_to_linear(pa_volume_t v) {
217 double f;
218
219 if (v <= PA_VOLUME_MUTED)
220 return 0.0;
221
222 if (v == PA_VOLUME_NORM)
223 return 1.0;
224
225 f = ((double) v / PA_VOLUME_NORM);
226
227 return f*f*f;
228 }
229
230 char *pa_cvolume_snprint(char *s, size_t l, const pa_cvolume *c) {
231 unsigned channel;
232 pa_bool_t first = TRUE;
233 char *e;
234
235 pa_assert(s);
236 pa_assert(l > 0);
237 pa_assert(c);
238
239 pa_init_i18n();
240
241 if (!pa_cvolume_valid(c)) {
242 pa_snprintf(s, l, _("(invalid)"));
243 return s;
244 }
245
246 *(e = s) = 0;
247
248 for (channel = 0; channel < c->channels && l > 1; channel++) {
249 l -= pa_snprintf(e, l, "%s%u: %3u%%",
250 first ? "" : " ",
251 channel,
252 (c->values[channel]*100)/PA_VOLUME_NORM);
253
254 e = strchr(e, 0);
255 first = FALSE;
256 }
257
258 return s;
259 }
260
261 char *pa_volume_snprint(char *s, size_t l, pa_volume_t v) {
262 pa_assert(s);
263 pa_assert(l > 0);
264
265 pa_init_i18n();
266
267 if (v == (pa_volume_t) -1) {
268 pa_snprintf(s, l, _("(invalid)"));
269 return s;
270 }
271
272 pa_snprintf(s, l, "%3u%%", (v*100)/PA_VOLUME_NORM);
273 return s;
274 }
275
276 char *pa_sw_cvolume_snprint_dB(char *s, size_t l, const pa_cvolume *c) {
277 unsigned channel;
278 pa_bool_t first = TRUE;
279 char *e;
280
281 pa_assert(s);
282 pa_assert(l > 0);
283 pa_assert(c);
284
285 pa_init_i18n();
286
287 if (!pa_cvolume_valid(c)) {
288 pa_snprintf(s, l, _("(invalid)"));
289 return s;
290 }
291
292 *(e = s) = 0;
293
294 for (channel = 0; channel < c->channels && l > 1; channel++) {
295 double f = pa_sw_volume_to_dB(c->values[channel]);
296
297 l -= pa_snprintf(e, l, "%s%u: %0.2f dB",
298 first ? "" : " ",
299 channel,
300 isinf(f) < 0 || f <= PA_DECIBEL_MININFTY ? -INFINITY : f);
301
302 e = strchr(e, 0);
303 first = FALSE;
304 }
305
306 return s;
307 }
308
309 char *pa_sw_volume_snprint_dB(char *s, size_t l, pa_volume_t v) {
310 double f;
311
312 pa_assert(s);
313 pa_assert(l > 0);
314
315 pa_init_i18n();
316
317 if (v == (pa_volume_t) -1) {
318 pa_snprintf(s, l, _("(invalid)"));
319 return s;
320 }
321
322 f = pa_sw_volume_to_dB(v);
323 pa_snprintf(s, l, "%0.2f dB",
324 isinf(f) < 0 || f <= PA_DECIBEL_MININFTY ? -INFINITY : f);
325
326 return s;
327 }
328
329 int pa_cvolume_channels_equal_to(const pa_cvolume *a, pa_volume_t v) {
330 unsigned c;
331 pa_assert(a);
332
333 pa_return_val_if_fail(pa_cvolume_valid(a), 0);
334
335 for (c = 0; c < a->channels; c++)
336 if (a->values[c] != v)
337 return 0;
338
339 return 1;
340 }
341
342 pa_cvolume *pa_sw_cvolume_multiply(pa_cvolume *dest, const pa_cvolume *a, const pa_cvolume *b) {
343 unsigned i;
344
345 pa_assert(dest);
346 pa_assert(a);
347 pa_assert(b);
348
349 pa_return_val_if_fail(pa_cvolume_valid(a), NULL);
350 pa_return_val_if_fail(pa_cvolume_valid(b), NULL);
351
352 for (i = 0; i < a->channels && i < b->channels; i++)
353 dest->values[i] = pa_sw_volume_multiply(a->values[i], b->values[i]);
354
355 dest->channels = (uint8_t) i;
356
357 return dest;
358 }
359
360 pa_cvolume *pa_sw_cvolume_multiply_scalar(pa_cvolume *dest, const pa_cvolume *a, pa_volume_t b) {
361 unsigned i;
362
363 pa_assert(dest);
364 pa_assert(a);
365
366 pa_return_val_if_fail(pa_cvolume_valid(a), NULL);
367
368 for (i = 0; i < a->channels; i++)
369 dest->values[i] = pa_sw_volume_multiply(a->values[i], b);
370
371 dest->channels = (uint8_t) i;
372
373 return dest;
374 }
375
376 pa_cvolume *pa_sw_cvolume_divide(pa_cvolume *dest, const pa_cvolume *a, const pa_cvolume *b) {
377 unsigned i;
378
379 pa_assert(dest);
380 pa_assert(a);
381 pa_assert(b);
382
383 pa_return_val_if_fail(pa_cvolume_valid(a), NULL);
384 pa_return_val_if_fail(pa_cvolume_valid(b), NULL);
385
386 for (i = 0; i < a->channels && i < b->channels; i++)
387 dest->values[i] = pa_sw_volume_divide(a->values[i], b->values[i]);
388
389 dest->channels = (uint8_t) i;
390
391 return dest;
392 }
393
394 pa_cvolume *pa_sw_cvolume_divide_scalar(pa_cvolume *dest, const pa_cvolume *a, pa_volume_t b) {
395 unsigned i;
396
397 pa_assert(dest);
398 pa_assert(a);
399
400 pa_return_val_if_fail(pa_cvolume_valid(a), NULL);
401
402 for (i = 0; i < a->channels; i++)
403 dest->values[i] = pa_sw_volume_divide(a->values[i], b);
404
405 dest->channels = (uint8_t) i;
406
407 return dest;
408 }
409
410 int pa_cvolume_valid(const pa_cvolume *v) {
411 unsigned c;
412
413 pa_assert(v);
414
415 if (v->channels <= 0 || v->channels > PA_CHANNELS_MAX)
416 return 0;
417
418 for (c = 0; c < v->channels; c++)
419 if (v->values[c] == (pa_volume_t) -1)
420 return 0;
421
422 return 1;
423 }
424
425 static pa_bool_t on_left(pa_channel_position_t p) {
426 return !!(PA_CHANNEL_POSITION_MASK(p) & PA_CHANNEL_POSITION_MASK_LEFT);
427 }
428
429 static pa_bool_t on_right(pa_channel_position_t p) {
430 return !!(PA_CHANNEL_POSITION_MASK(p) & PA_CHANNEL_POSITION_MASK_RIGHT);
431 }
432
433 static pa_bool_t on_center(pa_channel_position_t p) {
434 return !!(PA_CHANNEL_POSITION_MASK(p) & PA_CHANNEL_POSITION_MASK_CENTER);
435 }
436
437 static pa_bool_t on_lfe(pa_channel_position_t p) {
438 return p == PA_CHANNEL_POSITION_LFE;
439 }
440
441 static pa_bool_t on_front(pa_channel_position_t p) {
442 return !!(PA_CHANNEL_POSITION_MASK(p) & PA_CHANNEL_POSITION_MASK_FRONT);
443 }
444
445 static pa_bool_t on_rear(pa_channel_position_t p) {
446 return !!(PA_CHANNEL_POSITION_MASK(p) & PA_CHANNEL_POSITION_MASK_REAR);
447 }
448
449 pa_cvolume *pa_cvolume_remap(pa_cvolume *v, const pa_channel_map *from, const pa_channel_map *to) {
450 int a, b;
451 pa_cvolume result;
452
453 pa_assert(v);
454 pa_assert(from);
455 pa_assert(to);
456
457 pa_return_val_if_fail(pa_cvolume_valid(v), NULL);
458 pa_return_val_if_fail(pa_channel_map_valid(from), NULL);
459 pa_return_val_if_fail(pa_channel_map_valid(to), NULL);
460 pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(v, from), NULL);
461
462 if (pa_channel_map_equal(from, to))
463 return v;
464
465 result.channels = to->channels;
466
467 for (b = 0; b < to->channels; b++) {
468 pa_volume_t k = 0;
469 int n = 0;
470
471 for (a = 0; a < from->channels; a++)
472 if (from->map[a] == to->map[b]) {
473 k += v->values[a];
474 n ++;
475 }
476
477 if (n <= 0) {
478 for (a = 0; a < from->channels; a++)
479 if ((on_left(from->map[a]) && on_left(to->map[b])) ||
480 (on_right(from->map[a]) && on_right(to->map[b])) ||
481 (on_center(from->map[a]) && on_center(to->map[b])) ||
482 (on_lfe(from->map[a]) && on_lfe(to->map[b]))) {
483
484 k += v->values[a];
485 n ++;
486 }
487 }
488
489 if (n <= 0)
490 k = pa_cvolume_avg(v);
491 else
492 k /= n;
493
494 result.values[b] = k;
495 }
496
497 *v = result;
498 return v;
499 }
500
501 int pa_cvolume_compatible(const pa_cvolume *v, const pa_sample_spec *ss) {
502
503 pa_assert(v);
504 pa_assert(ss);
505
506 pa_return_val_if_fail(pa_cvolume_valid(v), 0);
507 pa_return_val_if_fail(pa_sample_spec_valid(ss), 0);
508
509 return v->channels == ss->channels;
510 }
511
512 int pa_cvolume_compatible_with_channel_map(const pa_cvolume *v, const pa_channel_map *cm) {
513 pa_assert(v);
514 pa_assert(cm);
515
516 pa_return_val_if_fail(pa_cvolume_valid(v), 0);
517 pa_return_val_if_fail(pa_channel_map_valid(cm), 0);
518
519 return v->channels == cm->channels;
520 }
521
522 static void get_avg_lr(const pa_channel_map *map, const pa_cvolume *v, pa_volume_t *l, pa_volume_t *r) {
523 int c;
524 pa_volume_t left = 0, right = 0;
525 unsigned n_left = 0, n_right = 0;
526
527 pa_assert(v);
528 pa_assert(map);
529 pa_assert(map->channels == v->channels);
530 pa_assert(l);
531 pa_assert(r);
532
533 for (c = 0; c < map->channels; c++) {
534 if (on_left(map->map[c])) {
535 left += v->values[c];
536 n_left++;
537 } else if (on_right(map->map[c])) {
538 right += v->values[c];
539 n_right++;
540 }
541 }
542
543 if (n_left <= 0)
544 *l = PA_VOLUME_NORM;
545 else
546 *l = left / n_left;
547
548 if (n_right <= 0)
549 *r = PA_VOLUME_NORM;
550 else
551 *r = right / n_right;
552 }
553
554 float pa_cvolume_get_balance(const pa_cvolume *v, const pa_channel_map *map) {
555 pa_volume_t left, right;
556
557 pa_assert(v);
558 pa_assert(map);
559
560 pa_return_val_if_fail(pa_cvolume_valid(v), 0.0f);
561 pa_return_val_if_fail(pa_channel_map_valid(map), 0.0f);
562 pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(v, map), 0.0f);
563
564 if (!pa_channel_map_can_balance(map))
565 return 0.0f;
566
567 get_avg_lr(map, v, &left, &right);
568
569 if (left == right)
570 return 0.0f;
571
572 /* 1.0, 0.0 => -1.0
573 0.0, 1.0 => 1.0
574 0.0, 0.0 => 0.0
575 0.5, 0.5 => 0.0
576 1.0, 0.5 => -0.5
577 1.0, 0.25 => -0.75
578 0.75, 0.25 => -0.66
579 0.5, 0.25 => -0.5 */
580
581 if (left > right)
582 return -1.0f + ((float) right / (float) left);
583 else
584 return 1.0f - ((float) left / (float) right);
585 }
586
587 pa_cvolume* pa_cvolume_set_balance(pa_cvolume *v, const pa_channel_map *map, float new_balance) {
588 pa_volume_t left, nleft, right, nright, m;
589 unsigned c;
590
591 pa_assert(map);
592 pa_assert(v);
593 pa_assert(new_balance >= -1.0f);
594 pa_assert(new_balance <= 1.0f);
595
596 pa_return_val_if_fail(pa_cvolume_valid(v), NULL);
597 pa_return_val_if_fail(pa_channel_map_valid(map), NULL);
598 pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(v, map), NULL);
599
600 if (!pa_channel_map_can_balance(map))
601 return v;
602
603 get_avg_lr(map, v, &left, &right);
604
605 m = PA_MAX(left, right);
606
607 if (new_balance <= 0) {
608 nright = (new_balance + 1.0f) * m;
609 nleft = m;
610 } else {
611 nleft = (1.0f - new_balance) * m;
612 nright = m;
613 }
614
615 for (c = 0; c < map->channels; c++) {
616 if (on_left(map->map[c])) {
617 if (left == 0)
618 v->values[c] = nleft;
619 else
620 v->values[c] = (pa_volume_t) (((uint64_t) v->values[c] * (uint64_t) nleft) / (uint64_t) left);
621 } else if (on_right(map->map[c])) {
622 if (right == 0)
623 v->values[c] = nright;
624 else
625 v->values[c] = (pa_volume_t) (((uint64_t) v->values[c] * (uint64_t) nright) / (uint64_t) right);
626 }
627 }
628
629 return v;
630 }
631
632 pa_cvolume* pa_cvolume_scale(pa_cvolume *v, pa_volume_t max) {
633 unsigned c;
634 pa_volume_t t = 0;
635
636 pa_assert(v);
637
638 pa_return_val_if_fail(pa_cvolume_valid(v), NULL);
639 pa_return_val_if_fail(max != (pa_volume_t) -1, NULL);
640
641 t = pa_cvolume_max(v);
642
643 if (t <= PA_VOLUME_MUTED)
644 return pa_cvolume_set(v, v->channels, max);
645
646 for (c = 0; c < v->channels; c++)
647 v->values[c] = (pa_volume_t) (((uint64_t) v->values[c] * (uint64_t) max) / (uint64_t) t);
648
649 return v;
650 }
651
652 pa_cvolume* pa_cvolume_scale_mask(pa_cvolume *v, pa_volume_t max, pa_channel_map *cm, pa_channel_position_mask_t mask) {
653 unsigned c;
654 pa_volume_t t = 0;
655
656 pa_assert(v);
657
658 pa_return_val_if_fail(pa_cvolume_valid(v), NULL);
659 pa_return_val_if_fail(max != (pa_volume_t) -1, NULL);
660
661 t = pa_cvolume_max_mask(v, cm, mask);
662
663 if (t <= PA_VOLUME_MUTED)
664 return pa_cvolume_set(v, v->channels, max);
665
666 for (c = 0; c < v->channels; c++)
667 v->values[c] = (pa_volume_t) (((uint64_t) v->values[c] * (uint64_t) max) / (uint64_t) t);
668
669 return v;
670 }
671
672 static void get_avg_fr(const pa_channel_map *map, const pa_cvolume *v, pa_volume_t *f, pa_volume_t *r) {
673 int c;
674 pa_volume_t front = 0, rear = 0;
675 unsigned n_front = 0, n_rear = 0;
676
677 pa_assert(v);
678 pa_assert(map);
679 pa_assert(map->channels == v->channels);
680 pa_assert(f);
681 pa_assert(r);
682
683 for (c = 0; c < map->channels; c++) {
684 if (on_front(map->map[c])) {
685 front += v->values[c];
686 n_front++;
687 } else if (on_rear(map->map[c])) {
688 rear += v->values[c];
689 n_rear++;
690 }
691 }
692
693 if (n_front <= 0)
694 *f = PA_VOLUME_NORM;
695 else
696 *f = front / n_front;
697
698 if (n_rear <= 0)
699 *r = PA_VOLUME_NORM;
700 else
701 *r = rear / n_rear;
702 }
703
704 float pa_cvolume_get_fade(const pa_cvolume *v, const pa_channel_map *map) {
705 pa_volume_t front, rear;
706
707 pa_assert(v);
708 pa_assert(map);
709
710 pa_return_val_if_fail(pa_cvolume_valid(v), 0.0f);
711 pa_return_val_if_fail(pa_channel_map_valid(map), 0.0f);
712 pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(v, map), 0.0f);
713
714 if (!pa_channel_map_can_fade(map))
715 return 0.0f;
716
717 get_avg_fr(map, v, &front, &rear);
718
719 if (front == rear)
720 return 0.0f;
721
722 if (rear > front)
723 return -1.0f + ((float) front / (float) rear);
724 else
725 return 1.0f - ((float) rear / (float) front);
726 }
727
728 pa_cvolume* pa_cvolume_set_fade(pa_cvolume *v, const pa_channel_map *map, float new_fade) {
729 pa_volume_t front, nfront, rear, nrear, m;
730 unsigned c;
731
732 pa_assert(map);
733 pa_assert(v);
734 pa_assert(new_fade >= -1.0f);
735 pa_assert(new_fade <= 1.0f);
736
737 pa_return_val_if_fail(pa_cvolume_valid(v), NULL);
738 pa_return_val_if_fail(pa_channel_map_valid(map), NULL);
739 pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(v, map), NULL);
740
741 if (!pa_channel_map_can_fade(map))
742 return v;
743
744 get_avg_fr(map, v, &front, &rear);
745
746 m = PA_MAX(front, rear);
747
748 if (new_fade <= 0) {
749 nfront = (new_fade + 1.0f) * m;
750 nrear = m;
751 } else {
752 nrear = (1.0f - new_fade) * m;
753 nfront = m;
754 }
755
756 for (c = 0; c < map->channels; c++) {
757 if (on_front(map->map[c])) {
758 if (front == 0)
759 v->values[c] = nfront;
760 else
761 v->values[c] = (pa_volume_t) (((uint64_t) v->values[c] * (uint64_t) nfront) / (uint64_t) front);
762 } else if (on_rear(map->map[c])) {
763 if (rear == 0)
764 v->values[c] = nrear;
765 else
766 v->values[c] = (pa_volume_t) (((uint64_t) v->values[c] * (uint64_t) nrear) / (uint64_t) rear);
767 }
768 }
769
770 return v;
771 }
772
773 pa_cvolume* pa_cvolume_set_position(
774 pa_cvolume *cv,
775 const pa_channel_map *map,
776 pa_channel_position_t t,
777 pa_volume_t v) {
778
779 unsigned c;
780 pa_bool_t good = FALSE;
781
782 pa_assert(cv);
783 pa_assert(map);
784
785 pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(cv, map), NULL);
786 pa_return_val_if_fail(t < PA_CHANNEL_POSITION_MAX, NULL);
787
788 for (c = 0; c < map->channels; c++)
789 if (map->map[c] == t) {
790 cv->values[c] = v;
791 good = TRUE;
792 }
793
794 return good ? cv : NULL;
795 }
796
797 pa_volume_t pa_cvolume_get_position(
798 pa_cvolume *cv,
799 const pa_channel_map *map,
800 pa_channel_position_t t) {
801
802 unsigned c;
803 pa_volume_t v = PA_VOLUME_MUTED;
804
805 pa_assert(cv);
806 pa_assert(map);
807
808 pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(cv, map), PA_VOLUME_MUTED);
809 pa_return_val_if_fail(t < PA_CHANNEL_POSITION_MAX, PA_VOLUME_MUTED);
810
811 for (c = 0; c < map->channels; c++)
812 if (map->map[c] == t)
813 if (cv->values[c] > v)
814 v = cv->values[c];
815
816 return v;
817 }