hpm_wav_codec.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2022 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_WAV_DECODER_H
  8. #define HPM_WAV_DECODER_H
  9. #include "hpm_audio_codec.h"
  10. /**
  11. * @brief riff header
  12. *
  13. */
  14. typedef struct {
  15. uint32_t groupid; /** fix "RIFF" */
  16. uint32_t size ; /** file size sub 8 */
  17. uint32_t riff_type; /** fix "WAVE" */
  18. } wav_riff;
  19. /**
  20. * @brief fmt header
  21. *
  22. */
  23. typedef struct {
  24. uint32_t id; /** fix "fmt" */
  25. uint32_t size ; /** sizeof(wav_formatchunk) - id - size */
  26. uint16_t audioformat; /** 0X01:PCM ;0X11:IMA ADPCM */
  27. uint16_t channels; /** channels num 1 or 2 */
  28. uint32_t samplerate; /** sample rate */
  29. uint32_t byterate; /** byterate=samplerate*channels*bitspersample/8 */
  30. uint16_t blockalign; /** bitspersample*channels/8 */
  31. uint16_t bitspersample; /** 8 16 24 32*/
  32. } wav_formatchunk;
  33. /**
  34. * @brief data header
  35. *
  36. */
  37. typedef struct {
  38. uint32_t id; /** fix "data" */
  39. uint32_t size; /** data size */
  40. } wav_data;
  41. /**
  42. * @brief wav head describe
  43. *
  44. */
  45. typedef struct {
  46. wav_riff riff_chunk; /** riff chunk */
  47. wav_formatchunk fmt_chunk; /** fmt chunk */
  48. wav_data data_chunk; /** data chunk */
  49. } hpm_wav_head;
  50. /**
  51. * @brief wav control
  52. *
  53. */
  54. typedef struct {
  55. char *file_name;
  56. hpm_wav_head wav_head;
  57. uint32_t sec_total; /** song total seconds */
  58. uint32_t sec_cur; /** song current seconds */
  59. uint32_t data_pos; /** data position */
  60. uint32_t remaining_data; /** The amount of data remaining, according to which the music has been played */
  61. hpm_audiocodec_callback func; /** callback function */
  62. } hpm_wav_ctrl;
  63. /**
  64. * @brief Init wav decode function
  65. *
  66. * @param[in] fname file path and name string
  67. * @param[inout] wav_ctrl @ref hpm_wav_ctrl
  68. * @param[in] pbuf buffer, minimum 512 bytes
  69. *
  70. * @return @ref hpm_stat_t
  71. */
  72. hpm_stat_t hpm_wav_decode_init(char *fname, hpm_wav_ctrl *wav_ctrl, uint8_t (*pbuf)[512]);
  73. /**
  74. * @brief hpm wav file decode
  75. *
  76. * @param[in] wav_ctrl @ref hpm_wav_ctrl
  77. * @param[out] buf output data
  78. * @param size buf size
  79. * @return uint32_t data size
  80. */
  81. uint32_t hpm_wav_decode(hpm_wav_ctrl *wav_ctrl, uint8_t *buf, uint32_t size);
  82. #endif