audio_test.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020-1-16 Wayne First version
  10. *
  11. ******************************************************************************/
  12. #include <rtconfig.h>
  13. #if defined(PKG_USING_WAVPLAYER)
  14. #include "wavrecorder.h"
  15. #include "wavplayer.h"
  16. #include <dfs_file.h>
  17. #include <unistd.h>
  18. #include <stdio.h>
  19. #include <sys/stat.h>
  20. #include <sys/statfs.h>
  21. /*
  22. The routine just for test automatically.
  23. - For record function: Run it w/o parameter.
  24. - For replay function: Run it with parameter.
  25. */
  26. static int audio_test(int argc, char **argv)
  27. {
  28. #define DEF_MAX_ARGV_NUM 8
  29. #define DEF_MAX_TEST_SECOND 5
  30. int smplrate[] = {8000, 16000, 44100, 48000};
  31. int smplbit[] = {16};
  32. int chnum[] = {1, 2};
  33. struct wavrecord_info info;
  34. char strbuf[128];
  35. int i, j, k;
  36. int bDoRecording = 1;
  37. struct stat stat_buf;
  38. if (argc > 1)
  39. bDoRecording = 0;
  40. for (i = 0; i < sizeof(smplrate) / sizeof(int); i++)
  41. {
  42. for (j = 0; j < sizeof(smplbit) / sizeof(int); j++)
  43. {
  44. for (k = 0; k < sizeof(chnum) / sizeof(int); k++)
  45. {
  46. snprintf(strbuf, sizeof(strbuf), "/mnt/sd0/%d_%d_%d.wav", smplrate[i], smplbit[j], chnum[k]);
  47. if (bDoRecording)
  48. {
  49. rt_kprintf("Recording file at %s\n", strbuf);
  50. info.uri = strbuf;
  51. info.samplerate = smplrate[i];
  52. info.samplebits = smplbit[j];
  53. info.channels = chnum[k];
  54. wavrecorder_start(&info);
  55. rt_thread_mdelay(DEF_MAX_TEST_SECOND * 1000);
  56. wavrecorder_stop();
  57. rt_thread_mdelay(DEF_MAX_TEST_SECOND * 1000);
  58. }
  59. else
  60. {
  61. if (stat((const char *)strbuf, &stat_buf) < 0)
  62. {
  63. rt_kprintf("%s non-exist.\n", strbuf);
  64. continue;
  65. }
  66. rt_kprintf("Replay file at %s\n", strbuf);
  67. wavplayer_play(strbuf);
  68. rt_thread_mdelay(DEF_MAX_TEST_SECOND * 1000);
  69. wavplayer_stop();
  70. }
  71. } // k
  72. } // j
  73. } // i
  74. return 0;
  75. }
  76. #ifdef FINSH_USING_MSH
  77. MSH_CMD_EXPORT(audio_test, Audio record / replay);
  78. #endif
  79. static int audio_overnight(int argc, char **argv)
  80. {
  81. #define DEF_MAX_TEST_SECOND 5
  82. struct wavrecord_info info;
  83. char strbuf[128];
  84. struct stat stat_buf;
  85. snprintf(strbuf, sizeof(strbuf), "/test.wav");
  86. while (1)
  87. {
  88. rt_kprintf("Recording file at %s\n", strbuf);
  89. info.uri = strbuf;
  90. info.samplerate = 16000;
  91. info.samplebits = 16;
  92. info.channels = 2;
  93. wavrecorder_start(&info);
  94. rt_thread_mdelay(DEF_MAX_TEST_SECOND * 1000);
  95. wavrecorder_stop();
  96. rt_thread_mdelay(1000);
  97. if (stat((const char *)strbuf, &stat_buf) < 0)
  98. {
  99. rt_kprintf("%s non-exist.\n", strbuf);
  100. break;
  101. }
  102. rt_kprintf("Replay file at %s\n", strbuf);
  103. wavplayer_play(strbuf);
  104. rt_thread_mdelay(DEF_MAX_TEST_SECOND * 1000);
  105. wavplayer_stop();
  106. rt_thread_mdelay(1000);
  107. }
  108. return 0;
  109. }
  110. #ifdef FINSH_USING_MSH
  111. MSH_CMD_EXPORT(audio_overnight, auto test record / replay);
  112. #endif
  113. #endif /* PKG_USING_WAVPLAYER */