]> code.delx.au - pulseaudio/blob - src/pulse/sample.c
Updated catalan po
[pulseaudio] / src / pulse / sample.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <math.h>
29 #include <string.h>
30
31 #include <pulse/timeval.h>
32 #include <pulse/i18n.h>
33
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/macro.h>
36
37 #include "sample.h"
38
39 size_t pa_sample_size_of_format(pa_sample_format_t f) {
40
41 static const size_t table[] = {
42 [PA_SAMPLE_U8] = 1,
43 [PA_SAMPLE_ULAW] = 1,
44 [PA_SAMPLE_ALAW] = 1,
45 [PA_SAMPLE_S16LE] = 2,
46 [PA_SAMPLE_S16BE] = 2,
47 [PA_SAMPLE_FLOAT32LE] = 4,
48 [PA_SAMPLE_FLOAT32BE] = 4,
49 [PA_SAMPLE_S32LE] = 4,
50 [PA_SAMPLE_S32BE] = 4,
51 [PA_SAMPLE_S24LE] = 3,
52 [PA_SAMPLE_S24BE] = 3,
53 [PA_SAMPLE_S24_32LE] = 4,
54 [PA_SAMPLE_S24_32BE] = 4
55 };
56
57 pa_assert(f >= 0);
58 pa_assert(f < PA_SAMPLE_MAX);
59
60 return table[f];
61 }
62
63 size_t pa_sample_size(const pa_sample_spec *spec) {
64
65 pa_assert(spec);
66 pa_return_val_if_fail(pa_sample_spec_valid(spec), 0);
67
68 return pa_sample_size_of_format(spec->format);
69 }
70
71 size_t pa_frame_size(const pa_sample_spec *spec) {
72 pa_assert(spec);
73 pa_return_val_if_fail(pa_sample_spec_valid(spec), 0);
74
75 return pa_sample_size(spec) * spec->channels;
76 }
77
78 size_t pa_bytes_per_second(const pa_sample_spec *spec) {
79 pa_assert(spec);
80 pa_return_val_if_fail(pa_sample_spec_valid(spec), 0);
81
82 return spec->rate*pa_frame_size(spec);
83 }
84
85 pa_usec_t pa_bytes_to_usec(uint64_t length, const pa_sample_spec *spec) {
86 pa_assert(spec);
87 pa_return_val_if_fail(pa_sample_spec_valid(spec), 0);
88
89 return (((pa_usec_t) (length / pa_frame_size(spec)) * PA_USEC_PER_SEC) / spec->rate);
90 }
91
92 size_t pa_usec_to_bytes(pa_usec_t t, const pa_sample_spec *spec) {
93 pa_assert(spec);
94 pa_return_val_if_fail(pa_sample_spec_valid(spec), 0);
95
96 return (size_t) (((t * spec->rate) / PA_USEC_PER_SEC)) * pa_frame_size(spec);
97 }
98
99 pa_sample_spec* pa_sample_spec_init(pa_sample_spec *spec) {
100 pa_assert(spec);
101
102 spec->format = PA_SAMPLE_INVALID;
103 spec->rate = 0;
104 spec->channels = 0;
105
106 return spec;
107 }
108
109 int pa_sample_spec_valid(const pa_sample_spec *spec) {
110 pa_assert(spec);
111
112 if (spec->rate <= 0 ||
113 spec->rate > PA_RATE_MAX ||
114 spec->channels <= 0 ||
115 spec->channels > PA_CHANNELS_MAX ||
116 spec->format >= PA_SAMPLE_MAX ||
117 spec->format < 0)
118 return 0;
119
120 return 1;
121 }
122
123 int pa_sample_spec_equal(const pa_sample_spec*a, const pa_sample_spec*b) {
124 pa_assert(a);
125 pa_assert(b);
126
127 pa_return_val_if_fail(pa_sample_spec_valid(a), 0);
128 pa_return_val_if_fail(pa_sample_spec_valid(b), 0);
129
130 return
131 (a->format == b->format) &&
132 (a->rate == b->rate) &&
133 (a->channels == b->channels);
134 }
135
136 const char *pa_sample_format_to_string(pa_sample_format_t f) {
137 static const char* const table[]= {
138 [PA_SAMPLE_U8] = "u8",
139 [PA_SAMPLE_ALAW] = "aLaw",
140 [PA_SAMPLE_ULAW] = "uLaw",
141 [PA_SAMPLE_S16LE] = "s16le",
142 [PA_SAMPLE_S16BE] = "s16be",
143 [PA_SAMPLE_FLOAT32LE] = "float32le",
144 [PA_SAMPLE_FLOAT32BE] = "float32be",
145 [PA_SAMPLE_S32LE] = "s32le",
146 [PA_SAMPLE_S32BE] = "s32be",
147 [PA_SAMPLE_S24LE] = "s24le",
148 [PA_SAMPLE_S24BE] = "s24be",
149 [PA_SAMPLE_S24_32LE] = "s24-32le",
150 [PA_SAMPLE_S24_32BE] = "s24-32be",
151 };
152
153 if (f < 0 || f >= PA_SAMPLE_MAX)
154 return NULL;
155
156 return table[f];
157 }
158
159 char *pa_sample_spec_snprint(char *s, size_t l, const pa_sample_spec *spec) {
160 pa_assert(s);
161 pa_assert(l > 0);
162 pa_assert(spec);
163
164 pa_init_i18n();
165
166 if (!pa_sample_spec_valid(spec))
167 pa_snprintf(s, l, _("(invalid)"));
168 else
169 pa_snprintf(s, l, _("%s %uch %uHz"), pa_sample_format_to_string(spec->format), spec->channels, spec->rate);
170
171 return s;
172 }
173
174 char* pa_bytes_snprint(char *s, size_t l, unsigned v) {
175 pa_assert(s);
176 pa_assert(l > 0);
177
178 pa_init_i18n();
179
180 if (v >= ((unsigned) 1024)*1024*1024)
181 pa_snprintf(s, l, _("%0.1f GiB"), ((double) v)/1024/1024/1024);
182 else if (v >= ((unsigned) 1024)*1024)
183 pa_snprintf(s, l, _("%0.1f MiB"), ((double) v)/1024/1024);
184 else if (v >= (unsigned) 1024)
185 pa_snprintf(s, l, _("%0.1f KiB"), ((double) v)/1024);
186 else
187 pa_snprintf(s, l, _("%u B"), (unsigned) v);
188
189 return s;
190 }
191
192 pa_sample_format_t pa_parse_sample_format(const char *format) {
193 pa_assert(format);
194
195 if (strcasecmp(format, "s16le") == 0)
196 return PA_SAMPLE_S16LE;
197 else if (strcasecmp(format, "s16be") == 0)
198 return PA_SAMPLE_S16BE;
199 else if (strcasecmp(format, "s16ne") == 0 || strcasecmp(format, "s16") == 0 || strcasecmp(format, "16") == 0)
200 return PA_SAMPLE_S16NE;
201 else if (strcasecmp(format, "s16re") == 0)
202 return PA_SAMPLE_S16RE;
203 else if (strcasecmp(format, "u8") == 0 || strcasecmp(format, "8") == 0)
204 return PA_SAMPLE_U8;
205 else if (strcasecmp(format, "float32") == 0 || strcasecmp(format, "float32ne") == 0 || strcasecmp(format, "float") == 0)
206 return PA_SAMPLE_FLOAT32NE;
207 else if (strcasecmp(format, "float32re") == 0)
208 return PA_SAMPLE_FLOAT32RE;
209 else if (strcasecmp(format, "float32le") == 0)
210 return PA_SAMPLE_FLOAT32LE;
211 else if (strcasecmp(format, "float32be") == 0)
212 return PA_SAMPLE_FLOAT32BE;
213 else if (strcasecmp(format, "ulaw") == 0 || strcasecmp(format, "mulaw") == 0)
214 return PA_SAMPLE_ULAW;
215 else if (strcasecmp(format, "alaw") == 0)
216 return PA_SAMPLE_ALAW;
217 else if (strcasecmp(format, "s32le") == 0)
218 return PA_SAMPLE_S32LE;
219 else if (strcasecmp(format, "s32be") == 0)
220 return PA_SAMPLE_S32BE;
221 else if (strcasecmp(format, "s32ne") == 0 || strcasecmp(format, "s32") == 0 || strcasecmp(format, "32") == 0)
222 return PA_SAMPLE_S32NE;
223 else if (strcasecmp(format, "s32re") == 0)
224 return PA_SAMPLE_S24RE;
225 else if (strcasecmp(format, "s24le") == 0)
226 return PA_SAMPLE_S24LE;
227 else if (strcasecmp(format, "s24be") == 0)
228 return PA_SAMPLE_S24BE;
229 else if (strcasecmp(format, "s24ne") == 0 || strcasecmp(format, "s24") == 0 || strcasecmp(format, "24") == 0)
230 return PA_SAMPLE_S24NE;
231 else if (strcasecmp(format, "s24re") == 0)
232 return PA_SAMPLE_S24RE;
233 else if (strcasecmp(format, "s24-32le") == 0)
234 return PA_SAMPLE_S24LE;
235 else if (strcasecmp(format, "s24-32be") == 0)
236 return PA_SAMPLE_S24BE;
237 else if (strcasecmp(format, "s24-32ne") == 0 || strcasecmp(format, "s24-32") == 0)
238 return PA_SAMPLE_S24NE;
239 else if (strcasecmp(format, "s24-32re") == 0)
240 return PA_SAMPLE_S24RE;
241
242 return -1;
243 }