]>
code.delx.au - pulseaudio/blob - src/pulse/sample.c
4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 #include <pulsecore/core-util.h>
37 size_t pa_sample_size(const pa_sample_spec
*spec
) {
40 switch (spec
->format
) {
48 case PA_SAMPLE_FLOAT32LE
:
49 case PA_SAMPLE_FLOAT32BE
:
57 size_t pa_frame_size(const pa_sample_spec
*spec
) {
60 return pa_sample_size(spec
) * spec
->channels
;
63 size_t pa_bytes_per_second(const pa_sample_spec
*spec
) {
65 return spec
->rate
*pa_frame_size(spec
);
68 pa_usec_t
pa_bytes_to_usec(uint64_t length
, const pa_sample_spec
*spec
) {
71 return (pa_usec_t
) (((double) length
/pa_frame_size(spec
)*1000000)/spec
->rate
);
74 size_t pa_usec_to_bytes(pa_usec_t t
, const pa_sample_spec
*spec
) {
77 return (size_t) (((double) t
* spec
->rate
/ 1000000))*pa_frame_size(spec
);
80 int pa_sample_spec_valid(const pa_sample_spec
*spec
) {
83 if (spec
->rate
<= 0 ||
84 spec
->rate
> PA_RATE_MAX
||
85 spec
->channels
<= 0 ||
86 spec
->channels
> PA_CHANNELS_MAX
||
87 spec
->format
>= PA_SAMPLE_MAX
||
94 int pa_sample_spec_equal(const pa_sample_spec
*a
, const pa_sample_spec
*b
) {
97 return (a
->format
== b
->format
) && (a
->rate
== b
->rate
) && (a
->channels
== b
->channels
);
100 const char *pa_sample_format_to_string(pa_sample_format_t f
) {
101 static const char* const table
[]= {
102 [PA_SAMPLE_U8
] = "u8",
103 [PA_SAMPLE_ALAW
] = "aLaw",
104 [PA_SAMPLE_ULAW
] = "uLaw",
105 [PA_SAMPLE_S16LE
] = "s16le",
106 [PA_SAMPLE_S16BE
] = "s16be",
107 [PA_SAMPLE_FLOAT32LE
] = "float32le",
108 [PA_SAMPLE_FLOAT32BE
] = "float32be",
111 if (f
>= PA_SAMPLE_MAX
)
117 char *pa_sample_spec_snprint(char *s
, size_t l
, const pa_sample_spec
*spec
) {
118 assert(s
&& l
&& spec
);
120 if (!pa_sample_spec_valid(spec
))
121 pa_snprintf(s
, l
, "Invalid");
123 pa_snprintf(s
, l
, "%s %uch %uHz", pa_sample_format_to_string(spec
->format
), spec
->channels
, spec
->rate
);
128 char* pa_bytes_snprint(char *s
, size_t l
, unsigned v
) {
129 if (v
>= ((unsigned) 1024)*1024*1024)
130 pa_snprintf(s
, l
, "%0.1f GiB", ((double) v
)/1024/1024/1024);
131 else if (v
>= ((unsigned) 1024)*1024)
132 pa_snprintf(s
, l
, "%0.1f MiB", ((double) v
)/1024/1024);
133 else if (v
>= (unsigned) 1024)
134 pa_snprintf(s
, l
, "%0.1f KiB", ((double) v
)/1024);
136 pa_snprintf(s
, l
, "%u B", (unsigned) v
);
141 pa_sample_format_t
pa_parse_sample_format(const char *format
) {
143 if (strcasecmp(format
, "s16le") == 0)
144 return PA_SAMPLE_S16LE
;
145 else if (strcasecmp(format
, "s16be") == 0)
146 return PA_SAMPLE_S16BE
;
147 else if (strcasecmp(format
, "s16ne") == 0 || strcasecmp(format
, "s16") == 0 || strcasecmp(format
, "16") == 0)
148 return PA_SAMPLE_S16NE
;
149 else if (strcasecmp(format
, "u8") == 0 || strcasecmp(format
, "8") == 0)
151 else if (strcasecmp(format
, "float32") == 0 || strcasecmp(format
, "float32ne") == 0)
152 return PA_SAMPLE_FLOAT32
;
153 else if (strcasecmp(format
, "float32le") == 0)
154 return PA_SAMPLE_FLOAT32LE
;
155 else if (strcasecmp(format
, "float32be") == 0)
156 return PA_SAMPLE_FLOAT32BE
;
157 else if (strcasecmp(format
, "ulaw") == 0)
158 return PA_SAMPLE_ULAW
;
159 else if (strcasecmp(format
, "alaw") == 0)
160 return PA_SAMPLE_ALAW
;