]> code.delx.au - pulseaudio/blob - polyp/sample.h
c1b98f1cf9ed3310f898200112aff288c93e3d20
[pulseaudio] / polyp / sample.h
1 #ifndef foosamplehfoo
2 #define foosamplehfoo
3
4 /* $Id$ */
5
6 /***
7 This file is part of polypaudio.
8
9 polypaudio 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 polypaudio 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 polypaudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #include <inttypes.h>
26 #include <sys/types.h>
27 #include <math.h>
28
29 #include <polyp/cdecl.h>
30
31 /** \file
32 * Constants and routines for sample type handling */
33
34 PA_C_DECL_BEGIN
35
36 /* Maximum allowed channels */
37 #define PA_CHANNELS_MAX 16
38
39 /** Sample format */
40 typedef enum pa_sample_format {
41 PA_SAMPLE_U8, /**< Unsigned 8 Bit PCM */
42 PA_SAMPLE_ALAW, /**< 8 Bit a-Law */
43 PA_SAMPLE_ULAW, /**< 8 Bit mu-Law */
44 PA_SAMPLE_S16LE, /**< Signed 16 Bit PCM, little endian (PC) */
45 PA_SAMPLE_S16BE, /**< Signed 16 Bit PCM, big endian */
46 PA_SAMPLE_FLOAT32LE, /**< 32 Bit IEEE floating point, little endian, range -1..1 */
47 PA_SAMPLE_FLOAT32BE, /**< 32 Bit IEEE floating point, big endian, range -1..1 */
48 PA_SAMPLE_MAX, /**< Upper limit of valid sample types */
49 PA_SAMPLE_INVALID = -1 /**< An invalid value */
50 } pa_sample_format_t;
51
52 #ifdef WORDS_BIGENDIAN
53 /** Signed 16 Bit PCM, native endian */
54 #define PA_SAMPLE_S16NE PA_SAMPLE_S16BE
55 /** 32 Bit IEEE floating point, native endian */
56 #define PA_SAMPLE_FLOAT32NE PA_SAMPLE_FLOAT32BE
57 /** Signed 16 Bit PCM reverse endian */
58 #define PA_SAMPLE_S16RE PA_SAMPLE_S16LE
59 /** 32 Bit IEEE floating point, reverse endian */
60 #define PA_SAMPLE_FLOAT32RE PA_SAMPLE_FLOAT32LE
61 #else
62 /** Signed 16 Bit PCM, native endian */
63 #define PA_SAMPLE_S16NE PA_SAMPLE_S16LE
64 /** 32 Bit IEEE floating point, native endian */
65 #define PA_SAMPLE_FLOAT32NE PA_SAMPLE_FLOAT32LE
66 /** Signed 16 Bit PCM reverse endian */
67 #define PA_SAMPLE_S16RE PA_SAMPLE_S16BE
68 /** 32 Bit IEEE floating point, reverse endian */
69 #define PA_SAMPLE_FLOAT32RE PA_SAMPLE_FLOAT32BE
70 #endif
71
72 /** A Shortcut for PA_SAMPLE_FLOAT32NE */
73 #define PA_SAMPLE_FLOAT32 PA_SAMPLE_FLOAT32NE
74
75 /** A sample format and attribute specification */
76 typedef struct pa_sample_spec {
77 pa_sample_format_t format; /**< The sample format */
78 uint32_t rate; /**< The sample rate. (e.g. 44100) */
79 uint8_t channels; /**< Audio channels. (1 for mono, 2 for stereo, ...) */
80 } pa_sample_spec;
81
82 /** Type for usec specifications (unsigned). May be either 32 or 64 bit, depending on the architecture */
83 typedef uint64_t pa_usec_t;
84
85 /** Return the amount of bytes playback of a second of audio with the specified sample type takes */
86 size_t pa_bytes_per_second(const pa_sample_spec *spec);
87
88 /** Return the size of a frame with the specific sample type */
89 size_t pa_frame_size(const pa_sample_spec *spec);
90
91 /** Return the size of a sample with the specific sample type */
92 size_t pa_sample_size(const pa_sample_spec *spec);
93
94 /** Calculate the time the specified bytes take to play with the specified sample type */
95 pa_usec_t pa_bytes_to_usec(uint64_t length, const pa_sample_spec *spec);
96
97 /** Return non-zero when the sample type specification is valid */
98 int pa_sample_spec_valid(const pa_sample_spec *spec);
99
100 /** Return non-zero when the two sample type specifications match */
101 int pa_sample_spec_equal(const pa_sample_spec*a, const pa_sample_spec*b);
102
103 /* Return a descriptive string for the specified sample format. \since 0.8 */
104 const char *pa_sample_format_to_string(pa_sample_format_t f);
105
106 /** Parse a sample format text. Inverse of pa_sample_format_to_string() */
107 pa_sample_format_t pa_parse_sample_format(const char *format);
108
109 /** Maximum required string length for pa_sample_spec_snprint() */
110 #define PA_SAMPLE_SPEC_SNPRINT_MAX 32
111
112 /** Pretty print a sample type specification to a string */
113 char* pa_sample_spec_snprint(char *s, size_t l, const pa_sample_spec *spec);
114
115 /** Pretty print a byte size value. (i.e. "2.5 MB") */
116 void pa_bytes_snprint(char *s, size_t l, unsigned v);
117
118 PA_C_DECL_END
119
120 #endif