1
1
/*
2
- * Copyright (c) 2006-2023, RT-Thread Development Team
2
+ * Copyright (c) 2006-2025 RT-Thread Development Team
3
3
*
4
4
* SPDX-License-Identifier: Apache-2.0
5
5
*
22
22
#define MIN (a , b ) ((a) < (b) ? (a) : (b))
23
23
#endif
24
24
25
+ /**
26
+ * @addtogroup group_AudioPipe
27
+ */
28
+
29
+ /** @{ */
30
+
25
31
enum
26
32
{
27
33
REPLAY_EVT_NONE = 0x00 ,
28
34
REPLAY_EVT_START = 0x01 ,
29
35
REPLAY_EVT_STOP = 0x02 ,
30
36
};
31
37
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
+ */
32
52
static rt_err_t _audio_send_replay_frame (struct rt_audio_device * audio )
33
53
{
34
54
rt_err_t result = RT_EOK ;
@@ -108,6 +128,13 @@ static rt_err_t _audio_send_replay_frame(struct rt_audio_device *audio)
108
128
return result ;
109
129
}
110
130
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
+ */
111
138
static rt_err_t _audio_flush_replay_frame (struct rt_audio_device * audio )
112
139
{
113
140
rt_err_t result = RT_EOK ;
@@ -125,6 +152,13 @@ static rt_err_t _audio_flush_replay_frame(struct rt_audio_device *audio)
125
152
return result ;
126
153
}
127
154
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
+ */
128
162
static rt_err_t _aduio_replay_start (struct rt_audio_device * audio )
129
163
{
130
164
rt_err_t result = RT_EOK ;
@@ -142,6 +176,16 @@ static rt_err_t _aduio_replay_start(struct rt_audio_device *audio)
142
176
return result ;
143
177
}
144
178
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
+ */
145
189
static rt_err_t _aduio_replay_stop (struct rt_audio_device * audio )
146
190
{
147
191
rt_err_t result = RT_EOK ;
@@ -170,6 +214,13 @@ static rt_err_t _aduio_replay_stop(struct rt_audio_device *audio)
170
214
return result ;
171
215
}
172
216
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
+ */
173
224
static rt_err_t _audio_record_start (struct rt_audio_device * audio )
174
225
{
175
226
rt_err_t result = RT_EOK ;
@@ -190,6 +241,13 @@ static rt_err_t _audio_record_start(struct rt_audio_device *audio)
190
241
return result ;
191
242
}
192
243
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
+ */
193
251
static rt_err_t _audio_record_stop (struct rt_audio_device * audio )
194
252
{
195
253
rt_err_t result = RT_EOK ;
@@ -210,6 +268,20 @@ static rt_err_t _audio_record_stop(struct rt_audio_device *audio)
210
268
return result ;
211
269
}
212
270
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
+ */
213
285
static rt_err_t _audio_dev_init (struct rt_device * dev )
214
286
{
215
287
rt_err_t result = RT_EOK ;
@@ -288,6 +360,15 @@ static rt_err_t _audio_dev_init(struct rt_device *dev)
288
360
return result ;
289
361
}
290
362
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
+ */
291
372
static rt_err_t _audio_dev_open (struct rt_device * dev , rt_uint16_t oflag )
292
373
{
293
374
struct rt_audio_device * audio ;
@@ -334,6 +415,13 @@ static rt_err_t _audio_dev_open(struct rt_device *dev, rt_uint16_t oflag)
334
415
return RT_EOK ;
335
416
}
336
417
418
+ /**
419
+ * @brief Stop record, replay or both.
420
+ *
421
+ * @param[in] dev pointer to audio device
422
+ *
423
+ * @return useless param
424
+ */
337
425
static rt_err_t _audio_dev_close (struct rt_device * dev )
338
426
{
339
427
struct rt_audio_device * audio ;
@@ -357,6 +445,21 @@ static rt_err_t _audio_dev_close(struct rt_device *dev)
357
445
return RT_EOK ;
358
446
}
359
447
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
+ */
360
463
static rt_ssize_t _audio_dev_read (struct rt_device * dev , rt_off_t pos , void * buffer , rt_size_t size )
361
464
{
362
465
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
369
472
return rt_device_read (RT_DEVICE (& audio -> record -> pipe ), pos , buffer , size );
370
473
}
371
474
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
+ */
372
490
static rt_ssize_t _audio_dev_write (struct rt_device * dev , rt_off_t pos , const void * buffer , rt_size_t size )
373
491
{
374
492
@@ -424,6 +542,17 @@ static rt_ssize_t _audio_dev_write(struct rt_device *dev, rt_off_t pos, const vo
424
542
return index ;
425
543
}
426
544
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
+ */
427
556
static rt_err_t _audio_dev_control (struct rt_device * dev , int cmd , void * args )
428
557
{
429
558
rt_err_t result = RT_EOK ;
@@ -513,6 +642,19 @@ const static struct rt_device_ops audio_ops =
513
642
};
514
643
#endif
515
644
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
+ */
516
658
rt_err_t rt_audio_register (struct rt_audio_device * audio , const char * name , rt_uint32_t flag , void * data )
517
659
{
518
660
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
547
689
return result ;
548
690
}
549
691
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
+ */
550
699
int rt_audio_samplerate_to_speed (rt_uint32_t bitValue )
551
700
{
552
701
int speed = 0 ;
@@ -595,12 +744,32 @@ int rt_audio_samplerate_to_speed(rt_uint32_t bitValue)
595
744
return speed ;
596
745
}
597
746
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
+ */
598
756
void rt_audio_tx_complete (struct rt_audio_device * audio )
599
757
{
600
758
/* try to send next frame */
601
759
_audio_send_replay_frame (audio );
602
760
}
603
761
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
+ */
604
773
void rt_audio_rx_done (struct rt_audio_device * audio , rt_uint8_t * pbuf , rt_size_t len )
605
774
{
606
775
/* 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
610
779
if (audio -> parent .rx_indicate != RT_NULL )
611
780
audio -> parent .rx_indicate (& audio -> parent , len );
612
781
}
782
+
783
+ /** @} group_Audio */
0 commit comments