]> code.delx.au - pulseaudio/blob - polyp/sample.c
* fix include file names in installed header files
[pulseaudio] / polyp / sample.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
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_frame_size(const struct pa_sample_spec *spec) {
34 size_t b = 1;
35 assert(spec);
36
37 switch (spec->format) {
38 case PA_SAMPLE_U8:
39 case PA_SAMPLE_ULAW:
40 case PA_SAMPLE_ALAW:
41 b = 1;
42 break;
43 case PA_SAMPLE_S16LE:
44 case PA_SAMPLE_S16BE:
45 b = 2;
46 break;
47 case PA_SAMPLE_FLOAT32LE:
48 case PA_SAMPLE_FLOAT32BE:
49 b = 4;
50 break;
51 default:
52 assert(0);
53 }
54
55 return b * spec->channels;
56 }
57
58 size_t pa_bytes_per_second(const struct pa_sample_spec *spec) {
59 assert(spec);
60 return spec->rate*pa_frame_size(spec);
61 }
62
63 pa_usec_t pa_bytes_to_usec(uint64_t length, const struct pa_sample_spec *spec) {
64 assert(spec);
65
66 return (pa_usec_t) (((double) length/pa_frame_size(spec)*1000000)/spec->rate);
67 }
68
69 int pa_sample_spec_valid(const struct pa_sample_spec *spec) {
70 assert(spec);
71
72 if (spec->rate <= 0 || spec->channels <= 0)
73 return 0;
74
75 if (spec->format >= PA_SAMPLE_MAX || spec->format < 0)
76 return 0;
77
78 return 1;
79 }
80
81 int pa_sample_spec_equal(const struct pa_sample_spec*a, const struct pa_sample_spec*b) {
82 assert(a && b);
83
84 return (a->format == b->format) && (a->rate == b->rate) && (a->channels == b->channels);
85 }
86
87 const char *pa_sample_format_to_string(enum pa_sample_format f) {
88 static const char* const table[]= {
89 [PA_SAMPLE_U8] = "U8",
90 [PA_SAMPLE_ALAW] = "ALAW",
91 [PA_SAMPLE_ULAW] = "ULAW",
92 [PA_SAMPLE_S16LE] = "S16LE",
93 [PA_SAMPLE_S16BE] = "S16BE",
94 [PA_SAMPLE_FLOAT32LE] = "FLOAT32LE",
95 [PA_SAMPLE_FLOAT32BE] = "FLOAT32BE",
96 };
97
98 if (f >= PA_SAMPLE_MAX)
99 return NULL;
100
101 return table[f];
102 }
103
104 void pa_sample_spec_snprint(char *s, size_t l, const struct pa_sample_spec *spec) {
105 assert(s && l && spec);
106
107 if (!pa_sample_spec_valid(spec)) {
108 snprintf(s, l, "Invalid");
109 return;
110 }
111
112 snprintf(s, l, "%s %uch %uHz", pa_sample_format_to_string(spec->format), spec->channels, spec->rate);
113 }
114
115 pa_volume_t pa_volume_multiply(pa_volume_t a, pa_volume_t b) {
116 uint64_t p = a;
117 p *= b;
118 p /= PA_VOLUME_NORM;
119
120 return (pa_volume_t) p;
121 }
122
123 pa_volume_t pa_volume_from_dB(double f) {
124 if (f <= PA_DECIBEL_MININFTY)
125 return PA_VOLUME_MUTED;
126
127 return (pa_volume_t) (pow(10, f/20)*PA_VOLUME_NORM);
128 }
129
130 double pa_volume_to_dB(pa_volume_t v) {
131 if (v == PA_VOLUME_MUTED)
132 return PA_DECIBEL_MININFTY;
133
134 return 20*log10((double) v/PA_VOLUME_NORM);
135 }
136
137 #define USER_DECIBEL_RANGE 30
138
139 double pa_volume_to_user(pa_volume_t v) {
140 double dB = pa_volume_to_dB(v);
141
142 return dB < -USER_DECIBEL_RANGE ? 0 : dB/USER_DECIBEL_RANGE+1;
143 }
144
145 pa_volume_t pa_volume_from_user(double v) {
146
147 if (v <= 0)
148 return PA_VOLUME_MUTED;
149
150 return pa_volume_from_dB((v-1)*USER_DECIBEL_RANGE);
151 }
152
153 void pa_bytes_snprint(char *s, size_t l, unsigned v) {
154 if (v >= ((unsigned) 1024)*1024*1024)
155 snprintf(s, l, "%0.1f GB", ((double) v)/1024/1024/1024);
156 else if (v >= ((unsigned) 1024)*1024)
157 snprintf(s, l, "%0.1f MB", ((double) v)/1024/1024);
158 else if (v >= (unsigned) 1024)
159 snprintf(s, l, "%0.1f KB", ((double) v)/1024);
160 else
161 snprintf(s, l, "%u B", (unsigned) v);
162 }
163
164 enum pa_sample_format pa_parse_sample_format(const char *format) {
165
166 if (strcasecmp(format, "s16le") == 0)
167 return PA_SAMPLE_S16LE;
168 else if (strcasecmp(format, "s16be") == 0)
169 return PA_SAMPLE_S16BE;
170 else if (strcasecmp(format, "s16ne") == 0 || strcasecmp(format, "s16") == 0 || strcasecmp(format, "16") == 0)
171 return PA_SAMPLE_S16NE;
172 else if (strcasecmp(format, "u8") == 0 || strcasecmp(format, "8") == 0)
173 return PA_SAMPLE_U8;
174 else if (strcasecmp(format, "float32") == 0 || strcasecmp(format, "float32ne") == 0)
175 return PA_SAMPLE_FLOAT32;
176 else if (strcasecmp(format, "float32le") == 0)
177 return PA_SAMPLE_FLOAT32LE;
178 else if (strcasecmp(format, "float32be") == 0)
179 return PA_SAMPLE_FLOAT32BE;
180 else if (strcasecmp(format, "ulaw") == 0)
181 return PA_SAMPLE_ULAW;
182 else if (strcasecmp(format, "alaw") == 0)
183 return PA_SAMPLE_ALAW;
184
185 return -1;
186 }