dev_audio.h 11 KB

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