format_wav.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #pragma once
  7. #include <stdint.h>
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. /**
  12. * @brief Header structure for WAV file with only one data chunk
  13. *
  14. * @note See this for reference: http://soundfile.sapp.org/doc/WaveFormat/
  15. *
  16. * @note Assignment to variables in this struct directely is only possible for little endian architectures
  17. * (including Xtensa & RISC-V)
  18. */
  19. typedef struct {
  20. struct {
  21. char chunk_id[4]; /*!< Contains the letters "RIFF" in ASCII form */
  22. uint32_t chunk_size; /*!< This is the size of the rest of the chunk following this number */
  23. char chunk_format[4]; /*!< Contains the letters "WAVE" */
  24. } descriptor_chunk; /*!< Canonical WAVE format starts with the RIFF header */
  25. struct {
  26. char subchunk_id[4]; /*!< Contains the letters "fmt " */
  27. uint32_t subchunk_size; /*!< This is the size of the rest of the Subchunk which follows this number */
  28. uint16_t audio_format; /*!< PCM = 1, values other than 1 indicate some form of compression */
  29. uint16_t num_of_channels; /*!< Mono = 1, Stereo = 2, etc. */
  30. uint32_t sample_rate; /*!< 8000, 44100, etc. */
  31. uint32_t byte_rate; /*!< ==SampleRate * NumChannels * BitsPerSample s/ 8 */
  32. uint16_t block_align; /*!< ==NumChannels * BitsPerSample / 8 */
  33. uint16_t bits_per_sample; /*!< 8 bits = 8, 16 bits = 16, etc. */
  34. } fmt_chunk; /*!< The "fmt " subchunk describes the sound data's format */
  35. struct {
  36. char subchunk_id[4]; /*!< Contains the letters "data" */
  37. uint32_t subchunk_size; /*!< ==NumSamples * NumChannels * BitsPerSample / 8 */
  38. int16_t data[0]; /*!< Holds raw audio data */
  39. } data_chunk; /*!< The "data" subchunk contains the size of the data and the actual sound */
  40. } wav_header_t;
  41. /**
  42. * @brief Default header for PCM format WAV files
  43. *
  44. */
  45. #define WAV_HEADER_PCM_DEFAULT(wav_sample_size, wav_sample_bits, wav_sample_rate, wav_channel_num) { \
  46. .descriptor_chunk = { \
  47. .chunk_id = {'R', 'I', 'F', 'F'}, \
  48. .chunk_size = (wav_sample_size) + sizeof(wav_header_t) - 8, \
  49. .chunk_format = {'W', 'A', 'V', 'E'} \
  50. }, \
  51. .fmt_chunk = { \
  52. .subchunk_id = {'f', 'm', 't', ' '}, \
  53. .subchunk_size = 16, /* 16 for PCM */ \
  54. .audio_format = 1, /* 1 for PCM */ \
  55. .num_of_channels = (wav_channel_num), \
  56. .sample_rate = (wav_sample_rate), \
  57. .byte_rate = (wav_sample_bits) * (wav_sample_rate) * (wav_channel_num) / 8, \
  58. .block_align = (wav_sample_bits) * (wav_channel_num) / 8, \
  59. .bits_per_sample = (wav_sample_bits)\
  60. }, \
  61. .data_chunk = { \
  62. .subchunk_id = {'d', 'a', 't', 'a'}, \
  63. .subchunk_size = (wav_sample_size) \
  64. } \
  65. }
  66. #ifdef __cplusplus
  67. }
  68. #endif