Home | History | Annotate | Download | only in pulse
      1 #ifndef foosamplehfoo
      2 #define foosamplehfoo
      3 
      4 /***
      5   This file is part of PulseAudio.
      6 
      7   Copyright 2004-2006 Lennart Poettering
      8   Copyright 2006 Pierre Ossman <ossman (at) cendio.se> for Cendio AB
      9 
     10   PulseAudio is free software; you can redistribute it and/or modify
     11   it under the terms of the GNU Lesser General Public License as published
     12   by the Free Software Foundation; either version 2.1 of the License,
     13   or (at your option) any later version.
     14 
     15   PulseAudio is distributed in the hope that it will be useful, but
     16   WITHOUT ANY WARRANTY; without even the implied warranty of
     17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
     18   General Public License for more details.
     19 
     20   You should have received a copy of the GNU Lesser General Public License
     21   along with PulseAudio; if not, write to the Free Software
     22   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
     23   USA.
     24 ***/
     25 
     26 #include <inttypes.h>
     27 #include <sys/types.h>
     28 #include <sys/param.h>
     29 #include <math.h>
     30 
     31 #include <pulse/gccmacro.h>
     32 #include <pulse/cdecl.h>
     33 #include <pulse/version.h>
     34 
     35 /** \page sample Sample Format Specifications
     36  *
     37  * \section overv_sec Overview
     38  *
     39  * PulseAudio is capable of handling a multitude of sample formats, rates
     40  * and channels, transparently converting and mixing them as needed.
     41  *
     42  * \section format_sec Sample Format
     43  *
     44  * PulseAudio supports the following sample formats:
     45  *
     46  * \li PA_SAMPLE_U8 - Unsigned 8 bit integer PCM.
     47  * \li PA_SAMPLE_S16LE - Signed 16 integer bit PCM, little endian.
     48  * \li PA_SAMPLE_S16BE - Signed 16 integer bit PCM, big endian.
     49  * \li PA_SAMPLE_FLOAT32LE - 32 bit IEEE floating point PCM, little endian.
     50  * \li PA_SAMPLE_FLOAT32BE - 32 bit IEEE floating point PCM, big endian.
     51  * \li PA_SAMPLE_ALAW - 8 bit a-Law.
     52  * \li PA_SAMPLE_ULAW - 8 bit mu-Law.
     53  * \li PA_SAMPLE_S32LE - Signed 32 bit integer PCM, little endian.
     54  * \li PA_SAMPLE_S32BE - Signed 32 bit integer PCM, big endian.
     55  * \li PA_SAMPLE_S24LE - Signed 24 bit integer PCM packed, little endian.
     56  * \li PA_SAMPLE_S24BE - Signed 24 bit integer PCM packed, big endian.
     57  * \li PA_SAMPLE_S24_32LE - Signed 24 bit integer PCM in LSB of 32 bit words, little endian.
     58  * \li PA_SAMPLE_S24_32BE - Signed 24 bit integer PCM in LSB of 32 bit words, big endian.
     59  *
     60  * The floating point sample formats have the range from -1.0 to 1.0.
     61  *
     62  * The sample formats that are sensitive to endianness have convenience
     63  * macros for native endian (NE), and reverse endian (RE).
     64  *
     65  * \section rate_sec Sample Rates
     66  *
     67  * PulseAudio supports any sample rate between 1 Hz and 192000 Hz. There is no
     68  * point trying to exceed the sample rate of the output device though as the
     69  * signal will only get downsampled, consuming CPU on the machine running the
     70  * server.
     71  *
     72  * \section chan_sec Channels
     73  *
     74  * PulseAudio supports up to 32 individual channels. The order of the
     75  * channels is up to the application, but they must be continous. To map
     76  * channels to speakers, see \ref channelmap.
     77  *
     78  * \section calc_sec Calculations
     79  *
     80  * The PulseAudio library contains a number of convenience functions to do
     81  * calculations on sample formats:
     82  *
     83  * \li pa_bytes_per_second() - The number of bytes one second of audio will
     84  *                             take given a sample format.
     85  * \li pa_frame_size() - The size, in bytes, of one frame (i.e. one set of
     86  *                       samples, one for each channel).
     87  * \li pa_sample_size() - The size, in bytes, of one sample.
     88  * \li pa_bytes_to_usec() - Calculate the time it would take to play a buffer
     89  *                          of a certain size.
     90  *
     91  * \section util_sec Convenience Functions
     92  *
     93  * The library also contains a couple of other convenience functions:
     94  *
     95  * \li pa_sample_spec_valid() - Tests if a sample format specification is
     96  *                              valid.
     97  * \li pa_sample_spec_equal() - Tests if the sample format specifications are
     98  *                              identical.
     99  * \li pa_sample_format_to_string() - Return a textual description of a
    100  *                                    sample format.
    101  * \li pa_parse_sample_format() - Parse a text string into a sample format.
    102  * \li pa_sample_spec_snprint() - Create a textual description of a complete
    103  *                                 sample format specification.
    104  * \li pa_bytes_snprint() - Pretty print a byte value (e.g. 2.5 MiB).
    105  */
    106 
    107 /** \file
    108  * Constants and routines for sample type handling */
    109 
    110 PA_C_DECL_BEGIN
    111 
    112 #if !defined(WORDS_BIGENDIAN)
    113 #if defined(__BYTE_ORDER)
    114 #if __BYTE_ORDER == __BIG_ENDIAN
    115 #define WORDS_BIGENDIAN
    116 #endif
    117 #endif
    118 #endif
    119 
    120 /** Maximum number of allowed channels */
    121 #define PA_CHANNELS_MAX 32U
    122 
    123 /** Maximum allowed sample rate */
    124 #define PA_RATE_MAX (48000U*4U)
    125 
    126 /** Sample format */
    127 typedef enum pa_sample_format {
    128     PA_SAMPLE_U8,
    129     /**< Unsigned 8 Bit PCM */
    130 
    131     PA_SAMPLE_ALAW,
    132     /**< 8 Bit a-Law */
    133 
    134     PA_SAMPLE_ULAW,
    135     /**< 8 Bit mu-Law */
    136 
    137     PA_SAMPLE_S16LE,
    138     /**< Signed 16 Bit PCM, little endian (PC) */
    139 
    140     PA_SAMPLE_S16BE,
    141     /**< Signed 16 Bit PCM, big endian */
    142 
    143     PA_SAMPLE_FLOAT32LE,
    144     /**< 32 Bit IEEE floating point, little endian (PC), range -1.0 to 1.0 */
    145 
    146     PA_SAMPLE_FLOAT32BE,
    147     /**< 32 Bit IEEE floating point, big endian, range -1.0 to 1.0 */
    148 
    149     PA_SAMPLE_S32LE,
    150     /**< Signed 32 Bit PCM, little endian (PC) */
    151 
    152     PA_SAMPLE_S32BE,
    153     /**< Signed 32 Bit PCM, big endian */
    154 
    155     PA_SAMPLE_S24LE,
    156     /**< Signed 24 Bit PCM packed, little endian (PC). \since 0.9.15 */
    157 
    158     PA_SAMPLE_S24BE,
    159     /**< Signed 24 Bit PCM packed, big endian. \since 0.9.15 */
    160 
    161     PA_SAMPLE_S24_32LE,
    162     /**< Signed 24 Bit PCM in LSB of 32 Bit words, little endian (PC). \since 0.9.15 */
    163 
    164     PA_SAMPLE_S24_32BE,
    165     /**< Signed 24 Bit PCM in LSB of 32 Bit words, big endian. \since 0.9.15 */
    166 
    167     PA_SAMPLE_MAX,
    168     /**< Upper limit of valid sample types */
    169 
    170     PA_SAMPLE_INVALID = -1
    171     /**< An invalid value */
    172 } pa_sample_format_t;
    173 
    174 #ifdef WORDS_BIGENDIAN
    175 /** Signed 16 Bit PCM, native endian */
    176 #define PA_SAMPLE_S16NE PA_SAMPLE_S16BE
    177 /** 32 Bit IEEE floating point, native endian */
    178 #define PA_SAMPLE_FLOAT32NE PA_SAMPLE_FLOAT32BE
    179 /** Signed 32 Bit PCM, native endian */
    180 #define PA_SAMPLE_S32NE PA_SAMPLE_S32BE
    181 /** Signed 24 Bit PCM packed, native endian. \since 0.9.15 */
    182 #define PA_SAMPLE_S24NE PA_SAMPLE_S24BE
    183 /** Signed 24 Bit PCM in LSB of 32 Bit words, native endian. \since 0.9.15 */
    184 #define PA_SAMPLE_S24_32NE PA_SAMPLE_S24_32BE
    185 
    186 /** Signed 16 Bit PCM reverse endian */
    187 #define PA_SAMPLE_S16RE PA_SAMPLE_S16LE
    188 /** 32 Bit IEEE floating point, reverse endian */
    189 #define PA_SAMPLE_FLOAT32RE PA_SAMPLE_FLOAT32LE
    190 /** Signed 32 Bit PCM, reverse endian */
    191 #define PA_SAMPLE_S32RE PA_SAMPLE_S32LE
    192 /** Signed 24 Bit PCM, packed reverse endian. \since 0.9.15 */
    193 #define PA_SAMPLE_S24RE PA_SAMPLE_S24LE
    194 /** Signed 24 Bit PCM, in LSB of 32 Bit words, reverse endian. \since 0.9.15 */
    195 #define PA_SAMPLE_S24_32RE PA_SAMPLE_S24_32LE
    196 #else
    197 /** Signed 16 Bit PCM, native endian */
    198 #define PA_SAMPLE_S16NE PA_SAMPLE_S16LE
    199 /** 32 Bit IEEE floating point, native endian */
    200 #define PA_SAMPLE_FLOAT32NE PA_SAMPLE_FLOAT32LE
    201 /** Signed 32 Bit PCM, native endian */
    202 #define PA_SAMPLE_S32NE PA_SAMPLE_S32LE
    203 /** Signed 24 Bit PCM packed, native endian. \since 0.9.15 */
    204 #define PA_SAMPLE_S24NE PA_SAMPLE_S24LE
    205 /** Signed 24 Bit PCM in LSB of 32 Bit words, native endian. \since 0.9.15 */
    206 #define PA_SAMPLE_S24_32NE PA_SAMPLE_S24_32LE
    207 
    208 /** Signed 16 Bit PCM, reverse endian */
    209 #define PA_SAMPLE_S16RE PA_SAMPLE_S16BE
    210 /** 32 Bit IEEE floating point, reverse endian */
    211 #define PA_SAMPLE_FLOAT32RE PA_SAMPLE_FLOAT32BE
    212 /** Signed 32 Bit PCM, reverse endian */
    213 #define PA_SAMPLE_S32RE PA_SAMPLE_S32BE
    214 /** Signed 24 Bit PCM, packed reverse endian. \since 0.9.15 */
    215 #define PA_SAMPLE_S24RE PA_SAMPLE_S24BE
    216 /** Signed 24 Bit PCM, in LSB of 32 Bit words, reverse endian. \since 0.9.15 */
    217 #define PA_SAMPLE_S24_32RE PA_SAMPLE_S24_32BE
    218 #endif
    219 
    220 /** A Shortcut for PA_SAMPLE_FLOAT32NE */
    221 #define PA_SAMPLE_FLOAT32 PA_SAMPLE_FLOAT32NE
    222 
    223 /** \cond fulldocs */
    224 /* Allow clients to check with #ifdef for these sample formats */
    225 #define PA_SAMPLE_U8 PA_SAMPLE_U8
    226 #define PA_SAMPLE_ALAW PA_SAMPLE_ALAW
    227 #define PA_SAMPLE_ULAW PA_SAMPLE_ULAW
    228 #define PA_SAMPLE_S16LE PA_SAMPLE_S16LE
    229 #define PA_SAMPLE_S16BE PA_SAMPLE_S16BE
    230 #define PA_SAMPLE_FLOAT32LE PA_SAMPLE_FLOAT32LE
    231 #define PA_SAMPLE_FLOAT32BE PA_SAMPLE_FLOAT32BE
    232 #define PA_SAMPLE_S32LE PA_SAMPLE_S32LE
    233 #define PA_SAMPLE_S32BE PA_SAMPLE_S32BE
    234 #define PA_SAMPLE_S24LE PA_SAMPLE_S24LE
    235 #define PA_SAMPLE_S24BE PA_SAMPLE_S24BE
    236 #define PA_SAMPLE_S24_32LE PA_SAMPLE_S24_32LE
    237 #define PA_SAMPLE_S24_32BE PA_SAMPLE_S24_32BE
    238 /** \endcond */
    239 
    240 /** A sample format and attribute specification */
    241 typedef struct pa_sample_spec {
    242     pa_sample_format_t format;
    243     /**< The sample format */
    244 
    245     uint32_t rate;
    246     /**< The sample rate. (e.g. 44100) */
    247 
    248     uint8_t channels;
    249     /**< Audio channels. (1 for mono, 2 for stereo, ...) */
    250 } pa_sample_spec;
    251 
    252 /** Type for usec specifications (unsigned). Always 64 bit. */
    253 typedef uint64_t pa_usec_t;
    254 
    255 /** Return the amount of bytes playback of a second of audio with the specified sample type takes */
    256 size_t pa_bytes_per_second(const pa_sample_spec *spec) PA_GCC_PURE;
    257 
    258 /** Return the size of a frame with the specific sample type */
    259 size_t pa_frame_size(const pa_sample_spec *spec) PA_GCC_PURE;
    260 
    261 /** Return the size of a sample with the specific sample type */
    262 size_t pa_sample_size(const pa_sample_spec *spec) PA_GCC_PURE;
    263 
    264 /** Similar to pa_sample_size() but take a sample format instead of a
    265  * full sample spec. \since 0.9.15 */
    266 size_t pa_sample_size_of_format(pa_sample_format_t f) PA_GCC_PURE;
    267 
    268 /** Calculate the time the specified bytes take to play with the
    269  * specified sample type. The return value will always be rounded
    270  * down for non-integral return values. */
    271 pa_usec_t pa_bytes_to_usec(uint64_t length, const pa_sample_spec *spec) PA_GCC_PURE;
    272 
    273 /** Calculates the number of bytes that are required for the specified
    274  * time. The return value will always be rounded down for non-integral
    275  * return values. \since 0.9 */
    276 size_t pa_usec_to_bytes(pa_usec_t t, const pa_sample_spec *spec) PA_GCC_PURE;
    277 
    278 /** Initialize the specified sample spec and return a pointer to
    279  * it. The sample spec will have a defined state but
    280  * pa_sample_spec_valid() will fail for it. \since 0.9.13 */
    281 pa_sample_spec* pa_sample_spec_init(pa_sample_spec *spec);
    282 
    283 /** Return non-zero when the sample type specification is valid */
    284 int pa_sample_spec_valid(const pa_sample_spec *spec) PA_GCC_PURE;
    285 
    286 /** Return non-zero when the two sample type specifications match */
    287 int pa_sample_spec_equal(const pa_sample_spec*a, const pa_sample_spec*b) PA_GCC_PURE;
    288 
    289 /** Return a descriptive string for the specified sample format. \since 0.8 */
    290 const char *pa_sample_format_to_string(pa_sample_format_t f) PA_GCC_PURE;
    291 
    292 /** Parse a sample format text. Inverse of pa_sample_format_to_string() */
    293 pa_sample_format_t pa_parse_sample_format(const char *format) PA_GCC_PURE;
    294 
    295 /** Maximum required string length for
    296  * pa_sample_spec_snprint(). Please note that this value can change
    297  * with any release without warning and without being considered API
    298  * or ABI breakage. You should not use this definition anywhere where
    299  * it might become part of an ABI. */
    300 #define PA_SAMPLE_SPEC_SNPRINT_MAX 32
    301 
    302 /** Pretty print a sample type specification to a string */
    303 char* pa_sample_spec_snprint(char *s, size_t l, const pa_sample_spec *spec);
    304 
    305 /** Maximum required string length for pa_bytes_snprint(). Please note
    306  * that this value can change with any release without warning and
    307  * without being considered API or ABI breakage. You should not use
    308  * this definition anywhere where it might become part of an
    309  * ABI. \since 0.9.16 */
    310 #define PA_BYTES_SNPRINT_MAX 11
    311 
    312 /** Pretty print a byte size value. (i.e. "2.5 MiB") */
    313 char* pa_bytes_snprint(char *s, size_t l, unsigned v);
    314 
    315 /** Return 1 when the specified format is little endian, return -1
    316  * when endianess does not apply to this format. \since 0.9.16 */
    317 int pa_sample_format_is_le(pa_sample_format_t f) PA_GCC_PURE;
    318 
    319 /** Return 1 when the specified format is big endian, return -1 when
    320  * endianess does not apply to this format. \since 0.9.16 */
    321 int pa_sample_format_is_be(pa_sample_format_t f) PA_GCC_PURE;
    322 
    323 #ifdef WORDS_BIGENDIAN
    324 #define pa_sample_format_is_ne(f) pa_sample_format_is_be(f)
    325 #define pa_sample_format_is_re(f) pa_sample_format_is_le(f)
    326 #else
    327 /** Return 1 when the specified format is native endian, return -1
    328  * when endianess does not apply to this format. \since 0.9.16 */
    329 #define pa_sample_format_is_ne(f) pa_sample_format_is_le(f)
    330 /** Return 1 when the specified format is reverse endian, return -1
    331  * when endianess does not apply to this format. \since 0.9.16 */
    332 #define pa_sample_format_is_re(f) pa_sample_format_is_be(f)
    333 #endif
    334 
    335 PA_C_DECL_END
    336 
    337 #endif
    338