i2s_mic_example.c 1.6 KB

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