wave.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJMEDIA_WAVE_H__
  20. #define __PJMEDIA_WAVE_H__
  21. /**
  22. * @file wave.h
  23. * @brief WAVE file manipulation.
  24. */
  25. #include <pjmedia/types.h>
  26. /**
  27. * @defgroup PJMEDIA_FILE_FORMAT File Formats
  28. * @brief Supported file formats
  29. */
  30. /**
  31. * @defgroup PJMEDIA_WAVE WAVE Header
  32. * @ingroup PJMEDIA_FILE_FORMAT
  33. * @brief Representation of RIFF/WAVE file format
  34. * @{
  35. *
  36. * This the the low level representation of RIFF/WAVE file format. For
  37. * higher abstraction, please see \ref PJMEDIA_FILE_PLAY and
  38. * \ref PJMEDIA_FILE_REC.
  39. */
  40. PJ_BEGIN_DECL
  41. /**
  42. * Standard RIFF tag to identify RIFF file format in the WAVE header.
  43. */
  44. #define PJMEDIA_RIFF_TAG ('F'<<24|'F'<<16|'I'<<8|'R')
  45. /**
  46. * Standard WAVE tag to identify WAVE header.
  47. */
  48. #define PJMEDIA_WAVE_TAG ('E'<<24|'V'<<16|'A'<<8|'W')
  49. /**
  50. * Standard FMT tag to identify format chunks.
  51. */
  52. #define PJMEDIA_FMT_TAG (' '<<24|'t'<<16|'m'<<8|'f')
  53. /**
  54. * Standard DATA tag to identify data chunks.
  55. */
  56. #define PJMEDIA_DATA_TAG ('a'<<24|'t'<<16|'a'<<8|'d')
  57. /**
  58. * Standard FACT tag to identify fact chunks.
  59. */
  60. #define PJMEDIA_FACT_TAG ('t'<<24|'c'<<16|'a'<<8|'f')
  61. /**
  62. * Enumeration of format compression tag.
  63. */
  64. typedef enum {
  65. PJMEDIA_WAVE_FMT_TAG_PCM = 1,
  66. PJMEDIA_WAVE_FMT_TAG_ALAW = 6,
  67. PJMEDIA_WAVE_FMT_TAG_ULAW = 7
  68. } pjmedia_wave_fmt_tag;
  69. /**
  70. * This file describes the simpler/canonical version of a WAVE file.
  71. * It does not support the full RIFF format specification.
  72. */
  73. #pragma pack(2)
  74. struct pjmedia_wave_hdr
  75. {
  76. /** This structure describes RIFF WAVE file header */
  77. struct {
  78. pj_uint32_t riff; /**< "RIFF" ASCII tag. */
  79. pj_uint32_t file_len; /**< File length minus 8 bytes */
  80. pj_uint32_t wave; /**< "WAVE" ASCII tag. */
  81. } riff_hdr;
  82. /** This structure describes format chunks/header */
  83. struct {
  84. pj_uint32_t fmt; /**< "fmt " ASCII tag. */
  85. pj_uint32_t len; /**< 16 for PCM. */
  86. pj_uint16_t fmt_tag; /**< 1 for PCM */
  87. pj_uint16_t nchan; /**< Number of channels. */
  88. pj_uint32_t sample_rate; /**< Sampling rate. */
  89. pj_uint32_t bytes_per_sec; /**< Average bytes per second. */
  90. pj_uint16_t block_align; /**< nchannels * bits / 8 */
  91. pj_uint16_t bits_per_sample; /**< Bits per sample. */
  92. } fmt_hdr;
  93. /** The data header preceeds the actual data in the file. */
  94. struct {
  95. pj_uint32_t data; /**< "data" ASCII tag. */
  96. pj_uint32_t len; /**< Data length. */
  97. } data_hdr;
  98. };
  99. #pragma pack()
  100. /**
  101. * @see pjmedia_wave_hdr
  102. */
  103. typedef struct pjmedia_wave_hdr pjmedia_wave_hdr;
  104. /**
  105. * This structure describes generic RIFF subchunk header.
  106. */
  107. typedef struct pjmedia_wave_subchunk
  108. {
  109. pj_uint32_t id; /**< Subchunk ASCII tag. */
  110. pj_uint32_t len; /**< Length following this field */
  111. } pjmedia_wave_subchunk;
  112. /**
  113. * Normalize subchunk header from little endian (the representation of
  114. * RIFF file) into host's endian.
  115. */
  116. #if defined(PJ_IS_BIG_ENDIAN) && PJ_IS_BIG_ENDIAN!=0
  117. # define PJMEDIA_WAVE_NORMALIZE_SUBCHUNK(ch) \
  118. do { \
  119. (ch)->id = pj_swap32((ch)->id); \
  120. (ch)->len = pj_swap32((ch)->len); \
  121. } while (0)
  122. #else
  123. # define PJMEDIA_WAVE_NORMALIZE_SUBCHUNK(ch)
  124. #endif
  125. /**
  126. * On big-endian hosts, this function swaps the byte order of the values
  127. * in the WAVE header fields. On little-endian hosts, this function does
  128. * nothing.
  129. *
  130. * Application SHOULD call this function after reading the WAVE header
  131. * chunks from a file.
  132. *
  133. * @param hdr The WAVE header.
  134. */
  135. PJ_DECL(void) pjmedia_wave_hdr_file_to_host( pjmedia_wave_hdr *hdr );
  136. /**
  137. * On big-endian hosts, this function swaps the byte order of the values
  138. * in the WAVE header fields. On little-endian hosts, this function does
  139. * nothing.
  140. *
  141. * Application SHOULD call this function before writing the WAVE header
  142. * to a file.
  143. *
  144. * @param hdr The WAVE header.
  145. */
  146. PJ_DECL(void) pjmedia_wave_hdr_host_to_file( pjmedia_wave_hdr *hdr );
  147. PJ_END_DECL
  148. /**
  149. * @}
  150. */
  151. #endif /* __PJMEDIA_WAVE_H__ */