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