]> code.delx.au - pulseaudio/blob - src/pulse/sample.c
ffdeedf75d91294f40e6d21780e2ff61f7dc2452
[pulseaudio] / 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 <assert.h>
31 #include <math.h>
32 #include <string.h>
33
34 #include "sample.h"
35
36 size_t pa_sample_size(const pa_sample_spec *spec) {
37 assert(spec);
38
39 switch (spec->format) {
40 case PA_SAMPLE_U8:
41 case PA_SAMPLE_ULAW:
42 case PA_SAMPLE_ALAW:
43 return 1;
44 case PA_SAMPLE_S16LE:
45 case PA_SAMPLE_S16BE:
46 return 2;
47 case PA_SAMPLE_FLOAT32LE:
48 case PA_SAMPLE_FLOAT32BE:
49 return 4;
50 default:
51 assert(0);
52 return 0;
53 }
54 }
55
56 size_t pa_frame_size(const pa_sample_spec *spec) {
57 assert(spec);
58
59 return pa_sample_size(spec) * spec->channels;
60 }
61
62 size_t pa_bytes_per_second(const pa_sample_spec *spec) {
63 assert(spec);
64 return spec->rate*pa_frame_size(spec);
65 }
66
67 pa_usec_t pa_bytes_to_usec(uint64_t length, const pa_sample_spec *spec) {
68 assert(spec);
69
70 return (pa_usec_t) (((double) length/pa_frame_size(spec)*1000000)/spec->rate);
71 }
72
73 size_t pa_usec_to_bytes(pa_usec_t t, const pa_sample_spec *spec) {
74 assert(spec);
75
76 return (size_t) (((double) t * spec->rate / 1000000))*pa_frame_size(spec);
77 }
78
79 int pa_sample_spec_valid(const pa_sample_spec *spec) {
80 assert(spec);
81
82 if (spec->rate <= 0 ||
83 spec->rate > PA_RATE_MAX ||
84 spec->channels <= 0 ||
85 spec->channels > PA_CHANNELS_MAX ||
86 spec->format >= PA_SAMPLE_MAX ||
87 spec->format < 0)
88 return 0;
89
90 return 1;
91 }
92
93 int pa_sample_spec_equal(const pa_sample_spec*a, const pa_sample_spec*b) {
94 assert(a && b);
95
96 return (a->format == b->format) && (a->rate == b->rate) && (a->channels == b->channels);
97 }
98
99 const char *pa_sample_format_to_string(pa_sample_format_t f) {
100 static const char* const table[]= {
101 [PA_SAMPLE_U8] = "u8",
102 [PA_SAMPLE_ALAW] = "aLaw",
103 [PA_SAMPLE_ULAW] = "uLaw",
104 [PA_SAMPLE_S16LE] = "s16le",
105 [PA_SAMPLE_S16BE] = "s16be",
106 [PA_SAMPLE_FLOAT32LE] = "float32le",
107 [PA_SAMPLE_FLOAT32BE] = "float32be",
108 };
109
110 if (f >= PA_SAMPLE_MAX)
111 return NULL;
112
113 return table[f];
114 }
115
116 char *pa_sample_spec_snprint(char *s, size_t l, const pa_sample_spec *spec) {
117 assert(s && l && spec);
118
119 if (!pa_sample_spec_valid(spec))
120 snprintf(s, l, "Invalid");
121 else
122 snprintf(s, l, "%s %uch %uHz", pa_sample_format_to_string(spec->format), spec->channels, spec->rate);
123
124 return s;
125 }
126
127 char* pa_bytes_snprint(char *s, size_t l, unsigned v) {
128 if (v >= ((unsigned) 1024)*1024*1024)
129 snprintf(s, l, "%0.1f GiB", ((double) v)/1024/1024/1024);
130 else if (v >= ((unsigned) 1024)*1024)
131 snprintf(s, l, "%0.1f MiB", ((double) v)/1024/1024);
132 else if (v >= (unsigned) 1024)
133 snprintf(s, l, "%0.1f KiB", ((double) v)/1024);
134 else
135 snprintf(s, l, "%u B", (unsigned) v);
136
137 return s;
138 }
139
140 pa_sample_format_t pa_parse_sample_format(const char *format) {
141
142 if (strcasecmp(format, "s16le") == 0)
143 return PA_SAMPLE_S16LE;
144 else if (strcasecmp(format, "s16be") == 0)
145 return PA_SAMPLE_S16BE;
146 else if (strcasecmp(format, "s16ne") == 0 || strcasecmp(format, "s16") == 0 || strcasecmp(format, "16") == 0)
147 return PA_SAMPLE_S16NE;
148 else if (strcasecmp(format, "u8") == 0 || strcasecmp(format, "8") == 0)
149 return PA_SAMPLE_U8;
150 else if (strcasecmp(format, "float32") == 0 || strcasecmp(format, "float32ne") == 0)
151 return PA_SAMPLE_FLOAT32;
152 else if (strcasecmp(format, "float32le") == 0)
153 return PA_SAMPLE_FLOAT32LE;
154 else if (strcasecmp(format, "float32be") == 0)
155 return PA_SAMPLE_FLOAT32BE;
156 else if (strcasecmp(format, "ulaw") == 0)
157 return PA_SAMPLE_ULAW;
158 else if (strcasecmp(format, "alaw") == 0)
159 return PA_SAMPLE_ALAW;
160
161 return -1;
162 }