1 // /* pcm_record.c */
2 // #include "rtconfig.h"
3 // #if defined(BSP_USING_I2S)||defined(BSP_USING_SDIF)
4 // #include <rtthread.h>
5 // #include <rtdevice.h>
6 // #include <dfs_posix.h>
7 
8 // #define RECORD_TIME_MS      5000
9 // #define RT_I2S_SAMPLERATE   8000
10 // #define RECORD_CHANNEL      2
11 // #define RECORD_CHUNK_SZ     ((RT_I2S_SAMPLERATE * RECORD_CHANNEL * 2) * 20 / 1000)
12 
13 // #define SOUND_DEVICE_NAME    "I2S0"      /* Audio 设备名称 */
14 // static rt_device_t mic_dev;              /* Audio 设备句柄 */
15 
16 // int pcm_record()
17 // {
18 //     int fd = -1;
19 //     uint8_t *buffer = NULL;
20 //     int length, total_length = 0;
21 
22 //     fd = open("file.pcm", O_WRONLY | O_CREAT);
23 //     if (fd < 0)
24 //     {
25 //         rt_kprintf("open file for recording failed!\n");
26 //         return -1;
27 //     }
28 //     buffer = rt_malloc(RECORD_CHUNK_SZ);
29 //     if (buffer == RT_NULL)
30 //         goto __exit;
31 //     mic_dev = rt_device_find(SOUND_DEVICE_NAME);
32 //     if (mic_dev == RT_NULL)
33 //         goto __exit;
34 //     rt_device_open(mic_dev, RT_DEVICE_OFLAG_RDONLY);
35 //     while (1)
36 //     {
37 //         length = rt_device_read(mic_dev, 0, buffer, RECORD_CHUNK_SZ);
38 //         if (length)
39 //         {
40 //             write(fd, buffer, length);
41 //             total_length += length;
42 //         }
43 
44 //         if ((total_length / RECORD_CHUNK_SZ) >  (RECORD_TIME_MS / 20))
45 //             break;
46 //     }
47 
48 //     close(fd);
49 
50 //     rt_device_close(mic_dev);
51 
52 // __exit:
53 //     if (fd >= 0)
54 //         close(fd);
55 
56 //     if (buffer)
57 //         rt_free(buffer);
58 
59 //     return 0;
60 // }
61 // MSH_CMD_EXPORT(pcm_record, record voice to a pcm file);  // 修改命令描述
62 // #endif