add new API function pa_sample_spec_init()
[profile/ivi/pulseaudio.git] / 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(const pa_sample_spec *spec) {
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     };
52
53     pa_assert(spec);
54     pa_assert(spec->format >= 0);
55     pa_assert(spec->format < PA_SAMPLE_MAX);
56
57     return table[spec->format];
58 }
59
60 size_t pa_frame_size(const pa_sample_spec *spec) {
61     pa_assert(spec);
62
63     return pa_sample_size(spec) * spec->channels;
64 }
65
66 size_t pa_bytes_per_second(const pa_sample_spec *spec) {
67     pa_assert(spec);
68     return spec->rate*pa_frame_size(spec);
69 }
70
71 pa_usec_t pa_bytes_to_usec(uint64_t length, const pa_sample_spec *spec) {
72     pa_assert(spec);
73
74     return (((pa_usec_t) (length / pa_frame_size(spec)) * PA_USEC_PER_SEC) / spec->rate);
75 }
76
77 size_t pa_usec_to_bytes(pa_usec_t t, const pa_sample_spec *spec) {
78     pa_assert(spec);
79
80     return (size_t) (((t * spec->rate) / PA_USEC_PER_SEC)) * pa_frame_size(spec);
81 }
82
83 pa_sample_spec* pa_sample_spec_init(pa_sample_spec *spec) {
84     pa_assert(spec);
85
86     spec->format = PA_SAMPLE_INVALID;
87     spec->rate = 0;
88     spec->channels = 0;
89
90     return spec;
91 }
92
93 int pa_sample_spec_valid(const pa_sample_spec *spec) {
94     pa_assert(spec);
95
96     if (spec->rate <= 0 ||
97         spec->rate > PA_RATE_MAX ||
98         spec->channels <= 0 ||
99         spec->channels > PA_CHANNELS_MAX ||
100         spec->format >= PA_SAMPLE_MAX ||
101         spec->format < 0)
102         return 0;
103
104     return 1;
105 }
106
107 int pa_sample_spec_equal(const pa_sample_spec*a, const pa_sample_spec*b) {
108     pa_assert(a);
109     pa_assert(b);
110
111     return
112         (a->format == b->format) &&
113         (a->rate == b->rate) &&
114         (a->channels == b->channels);
115 }
116
117 const char *pa_sample_format_to_string(pa_sample_format_t f) {
118     static const char* const table[]= {
119         [PA_SAMPLE_U8] = "u8",
120         [PA_SAMPLE_ALAW] = "aLaw",
121         [PA_SAMPLE_ULAW] = "uLaw",
122         [PA_SAMPLE_S16LE] = "s16le",
123         [PA_SAMPLE_S16BE] = "s16be",
124         [PA_SAMPLE_FLOAT32LE] = "float32le",
125         [PA_SAMPLE_FLOAT32BE] = "float32be",
126         [PA_SAMPLE_S32LE] = "s32le",
127         [PA_SAMPLE_S32BE] = "s32be",
128     };
129
130     if (f < 0 || f >= PA_SAMPLE_MAX)
131         return NULL;
132
133     return table[f];
134 }
135
136 char *pa_sample_spec_snprint(char *s, size_t l, const pa_sample_spec *spec) {
137     pa_assert(s);
138     pa_assert(l);
139     pa_assert(spec);
140
141     pa_init_i18n();
142
143     if (!pa_sample_spec_valid(spec))
144         pa_snprintf(s, l, _("(invalid)"));
145     else
146         pa_snprintf(s, l, "%s %uch %uHz", pa_sample_format_to_string(spec->format), spec->channels, spec->rate);
147
148     return s;
149 }
150
151 char* pa_bytes_snprint(char *s, size_t l, unsigned v) {
152     pa_assert(s);
153
154     if (v >= ((unsigned) 1024)*1024*1024)
155         pa_snprintf(s, l, "%0.1f GiB", ((double) v)/1024/1024/1024);
156     else if (v >= ((unsigned) 1024)*1024)
157         pa_snprintf(s, l, "%0.1f MiB", ((double) v)/1024/1024);
158     else if (v >= (unsigned) 1024)
159         pa_snprintf(s, l, "%0.1f KiB", ((double) v)/1024);
160     else
161         pa_snprintf(s, l, "%u B", (unsigned) v);
162
163     return s;
164 }
165
166 pa_sample_format_t pa_parse_sample_format(const char *format) {
167     pa_assert(format);
168
169     if (strcasecmp(format, "s16le") == 0)
170         return PA_SAMPLE_S16LE;
171     else if (strcasecmp(format, "s16be") == 0)
172         return PA_SAMPLE_S16BE;
173     else if (strcasecmp(format, "s16ne") == 0 || strcasecmp(format, "s16") == 0 || strcasecmp(format, "16") == 0)
174         return PA_SAMPLE_S16NE;
175     else if (strcasecmp(format, "s16re") == 0)
176         return PA_SAMPLE_S16RE;
177     else if (strcasecmp(format, "u8") == 0 || strcasecmp(format, "8") == 0)
178         return PA_SAMPLE_U8;
179     else if (strcasecmp(format, "float32") == 0 || strcasecmp(format, "float32ne") == 0 || strcasecmp(format, "float") == 0)
180         return PA_SAMPLE_FLOAT32NE;
181     else if (strcasecmp(format, "float32re") == 0)
182         return PA_SAMPLE_FLOAT32RE;
183     else if (strcasecmp(format, "float32le") == 0)
184         return PA_SAMPLE_FLOAT32LE;
185     else if (strcasecmp(format, "float32be") == 0)
186         return PA_SAMPLE_FLOAT32BE;
187     else if (strcasecmp(format, "ulaw") == 0 || strcasecmp(format, "mulaw") == 0)
188         return PA_SAMPLE_ULAW;
189     else if (strcasecmp(format, "alaw") == 0)
190         return PA_SAMPLE_ALAW;
191     else if (strcasecmp(format, "s32le") == 0)
192         return PA_SAMPLE_S32LE;
193     else if (strcasecmp(format, "s32be") == 0)
194         return PA_SAMPLE_S32BE;
195     else if (strcasecmp(format, "s32ne") == 0 || strcasecmp(format, "s32") == 0 || strcasecmp(format, "32") == 0)
196         return PA_SAMPLE_S32NE;
197     else if (strcasecmp(format, "s32re") == 0)
198         return PA_SAMPLE_S32RE;
199
200     return -1;
201 }