/ components / utils / include / wav_decode.h
wav_decode.h
  1  #ifndef _WAV_DECODE_H
  2  #define _WAV_DECODE_H
  3  
  4  /* .WAV file format :
  5  
  6   Endian      Offset      Length      Contents
  7    big         0           4 bytes     'RIFF'             // 0x52494646
  8    little      4           4 bytes     <file length - 8>
  9    big         8           4 bytes     'WAVE'             // 0x57415645
 10  
 11  Next, the fmt chunk describes the sample format:
 12  
 13    big         12          4 bytes     'fmt '          // 0x666D7420
 14    little      16          4 bytes     0x00000010      // Length of the fmt data (16 bytes)
 15    little      20          2 bytes     0x0001          // Format tag: 1 = PCM
 16    little      22          2 bytes     <channels>      // Channels: 1 = mono, 2 = stereo
 17    little      24          4 bytes     <sample rate>   // Samples per second: e.g., 22050
 18    little      28          4 bytes     <bytes/second>  // sample rate * block align
 19    little      32          2 bytes     <block align>   // channels * bits/sample / 8
 20    little      34          2 bytes     <bits/sample>   // 8 or 16
 21  (option)
 22  Finally, the data chunk contains the sample data:
 23  
 24    big         36          4 bytes     'data'        // 0x64617461
 25    little      40          4 bytes     <length of the data block>
 26    little      44          *           <sample data>
 27  
 28  */
 29  
 30  #include "stdint.h"
 31  
 32  //---------------------------------decode-----------------------------
 33  
 34  /* Audio file information structure */
 35  typedef struct _wav_t
 36  {
 37  	uint16_t numchannels;
 38  	uint32_t samplerate;
 39  	uint32_t byterate;
 40  	uint16_t blockalign;
 41  	uint16_t bitspersample;
 42  	uint32_t datasize;
 43  }wav_decode_t;
 44  
 45  /* Error Identification structure */
 46  typedef enum _wav_err_t
 47  {
 48  	OK = 0,						   //0
 49  	FILE_END,					   //1
 50  	FILE_FAIL,					   //2
 51  	UNVALID_RIFF_ID,			   //3
 52  	UNVALID_RIFF_SIZE,			   //4
 53  	UNVALID_WAVE_ID,			   //5
 54  	UNVALID_FMT_ID,				   //6
 55  	UNVALID_FMT_SIZE,			   //7
 56  	UNSUPPORETD_FORMATTAG,		   //8
 57  	UNSUPPORETD_NUMBER_OF_CHANNEL, //9
 58  	UNSUPPORETD_SAMPLE_RATE,	   //10
 59  	UNSUPPORETD_BITS_PER_SAMPLE,   //11
 60  	UNVALID_LIST_SIZE,			   //12
 61  	UNVALID_DATA_ID,			   //13
 62  }wav_err_t;
 63  
 64  wav_err_t wav_init(wav_decode_t *wav_obj,void* head, uint32_t file_size, uint32_t* head_len);
 65  
 66  //---------------------------------encode-----------------------------
 67  
 68  typedef struct
 69  {
 70  	uint32_t  riff_id;  /* {'r', 'i', 'f', 'f'}  */
 71  	uint32_t  file_size;
 72  	uint32_t  wave_id;
 73  } file_chunk_t;
 74  
 75  typedef struct
 76  {
 77    uint32_t fmt_ID;  /* {'f', 'm', 't', ' '} */
 78    uint32_t chunk_size;
 79    uint16_t format_tag;
 80    uint16_t numchannels;
 81    uint32_t samplerate;
 82    uint32_t byterate;
 83    uint16_t blockalign;
 84    uint16_t bitspersample;
 85    /* Note: there may be additional fields here, 
 86       depending upon wFormatTag. */
 87  } format_chunk_t;
 88  
 89  typedef struct
 90  {
 91    uint32_t data_ID;  /* {'d', 'a', 't', 'a'}  */
 92    uint32_t chunk_size;
 93    uint32_t* wave_data;
 94  } data_chunk_t;
 95  
 96  typedef struct _wav_encode_t
 97  {
 98  	file_chunk_t file;
 99  	format_chunk_t format;
100  	data_chunk_t data;
101  }wav_encode_t;
102  
103  #endif /* _WAV_DECODE_H */