Path: blob/master/thirdparty/linuxbsd_headers/pulse/sample.h
9905 views
#ifndef foosamplehfoo1#define foosamplehfoo23/***4This file is part of PulseAudio.56Copyright 2004-2006 Lennart Poettering7Copyright 2006 Pierre Ossman <[email protected]> for Cendio AB89PulseAudio is free software; you can redistribute it and/or modify10it under the terms of the GNU Lesser General Public License as published11by the Free Software Foundation; either version 2.1 of the License,12or (at your option) any later version.1314PulseAudio is distributed in the hope that it will be useful, but15WITHOUT ANY WARRANTY; without even the implied warranty of16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU17General Public License for more details.1819You should have received a copy of the GNU Lesser General Public License20along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.21***/2223#include <inttypes.h>24#include <sys/types.h>25#include <sys/param.h>2627#include <pulse/gccmacro.h>28#include <pulse/cdecl.h>29#include <pulse/version.h>3031/** \page sample Sample Format Specifications32*33* \section overv_sec Overview34*35* PulseAudio is capable of handling a multitude of sample formats, rates36* and channels, transparently converting and mixing them as needed.37*38* \section format_sec Sample Format39*40* PulseAudio supports the following sample formats:41*42* \li PA_SAMPLE_U8 - Unsigned 8 bit integer PCM.43* \li PA_SAMPLE_S16LE - Signed 16 integer bit PCM, little endian.44* \li PA_SAMPLE_S16BE - Signed 16 integer bit PCM, big endian.45* \li PA_SAMPLE_FLOAT32LE - 32 bit IEEE floating point PCM, little endian.46* \li PA_SAMPLE_FLOAT32BE - 32 bit IEEE floating point PCM, big endian.47* \li PA_SAMPLE_ALAW - 8 bit a-Law.48* \li PA_SAMPLE_ULAW - 8 bit mu-Law.49* \li PA_SAMPLE_S32LE - Signed 32 bit integer PCM, little endian.50* \li PA_SAMPLE_S32BE - Signed 32 bit integer PCM, big endian.51* \li PA_SAMPLE_S24LE - Signed 24 bit integer PCM packed, little endian.52* \li PA_SAMPLE_S24BE - Signed 24 bit integer PCM packed, big endian.53* \li PA_SAMPLE_S24_32LE - Signed 24 bit integer PCM in LSB of 32 bit words, little endian.54* \li PA_SAMPLE_S24_32BE - Signed 24 bit integer PCM in LSB of 32 bit words, big endian.55*56* The floating point sample formats have the range from -1.0 to 1.0.57*58* The sample formats that are sensitive to endianness have convenience59* macros for native endian (NE), and reverse endian (RE).60*61* \section rate_sec Sample Rates62*63* PulseAudio supports any sample rate between 1 Hz and 192000 Hz. There is no64* point trying to exceed the sample rate of the output device though as the65* signal will only get downsampled, consuming CPU on the machine running the66* server.67*68* \section chan_sec Channels69*70* PulseAudio supports up to 32 individual channels. The order of the71* channels is up to the application, but they must be continuous. To map72* channels to speakers, see \ref channelmap.73*74* \section calc_sec Calculations75*76* The PulseAudio library contains a number of convenience functions to do77* calculations on sample formats:78*79* \li pa_bytes_per_second() - The number of bytes one second of audio will80* take given a sample format.81* \li pa_frame_size() - The size, in bytes, of one frame (i.e. one set of82* samples, one for each channel).83* \li pa_sample_size() - The size, in bytes, of one sample.84* \li pa_bytes_to_usec() - Calculate the time it would take to play a buffer85* of a certain size.86*87* \section util_sec Convenience Functions88*89* The library also contains a couple of other convenience functions:90*91* \li pa_sample_spec_valid() - Tests if a sample format specification is92* valid.93* \li pa_sample_spec_equal() - Tests if the sample format specifications are94* identical.95* \li pa_sample_format_to_string() - Return a textual description of a96* sample format.97* \li pa_parse_sample_format() - Parse a text string into a sample format.98* \li pa_sample_spec_snprint() - Create a textual description of a complete99* sample format specification.100* \li pa_bytes_snprint() - Pretty print a byte value (e.g. 2.5 MiB).101*/102103/** \file104* Constants and routines for sample type handling105*106* See also \subpage sample107*/108109PA_C_DECL_BEGIN110111#if !defined(WORDS_BIGENDIAN)112113#if defined(__BYTE_ORDER)114#if __BYTE_ORDER == __BIG_ENDIAN115#define WORDS_BIGENDIAN116#endif117#endif118119/* On Sparc, WORDS_BIGENDIAN needs to be set if _BIG_ENDIAN is defined. */120#if defined(__sparc__) && defined(_BIG_ENDIAN)121#define WORDS_BIGENDIAN122#endif123124#endif125126/** Maximum number of allowed channels */127#define PA_CHANNELS_MAX 32U128129/** Maximum allowed sample rate */130#define PA_RATE_MAX (48000U*8U)131132/** Sample format */133typedef enum pa_sample_format {134PA_SAMPLE_U8,135/**< Unsigned 8 Bit PCM */136137PA_SAMPLE_ALAW,138/**< 8 Bit a-Law */139140PA_SAMPLE_ULAW,141/**< 8 Bit mu-Law */142143PA_SAMPLE_S16LE,144/**< Signed 16 Bit PCM, little endian (PC) */145146PA_SAMPLE_S16BE,147/**< Signed 16 Bit PCM, big endian */148149PA_SAMPLE_FLOAT32LE,150/**< 32 Bit IEEE floating point, little endian (PC), range -1.0 to 1.0 */151152PA_SAMPLE_FLOAT32BE,153/**< 32 Bit IEEE floating point, big endian, range -1.0 to 1.0 */154155PA_SAMPLE_S32LE,156/**< Signed 32 Bit PCM, little endian (PC) */157158PA_SAMPLE_S32BE,159/**< Signed 32 Bit PCM, big endian */160161PA_SAMPLE_S24LE,162/**< Signed 24 Bit PCM packed, little endian (PC). \since 0.9.15 */163164PA_SAMPLE_S24BE,165/**< Signed 24 Bit PCM packed, big endian. \since 0.9.15 */166167PA_SAMPLE_S24_32LE,168/**< Signed 24 Bit PCM in LSB of 32 Bit words, little endian (PC). \since 0.9.15 */169170PA_SAMPLE_S24_32BE,171/**< Signed 24 Bit PCM in LSB of 32 Bit words, big endian. \since 0.9.15 */172173PA_SAMPLE_MAX,174/**< Upper limit of valid sample types */175176PA_SAMPLE_INVALID = -1177/**< An invalid value */178} pa_sample_format_t;179180#ifdef WORDS_BIGENDIAN181/** Signed 16 Bit PCM, native endian */182#define PA_SAMPLE_S16NE PA_SAMPLE_S16BE183/** 32 Bit IEEE floating point, native endian */184#define PA_SAMPLE_FLOAT32NE PA_SAMPLE_FLOAT32BE185/** Signed 32 Bit PCM, native endian */186#define PA_SAMPLE_S32NE PA_SAMPLE_S32BE187/** Signed 24 Bit PCM packed, native endian. \since 0.9.15 */188#define PA_SAMPLE_S24NE PA_SAMPLE_S24BE189/** Signed 24 Bit PCM in LSB of 32 Bit words, native endian. \since 0.9.15 */190#define PA_SAMPLE_S24_32NE PA_SAMPLE_S24_32BE191192/** Signed 16 Bit PCM reverse endian */193#define PA_SAMPLE_S16RE PA_SAMPLE_S16LE194/** 32 Bit IEEE floating point, reverse endian */195#define PA_SAMPLE_FLOAT32RE PA_SAMPLE_FLOAT32LE196/** Signed 32 Bit PCM, reverse endian */197#define PA_SAMPLE_S32RE PA_SAMPLE_S32LE198/** Signed 24 Bit PCM, packed reverse endian. \since 0.9.15 */199#define PA_SAMPLE_S24RE PA_SAMPLE_S24LE200/** Signed 24 Bit PCM, in LSB of 32 Bit words, reverse endian. \since 0.9.15 */201#define PA_SAMPLE_S24_32RE PA_SAMPLE_S24_32LE202#else203/** Signed 16 Bit PCM, native endian */204#define PA_SAMPLE_S16NE PA_SAMPLE_S16LE205/** 32 Bit IEEE floating point, native endian */206#define PA_SAMPLE_FLOAT32NE PA_SAMPLE_FLOAT32LE207/** Signed 32 Bit PCM, native endian */208#define PA_SAMPLE_S32NE PA_SAMPLE_S32LE209/** Signed 24 Bit PCM packed, native endian. \since 0.9.15 */210#define PA_SAMPLE_S24NE PA_SAMPLE_S24LE211/** Signed 24 Bit PCM in LSB of 32 Bit words, native endian. \since 0.9.15 */212#define PA_SAMPLE_S24_32NE PA_SAMPLE_S24_32LE213214/** Signed 16 Bit PCM, reverse endian */215#define PA_SAMPLE_S16RE PA_SAMPLE_S16BE216/** 32 Bit IEEE floating point, reverse endian */217#define PA_SAMPLE_FLOAT32RE PA_SAMPLE_FLOAT32BE218/** Signed 32 Bit PCM, reverse endian */219#define PA_SAMPLE_S32RE PA_SAMPLE_S32BE220/** Signed 24 Bit PCM, packed reverse endian. \since 0.9.15 */221#define PA_SAMPLE_S24RE PA_SAMPLE_S24BE222/** Signed 24 Bit PCM, in LSB of 32 Bit words, reverse endian. \since 0.9.15 */223#define PA_SAMPLE_S24_32RE PA_SAMPLE_S24_32BE224#endif225226/** A Shortcut for PA_SAMPLE_FLOAT32NE */227#define PA_SAMPLE_FLOAT32 PA_SAMPLE_FLOAT32NE228229/** \cond fulldocs */230/* Allow clients to check with #ifdef for these sample formats */231#define PA_SAMPLE_U8 PA_SAMPLE_U8232#define PA_SAMPLE_ALAW PA_SAMPLE_ALAW233#define PA_SAMPLE_ULAW PA_SAMPLE_ULAW234#define PA_SAMPLE_S16LE PA_SAMPLE_S16LE235#define PA_SAMPLE_S16BE PA_SAMPLE_S16BE236#define PA_SAMPLE_FLOAT32LE PA_SAMPLE_FLOAT32LE237#define PA_SAMPLE_FLOAT32BE PA_SAMPLE_FLOAT32BE238#define PA_SAMPLE_S32LE PA_SAMPLE_S32LE239#define PA_SAMPLE_S32BE PA_SAMPLE_S32BE240#define PA_SAMPLE_S24LE PA_SAMPLE_S24LE241#define PA_SAMPLE_S24BE PA_SAMPLE_S24BE242#define PA_SAMPLE_S24_32LE PA_SAMPLE_S24_32LE243#define PA_SAMPLE_S24_32BE PA_SAMPLE_S24_32BE244/** \endcond */245246/** A sample format and attribute specification */247typedef struct pa_sample_spec {248pa_sample_format_t format;249/**< The sample format */250251uint32_t rate;252/**< The sample rate. (e.g. 44100) */253254uint8_t channels;255/**< Audio channels. (1 for mono, 2 for stereo, ...) */256} pa_sample_spec;257258/** Type for usec specifications (unsigned). Always 64 bit. */259typedef uint64_t pa_usec_t;260261/** Return the amount of bytes playback of a second of audio with the specified sample type takes */262size_t pa_bytes_per_second(const pa_sample_spec *spec) PA_GCC_PURE;263264/** Return the size of a frame with the specific sample type */265size_t pa_frame_size(const pa_sample_spec *spec) PA_GCC_PURE;266267/** Return the size of a sample with the specific sample type */268size_t pa_sample_size(const pa_sample_spec *spec) PA_GCC_PURE;269270/** Similar to pa_sample_size() but take a sample format instead of a271* full sample spec. \since 0.9.15 */272size_t pa_sample_size_of_format(pa_sample_format_t f) PA_GCC_PURE;273274/** Calculate the time the specified bytes take to play with the275* specified sample type. The return value will always be rounded276* down for non-integral return values. */277pa_usec_t pa_bytes_to_usec(uint64_t length, const pa_sample_spec *spec) PA_GCC_PURE;278279/** Calculates the number of bytes that are required for the specified280* time. The return value will always be rounded down for non-integral281* return values. \since 0.9 */282size_t pa_usec_to_bytes(pa_usec_t t, const pa_sample_spec *spec) PA_GCC_PURE;283284/** Initialize the specified sample spec and return a pointer to285* it. The sample spec will have a defined state but286* pa_sample_spec_valid() will fail for it. \since 0.9.13 */287pa_sample_spec* pa_sample_spec_init(pa_sample_spec *spec);288289/** Return non-zero if the given integer is a valid sample format. \since 5.0 */290int pa_sample_format_valid(unsigned format) PA_GCC_PURE;291292/** Return non-zero if the rate is within the supported range. \since 5.0 */293int pa_sample_rate_valid(uint32_t rate) PA_GCC_PURE;294295/** Return non-zero if the channel count is within the supported range.296* \since 5.0 */297int pa_channels_valid(uint8_t channels) PA_GCC_PURE;298299/** Return non-zero when the sample type specification is valid */300int pa_sample_spec_valid(const pa_sample_spec *spec) PA_GCC_PURE;301302/** Return non-zero when the two sample type specifications match */303int pa_sample_spec_equal(const pa_sample_spec*a, const pa_sample_spec*b) PA_GCC_PURE;304305/** Return a descriptive string for the specified sample format. \since 0.8 */306const char *pa_sample_format_to_string(pa_sample_format_t f) PA_GCC_PURE;307308/** Parse a sample format text. Inverse of pa_sample_format_to_string() */309pa_sample_format_t pa_parse_sample_format(const char *format) PA_GCC_PURE;310311/** Maximum required string length for312* pa_sample_spec_snprint(). Please note that this value can change313* with any release without warning and without being considered API314* or ABI breakage. You should not use this definition anywhere where315* it might become part of an ABI. */316#define PA_SAMPLE_SPEC_SNPRINT_MAX 32317318/** Pretty print a sample type specification to a string */319char* pa_sample_spec_snprint(char *s, size_t l, const pa_sample_spec *spec);320321/** Maximum required string length for pa_bytes_snprint(). Please note322* that this value can change with any release without warning and323* without being considered API or ABI breakage. You should not use324* this definition anywhere where it might become part of an325* ABI. \since 0.9.16 */326#define PA_BYTES_SNPRINT_MAX 11327328/** Pretty print a byte size value (i.e.\ "2.5 MiB") */329char* pa_bytes_snprint(char *s, size_t l, unsigned v);330331/** Return 1 when the specified format is little endian, return -1332* when endianness does not apply to this format. \since 0.9.16 */333int pa_sample_format_is_le(pa_sample_format_t f) PA_GCC_PURE;334335/** Return 1 when the specified format is big endian, return -1 when336* endianness does not apply to this format. \since 0.9.16 */337int pa_sample_format_is_be(pa_sample_format_t f) PA_GCC_PURE;338339#ifdef WORDS_BIGENDIAN340#define pa_sample_format_is_ne(f) pa_sample_format_is_be(f)341#define pa_sample_format_is_re(f) pa_sample_format_is_le(f)342#else343/** Return 1 when the specified format is native endian, return -1344* when endianness does not apply to this format. \since 0.9.16 */345#define pa_sample_format_is_ne(f) pa_sample_format_is_le(f)346/** Return 1 when the specified format is reverse endian, return -1347* when endianness does not apply to this format. \since 0.9.16 */348#define pa_sample_format_is_re(f) pa_sample_format_is_be(f)349#endif350351PA_C_DECL_END352353#endif354355356