]> code.delx.au - pulseaudio/blob - polyp/sample.c
sample cache work
[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
29 #include "sample.h"
30
31 size_t pa_frame_size(const struct pa_sample_spec *spec) {
32 assert(spec);
33 size_t b = 1;
34
35 switch (spec->format) {
36 case PA_SAMPLE_U8:
37 case PA_SAMPLE_ULAW:
38 case PA_SAMPLE_ALAW:
39 b = 1;
40 break;
41 case PA_SAMPLE_S16LE:
42 case PA_SAMPLE_S16BE:
43 b = 2;
44 break;
45 case PA_SAMPLE_FLOAT32LE:
46 case PA_SAMPLE_FLOAT32BE:
47 b = 4;
48 break;
49 default:
50 assert(0);
51 }
52
53 return b * spec->channels;
54 }
55
56 size_t pa_bytes_per_second(const struct pa_sample_spec *spec) {
57 assert(spec);
58 return spec->rate*pa_frame_size(spec);
59 }
60
61 uint32_t pa_bytes_to_usec(size_t length, const struct pa_sample_spec *spec) {
62 assert(spec);
63
64 return (uint32_t) (((double) length /pa_frame_size(spec))/spec->rate*1000000);
65 }
66
67 int pa_sample_spec_valid(const struct pa_sample_spec *spec) {
68 assert(spec);
69
70 if (!spec->rate || !spec->channels)
71 return 0;
72
73 if (spec->format >= PA_SAMPLE_MAX)
74 return 0;
75
76 return 1;
77 }
78
79 int pa_sample_spec_equal(const struct pa_sample_spec*a, const struct pa_sample_spec*b) {
80 assert(a && b);
81
82 return (a->format == b->format) && (a->rate == b->rate) && (a->channels == b->channels);
83 }
84
85 void pa_sample_snprint(char *s, size_t l, const struct pa_sample_spec *spec) {
86 static const char* const table[]= {
87 [PA_SAMPLE_U8] = "U8",
88 [PA_SAMPLE_ALAW] = "ALAW",
89 [PA_SAMPLE_ULAW] = "ULAW",
90 [PA_SAMPLE_S16LE] = "S16LE",
91 [PA_SAMPLE_S16BE] = "S16BE",
92 [PA_SAMPLE_FLOAT32LE] = "FLOAT32LE",
93 [PA_SAMPLE_FLOAT32BE] = "FLOAT32BE",
94 };
95
96 assert(pa_sample_spec_valid(spec));
97 snprintf(s, l, "%s %uch %uHz", table[spec->format], spec->channels, spec->rate);
98 }