]> code.delx.au - pulseaudio/blob - polyp/sample.c
add PA_MININFTY
[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 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 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
30 #include "sample.h"
31
32 size_t pa_frame_size(const struct pa_sample_spec *spec) {
33 size_t b = 1;
34 assert(spec);
35
36 switch (spec->format) {
37 case PA_SAMPLE_U8:
38 case PA_SAMPLE_ULAW:
39 case PA_SAMPLE_ALAW:
40 b = 1;
41 break;
42 case PA_SAMPLE_S16LE:
43 case PA_SAMPLE_S16BE:
44 b = 2;
45 break;
46 case PA_SAMPLE_FLOAT32LE:
47 case PA_SAMPLE_FLOAT32BE:
48 b = 4;
49 break;
50 default:
51 assert(0);
52 }
53
54 return b * spec->channels;
55 }
56
57 size_t pa_bytes_per_second(const struct pa_sample_spec *spec) {
58 assert(spec);
59 return spec->rate*pa_frame_size(spec);
60 }
61
62 uint32_t pa_bytes_to_usec(size_t length, const struct pa_sample_spec *spec) {
63 assert(spec);
64
65 return (uint32_t) (((double) length/pa_frame_size(spec)*1000000)/spec->rate);
66 }
67
68 int pa_sample_spec_valid(const struct pa_sample_spec *spec) {
69 assert(spec);
70
71 if (!spec->rate || !spec->channels)
72 return 0;
73
74 if (spec->format >= PA_SAMPLE_MAX)
75 return 0;
76
77 return 1;
78 }
79
80 int pa_sample_spec_equal(const struct pa_sample_spec*a, const struct pa_sample_spec*b) {
81 assert(a && b);
82
83 return (a->format == b->format) && (a->rate == b->rate) && (a->channels == b->channels);
84 }
85
86 void pa_sample_spec_snprint(char *s, size_t l, const struct pa_sample_spec *spec) {
87 static const char* const table[]= {
88 [PA_SAMPLE_U8] = "U8",
89 [PA_SAMPLE_ALAW] = "ALAW",
90 [PA_SAMPLE_ULAW] = "ULAW",
91 [PA_SAMPLE_S16LE] = "S16LE",
92 [PA_SAMPLE_S16BE] = "S16BE",
93 [PA_SAMPLE_FLOAT32LE] = "FLOAT32LE",
94 [PA_SAMPLE_FLOAT32BE] = "FLOAT32BE",
95 };
96
97 assert(pa_sample_spec_valid(spec));
98 snprintf(s, l, "%s %uch %uHz", table[spec->format], spec->channels, spec->rate);
99 }
100
101 pa_volume_t pa_volume_multiply(pa_volume_t a, pa_volume_t b) {
102 uint64_t p = a;
103 p *= b;
104 p /= PA_VOLUME_NORM;
105
106 return (pa_volume_t) p;
107 }
108
109 pa_volume_t pa_volume_from_dB(double f) {
110 if (f <= PA_DECIBEL_MININFTY)
111 return PA_VOLUME_MUTED;
112
113 return (pa_volume_t) (pow(10, f/20)*PA_VOLUME_NORM);
114 }
115
116 double pa_volume_to_dB(pa_volume_t v) {
117 if (v == PA_VOLUME_MUTED)
118 return PA_DECIBEL_MININFTY;
119
120 return 20*log10((double) v/PA_VOLUME_NORM);
121 }