merge 'lennart' branch back into trunk.
[profile/ivi/pulseaudio.git] / src / pulse / sample.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2004-2006 Lennart Poettering
7   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
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.
13
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.
18
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
22   USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <math.h>
31 #include <string.h>
32
33 #include <pulsecore/core-util.h>
34 #include <pulsecore/macro.h>
35
36 #include "sample.h"
37
38 size_t pa_sample_size(const pa_sample_spec *spec) {
39
40     static const size_t table[] = {
41         [PA_SAMPLE_U8] = 1,
42         [PA_SAMPLE_ULAW] = 1,
43         [PA_SAMPLE_ALAW] = 1,
44         [PA_SAMPLE_S16LE] = 2,
45         [PA_SAMPLE_S16BE] = 2,
46         [PA_SAMPLE_FLOAT32LE] = 4,
47         [PA_SAMPLE_FLOAT32BE] = 4
48     };
49
50     pa_assert(spec);
51     pa_assert(spec->format >= 0);
52     pa_assert(spec->format < PA_SAMPLE_MAX);
53
54     return table[spec->format];
55 }
56
57 size_t pa_frame_size(const pa_sample_spec *spec) {
58     pa_assert(spec);
59
60     return pa_sample_size(spec) * spec->channels;
61 }
62
63 size_t pa_bytes_per_second(const pa_sample_spec *spec) {
64     pa_assert(spec);
65     return spec->rate*pa_frame_size(spec);
66 }
67
68 pa_usec_t pa_bytes_to_usec(uint64_t length, const pa_sample_spec *spec) {
69     pa_assert(spec);
70
71     return (pa_usec_t) (((double) length/pa_frame_size(spec)*1000000)/spec->rate);
72 }
73
74 size_t pa_usec_to_bytes(pa_usec_t t, const pa_sample_spec *spec) {
75     pa_assert(spec);
76
77     return (size_t) (((double) t * spec->rate / 1000000))*pa_frame_size(spec);
78 }
79
80 int pa_sample_spec_valid(const pa_sample_spec *spec) {
81     pa_assert(spec);
82
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 ||
88         spec->format < 0)
89         return 0;
90
91     return 1;
92 }
93
94 int pa_sample_spec_equal(const pa_sample_spec*a, const pa_sample_spec*b) {
95     pa_assert(a);
96     pa_assert(b);
97
98     return (a->format == b->format) && (a->rate == b->rate) && (a->channels == b->channels);
99 }
100
101 const char *pa_sample_format_to_string(pa_sample_format_t f) {
102     static const char* const table[]= {
103         [PA_SAMPLE_U8] = "u8",
104         [PA_SAMPLE_ALAW] = "aLaw",
105         [PA_SAMPLE_ULAW] = "uLaw",
106         [PA_SAMPLE_S16LE] = "s16le",
107         [PA_SAMPLE_S16BE] = "s16be",
108         [PA_SAMPLE_FLOAT32LE] = "float32le",
109         [PA_SAMPLE_FLOAT32BE] = "float32be",
110     };
111
112     if (f < 0 || f >= PA_SAMPLE_MAX)
113         return NULL;
114
115     return table[f];
116 }
117
118 char *pa_sample_spec_snprint(char *s, size_t l, const pa_sample_spec *spec) {
119     pa_assert(s);
120     pa_assert(l);
121     pa_assert(spec);
122
123     if (!pa_sample_spec_valid(spec))
124         pa_snprintf(s, l, "Invalid");
125     else
126         pa_snprintf(s, l, "%s %uch %uHz", pa_sample_format_to_string(spec->format), spec->channels, spec->rate);
127
128     return s;
129 }
130
131 char* pa_bytes_snprint(char *s, size_t l, unsigned v) {
132     pa_assert(s);
133
134     if (v >= ((unsigned) 1024)*1024*1024)
135         pa_snprintf(s, l, "%0.1f GiB", ((double) v)/1024/1024/1024);
136     else if (v >= ((unsigned) 1024)*1024)
137         pa_snprintf(s, l, "%0.1f MiB", ((double) v)/1024/1024);
138     else if (v >= (unsigned) 1024)
139         pa_snprintf(s, l, "%0.1f KiB", ((double) v)/1024);
140     else
141         pa_snprintf(s, l, "%u B", (unsigned) v);
142
143     return s;
144 }
145
146 pa_sample_format_t pa_parse_sample_format(const char *format) {
147     pa_assert(format);
148
149     if (strcasecmp(format, "s16le") == 0)
150         return PA_SAMPLE_S16LE;
151     else if (strcasecmp(format, "s16be") == 0)
152         return PA_SAMPLE_S16BE;
153     else if (strcasecmp(format, "s16ne") == 0 || strcasecmp(format, "s16") == 0 || strcasecmp(format, "16") == 0)
154         return PA_SAMPLE_S16NE;
155     else if (strcasecmp(format, "s16re") == 0)
156         return PA_SAMPLE_S16RE;
157     else if (strcasecmp(format, "u8") == 0 || strcasecmp(format, "8") == 0)
158         return PA_SAMPLE_U8;
159     else if (strcasecmp(format, "float32") == 0 || strcasecmp(format, "float32ne") == 0)
160         return PA_SAMPLE_FLOAT32NE;
161     else if (strcasecmp(format, "float32re") == 0)
162         return PA_SAMPLE_FLOAT32RE;
163     else if (strcasecmp(format, "float32le") == 0)
164         return PA_SAMPLE_FLOAT32LE;
165     else if (strcasecmp(format, "float32be") == 0)
166         return PA_SAMPLE_FLOAT32BE;
167     else if (strcasecmp(format, "ulaw") == 0 || strcasecmp(format, "mulaw") == 0)
168         return PA_SAMPLE_ULAW;
169     else if (strcasecmp(format, "alaw") == 0)
170         return PA_SAMPLE_ALAW;
171
172     return -1;
173 }