dev_audio.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright (c) 2006-2025 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. * 2025-03-04 wumingzi add doxygen comments.
  11. */
  12. #ifndef __DEV_AUDIO_H__
  13. #define __DEV_AUDIO_H__
  14. #include "dev_audio_pipe.h"
  15. /**
  16. * @defgroup group_Audio Audio
  17. * @brief Audio driver API.
  18. * @ingroup group_device_driver
  19. *
  20. * @{
  21. */
  22. /**
  23. * @defgroup group_audio_control AUDIO_CTL
  24. *
  25. * @brief Control audio device.
  26. *
  27. * @{
  28. */
  29. /**
  30. * @brief Generate audio command code with @a a
  31. *
  32. * @param[in] a offset of command.
  33. *
  34. * @return audio device control command code.
  35. */
  36. #define _AUDIO_CTL(a) (RT_DEVICE_CTRL_BASE(Sound) + a)
  37. #define AUDIO_CTL_GETCAPS _AUDIO_CTL(1) /**< Get audio device capabilities */
  38. #define AUDIO_CTL_CONFIGURE _AUDIO_CTL(2) /**< Get audio device configuration */
  39. #define AUDIO_CTL_START _AUDIO_CTL(3) /**< Start audio device */
  40. #define AUDIO_CTL_STOP _AUDIO_CTL(4) /**< Stop audio device */
  41. #define AUDIO_CTL_GETBUFFERINFO _AUDIO_CTL(5) /**< Get audio device buffer information */
  42. /** @} */ /* End of group_audio_control */
  43. /**
  44. * @defgroup group_audio_type AUDIO_TYPE
  45. *
  46. * @brief Audio Device Types
  47. *
  48. * @{
  49. */
  50. #define AUDIO_TYPE_QUERY 0x00 /**< Query audio device type */
  51. #define AUDIO_TYPE_INPUT 0x01 /**< Set audio device type to input type */
  52. #define AUDIO_TYPE_OUTPUT 0x02 /**< Set audio device type to output type */
  53. #define AUDIO_TYPE_MIXER 0x04 /**< Set audio device type to mixer type */
  54. /** @} */ /* End of group_audio_type */
  55. /**
  56. * @defgroup group_audio_samp_rates AUDIO_SAMP_RATES
  57. *
  58. * @brief Supported audio sample rates for the audio device.
  59. *
  60. * @{
  61. */
  62. #define AUDIO_SAMP_RATE_8K 0x0001 /**< Set audio device sample rate to 8K */
  63. #define AUDIO_SAMP_RATE_11K 0x0002 /**< Set audio device sample rate to 11K */
  64. #define AUDIO_SAMP_RATE_16K 0x0004 /**< Set audio device sample rate to 16K */
  65. #define AUDIO_SAMP_RATE_22K 0x0008 /**< Set audio device sample rate to 22K */
  66. #define AUDIO_SAMP_RATE_32K 0x0010 /**< Set audio device sample rate to 32K */
  67. #define AUDIO_SAMP_RATE_44K 0x0020 /**< Set audio device sample rate to 44K */
  68. #define AUDIO_SAMP_RATE_48K 0x0040 /**< Set audio device sample rate to 48K */
  69. #define AUDIO_SAMP_RATE_96K 0x0080 /**< Set audio device sample rate to 96K */
  70. #define AUDIO_SAMP_RATE_128K 0x0100 /**< Set audio device sample rate to 128K */
  71. #define AUDIO_SAMP_RATE_160K 0x0200 /**< Set audio device sample rate to 160K */
  72. #define AUDIO_SAMP_RATE_172K 0x0400 /**< Set audio device sample rate to 172K */
  73. #define AUDIO_SAMP_RATE_192K 0x0800 /**< Set audio device sample rate to 192K */
  74. /** @} */ /* End of group_audio_samp_rates */
  75. /**
  76. * @defgroup group_audio_bit_rates AUDIO_BIT_RATES
  77. *
  78. * @brief Supported bit rates for the audio device.
  79. *
  80. * @{
  81. */
  82. #define AUDIO_BIT_RATE_22K 0x01 /**< Set audio device bit rates to 22K */
  83. #define AUDIO_BIT_RATE_44K 0x02 /**< Set audio device bit rates to 44K */
  84. #define AUDIO_BIT_RATE_48K 0x04 /**< Set audio device bit rates to 48K */
  85. #define AUDIO_BIT_RATE_96K 0x08 /**< Set audio device bit rates to 96K */
  86. #define AUDIO_BIT_RATE_128K 0x10 /**< Set audio device bit rates to 128K */
  87. #define AUDIO_BIT_RATE_160K 0x20 /**< Set audio device bit rates to 160K */
  88. #define AUDIO_BIT_RATE_172K 0x40 /**< Set audio device bit rates to 172K */
  89. #define AUDIO_BIT_RATE_192K 0x80 /**< Set audio device bit rates to 192K */
  90. /** @} */ /* End of group_audio_bit_rates */
  91. /**
  92. * @defgroup group_audio_dsp AUDIO_DSP
  93. *
  94. * @brief Support Dsp(input/output) Units controls. The macro group from application level, can
  95. * set audio mixer parameters including samplerate, channels etc.
  96. *
  97. * @{
  98. */
  99. #define AUDIO_DSP_PARAM 0 /**< get/set all params */
  100. #define AUDIO_DSP_SAMPLERATE 1 /**< samplerate */
  101. #define AUDIO_DSP_CHANNELS 2 /**< channels */
  102. #define AUDIO_DSP_SAMPLEBITS 3 /**< sample bits width */
  103. /** @} */ /* End of group_audio_dsp */
  104. /**
  105. * @defgroup group_audio_mixer AUDIO_MIXER
  106. *
  107. * @brief Supported Mixer Units controls. The macro group from driver level, can set audio mixer
  108. * parameters including volume, frequence db, microphone etc.
  109. *
  110. * @{
  111. */
  112. #define AUDIO_MIXER_QUERY 0x0000 /**< Query mixer capabilities */
  113. #define AUDIO_MIXER_MUTE 0x0001 /**< Mute audio device */
  114. #define AUDIO_MIXER_VOLUME 0x0002 /**< Set mixer volume */
  115. #define AUDIO_MIXER_BASS 0x0004 /**< Set the low-frequency section of the mixer */
  116. #define AUDIO_MIXER_MID 0x0008 /**< Set the mid-frequency section of the mixer */
  117. #define AUDIO_MIXER_TREBLE 0x0010 /**< Set the high-frequency section of the mixer */
  118. #define AUDIO_MIXER_EQUALIZER 0x0020 /**< Set equalizer option */
  119. #define AUDIO_MIXER_LINE 0x0040 /**< Set line control option */
  120. #define AUDIO_MIXER_DIGITAL 0x0080 /**< Set digital source */
  121. #define AUDIO_MIXER_MIC 0x0100 /**< Set microphone option */
  122. #define AUDIO_MIXER_VITURAL 0x0200 /**< Set virtual audio option */
  123. #define AUDIO_MIXER_EXTEND 0x8000 /**< Extend mixer command */
  124. /** @} */ /* End of group_audio_mixer */
  125. #define AUDIO_VOLUME_MAX (100)
  126. #define AUDIO_VOLUME_MIN (0)
  127. #define CFG_AUDIO_REPLAY_QUEUE_COUNT 4
  128. /**
  129. * @enum audio_stream
  130. *
  131. * @brief Audio stream control command
  132. */
  133. enum audio_stream
  134. {
  135. AUDIO_STREAM_REPLAY = 0,
  136. AUDIO_STREAM_RECORD,
  137. AUDIO_STREAM_LAST = AUDIO_STREAM_RECORD,
  138. };
  139. /**
  140. * @brief Audio buffer info
  141. *
  142. * The preferred number and size of audio pipeline buffer for the audio device, it
  143. * will be used in rt_audio_replay struct.
  144. *
  145. */
  146. struct rt_audio_buf_info
  147. {
  148. rt_uint8_t *buffer; /**< Audio buffer information */
  149. rt_uint16_t block_size; /**< Audio block_size information for replay function */
  150. rt_uint16_t block_count; /**< Audio block_count information for replay function */
  151. rt_uint32_t total_size; /**< Audio total_size which is equal to block_size multiplying
  152. * block_count information for replay function */
  153. };
  154. struct rt_audio_device;
  155. struct rt_audio_caps;
  156. struct rt_audio_configure;
  157. /**
  158. * @brief Aduio device operators
  159. */
  160. struct rt_audio_ops
  161. {
  162. /** Get audio capabilities information */
  163. rt_err_t (*getcaps)(struct rt_audio_device *audio, struct rt_audio_caps *caps);
  164. /** Configure audio devices */
  165. rt_err_t (*configure)(struct rt_audio_device *audio, struct rt_audio_caps *caps);
  166. /** Initialize audio device */
  167. rt_err_t (*init)(struct rt_audio_device *audio);
  168. /** Turn on the audio device */
  169. rt_err_t (*start)(struct rt_audio_device *audio, int stream);
  170. /** Turn off the audio device */
  171. rt_err_t (*stop)(struct rt_audio_device *audio, int stream);
  172. /** Transmit data between application and device */
  173. rt_ssize_t (*transmit)(struct rt_audio_device *audio, const void *writeBuf, void *readBuf, rt_size_t size);
  174. /** Get page size of codec or private buffer's info */
  175. void (*buffer_info)(struct rt_audio_device *audio, struct rt_audio_buf_info *info);
  176. };
  177. /**
  178. * @brief Audio configuration
  179. *
  180. * The preferred number and size of audio pipeline buffer for the audio device, it
  181. * will be used in rt_audio_caps struct.
  182. *
  183. */
  184. struct rt_audio_configure
  185. {
  186. rt_uint32_t samplerate; /**< Audio samplerate information */
  187. rt_uint16_t channels; /**< Audio channels information */
  188. rt_uint16_t samplebits; /**< Audio samplebits information */
  189. };
  190. /**
  191. * @brief Audio capabilities
  192. */
  193. struct rt_audio_caps
  194. {
  195. int main_type; /**< Audio main type, one value of @ref group_audio_type */
  196. int sub_type; /**< Audio sub type, one value of @ref group_audio_dsp or @ref group_audio_mixer */
  197. union
  198. {
  199. rt_uint32_t mask; /**< Capabilities mask */
  200. int value; /**< Capabilities value */
  201. struct rt_audio_configure config; /**< Audio samplebits information */
  202. } udata; /**< User data */
  203. };
  204. /**
  205. * @brief Audio replay
  206. */
  207. struct rt_audio_replay
  208. {
  209. struct rt_mempool *mp; /**< Memory pool for audio replay */
  210. struct rt_data_queue queue; /**< Replay data queue */
  211. struct rt_mutex lock; /**< Replay mutex lock */
  212. struct rt_completion cmp; /**< Replay completion, it will be */
  213. struct rt_audio_buf_info buf_info; /**< Replay buffer information */
  214. rt_uint8_t *write_data; /**< Pointer to the data to be written into data queue */
  215. rt_uint16_t write_index; /**< Index of pointer write_data.It records how much data
  216. * has been written in currently being played block */
  217. rt_uint16_t read_index; /**< Index of replaying data for audio device, it indicates index
  218. * of replay in the blocks which is currently being played */
  219. rt_uint32_t pos; /**< Global position of audio replay */
  220. rt_uint8_t event; /**< Event flag */
  221. rt_bool_t activated; /**< Activaty flag */
  222. };
  223. /**
  224. * @brief Audio record, the audio device pipe container of ringbuffer
  225. */
  226. struct rt_audio_record
  227. {
  228. struct rt_audio_pipe pipe;
  229. rt_bool_t activated;
  230. };
  231. /**
  232. * @brief Audio device
  233. */
  234. struct rt_audio_device
  235. {
  236. struct rt_device parent; /**< Audio device parents */
  237. struct rt_audio_ops *ops; /**< Audio device operator */
  238. struct rt_audio_replay *replay; /**< Pointer to audio replay structure */
  239. struct rt_audio_record *record; /**< Pointer to audio record structure */
  240. };
  241. rt_err_t rt_audio_register(struct rt_audio_device *audio, const char *name, rt_uint32_t flag, void *data);
  242. void rt_audio_tx_complete(struct rt_audio_device *audio);
  243. void rt_audio_rx_done(struct rt_audio_device *audio, rt_uint8_t *pbuf, rt_size_t len);
  244. /**
  245. * @defgroup group_audio_codec_cmd CODEC_CMD
  246. *
  247. * @brief Device Control Commands. The macro group from hardware level, can set codec
  248. * parametes including volume, EQ and 3D etc.
  249. *
  250. * @{
  251. */
  252. #define CODEC_CMD_RESET 0 /**< Reset audio device by codec */
  253. #define CODEC_CMD_SET_VOLUME 1 /**< Set volume by codec */
  254. #define CODEC_CMD_GET_VOLUME 2 /**< Get volume by codec */
  255. #define CODEC_CMD_SAMPLERATE 3 /**< Set sample rate by codec */
  256. #define CODEC_CMD_EQ 4 /**< Set equalizer by codec */
  257. #define CODEC_CMD_3D 5 /**< Set 3D effect by codec */
  258. #define CODEC_VOLUME_MAX (63)
  259. /** @} */ /* End of group_audio_codec_cmd */
  260. /** @} group_Audio */
  261. #endif /* __DEV_AUDIO_H__ */