]>
code.delx.au - pulseaudio/blob - polyp/sample.c
d587170c9297c37d57de4422aa233db5298b8bfa
4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 size_t pa_sample_size(const pa_sample_spec
*spec
) {
36 switch (spec
->format
) {
44 case PA_SAMPLE_FLOAT32LE
:
45 case PA_SAMPLE_FLOAT32BE
:
52 size_t pa_frame_size(const pa_sample_spec
*spec
) {
55 return pa_sample_size(spec
) * spec
->channels
;
58 size_t pa_bytes_per_second(const pa_sample_spec
*spec
) {
60 return spec
->rate
*pa_frame_size(spec
);
63 pa_usec_t
pa_bytes_to_usec(uint64_t length
, const pa_sample_spec
*spec
) {
66 return (pa_usec_t
) (((double) length
/pa_frame_size(spec
)*1000000)/spec
->rate
);
69 int pa_sample_spec_valid(const pa_sample_spec
*spec
) {
72 if (spec
->rate
<= 0 ||
73 spec
->channels
<= 0 ||
74 spec
->channels
> PA_CHANNELS_MAX
||
75 spec
->format
>= PA_SAMPLE_MAX
||
82 int pa_sample_spec_equal(const pa_sample_spec
*a
, const pa_sample_spec
*b
) {
85 return (a
->format
== b
->format
) && (a
->rate
== b
->rate
) && (a
->channels
== b
->channels
);
88 const char *pa_sample_format_to_string(pa_sample_format_t f
) {
89 static const char* const table
[]= {
90 [PA_SAMPLE_U8
] = "u8",
91 [PA_SAMPLE_ALAW
] = "aLaw",
92 [PA_SAMPLE_ULAW
] = "uLaw",
93 [PA_SAMPLE_S16LE
] = "s16le",
94 [PA_SAMPLE_S16BE
] = "s16be",
95 [PA_SAMPLE_FLOAT32LE
] = "float32le",
96 [PA_SAMPLE_FLOAT32BE
] = "float32be",
99 if (f
>= PA_SAMPLE_MAX
)
105 char *pa_sample_spec_snprint(char *s
, size_t l
, const pa_sample_spec
*spec
) {
106 assert(s
&& l
&& spec
);
108 if (!pa_sample_spec_valid(spec
))
109 snprintf(s
, l
, "Invalid");
111 snprintf(s
, l
, "%s %uch %uHz", pa_sample_format_to_string(spec
->format
), spec
->channels
, spec
->rate
);
116 void pa_bytes_snprint(char *s
, size_t l
, unsigned v
) {
117 if (v
>= ((unsigned) 1024)*1024*1024)
118 snprintf(s
, l
, "%0.1f GB", ((double) v
)/1024/1024/1024);
119 else if (v
>= ((unsigned) 1024)*1024)
120 snprintf(s
, l
, "%0.1f MB", ((double) v
)/1024/1024);
121 else if (v
>= (unsigned) 1024)
122 snprintf(s
, l
, "%0.1f KB", ((double) v
)/1024);
124 snprintf(s
, l
, "%u B", (unsigned) v
);
127 pa_sample_format_t
pa_parse_sample_format(const char *format
) {
129 if (strcasecmp(format
, "s16le") == 0)
130 return PA_SAMPLE_S16LE
;
131 else if (strcasecmp(format
, "s16be") == 0)
132 return PA_SAMPLE_S16BE
;
133 else if (strcasecmp(format
, "s16ne") == 0 || strcasecmp(format
, "s16") == 0 || strcasecmp(format
, "16") == 0)
134 return PA_SAMPLE_S16NE
;
135 else if (strcasecmp(format
, "u8") == 0 || strcasecmp(format
, "8") == 0)
137 else if (strcasecmp(format
, "float32") == 0 || strcasecmp(format
, "float32ne") == 0)
138 return PA_SAMPLE_FLOAT32
;
139 else if (strcasecmp(format
, "float32le") == 0)
140 return PA_SAMPLE_FLOAT32LE
;
141 else if (strcasecmp(format
, "float32be") == 0)
142 return PA_SAMPLE_FLOAT32BE
;
143 else if (strcasecmp(format
, "ulaw") == 0)
144 return PA_SAMPLE_ULAW
;
145 else if (strcasecmp(format
, "alaw") == 0)
146 return PA_SAMPLE_ALAW
;