audio.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-05-09 Urey first version
  9. * 2019-07-09 Zero-Free improve device ops interface and data flows
  10. *
  11. */
  12. #ifndef __AUDIO_H__
  13. #define __AUDIO_H__
  14. #include "audio_pipe.h"
  15. /* AUDIO command */
  16. #define _AUDIO_CTL(a) (0x10 + a)
  17. #define AUDIO_CTL_GETCAPS _AUDIO_CTL(1)
  18. #define AUDIO_CTL_CONFIGURE _AUDIO_CTL(2)
  19. #define AUDIO_CTL_START _AUDIO_CTL(3)
  20. #define AUDIO_CTL_STOP _AUDIO_CTL(4)
  21. #define AUDIO_CTL_GETBUFFERINFO _AUDIO_CTL(5)
  22. /* Audio Device Types */
  23. #define AUDIO_TYPE_QUERY 0x00
  24. #define AUDIO_TYPE_INPUT 0x01
  25. #define AUDIO_TYPE_OUTPUT 0x02
  26. #define AUDIO_TYPE_MIXER 0x04
  27. /* Supported Sampling Rates */
  28. #define AUDIO_SAMP_RATE_8K 0x0001
  29. #define AUDIO_SAMP_RATE_11K 0x0002
  30. #define AUDIO_SAMP_RATE_16K 0x0004
  31. #define AUDIO_SAMP_RATE_22K 0x0008
  32. #define AUDIO_SAMP_RATE_32K 0x0010
  33. #define AUDIO_SAMP_RATE_44K 0x0020
  34. #define AUDIO_SAMP_RATE_48K 0x0040
  35. #define AUDIO_SAMP_RATE_96K 0x0080
  36. #define AUDIO_SAMP_RATE_128K 0x0100
  37. #define AUDIO_SAMP_RATE_160K 0x0200
  38. #define AUDIO_SAMP_RATE_172K 0x0400
  39. #define AUDIO_SAMP_RATE_192K 0x0800
  40. /* Supported Bit Rates */
  41. #define AUDIO_BIT_RATE_22K 0x01
  42. #define AUDIO_BIT_RATE_44K 0x02
  43. #define AUDIO_BIT_RATE_48K 0x04
  44. #define AUDIO_BIT_RATE_96K 0x08
  45. #define AUDIO_BIT_RATE_128K 0x10
  46. #define AUDIO_BIT_RATE_160K 0x20
  47. #define AUDIO_BIT_RATE_172K 0x40
  48. #define AUDIO_BIT_RATE_192K 0x80
  49. /* Support Dsp(input/output) Units controls */
  50. #define AUDIO_DSP_PARAM 0 /* get/set all params */
  51. #define AUDIO_DSP_SAMPLERATE 1 /* samplerate */
  52. #define AUDIO_DSP_CHANNELS 2 /* channels */
  53. #define AUDIO_DSP_SAMPLEBITS 3 /* sample bits width */
  54. /* Supported Mixer Units controls */
  55. #define AUDIO_MIXER_QUERY 0x0000
  56. #define AUDIO_MIXER_MUTE 0x0001
  57. #define AUDIO_MIXER_VOLUME 0x0002
  58. #define AUDIO_MIXER_BASS 0x0004
  59. #define AUDIO_MIXER_MID 0x0008
  60. #define AUDIO_MIXER_TREBLE 0x0010
  61. #define AUDIO_MIXER_EQUALIZER 0x0020
  62. #define AUDIO_MIXER_LINE 0x0040
  63. #define AUDIO_MIXER_DIGITAL 0x0080
  64. #define AUDIO_MIXER_MIC 0x0100
  65. #define AUDIO_MIXER_VITURAL 0x0200
  66. #define AUDIO_MIXER_EXTEND 0x8000 /* extend mixer command */
  67. #define AUDIO_VOLUME_MAX (100)
  68. #define AUDIO_VOLUME_MIN (0)
  69. #define CFG_AUDIO_REPLAY_QUEUE_COUNT 4
  70. enum
  71. {
  72. AUDIO_STREAM_REPLAY = 0,
  73. AUDIO_STREAM_RECORD,
  74. AUDIO_STREAM_LAST = AUDIO_STREAM_RECORD,
  75. };
  76. /* the preferred number and size of audio pipeline buffer for the audio device */
  77. struct rt_audio_buf_info
  78. {
  79. rt_uint8_t *buffer;
  80. rt_uint16_t block_size;
  81. rt_uint16_t block_count;
  82. rt_uint32_t total_size;
  83. };
  84. struct rt_audio_device;
  85. struct rt_audio_caps;
  86. struct rt_audio_configure;
  87. struct rt_audio_ops
  88. {
  89. rt_err_t (*getcaps)(struct rt_audio_device *audio, struct rt_audio_caps *caps);
  90. rt_err_t (*configure)(struct rt_audio_device *audio, struct rt_audio_caps *caps);
  91. rt_err_t (*init)(struct rt_audio_device *audio);
  92. rt_err_t (*start)(struct rt_audio_device *audio, int stream);
  93. rt_err_t (*stop)(struct rt_audio_device *audio, int stream);
  94. rt_size_t (*transmit)(struct rt_audio_device *audio, const void *writeBuf, void *readBuf, rt_size_t size);
  95. /* get page size of codec or private buffer's info */
  96. void (*buffer_info)(struct rt_audio_device *audio, struct rt_audio_buf_info *info);
  97. };
  98. struct rt_audio_configure
  99. {
  100. rt_uint32_t samplerate;
  101. rt_uint16_t channels;
  102. rt_uint16_t samplebits;
  103. };
  104. struct rt_audio_caps
  105. {
  106. int main_type;
  107. int sub_type;
  108. union
  109. {
  110. rt_uint32_t mask;
  111. int value;
  112. struct rt_audio_configure config;
  113. } udata;
  114. };
  115. struct rt_audio_replay
  116. {
  117. struct rt_mempool *mp;
  118. struct rt_data_queue queue;
  119. struct rt_mutex lock;
  120. struct rt_completion cmp;
  121. struct rt_audio_buf_info buf_info;
  122. rt_uint8_t *write_data;
  123. rt_uint16_t write_index;
  124. rt_uint16_t read_index;
  125. rt_uint32_t pos;
  126. rt_uint8_t event;
  127. rt_bool_t activated;
  128. };
  129. struct rt_audio_record
  130. {
  131. struct rt_audio_pipe pipe;
  132. rt_bool_t activated;
  133. };
  134. struct rt_audio_device
  135. {
  136. struct rt_device parent;
  137. struct rt_audio_ops *ops;
  138. struct rt_audio_replay *replay;
  139. struct rt_audio_record *record;
  140. };
  141. rt_err_t rt_audio_register(struct rt_audio_device *audio, const char *name, rt_uint32_t flag, void *data);
  142. void rt_audio_tx_complete(struct rt_audio_device *audio);
  143. void rt_audio_rx_done(struct rt_audio_device *audio, rt_uint8_t *pbuf, rt_size_t len);
  144. /* Device Control Commands */
  145. #define CODEC_CMD_RESET 0
  146. #define CODEC_CMD_SET_VOLUME 1
  147. #define CODEC_CMD_GET_VOLUME 2
  148. #define CODEC_CMD_SAMPLERATE 3
  149. #define CODEC_CMD_EQ 4
  150. #define CODEC_CMD_3D 5
  151. #define CODEC_VOLUME_MAX (63)
  152. #endif /* __AUDIO_H__ */