sample.h 13 KB

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