]> code.delx.au - pulseaudio/blob - polyp/sample.h
Make the whole stuff LGPL only
[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 "cdecl.h"
30
31 /** \file
32 * Constants and routines for sample type handling */
33
34 PA_C_DECL_BEGIN
35
36 /** Sample format */
37 enum pa_sample_format {
38 PA_SAMPLE_U8, /**< Unsigned 8 Bit PCM */
39 PA_SAMPLE_ALAW, /**< 8 Bit a-Law */
40 PA_SAMPLE_ULAW, /**< 8 Bit mu-Law */
41 PA_SAMPLE_S16LE, /**< Signed 16 Bit PCM, little endian (PC) */
42 PA_SAMPLE_S16BE, /**< Signed 16 Bit PCM, big endian */
43 PA_SAMPLE_FLOAT32LE, /**< 32 Bit IEEE floating point, little endian, range -1..1 */
44 PA_SAMPLE_FLOAT32BE, /**< 32 Bit IEEE floating point, big endian, range -1..1 */
45 PA_SAMPLE_MAX, /**< Upper limit of valid sample types */
46 PA_SAMPLE_INVALID = -1 /**< An invalid value */
47 };
48
49 #ifdef WORDS_BIGENDIAN
50 /** Signed 16 Bit PCM, native endian */
51 #define PA_SAMPLE_S16NE PA_SAMPLE_S16BE
52 /** 32 Bit IEEE floating point, native endian */
53 #define PA_SAMPLE_FLOAT32NE PA_SAMPLE_FLOAT32BE
54 #else
55 /** Signed 16 Bit PCM, native endian */
56 #define PA_SAMPLE_S16NE PA_SAMPLE_S16LE
57 /** 32 Bit IEEE floating point, native endian */
58 #define PA_SAMPLE_FLOAT32NE PA_SAMPLE_FLOAT32LE
59 #endif
60
61 /** A Shortcut for PA_SAMPLE_FLOAT32NE */
62 #define PA_SAMPLE_FLOAT32 PA_SAMPLE_FLOAT32NE
63
64 /** A sample format and attribute specification */
65 struct pa_sample_spec {
66 enum pa_sample_format format; /**< The sample format */
67 uint32_t rate; /**< The sample rate. (e.g. 44100) */
68 uint8_t channels; /**< Audio channels. (1 for mono, 2 for stereo, ...) */
69 };
70
71 /** Type for usec specifications (unsigned). May be either 32 or 64 bit, depending on the architecture */
72 typedef uint64_t pa_usec_t;
73
74 /** Return the amount of bytes playback of a second of audio with the specified sample type takes */
75 size_t pa_bytes_per_second(const struct pa_sample_spec *spec);
76
77 /** Return the size of a frame with the specific sample type */
78 size_t pa_frame_size(const struct pa_sample_spec *spec);
79
80 /** Calculate the time the specified bytes take to play with the specified sample type */
81 pa_usec_t pa_bytes_to_usec(uint64_t length, const struct pa_sample_spec *spec);
82
83 /** Return non-zero when the sample type specification is valid */
84 int pa_sample_spec_valid(const struct pa_sample_spec *spec);
85
86 /** Return non-zero when the two sample type specifications match */
87 int pa_sample_spec_equal(const struct pa_sample_spec*a, const struct pa_sample_spec*b);
88
89 /** Maximum required string length for pa_sample_spec_snprint() */
90 #define PA_SAMPLE_SPEC_SNPRINT_MAX 32
91
92 /** Pretty print a sample type specification to a string */
93 void pa_sample_spec_snprint(char *s, size_t l, const struct pa_sample_spec *spec);
94
95 /** Volume specification: 0: silence; < 256: diminished volume; 256: normal volume; > 256 amplified volume */
96 typedef uint32_t pa_volume_t;
97
98 /** Normal volume (100%) */
99 #define PA_VOLUME_NORM (0x100)
100
101 /** Muted volume (0%) */
102 #define PA_VOLUME_MUTED (0)
103
104 /** Multiply two volumes specifications, return the result. This uses PA_VOLUME_NORM as neutral element of multiplication. */
105 pa_volume_t pa_volume_multiply(pa_volume_t a, pa_volume_t b);
106
107 /** Convert volume from decibel to linear level. \since 0.4 */
108 pa_volume_t pa_volume_from_dB(double f);
109
110 /** Convert volume from linear level to decibel. \since 0.4 */
111 double pa_volume_to_dB(pa_volume_t v);
112
113 /** Convert volume to scaled value understandable by the user (between 0 and 1). \since 0.6 */
114 double pa_volume_to_user(pa_volume_t v);
115
116 /** Convert user volume to polypaudio volume. \since 0.6 */
117 pa_volume_t pa_volume_from_user(double v);
118
119 #ifdef INFINITY
120 #define PA_DECIBEL_MININFTY (-INFINITY)
121 #else
122 /** This value is used as minus infinity when using pa_volume_{to,from}_dB(). \since 0.4 */
123 #define PA_DECIBEL_MININFTY (-200)
124 #endif
125
126 /** Pretty print a byte size value. (i.e. "2.5 MB") */
127 void pa_bytes_snprint(char *s, size_t l, unsigned v);
128
129 /** Parse a sample format text */
130 enum pa_sample_format pa_parse_sample_format(const char *format);
131
132 PA_C_DECL_END
133
134 #endif