volume ramp: additions to the low level infra
[platform/upstream/pulseaudio.git] / 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 #include <math.h>
29
30 #include <pulsecore/core-util.h>
31 #include <pulsecore/i18n.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
44     if (PA_UNLIKELY(a == b))
45         return 1;
46
47     pa_return_val_if_fail(pa_cvolume_valid(b), 0);
48
49     if (a->channels != b->channels)
50         return 0;
51
52     for (i = 0; i < a->channels; i++)
53         if (a->values[i] != b->values[i])
54             return 0;
55
56     return 1;
57 }
58
59 pa_cvolume* pa_cvolume_init(pa_cvolume *a) {
60     unsigned c;
61
62     pa_assert(a);
63
64     a->channels = 0;
65
66     for (c = 0; c < PA_CHANNELS_MAX; c++)
67         a->values[c] = PA_VOLUME_INVALID;
68
69     return a;
70 }
71
72 pa_cvolume* pa_cvolume_set(pa_cvolume *a, unsigned channels, pa_volume_t v) {
73     int i;
74
75     pa_assert(a);
76     pa_assert(pa_channels_valid(channels));
77
78     a->channels = (uint8_t) channels;
79
80     for (i = 0; i < a->channels; i++)
81         /* Clamp in case there is stale data that exceeds the current
82          * PA_VOLUME_MAX */
83         a->values[i] = PA_CLAMP_VOLUME(v);
84
85     return a;
86 }
87
88 pa_volume_t pa_cvolume_avg(const pa_cvolume *a) {
89     uint64_t sum = 0;
90     unsigned c;
91
92     pa_assert(a);
93     pa_return_val_if_fail(pa_cvolume_valid(a), PA_VOLUME_MUTED);
94
95     for (c = 0; c < a->channels; c++)
96         sum += a->values[c];
97
98     sum /= a->channels;
99
100     return (pa_volume_t) sum;
101 }
102
103 pa_volume_t pa_cvolume_avg_mask(const pa_cvolume *a, const pa_channel_map *cm, pa_channel_position_mask_t mask) {
104     uint64_t sum = 0;
105     unsigned c, n;
106
107     pa_assert(a);
108
109     if (!cm)
110         return pa_cvolume_avg(a);
111
112     pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(a, cm), PA_VOLUME_MUTED);
113
114     for (c = n = 0; c < a->channels; c++) {
115
116         if (!(PA_CHANNEL_POSITION_MASK(cm->map[c]) & mask))
117             continue;
118
119         sum += a->values[c];
120         n ++;
121     }
122
123     if (n > 0)
124         sum /= n;
125
126     return (pa_volume_t) sum;
127 }
128
129 pa_volume_t pa_cvolume_max(const pa_cvolume *a) {
130     pa_volume_t m = PA_VOLUME_MUTED;
131     unsigned c;
132
133     pa_assert(a);
134     pa_return_val_if_fail(pa_cvolume_valid(a), PA_VOLUME_MUTED);
135
136     for (c = 0; c < a->channels; c++)
137         if (a->values[c] > m)
138             m = a->values[c];
139
140     return m;
141 }
142
143 pa_volume_t pa_cvolume_min(const pa_cvolume *a) {
144     pa_volume_t m = PA_VOLUME_MAX;
145     unsigned c;
146
147     pa_assert(a);
148     pa_return_val_if_fail(pa_cvolume_valid(a), PA_VOLUME_MUTED);
149
150     for (c = 0; c < a->channels; c++)
151         if (a->values[c] < m)
152             m = a->values[c];
153
154     return m;
155 }
156
157 pa_volume_t pa_cvolume_max_mask(const pa_cvolume *a, const pa_channel_map *cm, pa_channel_position_mask_t mask) {
158     pa_volume_t m = PA_VOLUME_MUTED;
159     unsigned c;
160
161     pa_assert(a);
162
163     if (!cm)
164         return pa_cvolume_max(a);
165
166     pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(a, cm), PA_VOLUME_MUTED);
167
168     for (c = 0; c < a->channels; c++) {
169
170         if (!(PA_CHANNEL_POSITION_MASK(cm->map[c]) & mask))
171             continue;
172
173         if (a->values[c] > m)
174             m = a->values[c];
175     }
176
177     return m;
178 }
179
180 pa_volume_t pa_cvolume_min_mask(const pa_cvolume *a, const pa_channel_map *cm, pa_channel_position_mask_t mask) {
181     pa_volume_t m = PA_VOLUME_MAX;
182     unsigned c;
183
184     pa_assert(a);
185
186     if (!cm)
187         return pa_cvolume_min(a);
188
189     pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(a, cm), PA_VOLUME_MUTED);
190
191     for (c = 0; c < a->channels; c++) {
192
193         if (!(PA_CHANNEL_POSITION_MASK(cm->map[c]) & mask))
194             continue;
195
196         if (a->values[c] < m)
197             m = a->values[c];
198     }
199
200     return m;
201 }
202
203 pa_volume_t pa_sw_volume_multiply(pa_volume_t a, pa_volume_t b) {
204
205     pa_return_val_if_fail(PA_VOLUME_IS_VALID(a), PA_VOLUME_INVALID);
206     pa_return_val_if_fail(PA_VOLUME_IS_VALID(b), PA_VOLUME_INVALID);
207
208     /* cbrt((a/PA_VOLUME_NORM)^3*(b/PA_VOLUME_NORM)^3)*PA_VOLUME_NORM = a*b/PA_VOLUME_NORM */
209
210     return (pa_volume_t) PA_CLAMP_VOLUME((((uint64_t) a * (uint64_t) b + (uint64_t) PA_VOLUME_NORM / 2ULL) / (uint64_t) PA_VOLUME_NORM));
211 }
212
213 pa_volume_t pa_sw_volume_divide(pa_volume_t a, pa_volume_t b) {
214
215     pa_return_val_if_fail(PA_VOLUME_IS_VALID(a), PA_VOLUME_INVALID);
216     pa_return_val_if_fail(PA_VOLUME_IS_VALID(b), PA_VOLUME_INVALID);
217
218     if (b <= PA_VOLUME_MUTED)
219         return 0;
220
221     return (pa_volume_t) (((uint64_t) a * (uint64_t) PA_VOLUME_NORM + (uint64_t) b / 2ULL) / (uint64_t) b);
222 }
223
224 /* Amplitude, not power */
225 static double linear_to_dB(double v) {
226     return 20.0 * log10(v);
227 }
228
229 static double dB_to_linear(double v) {
230     return pow(10.0, v / 20.0);
231 }
232
233 pa_volume_t pa_sw_volume_from_dB(double dB) {
234     if (isinf(dB) < 0 || dB <= PA_DECIBEL_MININFTY)
235         return PA_VOLUME_MUTED;
236
237     return pa_sw_volume_from_linear(dB_to_linear(dB));
238 }
239
240 double pa_sw_volume_to_dB(pa_volume_t v) {
241
242     pa_return_val_if_fail(PA_VOLUME_IS_VALID(v), PA_DECIBEL_MININFTY);
243
244     if (v <= PA_VOLUME_MUTED)
245         return PA_DECIBEL_MININFTY;
246
247     return linear_to_dB(pa_sw_volume_to_linear(v));
248 }
249
250 pa_volume_t pa_sw_volume_from_linear(double v) {
251
252     if (v <= 0.0)
253         return PA_VOLUME_MUTED;
254
255     /*
256      * We use a cubic mapping here, as suggested and discussed here:
257      *
258      * http://www.robotplanet.dk/audio/audio_gui_design/
259      * http://lists.linuxaudio.org/pipermail/linux-audio-dev/2009-May/thread.html#23151
260      *
261      * We make sure that the conversion to linear and back yields the
262      * same volume value! That's why we need the lround() below!
263      */
264
265     return (pa_volume_t) PA_CLAMP_VOLUME((uint64_t) lround(cbrt(v) * PA_VOLUME_NORM));
266 }
267
268 double pa_sw_volume_to_linear(pa_volume_t v) {
269     double f;
270
271     pa_return_val_if_fail(PA_VOLUME_IS_VALID(v), 0.0);
272
273     if (v <= PA_VOLUME_MUTED)
274         return 0.0;
275
276     if (v == PA_VOLUME_NORM)
277         return 1.0;
278
279     f = ((double) v / PA_VOLUME_NORM);
280
281     return f*f*f;
282 }
283
284 char *pa_cvolume_snprint(char *s, size_t l, const pa_cvolume *c) {
285     unsigned channel;
286     bool first = true;
287     char *e;
288
289     pa_assert(s);
290     pa_assert(l > 0);
291     pa_assert(c);
292
293     pa_init_i18n();
294
295     if (!pa_cvolume_valid(c)) {
296         pa_snprintf(s, l, _("(invalid)"));
297         return s;
298     }
299
300     *(e = s) = 0;
301
302     for (channel = 0; channel < c->channels && l > 1; channel++) {
303         l -= pa_snprintf(e, l, "%s%u: %3u%%",
304                       first ? "" : " ",
305                       channel,
306                       (c->values[channel]*100+PA_VOLUME_NORM/2)/PA_VOLUME_NORM);
307
308         e = strchr(e, 0);
309         first = false;
310     }
311
312     return s;
313 }
314
315 char *pa_volume_snprint(char *s, size_t l, pa_volume_t v) {
316     pa_assert(s);
317     pa_assert(l > 0);
318
319     pa_init_i18n();
320
321     if (!PA_VOLUME_IS_VALID(v)) {
322         pa_snprintf(s, l, _("(invalid)"));
323         return s;
324     }
325
326     pa_snprintf(s, l, "%3u%%", (v*100+PA_VOLUME_NORM/2)/PA_VOLUME_NORM);
327     return s;
328 }
329
330 char *pa_sw_cvolume_snprint_dB(char *s, size_t l, const pa_cvolume *c) {
331     unsigned channel;
332     bool first = true;
333     char *e;
334
335     pa_assert(s);
336     pa_assert(l > 0);
337     pa_assert(c);
338
339     pa_init_i18n();
340
341     if (!pa_cvolume_valid(c)) {
342         pa_snprintf(s, l, _("(invalid)"));
343         return s;
344     }
345
346     *(e = s) = 0;
347
348     for (channel = 0; channel < c->channels && l > 1; channel++) {
349         double f = pa_sw_volume_to_dB(c->values[channel]);
350
351         l -= pa_snprintf(e, l, "%s%u: %0.2f dB",
352                          first ? "" : " ",
353                          channel,
354                          isinf(f) < 0 || f <= PA_DECIBEL_MININFTY ? -INFINITY : f);
355
356         e = strchr(e, 0);
357         first = false;
358     }
359
360     return s;
361 }
362
363 char *pa_cvolume_snprint_verbose(char *s, size_t l, const pa_cvolume *c, const pa_channel_map *map, int print_dB) {
364     char *current = s;
365     bool first = true;
366
367     pa_assert(s);
368     pa_assert(l > 0);
369     pa_assert(c);
370
371     pa_init_i18n();
372
373     if (!pa_cvolume_valid(c)) {
374         pa_snprintf(s, l, _("(invalid)"));
375         return s;
376     }
377
378     pa_assert(!map || (map->channels == c->channels));
379     pa_assert(!map || pa_channel_map_valid(map));
380
381     current[0] = 0;
382
383     for (unsigned channel = 0; channel < c->channels && l > 1; channel++) {
384         char channel_position[32];
385         size_t bytes_printed;
386         char buf[PA_VOLUME_SNPRINT_VERBOSE_MAX];
387
388         if (map)
389             pa_snprintf(channel_position, sizeof(channel_position), "%s", pa_channel_position_to_string(map->map[channel]));
390         else
391             pa_snprintf(channel_position, sizeof(channel_position), "%u", channel);
392
393         bytes_printed = pa_snprintf(current, l, "%s%s: %s",
394                                     first ? "" : ",   ",
395                                     channel_position,
396                                     pa_volume_snprint_verbose(buf, sizeof(buf), c->values[channel], print_dB));
397         l -= bytes_printed;
398         current += bytes_printed;
399         first = false;
400     }
401
402     return s;
403 }
404
405 char *pa_sw_volume_snprint_dB(char *s, size_t l, pa_volume_t v) {
406     double f;
407
408     pa_assert(s);
409     pa_assert(l > 0);
410
411     pa_init_i18n();
412
413     if (!PA_VOLUME_IS_VALID(v)) {
414         pa_snprintf(s, l, _("(invalid)"));
415         return s;
416     }
417
418     f = pa_sw_volume_to_dB(v);
419     pa_snprintf(s, l, "%0.2f dB", isinf(f) < 0 || f <= PA_DECIBEL_MININFTY ? -INFINITY : f);
420
421     return s;
422 }
423
424 char *pa_volume_snprint_verbose(char *s, size_t l, pa_volume_t v, int print_dB) {
425     char dB[PA_SW_VOLUME_SNPRINT_DB_MAX];
426
427     pa_assert(s);
428     pa_assert(l > 0);
429
430     pa_init_i18n();
431
432     if (!PA_VOLUME_IS_VALID(v)) {
433         pa_snprintf(s, l, _("(invalid)"));
434         return s;
435     }
436
437     pa_snprintf(s, l, "%" PRIu32 " / %3u%%%s%s",
438                 v,
439                 (v * 100 + PA_VOLUME_NORM / 2) / PA_VOLUME_NORM,
440                 print_dB ? " / " : "",
441                 print_dB ? pa_sw_volume_snprint_dB(dB, sizeof(dB), v) : "");
442
443     return s;
444 }
445
446 int pa_cvolume_channels_equal_to(const pa_cvolume *a, pa_volume_t v) {
447     unsigned c;
448     pa_assert(a);
449
450     if (pa_cvolume_valid(a) == 0)
451         abort();
452
453     /* pa_return_val_if_fail(pa_cvolume_valid(a), 0); */
454     pa_return_val_if_fail(PA_VOLUME_IS_VALID(v), 0);
455
456     for (c = 0; c < a->channels; c++)
457         if (a->values[c] != v)
458             return 0;
459
460     return 1;
461 }
462
463 pa_cvolume *pa_sw_cvolume_multiply(pa_cvolume *dest, const pa_cvolume *a, const pa_cvolume *b) {
464     unsigned i;
465
466     pa_assert(dest);
467     pa_assert(a);
468     pa_assert(b);
469
470     pa_return_val_if_fail(pa_cvolume_valid(a), NULL);
471     pa_return_val_if_fail(pa_cvolume_valid(b), NULL);
472
473     for (i = 0; i < a->channels && i < b->channels; i++)
474         dest->values[i] = pa_sw_volume_multiply(a->values[i], b->values[i]);
475
476     dest->channels = (uint8_t) i;
477
478     return dest;
479 }
480
481 pa_cvolume *pa_sw_cvolume_multiply_scalar(pa_cvolume *dest, const pa_cvolume *a, pa_volume_t b) {
482     unsigned i;
483
484     pa_assert(dest);
485     pa_assert(a);
486
487     pa_return_val_if_fail(pa_cvolume_valid(a), NULL);
488     pa_return_val_if_fail(PA_VOLUME_IS_VALID(b), NULL);
489
490     for (i = 0; i < a->channels; i++)
491         dest->values[i] = pa_sw_volume_multiply(a->values[i], b);
492
493     dest->channels = (uint8_t) i;
494
495     return dest;
496 }
497
498 pa_cvolume *pa_sw_cvolume_divide(pa_cvolume *dest, const pa_cvolume *a, const pa_cvolume *b) {
499     unsigned i;
500
501     pa_assert(dest);
502     pa_assert(a);
503     pa_assert(b);
504
505     pa_return_val_if_fail(pa_cvolume_valid(a), NULL);
506     pa_return_val_if_fail(pa_cvolume_valid(b), NULL);
507
508     for (i = 0; i < a->channels && i < b->channels; i++)
509         dest->values[i] = pa_sw_volume_divide(a->values[i], b->values[i]);
510
511     dest->channels = (uint8_t) i;
512
513     return dest;
514 }
515
516 pa_cvolume *pa_sw_cvolume_divide_scalar(pa_cvolume *dest, const pa_cvolume *a, pa_volume_t b) {
517     unsigned i;
518
519     pa_assert(dest);
520     pa_assert(a);
521
522     pa_return_val_if_fail(pa_cvolume_valid(a), NULL);
523     pa_return_val_if_fail(PA_VOLUME_IS_VALID(b), NULL);
524
525     for (i = 0; i < a->channels; i++)
526         dest->values[i] = pa_sw_volume_divide(a->values[i], b);
527
528     dest->channels = (uint8_t) i;
529
530     return dest;
531 }
532
533 int pa_cvolume_valid(const pa_cvolume *v) {
534     unsigned c;
535
536     pa_assert(v);
537
538     if (!pa_channels_valid(v->channels))
539         return 0;
540
541     for (c = 0; c < v->channels; c++)
542         if (!PA_VOLUME_IS_VALID(v->values[c]))
543             return 0;
544
545     return 1;
546 }
547
548 static bool on_left(pa_channel_position_t p) {
549     return !!(PA_CHANNEL_POSITION_MASK(p) & PA_CHANNEL_POSITION_MASK_LEFT);
550 }
551
552 static bool on_right(pa_channel_position_t p) {
553     return !!(PA_CHANNEL_POSITION_MASK(p) & PA_CHANNEL_POSITION_MASK_RIGHT);
554 }
555
556 static bool on_center(pa_channel_position_t p) {
557     return !!(PA_CHANNEL_POSITION_MASK(p) & PA_CHANNEL_POSITION_MASK_CENTER);
558 }
559
560 static bool on_lfe(pa_channel_position_t p) {
561     return p == PA_CHANNEL_POSITION_LFE;
562 }
563
564 static bool on_front(pa_channel_position_t p) {
565     return !!(PA_CHANNEL_POSITION_MASK(p) & PA_CHANNEL_POSITION_MASK_FRONT);
566 }
567
568 static bool on_rear(pa_channel_position_t p) {
569     return !!(PA_CHANNEL_POSITION_MASK(p) & PA_CHANNEL_POSITION_MASK_REAR);
570 }
571
572 pa_cvolume *pa_cvolume_remap(pa_cvolume *v, const pa_channel_map *from, const pa_channel_map *to) {
573     int a, b;
574     pa_cvolume result;
575
576     pa_assert(v);
577     pa_assert(from);
578     pa_assert(to);
579
580     pa_return_val_if_fail(pa_channel_map_valid(to), NULL);
581     pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(v, from), NULL);
582
583     if (pa_channel_map_equal(from, to))
584         return v;
585
586     result.channels = to->channels;
587
588     for (b = 0; b < to->channels; b++) {
589         pa_volume_t k = 0;
590         int n = 0;
591
592         for (a = 0; a < from->channels; a++)
593             if (from->map[a] == to->map[b]) {
594                 k += v->values[a];
595                 n ++;
596             }
597
598         if (n <= 0) {
599             for (a = 0; a < from->channels; a++)
600                 if ((on_left(from->map[a]) && on_left(to->map[b])) ||
601                     (on_right(from->map[a]) && on_right(to->map[b])) ||
602                     (on_center(from->map[a]) && on_center(to->map[b])) ||
603                     (on_lfe(from->map[a]) && on_lfe(to->map[b]))) {
604
605                     k += v->values[a];
606                     n ++;
607                 }
608         }
609
610         if (n <= 0)
611             k = pa_cvolume_avg(v);
612         else
613             k /= n;
614
615         result.values[b] = k;
616     }
617
618     *v = result;
619     return v;
620 }
621
622 int pa_cvolume_compatible(const pa_cvolume *v, const pa_sample_spec *ss) {
623
624     pa_assert(v);
625     pa_assert(ss);
626
627     pa_return_val_if_fail(pa_cvolume_valid(v), 0);
628     pa_return_val_if_fail(pa_sample_spec_valid(ss), 0);
629
630     return v->channels == ss->channels;
631 }
632
633 int pa_cvolume_compatible_with_channel_map(const pa_cvolume *v, const pa_channel_map *cm) {
634     pa_assert(v);
635     pa_assert(cm);
636
637     pa_return_val_if_fail(pa_cvolume_valid(v), 0);
638     pa_return_val_if_fail(pa_channel_map_valid(cm), 0);
639
640     return v->channels == cm->channels;
641 }
642
643 static void get_avg_lr(const pa_channel_map *map, const pa_cvolume *v, pa_volume_t *l, pa_volume_t *r) {
644     int c;
645     pa_volume_t left = 0, right = 0;
646     unsigned n_left = 0, n_right = 0;
647
648     pa_assert(v);
649     pa_assert(map);
650     pa_assert(map->channels == v->channels);
651     pa_assert(l);
652     pa_assert(r);
653
654     for (c = 0; c < map->channels; c++) {
655         if (on_left(map->map[c])) {
656             left += v->values[c];
657             n_left++;
658         } else if (on_right(map->map[c])) {
659             right += v->values[c];
660             n_right++;
661         }
662     }
663
664     if (n_left <= 0)
665         *l = PA_VOLUME_NORM;
666     else
667         *l = left / n_left;
668
669     if (n_right <= 0)
670         *r = PA_VOLUME_NORM;
671     else
672         *r = right / n_right;
673 }
674
675 float pa_cvolume_get_balance(const pa_cvolume *v, const pa_channel_map *map) {
676     pa_volume_t left, right;
677
678     pa_assert(v);
679     pa_assert(map);
680
681     pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(v, map), 0.0f);
682
683     if (!pa_channel_map_can_balance(map))
684         return 0.0f;
685
686     get_avg_lr(map, v, &left, &right);
687
688     if (left == right)
689         return 0.0f;
690
691     /*   1.0,  0.0  =>  -1.0
692          0.0,  1.0  =>   1.0
693          0.0,  0.0  =>   0.0
694          0.5,  0.5  =>   0.0
695          1.0,  0.5  =>  -0.5
696          1.0,  0.25 => -0.75
697          0.75, 0.25 => -0.66
698          0.5,  0.25 => -0.5   */
699
700     if (left > right)
701         return -1.0f + ((float) right / (float) left);
702     else
703         return 1.0f - ((float) left / (float) right);
704 }
705
706 pa_cvolume* pa_cvolume_set_balance(pa_cvolume *v, const pa_channel_map *map, float new_balance) {
707     pa_volume_t left, nleft, right, nright, m;
708     unsigned c;
709
710     pa_assert(map);
711     pa_assert(v);
712
713     pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(v, map), NULL);
714     pa_return_val_if_fail(new_balance >= -1.0f, NULL);
715     pa_return_val_if_fail(new_balance <= 1.0f, NULL);
716
717     if (!pa_channel_map_can_balance(map))
718         return v;
719
720     get_avg_lr(map, v, &left, &right);
721
722     m = PA_MAX(left, right);
723
724     if (new_balance <= 0) {
725         nright = (new_balance + 1.0f) * m;
726         nleft = m;
727     } else {
728         nleft = (1.0f - new_balance) * m;
729         nright = m;
730     }
731
732     for (c = 0; c < map->channels; c++) {
733         if (on_left(map->map[c])) {
734             if (left == 0)
735                 v->values[c] = nleft;
736             else
737                 v->values[c] = (pa_volume_t) PA_CLAMP_VOLUME(((uint64_t) v->values[c] * (uint64_t) nleft) / (uint64_t) left);
738         } else if (on_right(map->map[c])) {
739             if (right == 0)
740                 v->values[c] = nright;
741             else
742                 v->values[c] = (pa_volume_t) PA_CLAMP_VOLUME(((uint64_t) v->values[c] * (uint64_t) nright) / (uint64_t) right);
743         }
744     }
745
746     return v;
747 }
748
749 pa_cvolume* pa_cvolume_scale(pa_cvolume *v, pa_volume_t max) {
750     unsigned c;
751     pa_volume_t t = 0;
752
753     pa_assert(v);
754
755     pa_return_val_if_fail(pa_cvolume_valid(v), NULL);
756     pa_return_val_if_fail(PA_VOLUME_IS_VALID(max), NULL);
757
758     t = pa_cvolume_max(v);
759
760     if (t <= PA_VOLUME_MUTED)
761         return pa_cvolume_set(v, v->channels, max);
762
763     for (c = 0; c < v->channels; c++)
764         v->values[c] = (pa_volume_t) PA_CLAMP_VOLUME(((uint64_t) v->values[c] * (uint64_t) max) / (uint64_t) t);
765
766     return v;
767 }
768
769 pa_cvolume* pa_cvolume_scale_mask(pa_cvolume *v, pa_volume_t max, pa_channel_map *cm, pa_channel_position_mask_t mask) {
770     unsigned c;
771     pa_volume_t t = 0;
772
773     pa_assert(v);
774
775     pa_return_val_if_fail(PA_VOLUME_IS_VALID(max), NULL);
776
777     if (!cm)
778         return pa_cvolume_scale(v, max);
779
780     pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(v, cm), NULL);
781
782     t = pa_cvolume_max_mask(v, cm, mask);
783
784     if (t <= PA_VOLUME_MUTED)
785         return pa_cvolume_set(v, v->channels, max);
786
787     for (c = 0; c < v->channels; c++)
788         v->values[c] = (pa_volume_t) PA_CLAMP_VOLUME(((uint64_t) v->values[c] * (uint64_t) max) / (uint64_t) t);
789
790     return v;
791 }
792
793 static void get_avg_fr(const pa_channel_map *map, const pa_cvolume *v, pa_volume_t *f, pa_volume_t *r) {
794     int c;
795     pa_volume_t front = 0, rear = 0;
796     unsigned n_front = 0, n_rear = 0;
797
798     pa_assert(v);
799     pa_assert(map);
800     pa_assert(map->channels == v->channels);
801     pa_assert(f);
802     pa_assert(r);
803
804     for (c = 0; c < map->channels; c++) {
805         if (on_front(map->map[c])) {
806             front += v->values[c];
807             n_front++;
808         } else if (on_rear(map->map[c])) {
809             rear += v->values[c];
810             n_rear++;
811         }
812     }
813
814     if (n_front <= 0)
815         *f = PA_VOLUME_NORM;
816     else
817         *f = front / n_front;
818
819     if (n_rear <= 0)
820         *r = PA_VOLUME_NORM;
821     else
822         *r = rear / n_rear;
823 }
824
825 float pa_cvolume_get_fade(const pa_cvolume *v, const pa_channel_map *map) {
826     pa_volume_t front, rear;
827
828     pa_assert(v);
829     pa_assert(map);
830
831     pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(v, map), 0.0f);
832
833     if (!pa_channel_map_can_fade(map))
834         return 0.0f;
835
836     get_avg_fr(map, v, &front, &rear);
837
838     if (front == rear)
839         return 0.0f;
840
841     if (rear > front)
842         return -1.0f + ((float) front / (float) rear);
843     else
844         return 1.0f - ((float) rear / (float) front);
845 }
846
847 pa_cvolume* pa_cvolume_set_fade(pa_cvolume *v, const pa_channel_map *map, float new_fade) {
848     pa_volume_t front, nfront, rear, nrear, m;
849     unsigned c;
850
851     pa_assert(map);
852     pa_assert(v);
853
854     pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(v, map), NULL);
855     pa_return_val_if_fail(new_fade >= -1.0f, NULL);
856     pa_return_val_if_fail(new_fade <= 1.0f, NULL);
857
858     if (!pa_channel_map_can_fade(map))
859         return v;
860
861     get_avg_fr(map, v, &front, &rear);
862
863     m = PA_MAX(front, rear);
864
865     if (new_fade <= 0) {
866         nfront = (new_fade + 1.0f) * m;
867         nrear = m;
868     } else {
869         nrear = (1.0f - new_fade) * m;
870         nfront = m;
871     }
872
873     for (c = 0; c < map->channels; c++) {
874         if (on_front(map->map[c])) {
875             if (front == 0)
876                 v->values[c] = nfront;
877             else
878                 v->values[c] = (pa_volume_t) PA_CLAMP_VOLUME(((uint64_t) v->values[c] * (uint64_t) nfront) / (uint64_t) front);
879         } else if (on_rear(map->map[c])) {
880             if (rear == 0)
881                 v->values[c] = nrear;
882             else
883                 v->values[c] = (pa_volume_t) PA_CLAMP_VOLUME(((uint64_t) v->values[c] * (uint64_t) nrear) / (uint64_t) rear);
884         }
885     }
886
887     return v;
888 }
889
890 pa_cvolume* pa_cvolume_set_position(
891         pa_cvolume *cv,
892         const pa_channel_map *map,
893         pa_channel_position_t t,
894         pa_volume_t v) {
895
896     unsigned c;
897     bool good = false;
898
899     pa_assert(cv);
900     pa_assert(map);
901
902     pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(cv, map), NULL);
903     pa_return_val_if_fail(t < PA_CHANNEL_POSITION_MAX, NULL);
904     pa_return_val_if_fail(PA_VOLUME_IS_VALID(v), NULL);
905
906     for (c = 0; c < map->channels; c++)
907         if (map->map[c] == t) {
908             cv->values[c] = v;
909             good = true;
910         }
911
912     return good ? cv : NULL;
913 }
914
915 pa_volume_t pa_cvolume_get_position(
916         pa_cvolume *cv,
917         const pa_channel_map *map,
918         pa_channel_position_t t) {
919
920     unsigned c;
921     pa_volume_t v = PA_VOLUME_MUTED;
922
923     pa_assert(cv);
924     pa_assert(map);
925
926     pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(cv, map), PA_VOLUME_MUTED);
927     pa_return_val_if_fail(t < PA_CHANNEL_POSITION_MAX, PA_VOLUME_MUTED);
928
929     for (c = 0; c < map->channels; c++)
930         if (map->map[c] == t)
931             if (cv->values[c] > v)
932                 v = cv->values[c];
933
934     return v;
935 }
936
937 pa_cvolume* pa_cvolume_merge(pa_cvolume *dest, const pa_cvolume *a, const pa_cvolume *b) {
938     unsigned i;
939
940     pa_assert(dest);
941     pa_assert(a);
942     pa_assert(b);
943
944     pa_return_val_if_fail(pa_cvolume_valid(a), NULL);
945     pa_return_val_if_fail(pa_cvolume_valid(b), NULL);
946
947     for (i = 0; i < a->channels && i < b->channels; i++)
948         dest->values[i] = PA_MAX(a->values[i], b->values[i]);
949
950     dest->channels = (uint8_t) i;
951
952     return dest;
953 }
954
955 pa_cvolume* pa_cvolume_inc_clamp(pa_cvolume *v, pa_volume_t inc, pa_volume_t limit) {
956     pa_volume_t m;
957
958     pa_assert(v);
959
960     pa_return_val_if_fail(pa_cvolume_valid(v), NULL);
961     pa_return_val_if_fail(PA_VOLUME_IS_VALID(inc), NULL);
962
963     m = pa_cvolume_max(v);
964
965     if (m >= limit - inc)
966         m = limit;
967     else
968         m += inc;
969
970     return pa_cvolume_scale(v, m);
971 }
972
973 pa_cvolume* pa_cvolume_inc(pa_cvolume *v, pa_volume_t inc) {
974     return pa_cvolume_inc_clamp(v, inc, PA_VOLUME_MAX);
975 }
976
977 pa_cvolume* pa_cvolume_dec(pa_cvolume *v, pa_volume_t dec) {
978     pa_volume_t m;
979
980     pa_assert(v);
981
982     pa_return_val_if_fail(pa_cvolume_valid(v), NULL);
983     pa_return_val_if_fail(PA_VOLUME_IS_VALID(dec), NULL);
984
985     m = pa_cvolume_max(v);
986
987     if (m <= PA_VOLUME_MUTED + dec)
988         m = PA_VOLUME_MUTED;
989     else
990         m -= dec;
991
992     return pa_cvolume_scale(v, m);
993 }
994
995 int pa_cvolume_ramp_equal(const pa_cvolume_ramp *a, const pa_cvolume_ramp *b) {
996     int i;
997     pa_assert(a);
998     pa_assert(b);
999
1000     if (PA_UNLIKELY(a == b))
1001         return 1;
1002
1003     if (a->channels != b->channels)
1004         return 0;
1005
1006     for (i = 0; i < a->channels; i++) {
1007         if (a->ramps[i].type != b->ramps[i].type ||
1008             a->ramps[i].length != b->ramps[i].length ||
1009             a->ramps[i].target != b->ramps[i].target)
1010             return 0;
1011     }
1012
1013     return 1;
1014 }
1015
1016 pa_cvolume_ramp* pa_cvolume_ramp_init(pa_cvolume_ramp *ramp) {
1017     unsigned c;
1018
1019     pa_assert(ramp);
1020
1021     ramp->channels = 0;
1022
1023     for (c = 0; c < PA_CHANNELS_MAX; c++) {
1024         ramp->ramps[c].type = PA_VOLUME_RAMP_TYPE_LINEAR;
1025         ramp->ramps[c].length = 0;
1026         ramp->ramps[c].target = PA_VOLUME_INVALID;
1027     }
1028
1029     return ramp;
1030 }
1031
1032 pa_cvolume_ramp* pa_cvolume_ramp_set(pa_cvolume_ramp *ramp, unsigned channels, pa_volume_ramp_type_t type, long time, pa_volume_t vol) {
1033     int i;
1034
1035     pa_assert(ramp);
1036     pa_assert(channels > 0);
1037     pa_assert(time >= 0);
1038     pa_assert(channels <= PA_CHANNELS_MAX);
1039
1040     ramp->channels = (uint8_t) channels;
1041
1042     for (i = 0; i < ramp->channels; i++) {
1043         ramp->ramps[i].type = type;
1044         ramp->ramps[i].length = time;
1045         ramp->ramps[i].target = PA_CLAMP_VOLUME(vol);
1046     }
1047
1048     return ramp;
1049 }
1050
1051 pa_cvolume_ramp* pa_cvolume_ramp_channel_ramp_set(pa_cvolume_ramp *ramp, unsigned channel, pa_volume_ramp_type_t type, long time, pa_volume_t vol) {
1052
1053     pa_assert(ramp);
1054     pa_assert(channel <= ramp->channels);
1055     pa_assert(time >= 0);
1056
1057     ramp->ramps[channel].type = type;
1058     ramp->ramps[channel].length = time;
1059     ramp->ramps[channel].target = PA_CLAMP_VOLUME(vol);
1060
1061     return ramp;
1062 }