[3457] | 1 | //volume.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010 |
---|
| 5 | * The code (may) include prototypes and comments (and maybe |
---|
| 6 | * other code fragements) from libpulse*. They are mostly copyrighted by: |
---|
| 7 | * Lennart Poettering <poettering@users.sourceforge.net> and |
---|
| 8 | * Pierre Ossman <drzeus@drzeus.cx> |
---|
| 9 | * |
---|
| 10 | * This file is part of libroarpulse a part of RoarAudio, |
---|
| 11 | * a cross-platform sound system for both, home and professional use. |
---|
| 12 | * See README for details. |
---|
| 13 | * |
---|
| 14 | * This file is free software; you can redistribute it and/or modify |
---|
| 15 | * it under the terms of the GNU General Public License version 3 |
---|
| 16 | * as published by the Free Software Foundation. |
---|
| 17 | * |
---|
| 18 | * RoarAudio is distributed in the hope that it will be useful, |
---|
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 21 | * GNU General Public License for more details. |
---|
| 22 | * |
---|
| 23 | * You should have received a copy of the GNU General Public License |
---|
| 24 | * along with this software; see the file COPYING. If not, write to |
---|
| 25 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 26 | * |
---|
| 27 | * NOTE for everyone want's to change something and send patches: |
---|
| 28 | * read README and HACKING! There a addition information on |
---|
| 29 | * the license of this document you need to read before you send |
---|
| 30 | * any patches. |
---|
| 31 | * |
---|
| 32 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 33 | * or libpulse*: |
---|
| 34 | * The libs libroaresd, libroararts and libroarpulse link this libroar |
---|
| 35 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 36 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 37 | */ |
---|
| 38 | |
---|
| 39 | #include <libroarpulse/libroarpulse.h> |
---|
| 40 | |
---|
[3458] | 41 | /** Return non-zero when *a == *b */ |
---|
[3459] | 42 | int pa_cvolume_equal(const pa_cvolume *a, const pa_cvolume *b) { |
---|
| 43 | int i; |
---|
| 44 | |
---|
| 45 | if ( a == b ) |
---|
| 46 | return 1; |
---|
| 47 | |
---|
| 48 | if ( a == NULL || b == NULL ) |
---|
| 49 | return 0; |
---|
| 50 | |
---|
| 51 | if ( a->channels != b->channels ) |
---|
| 52 | return 0; |
---|
| 53 | |
---|
| 54 | for (i = 0; i < a->channels; i++) |
---|
| 55 | if ( a->values[i] != b->values[i] ) |
---|
| 56 | return 0; |
---|
| 57 | |
---|
| 58 | return 1; |
---|
| 59 | } |
---|
[3458] | 60 | |
---|
| 61 | /** Set the volume of all channels to the specified parameter */ |
---|
[3459] | 62 | pa_cvolume* pa_cvolume_set(pa_cvolume *a, unsigned channels, pa_volume_t v) { |
---|
| 63 | int i; |
---|
| 64 | |
---|
| 65 | if ( a == NULL ) |
---|
| 66 | return NULL; |
---|
| 67 | |
---|
| 68 | if ( channels > PA_CHANNELS_MAX ) |
---|
| 69 | return NULL; |
---|
| 70 | |
---|
| 71 | a->channels = channels; |
---|
| 72 | |
---|
| 73 | for (i = 0; i < channels; i++) { |
---|
| 74 | a->values[i] = v; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | return a; |
---|
| 78 | } |
---|
[3458] | 79 | |
---|
| 80 | /** Pretty print a volume structure */ |
---|
| 81 | char *pa_cvolume_snprint(char *s, size_t l, const pa_cvolume *c); |
---|
| 82 | |
---|
| 83 | /** Return the average volume of all channels */ |
---|
[3459] | 84 | pa_volume_t pa_cvolume_avg(const pa_cvolume *a) { |
---|
| 85 | int64_t sum = 0; |
---|
| 86 | int i; |
---|
| 87 | |
---|
| 88 | #ifndef PA_VOLUME_INVALID |
---|
| 89 | #define PA_VOLUME_INVALID ((pa_volume_t) UINT32_MAX) |
---|
| 90 | #endif |
---|
| 91 | |
---|
| 92 | if ( a == NULL ) |
---|
| 93 | return PA_VOLUME_INVALID; |
---|
| 94 | |
---|
| 95 | for (i = 0; i < a->channels; i++) |
---|
| 96 | sum += a->values[i]; |
---|
| 97 | |
---|
| 98 | return sum/a->channels; |
---|
| 99 | } |
---|
[3458] | 100 | |
---|
| 101 | /** Return TRUE when the passed cvolume structure is valid, FALSE otherwise */ |
---|
[3459] | 102 | int pa_cvolume_valid(const pa_cvolume *v) { |
---|
| 103 | if ( v == NULL ) |
---|
| 104 | return 0; |
---|
| 105 | |
---|
| 106 | if ( v->channels <= 0 ) |
---|
| 107 | return 0; |
---|
| 108 | |
---|
| 109 | if ( v->channels > PA_CHANNELS_MAX ) |
---|
| 110 | return 0; |
---|
| 111 | |
---|
| 112 | return 1; |
---|
| 113 | } |
---|
[3458] | 114 | |
---|
| 115 | /** Return non-zero if the volume of all channels is equal to the specified value */ |
---|
[3459] | 116 | int pa_cvolume_channels_equal_to(const pa_cvolume *a, pa_volume_t v) { |
---|
| 117 | int i; |
---|
| 118 | |
---|
| 119 | if ( a == NULL ) |
---|
| 120 | return 0; |
---|
| 121 | |
---|
| 122 | for (i = 0; i < a->channels; i++) |
---|
| 123 | if ( a->values[i] != v ) |
---|
| 124 | return 0; |
---|
| 125 | |
---|
| 126 | return 1; |
---|
| 127 | } |
---|
[3458] | 128 | |
---|
| 129 | /** Multiply two volumes specifications, return the result. This uses PA_VOLUME_NORM as neutral element of multiplication. This is only valid for software volumes! */ |
---|
| 130 | pa_volume_t pa_sw_volume_multiply(pa_volume_t a, pa_volume_t b); |
---|
| 131 | |
---|
| 132 | /** Multiply to per-channel volumes and return the result in *dest. This is only valid for software volumes! */ |
---|
| 133 | pa_cvolume *pa_sw_cvolume_multiply(pa_cvolume *dest, const pa_cvolume *a, const pa_cvolume *b); |
---|
| 134 | |
---|
| 135 | /** Convert a decibel value to a volume. This is only valid for software volumes! \since 0.4 */ |
---|
| 136 | pa_volume_t pa_sw_volume_from_dB(double f); |
---|
| 137 | |
---|
| 138 | /** Convert a volume to a decibel value. This is only valid for software volumes! \since 0.4 */ |
---|
| 139 | double pa_sw_volume_to_dB(pa_volume_t v); |
---|
| 140 | |
---|
| 141 | /** Convert a linear factor to a volume. This is only valid for software volumes! \since 0.8 */ |
---|
| 142 | pa_volume_t pa_sw_volume_from_linear(double v); |
---|
| 143 | |
---|
| 144 | /** Convert a volume to a linear factor. This is only valid for software volumes! \since 0.8 */ |
---|
| 145 | double pa_sw_volume_to_linear(pa_volume_t v); |
---|
| 146 | |
---|
[3457] | 147 | //ll |
---|