11/*
2- * Copyright (c) 2006-2023, RT-Thread Development Team
2+ * Copyright (c) 2006-2025 RT-Thread Development Team
33 *
44 * SPDX-License-Identifier: Apache-2.0
55 *
2222#define MIN (a , b ) ((a) < (b) ? (a) : (b))
2323#endif
2424
25+ /**
26+ * @addtogroup group_AudioPipe
27+ */
28+
29+ /** @{ */
30+
2531enum
2632{
2733 REPLAY_EVT_NONE = 0x00 ,
2834 REPLAY_EVT_START = 0x01 ,
2935 REPLAY_EVT_STOP = 0x02 ,
3036};
3137
38+ /**
39+ * @brief Send a replay frame to the audio hardware device
40+ *
41+ * This function handles sending audio data from the memory queue to the hardware buffer for playback.
42+ * If there is no data available in the queue, it sends zero frames. Otherwise, it copies data from the memory pool
43+ * to the hardware device FIFO and manages the read index and position accordingly.
44+ *
45+ * @param[in] audio pointer to the audio device structure
46+ *
47+ * @return error code, RT_EOK is successful otherwise means failure
48+ *
49+ * @note This function may temporarily disable interrupts or perform time-consuming operations like memcpy,
50+ * which could affect system responsiveness
51+ */
3252static rt_err_t _audio_send_replay_frame (struct rt_audio_device * audio )
3353{
3454 rt_err_t result = RT_EOK ;
@@ -108,6 +128,13 @@ static rt_err_t _audio_send_replay_frame(struct rt_audio_device *audio)
108128 return result ;
109129}
110130
131+ /**
132+ * @brief Write replay frame into audio device replay queue
133+ *
134+ * @param[in] audio pointer to audio device
135+ *
136+ * @return error code, RT_EOK is successful otherwise means failure
137+ */
111138static rt_err_t _audio_flush_replay_frame (struct rt_audio_device * audio )
112139{
113140 rt_err_t result = RT_EOK ;
@@ -125,6 +152,13 @@ static rt_err_t _audio_flush_replay_frame(struct rt_audio_device *audio)
125152 return result ;
126153}
127154
155+ /**
156+ * @brief Replay audio
157+ *
158+ * @param[in] audio pointer to audio device
159+ *
160+ * @return error code, RT_EOK is successful otherwise means failure
161+ */
128162static rt_err_t _aduio_replay_start (struct rt_audio_device * audio )
129163{
130164 rt_err_t result = RT_EOK ;
@@ -142,6 +176,16 @@ static rt_err_t _aduio_replay_start(struct rt_audio_device *audio)
142176 return result ;
143177}
144178
179+ /**
180+ * @brief Stop replaying audio
181+ *
182+ * When audio->replay->queue is empty and the audio->replay->event was set REPLAY_EVT_STOP,
183+ * _audio_send_replay_frame will send completion to stop replaying audio.
184+ *
185+ * @param[in] audio pointer to audio device
186+ *
187+ * @return error code, RT_EOK is successful otherwise means failure
188+ */
145189static rt_err_t _aduio_replay_stop (struct rt_audio_device * audio )
146190{
147191 rt_err_t result = RT_EOK ;
@@ -170,6 +214,13 @@ static rt_err_t _aduio_replay_stop(struct rt_audio_device *audio)
170214 return result ;
171215}
172216
217+ /**
218+ * @brief Open audio pipe and start to record audio
219+ *
220+ * @param[in] audio pointer to audio device
221+ *
222+ * @return error code, RT_EOK is successful otherwise means failure
223+ */
173224static rt_err_t _audio_record_start (struct rt_audio_device * audio )
174225{
175226 rt_err_t result = RT_EOK ;
@@ -190,6 +241,13 @@ static rt_err_t _audio_record_start(struct rt_audio_device *audio)
190241 return result ;
191242}
192243
244+ /**
245+ * @brief stop recording audio and closeaudio pipe
246+ *
247+ * @param[in] audio pointer to audio device
248+ *
249+ * @return error code, RT_EOK is successful otherwise means failure
250+ */
193251static rt_err_t _audio_record_stop (struct rt_audio_device * audio )
194252{
195253 rt_err_t result = RT_EOK ;
@@ -210,6 +268,20 @@ static rt_err_t _audio_record_stop(struct rt_audio_device *audio)
210268 return result ;
211269}
212270
271+ /**
272+ * @brief Init audio pipe
273+ *
274+ * In kernel, this function will set replay or record function depending on device
275+ * flag. For replaying, it will malloc for managing audio replay struct meanwhile
276+ * creating mempool and dataqueue.For recording, it will creat audio pipe and
277+ * it's ringbuffer.
278+ * In driver, this function will only execute hardware driver initialization code
279+ * and get hardware buffer infomation.
280+ *
281+ * @param[in] dev pointer to audio device
282+ *
283+ * @return error code, RT_EOK is successful otherwise means failure
284+ */
213285static rt_err_t _audio_dev_init (struct rt_device * dev )
214286{
215287 rt_err_t result = RT_EOK ;
@@ -288,6 +360,15 @@ static rt_err_t _audio_dev_init(struct rt_device *dev)
288360 return result ;
289361}
290362
363+ /**
364+ * @brief Start record audio
365+ *
366+ * @param[in] dev pointer to audio device
367+ *
368+ * @param[in] oflag device flag
369+ *
370+ * @return error code, RT_EOK is successful otherwise means failure
371+ */
291372static rt_err_t _audio_dev_open (struct rt_device * dev , rt_uint16_t oflag )
292373{
293374 struct rt_audio_device * audio ;
@@ -334,6 +415,13 @@ static rt_err_t _audio_dev_open(struct rt_device *dev, rt_uint16_t oflag)
334415 return RT_EOK ;
335416}
336417
418+ /**
419+ * @brief Stop record, replay or both.
420+ *
421+ * @param[in] dev pointer to audio device
422+ *
423+ * @return useless param
424+ */
337425static rt_err_t _audio_dev_close (struct rt_device * dev )
338426{
339427 struct rt_audio_device * audio ;
@@ -357,6 +445,21 @@ static rt_err_t _audio_dev_close(struct rt_device *dev)
357445 return RT_EOK ;
358446}
359447
448+ /**
449+ * @brief Read audio device
450+ *
451+ * @param[in] dev pointer to device
452+ *
453+ * @param[in] pos position when reading
454+ *
455+ * @param[out] buffer a data buffer to save the read data
456+ *
457+ * @param[in] size buffer size
458+ *
459+ * @return the actually read size on successfully, otherwise 0 will be returned.
460+ *
461+ * @note
462+ */
360463static rt_ssize_t _audio_dev_read (struct rt_device * dev , rt_off_t pos , void * buffer , rt_size_t size )
361464{
362465 struct rt_audio_device * audio ;
@@ -369,6 +472,21 @@ static rt_ssize_t _audio_dev_read(struct rt_device *dev, rt_off_t pos, void *buf
369472 return rt_device_read (RT_DEVICE (& audio -> record -> pipe ), pos , buffer , size );
370473}
371474
475+ /**
476+ * @brief Write data into replay data queue and replay it
477+ *
478+ * @param[in] dev pointer to device
479+ *
480+ * @param[in] pos useless param
481+ *
482+ * @param[in] buffer a data buffer to be written into data queue
483+ *
484+ * @param[in] size buffer size
485+ *
486+ * @return the actually read size on successfully, otherwise 0 will be returned.
487+ *
488+ * @note This function will take mutex.
489+ */
372490static rt_ssize_t _audio_dev_write (struct rt_device * dev , rt_off_t pos , const void * buffer , rt_size_t size )
373491{
374492
@@ -424,6 +542,17 @@ static rt_ssize_t _audio_dev_write(struct rt_device *dev, rt_off_t pos, const vo
424542 return index ;
425543}
426544
545+ /**
546+ * @brief Control audio device
547+ *
548+ * @param[in] dev pointer to device
549+ *
550+ * @param[in] cmd audio cmd, it can be one of value in @ref audio_control
551+ *
552+ * @param[in] args command argument
553+ *
554+ * @return error code, RT_EOK is successful otherwise means failure
555+ */
427556static rt_err_t _audio_dev_control (struct rt_device * dev , int cmd , void * args )
428557{
429558 rt_err_t result = RT_EOK ;
@@ -513,6 +642,19 @@ const static struct rt_device_ops audio_ops =
513642};
514643#endif
515644
645+ /**
646+ * @brief Register and initialize audio device
647+ *
648+ * @param[in] audio pointer to audio deive
649+ *
650+ * @param[in] name device name
651+ *
652+ * @param[in] flag device flags
653+ *
654+ * @param[in] data user data
655+ *
656+ * @return error code, RT_EOK is successful otherwise means failure
657+ */
516658rt_err_t rt_audio_register (struct rt_audio_device * audio , const char * name , rt_uint32_t flag , void * data )
517659{
518660 rt_err_t result = RT_EOK ;
@@ -547,6 +689,13 @@ rt_err_t rt_audio_register(struct rt_audio_device *audio, const char *name, rt_u
547689 return result ;
548690}
549691
692+ /**
693+ * @brief Set audio sample rate
694+ *
695+ * @param[in] bitValue audio sample rate, it can be one of value in @ref audio_samp_rates
696+ *
697+ * @return speed has been set
698+ */
550699int rt_audio_samplerate_to_speed (rt_uint32_t bitValue )
551700{
552701 int speed = 0 ;
@@ -595,12 +744,32 @@ int rt_audio_samplerate_to_speed(rt_uint32_t bitValue)
595744 return speed ;
596745}
597746
747+ /**
748+ * @brief Send a replay frame to the audio hardware device
749+ *
750+ * See _audio_send_replay_frame for details
751+ *
752+ * @param[in] audio pointer to audio device
753+ *
754+ * @return void
755+ */
598756void rt_audio_tx_complete (struct rt_audio_device * audio )
599757{
600758 /* try to send next frame */
601759 _audio_send_replay_frame (audio );
602760}
603761
762+ /**
763+ * @brief Receive recording from audio device
764+ *
765+ * @param[in] audio pointer to audio device
766+ *
767+ * @param[in] pbuf pointer ro data to be received
768+ *
769+ * @param[in] len buffer size
770+ *
771+ * @return void
772+ */
604773void rt_audio_rx_done (struct rt_audio_device * audio , rt_uint8_t * pbuf , rt_size_t len )
605774{
606775 /* save data to record pipe */
@@ -610,3 +779,5 @@ void rt_audio_rx_done(struct rt_audio_device *audio, rt_uint8_t *pbuf, rt_size_t
610779 if (audio -> parent .rx_indicate != RT_NULL )
611780 audio -> parent .rx_indicate (& audio -> parent , len );
612781}
782+
783+ /** @} group_Audio */
0 commit comments