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, 51 Franklin Street, Fifth Floor, |
---|
26 | * Boston, MA 02110-1301, USA. |
---|
27 | * |
---|
28 | * NOTE for everyone want's to change something and send patches: |
---|
29 | * read README and HACKING! There a addition information on |
---|
30 | * the license of this document you need to read before you send |
---|
31 | * any patches. |
---|
32 | * |
---|
33 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
34 | * or libpulse*: |
---|
35 | * The libs libroaresd, libroararts and libroarpulse link this libroar |
---|
36 | * and are therefore GPL. Because of this it may be illigal to use |
---|
37 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
38 | */ |
---|
39 | |
---|
40 | #include <libroarpulse/libroarpulse.h> |
---|
41 | |
---|
42 | /** Return the amount of bytes playback of a second of audio with the specified sample type takes */ |
---|
43 | size_t pa_bytes_per_second(const pa_sample_spec *spec); |
---|
44 | |
---|
45 | /** Return the size of a frame with the specific sample type */ |
---|
46 | size_t pa_frame_size(const pa_sample_spec *spec) { |
---|
47 | if ( spec == NULL ) |
---|
48 | return 0; |
---|
49 | |
---|
50 | return pa_sample_size(spec) * spec->channels; |
---|
51 | } |
---|
52 | |
---|
53 | /** Return the size of a sample with the specific sample type */ |
---|
54 | size_t pa_sample_size(const pa_sample_spec *spec) { |
---|
55 | if ( spec == NULL ) |
---|
56 | return -1; |
---|
57 | |
---|
58 | switch (spec->format) { |
---|
59 | case PA_SAMPLE_ALAW: |
---|
60 | case PA_SAMPLE_ULAW: |
---|
61 | case PA_SAMPLE_U8: |
---|
62 | return 1; |
---|
63 | break; |
---|
64 | case PA_SAMPLE_S16LE: |
---|
65 | case PA_SAMPLE_S16BE: |
---|
66 | return 2; |
---|
67 | break; |
---|
68 | default: |
---|
69 | return 0; |
---|
70 | break; |
---|
71 | } |
---|
72 | |
---|
73 | return 0; |
---|
74 | } |
---|
75 | |
---|
76 | /** Calculate the time the specified bytes take to play with the specified sample type */ |
---|
77 | pa_usec_t pa_bytes_to_usec(uint64_t length, const pa_sample_spec *spec) { |
---|
78 | if ( spec == NULL ) |
---|
79 | return 0; |
---|
80 | |
---|
81 | return (pa_usec_t) (((double) length/pa_frame_size(spec)*1000000)/spec->rate); |
---|
82 | } |
---|
83 | |
---|
84 | /** Calculates the number of bytes that are required for the specified time. \since 0.9 */ |
---|
85 | size_t pa_usec_to_bytes(pa_usec_t t, const pa_sample_spec *spec) { |
---|
86 | if ( spec == NULL ) |
---|
87 | return 0; |
---|
88 | |
---|
89 | return (size_t) (((double) t * spec->rate / 1000000))*pa_frame_size(spec); |
---|
90 | } |
---|
91 | |
---|
92 | /** Return non-zero when the sample type specification is valid */ |
---|
93 | int pa_sample_spec_valid(const pa_sample_spec *spec) { |
---|
94 | if ( spec == NULL ) |
---|
95 | return 0; |
---|
96 | |
---|
97 | if ( spec->channels < 1 || spec->channels > ROAR_MAX_CHANNELS ) |
---|
98 | return 0; |
---|
99 | |
---|
100 | if ( spec->format < 0 || spec->format > PA_SAMPLE_MAX ) |
---|
101 | return 0; |
---|
102 | |
---|
103 | return 1; |
---|
104 | } |
---|
105 | |
---|
106 | /** Return non-zero when the two sample type specifications match */ |
---|
107 | int pa_sample_spec_equal(const pa_sample_spec*a, const pa_sample_spec*b) { |
---|
108 | if ( a->rate != b->rate ) |
---|
109 | return 0; |
---|
110 | |
---|
111 | if ( a->channels != b->channels ) |
---|
112 | return 0; |
---|
113 | |
---|
114 | if ( a->format != b->format ) |
---|
115 | return 0; |
---|
116 | |
---|
117 | return 1; |
---|
118 | } |
---|
119 | |
---|
120 | /** Return a descriptive string for the specified sample format. \since 0.8 */ |
---|
121 | static struct { |
---|
122 | pa_sample_format_t format; |
---|
123 | const char * name; |
---|
124 | } _roar_pa_format[] = { |
---|
125 | {PA_SAMPLE_U8, "u8" }, |
---|
126 | {PA_SAMPLE_ALAW, "aLaw" }, |
---|
127 | {PA_SAMPLE_ULAW, "uLaw" }, |
---|
128 | {PA_SAMPLE_S16LE, "s16le" }, |
---|
129 | {PA_SAMPLE_S16BE, "s16be" }, |
---|
130 | {PA_SAMPLE_FLOAT32LE, "float32le"}, |
---|
131 | {PA_SAMPLE_FLOAT32BE, "float32be"}, |
---|
132 | {PA_SAMPLE_INVALID, NULL } |
---|
133 | }; |
---|
134 | |
---|
135 | const char *pa_sample_format_to_string(pa_sample_format_t f) { |
---|
136 | int i; |
---|
137 | |
---|
138 | for (i = 0; _roar_pa_format[i].name != NULL; i++) |
---|
139 | if ( _roar_pa_format[i].format == f ) |
---|
140 | return _roar_pa_format[i].name; |
---|
141 | |
---|
142 | return NULL; |
---|
143 | } |
---|
144 | |
---|
145 | /** Parse a sample format text. Inverse of pa_sample_format_to_string() */ |
---|
146 | pa_sample_format_t pa_parse_sample_format(const char *format) { |
---|
147 | int i; |
---|
148 | |
---|
149 | for (i = 0; _roar_pa_format[i].name != NULL; i++) |
---|
150 | if ( !strcasecmp(_roar_pa_format[i].name, format) ) |
---|
151 | return _roar_pa_format[i].format; |
---|
152 | |
---|
153 | return PA_SAMPLE_INVALID; |
---|
154 | } |
---|
155 | |
---|
156 | /** Maximum required string length for pa_sample_spec_snprint() */ |
---|
157 | #define PA_SAMPLE_SPEC_SNPRINT_MAX 32 |
---|
158 | |
---|
159 | /** Pretty print a sample type specification to a string */ |
---|
160 | char* pa_sample_spec_snprint(char *s, size_t l, const pa_sample_spec *spec) { |
---|
161 | if ( s == NULL || l == 0 || spec == NULL ) |
---|
162 | return NULL; |
---|
163 | |
---|
164 | if ( pa_sample_spec_valid(spec) ) { |
---|
165 | snprintf(s, l, "%s %uch %uHz", pa_sample_format_to_string(spec->format), spec->channels, spec->rate); |
---|
166 | } else { |
---|
167 | snprintf(s, l, "Invalid"); |
---|
168 | } |
---|
169 | |
---|
170 | return s; |
---|
171 | } |
---|
172 | |
---|
173 | /** Pretty print a byte size value. (i.e. "2.5 MiB") */ |
---|
174 | char* pa_bytes_snprint(char *s, size_t l, unsigned v) { |
---|
175 | double val = v; |
---|
176 | int i; |
---|
177 | const char pre[] = "KMGTP"; |
---|
178 | |
---|
179 | if ( v <= 1024 ) { |
---|
180 | snprintf(s, l, "%u B", v); |
---|
181 | return s; |
---|
182 | } |
---|
183 | |
---|
184 | for (i = 0; pre[i] != 0; i++) { |
---|
185 | val /= 1024; |
---|
186 | if ( val <= 1024 ) { |
---|
187 | snprintf(s, l, "%0.1f %ciB", val, pre[i]); |
---|
188 | return s; |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | snprintf(s, l, "%0.1f %ciB", val*1024., pre[i-1]); |
---|
193 | return s; |
---|
194 | } |
---|
195 | |
---|
196 | //ll |
---|