[3413] | 1 | //sample.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 | |
---|
[3414] | 41 | /** Return the amount of bytes playback of a second of audio with the specified sample type takes */ |
---|
| 42 | size_t pa_bytes_per_second(const pa_sample_spec *spec); |
---|
| 43 | |
---|
| 44 | /** Return the size of a frame with the specific sample type */ |
---|
[3415] | 45 | size_t pa_frame_size(const pa_sample_spec *spec) { |
---|
| 46 | if ( spec == NULL ) |
---|
| 47 | return 0; |
---|
| 48 | |
---|
| 49 | return pa_sample_size(spec) * spec->channels; |
---|
| 50 | } |
---|
[3414] | 51 | |
---|
| 52 | /** Return the size of a sample with the specific sample type */ |
---|
[3415] | 53 | size_t pa_sample_size(const pa_sample_spec *spec) { |
---|
| 54 | if ( spec == NULL ) |
---|
| 55 | return -1; |
---|
| 56 | |
---|
| 57 | switch (spec->format) { |
---|
| 58 | case PA_SAMPLE_ALAW: |
---|
| 59 | case PA_SAMPLE_ULAW: |
---|
| 60 | case PA_SAMPLE_U8: |
---|
| 61 | return 1; |
---|
| 62 | break; |
---|
| 63 | case PA_SAMPLE_S16LE: |
---|
| 64 | case PA_SAMPLE_S16BE: |
---|
| 65 | return 2; |
---|
| 66 | break; |
---|
| 67 | default: |
---|
| 68 | return 0; |
---|
| 69 | break; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | return 0; |
---|
| 73 | } |
---|
[3414] | 74 | |
---|
| 75 | /** Calculate the time the specified bytes take to play with the specified sample type */ |
---|
| 76 | pa_usec_t pa_bytes_to_usec(uint64_t length, const pa_sample_spec *spec); |
---|
| 77 | |
---|
| 78 | /** Calculates the number of bytes that are required for the specified time. \since 0.9 */ |
---|
| 79 | size_t pa_usec_to_bytes(pa_usec_t t, const pa_sample_spec *spec); |
---|
| 80 | |
---|
| 81 | /** Return non-zero when the sample type specification is valid */ |
---|
[3415] | 82 | int pa_sample_spec_valid(const pa_sample_spec *spec) { |
---|
| 83 | if ( spec == NULL ) |
---|
| 84 | return 0; |
---|
| 85 | |
---|
| 86 | if ( spec->channels < 1 || spec->channels > ROAR_MAX_CHANNELS ) |
---|
| 87 | return 0; |
---|
| 88 | |
---|
| 89 | if ( spec->format < 0 || spec->format > PA_SAMPLE_MAX ) |
---|
| 90 | return 0; |
---|
| 91 | |
---|
| 92 | return 1; |
---|
| 93 | } |
---|
[3414] | 94 | |
---|
| 95 | /** Return non-zero when the two sample type specifications match */ |
---|
[3415] | 96 | int pa_sample_spec_equal(const pa_sample_spec*a, const pa_sample_spec*b) { |
---|
| 97 | if ( a->rate != b->rate ) |
---|
| 98 | return 0; |
---|
| 99 | |
---|
| 100 | if ( a->channels != b->channels ) |
---|
| 101 | return 0; |
---|
| 102 | |
---|
| 103 | if ( a->format != b->format ) |
---|
| 104 | return 0; |
---|
| 105 | |
---|
| 106 | return 1; |
---|
| 107 | } |
---|
[3414] | 108 | |
---|
| 109 | /** Return a descriptive string for the specified sample format. \since 0.8 */ |
---|
[3415] | 110 | static struct { |
---|
| 111 | pa_sample_format_t format; |
---|
| 112 | const char * name; |
---|
| 113 | } _roar_pa_format[] = { |
---|
[3416] | 114 | {PA_SAMPLE_U8, "u8" }, |
---|
| 115 | {PA_SAMPLE_ALAW, "aLaw" }, |
---|
| 116 | {PA_SAMPLE_ULAW, "uLaw" }, |
---|
| 117 | {PA_SAMPLE_S16LE, "s16le"}, |
---|
| 118 | {PA_SAMPLE_S16BE, "s16be"}, |
---|
| 119 | {PA_SAMPLE_INVALID, NULL } |
---|
[3415] | 120 | }; |
---|
| 121 | |
---|
[3416] | 122 | const char *pa_sample_format_to_string(pa_sample_format_t f) { |
---|
| 123 | int i; |
---|
| 124 | |
---|
| 125 | for (i = 0; _roar_pa_format[i].name != NULL; i++) |
---|
| 126 | if ( _roar_pa_format[i].format == f ) |
---|
| 127 | return _roar_pa_format[i].name; |
---|
| 128 | |
---|
| 129 | return NULL; |
---|
| 130 | } |
---|
[3414] | 131 | |
---|
| 132 | /** Parse a sample format text. Inverse of pa_sample_format_to_string() */ |
---|
[3416] | 133 | pa_sample_format_t pa_parse_sample_format(const char *format) { |
---|
| 134 | int i; |
---|
| 135 | |
---|
| 136 | for (i = 0; _roar_pa_format[i].name != NULL; i++) |
---|
| 137 | if ( !strcasecmp(_roar_pa_format[i].name, format) ) |
---|
| 138 | return _roar_pa_format[i].format; |
---|
| 139 | |
---|
| 140 | return PA_SAMPLE_INVALID; |
---|
| 141 | } |
---|
[3414] | 142 | |
---|
| 143 | /** Maximum required string length for pa_sample_spec_snprint() */ |
---|
| 144 | #define PA_SAMPLE_SPEC_SNPRINT_MAX 32 |
---|
| 145 | |
---|
| 146 | /** Pretty print a sample type specification to a string */ |
---|
[3416] | 147 | char* pa_sample_spec_snprint(char *s, size_t l, const pa_sample_spec *spec) { |
---|
| 148 | if ( s == NULL || l == 0 || spec == NULL ) |
---|
| 149 | return NULL; |
---|
| 150 | |
---|
| 151 | if ( pa_sample_spec_valid(spec) ) { |
---|
| 152 | snprintf(s, l, "%s %uch %uHz", pa_sample_format_to_string(spec->format), spec->channels, spec->rate); |
---|
| 153 | } else { |
---|
| 154 | snprintf(s, l, "Invalid"); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | return s; |
---|
| 158 | } |
---|
[3414] | 159 | |
---|
| 160 | /** Pretty print a byte size value. (i.e. "2.5 MiB") */ |
---|
[3416] | 161 | char* pa_bytes_snprint(char *s, size_t l, unsigned v) { |
---|
| 162 | double val = v; |
---|
| 163 | int i; |
---|
| 164 | const char pre[] = "KMGTP"; |
---|
| 165 | |
---|
| 166 | if ( v <= 1024 ) { |
---|
| 167 | snprintf(s, l, "%u B", v); |
---|
| 168 | return s; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | for (i = 0; pre[i] != 0; i++) { |
---|
| 172 | val /= 1024; |
---|
| 173 | if ( val <= 1024 ) { |
---|
| 174 | snprintf(s, l, "%0.1f %ciB", val, pre[i]); |
---|
| 175 | return s; |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | snprintf(s, l, "%0.1f %ciB", val*1024., pre[i-1]); |
---|
| 180 | return s; |
---|
| 181 | } |
---|
[3414] | 182 | |
---|
[3413] | 183 | //ll |
---|