]>
code.delx.au - pulseaudio/blob - src/pulse/sample.c
2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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
31 #include <pulsecore/core-util.h>
32 #include <pulsecore/macro.h>
33 #include <pulse/timeval.h>
37 size_t pa_sample_size(const pa_sample_spec
*spec
) {
39 static const size_t table
[] = {
43 [PA_SAMPLE_S16LE
] = 2,
44 [PA_SAMPLE_S16BE
] = 2,
45 [PA_SAMPLE_FLOAT32LE
] = 4,
46 [PA_SAMPLE_FLOAT32BE
] = 4,
47 [PA_SAMPLE_S32LE
] = 4,
48 [PA_SAMPLE_S32BE
] = 4,
52 pa_assert(spec
->format
>= 0);
53 pa_assert(spec
->format
< PA_SAMPLE_MAX
);
55 return table
[spec
->format
];
58 size_t pa_frame_size(const pa_sample_spec
*spec
) {
61 return pa_sample_size(spec
) * spec
->channels
;
64 size_t pa_bytes_per_second(const pa_sample_spec
*spec
) {
66 return spec
->rate
*pa_frame_size(spec
);
69 pa_usec_t
pa_bytes_to_usec(uint64_t length
, const pa_sample_spec
*spec
) {
72 return (((pa_usec_t
) (length
/ pa_frame_size(spec
)) * PA_USEC_PER_SEC
) / spec
->rate
);
75 size_t pa_usec_to_bytes(pa_usec_t t
, const pa_sample_spec
*spec
) {
78 return (size_t) (((t
* spec
->rate
) / PA_USEC_PER_SEC
)) * pa_frame_size(spec
);
81 int pa_sample_spec_valid(const pa_sample_spec
*spec
) {
84 if (spec
->rate
<= 0 ||
85 spec
->rate
> PA_RATE_MAX
||
86 spec
->channels
<= 0 ||
87 spec
->channels
> PA_CHANNELS_MAX
||
88 spec
->format
>= PA_SAMPLE_MAX
||
95 int pa_sample_spec_equal(const pa_sample_spec
*a
, const pa_sample_spec
*b
) {
100 (a
->format
== b
->format
) &&
101 (a
->rate
== b
->rate
) &&
102 (a
->channels
== b
->channels
);
105 const char *pa_sample_format_to_string(pa_sample_format_t f
) {
106 static const char* const table
[]= {
107 [PA_SAMPLE_U8
] = "u8",
108 [PA_SAMPLE_ALAW
] = "aLaw",
109 [PA_SAMPLE_ULAW
] = "uLaw",
110 [PA_SAMPLE_S16LE
] = "s16le",
111 [PA_SAMPLE_S16BE
] = "s16be",
112 [PA_SAMPLE_FLOAT32LE
] = "float32le",
113 [PA_SAMPLE_FLOAT32BE
] = "float32be",
114 [PA_SAMPLE_S32LE
] = "s32le",
115 [PA_SAMPLE_S32BE
] = "s32be",
118 if (f
< 0 || f
>= PA_SAMPLE_MAX
)
124 char *pa_sample_spec_snprint(char *s
, size_t l
, const pa_sample_spec
*spec
) {
129 if (!pa_sample_spec_valid(spec
))
130 pa_snprintf(s
, l
, "Invalid");
132 pa_snprintf(s
, l
, "%s %uch %uHz", pa_sample_format_to_string(spec
->format
), spec
->channels
, spec
->rate
);
137 char* pa_bytes_snprint(char *s
, size_t l
, unsigned v
) {
140 if (v
>= ((unsigned) 1024)*1024*1024)
141 pa_snprintf(s
, l
, "%0.1f GiB", ((double) v
)/1024/1024/1024);
142 else if (v
>= ((unsigned) 1024)*1024)
143 pa_snprintf(s
, l
, "%0.1f MiB", ((double) v
)/1024/1024);
144 else if (v
>= (unsigned) 1024)
145 pa_snprintf(s
, l
, "%0.1f KiB", ((double) v
)/1024);
147 pa_snprintf(s
, l
, "%u B", (unsigned) v
);
152 pa_sample_format_t
pa_parse_sample_format(const char *format
) {
155 if (strcasecmp(format
, "s16le") == 0)
156 return PA_SAMPLE_S16LE
;
157 else if (strcasecmp(format
, "s16be") == 0)
158 return PA_SAMPLE_S16BE
;
159 else if (strcasecmp(format
, "s16ne") == 0 || strcasecmp(format
, "s16") == 0 || strcasecmp(format
, "16") == 0)
160 return PA_SAMPLE_S16NE
;
161 else if (strcasecmp(format
, "s16re") == 0)
162 return PA_SAMPLE_S16RE
;
163 else if (strcasecmp(format
, "u8") == 0 || strcasecmp(format
, "8") == 0)
165 else if (strcasecmp(format
, "float32") == 0 || strcasecmp(format
, "float32ne") == 0 || strcasecmp(format
, "float") == 0)
166 return PA_SAMPLE_FLOAT32NE
;
167 else if (strcasecmp(format
, "float32re") == 0)
168 return PA_SAMPLE_FLOAT32RE
;
169 else if (strcasecmp(format
, "float32le") == 0)
170 return PA_SAMPLE_FLOAT32LE
;
171 else if (strcasecmp(format
, "float32be") == 0)
172 return PA_SAMPLE_FLOAT32BE
;
173 else if (strcasecmp(format
, "ulaw") == 0 || strcasecmp(format
, "mulaw") == 0)
174 return PA_SAMPLE_ULAW
;
175 else if (strcasecmp(format
, "alaw") == 0)
176 return PA_SAMPLE_ALAW
;
177 else if (strcasecmp(format
, "s32le") == 0)
178 return PA_SAMPLE_S32LE
;
179 else if (strcasecmp(format
, "s32be") == 0)
180 return PA_SAMPLE_S32BE
;
181 else if (strcasecmp(format
, "s32ne") == 0 || strcasecmp(format
, "s32") == 0 || strcasecmp(format
, "32") == 0)
182 return PA_SAMPLE_S32NE
;
183 else if (strcasecmp(format
, "s32re") == 0)
184 return PA_SAMPLE_S32RE
;