modify pa_bytes_snprint() to return the string we just wrote to. This should be binar...
[profile/ivi/pulseaudio.git] / src / pulse / sample.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5  
6   PulseAudio 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.
10  
11   PulseAudio 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.
15  
16   You should have received a copy of the GNU Lesser General Public License
17   along with PulseAudio; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <assert.h>
28 #include <math.h>
29 #include <string.h>
30
31 #include "sample.h"
32
33 size_t pa_sample_size(const pa_sample_spec *spec) {
34     assert(spec);
35
36     switch (spec->format) {
37         case PA_SAMPLE_U8:
38         case PA_SAMPLE_ULAW:
39         case PA_SAMPLE_ALAW:
40             return 1;
41         case PA_SAMPLE_S16LE:
42         case PA_SAMPLE_S16BE:
43             return 2;
44         case PA_SAMPLE_FLOAT32LE:
45         case PA_SAMPLE_FLOAT32BE:
46             return 4;
47         default:
48             assert(0);
49             return 0;
50     }
51 }
52
53 size_t pa_frame_size(const pa_sample_spec *spec) {
54     assert(spec);
55
56     return pa_sample_size(spec) * spec->channels;
57 }
58
59 size_t pa_bytes_per_second(const pa_sample_spec *spec) {
60     assert(spec);
61     return spec->rate*pa_frame_size(spec);
62 }
63
64 pa_usec_t pa_bytes_to_usec(uint64_t length, const pa_sample_spec *spec) {
65     assert(spec);
66
67     return (pa_usec_t) (((double) length/pa_frame_size(spec)*1000000)/spec->rate);
68 }
69
70 size_t pa_usec_to_bytes(pa_usec_t t, const pa_sample_spec *spec) {
71     assert(spec);
72
73     return (size_t) (((double) t * spec->rate / 1000000))*pa_frame_size(spec); 
74 }
75
76 int pa_sample_spec_valid(const pa_sample_spec *spec) {
77     assert(spec);
78
79     if (spec->rate <= 0 ||
80         spec->channels <= 0 ||
81         spec->channels > PA_CHANNELS_MAX ||
82         spec->format >= PA_SAMPLE_MAX ||
83         spec->format < 0)
84         return 0;
85
86     return 1;
87 }
88
89 int pa_sample_spec_equal(const pa_sample_spec*a, const pa_sample_spec*b) {
90     assert(a && b);
91
92     return (a->format == b->format) && (a->rate == b->rate) && (a->channels == b->channels);
93 }
94
95 const char *pa_sample_format_to_string(pa_sample_format_t f) {
96     static const char* const table[]= {
97         [PA_SAMPLE_U8] = "u8",
98         [PA_SAMPLE_ALAW] = "aLaw",
99         [PA_SAMPLE_ULAW] = "uLaw",
100         [PA_SAMPLE_S16LE] = "s16le",
101         [PA_SAMPLE_S16BE] = "s16be",
102         [PA_SAMPLE_FLOAT32LE] = "float32le",
103         [PA_SAMPLE_FLOAT32BE] = "float32be",
104     };
105
106     if (f >= PA_SAMPLE_MAX)
107         return NULL;
108
109     return table[f];
110 }
111
112 char *pa_sample_spec_snprint(char *s, size_t l, const pa_sample_spec *spec) {
113     assert(s && l && spec);
114     
115     if (!pa_sample_spec_valid(spec))
116         snprintf(s, l, "Invalid");
117     else
118         snprintf(s, l, "%s %uch %uHz", pa_sample_format_to_string(spec->format), spec->channels, spec->rate);
119
120     return s;
121 }
122
123 char* pa_bytes_snprint(char *s, size_t l, unsigned v) {
124     if (v >= ((unsigned) 1024)*1024*1024)
125         snprintf(s, l, "%0.1f GiB", ((double) v)/1024/1024/1024);
126     else if (v >= ((unsigned) 1024)*1024)
127         snprintf(s, l, "%0.1f MiB", ((double) v)/1024/1024);
128     else if (v >= (unsigned) 1024)
129         snprintf(s, l, "%0.1f KiB", ((double) v)/1024);
130     else
131         snprintf(s, l, "%u B", (unsigned) v);
132
133     return s;
134 }
135
136 pa_sample_format_t pa_parse_sample_format(const char *format) {
137     
138     if (strcasecmp(format, "s16le") == 0)
139         return PA_SAMPLE_S16LE;
140     else if (strcasecmp(format, "s16be") == 0)
141         return PA_SAMPLE_S16BE;
142     else if (strcasecmp(format, "s16ne") == 0 || strcasecmp(format, "s16") == 0 || strcasecmp(format, "16") == 0)
143         return PA_SAMPLE_S16NE;
144     else if (strcasecmp(format, "u8") == 0 || strcasecmp(format, "8") == 0)
145         return PA_SAMPLE_U8;
146     else if (strcasecmp(format, "float32") == 0 || strcasecmp(format, "float32ne") == 0)
147         return PA_SAMPLE_FLOAT32;
148     else if (strcasecmp(format, "float32le") == 0)
149         return PA_SAMPLE_FLOAT32LE;
150     else if (strcasecmp(format, "float32be") == 0)
151         return PA_SAMPLE_FLOAT32BE;
152     else if (strcasecmp(format, "ulaw") == 0)
153         return PA_SAMPLE_ULAW;
154     else if (strcasecmp(format, "alaw") == 0)
155         return PA_SAMPLE_ALAW;
156
157     return -1;
158 }