4
4
#include < memory>
5
5
#include < filesystem>
6
6
7
- extern " C" {
8
- #include " simplevad/simple_vad.h"
9
- #include " simplevad/period_format.h"
10
- #include " simplevad/file_cut.h"
11
- }
12
- int run (FILE *fp, simple_vad *vad, struct cut_info *cut);
13
-
14
- int add_period_activity (struct periods *per, int is_active, int is_last);
15
-
16
- int main (int argc,char ** argv) {
7
+ int main (int argc, char **argv) {
17
8
// default cmake-build-debug/main
18
9
const char filename[] = " ../pcm/16k_1.pcm" ;
19
10
const char output_dir[] = " output_pcm" ;
20
11
const char output_filename_prefix[] = " 16k_1.pcm" ;
21
12
if (!std::filesystem::exists (output_dir)) {
22
13
std::filesystem::create_directories (output_dir);
23
14
}
24
-
25
- std::unique_ptr<FILE, decltype (&fclose)> fp (fopen (filename, " rb" ), &fclose);
26
- if (!fp) {
27
- std::cerr << filename << " does not exist\n " ;
28
- return 3 ;
29
- }
30
-
31
- std::unique_ptr<simple_vad, decltype (&simple_vad_free)> vad (
32
- simple_vad_create (), &simple_vad_free);
33
- if (!vad) {
34
- return 4 ;
35
- }
36
-
37
- std::unique_ptr<FILE, decltype (&fclose)> fp2 (fopen (filename, " rb" ), &fclose);
38
- std::unique_ptr<struct cut_info , decltype (&cut_info_free)> cut (cut_info_create (fp2.get ()), &cut_info_free);
39
-
40
- snprintf (cut->output_filename_prefix , sizeof (cut->output_filename_prefix ), " %s" ,
41
- output_filename_prefix);
42
- snprintf (cut->output_file_dir , sizeof (cut->output_file_dir ), " %s" , output_dir);
43
-
44
- int res = run (fp.get (), vad.get (), cut.get ());
45
-
46
- std::cout << " PROGRAM FINISH\n " ;
47
- return res;
48
- }
49
-
50
- int run (FILE *fp, simple_vad *vad, struct cut_info *cut) {
51
- int16_t data[FRAME_SIZE];
52
- int res = 0 ;
53
- struct periods *per = periods_create ();
54
-
55
- while (res == 0 ) {
56
- res = read_int16_bytes (fp, data);
57
- if (res <= 1 ) {
58
- int is_last = (res == 1 );
59
- int is_active = process_vad (vad, data);
60
- add_period_activity (per, is_active, is_last);
61
- int vad_file_res = cut_add_vad_activity (cut, is_active, is_last);
62
- if (vad_file_res < 0 ) {
63
- std::cout << " file write success " << cut->result_filename << " \n " ;
64
- }
65
- } else if (ferror (fp)) {
66
- std::cout << " read failed ferror result : " << ferror (fp) << " \n " ;
67
- }
68
- }
69
-
70
- periods_free (per);
71
-
72
- if (res != 1 ) {
73
- std::cerr << " read file error " << res << " \n " ;
74
- return res;
75
- }
76
- return 0 ;
77
- }
78
-
79
- int add_period_activity (struct periods *per, int is_active, int is_last) {
80
- static int old_is_active = 0 ;
81
- static int count = 0 ;
82
- int res_add = period_add_vad_activity (per, is_active, is_last);
83
- if (res_add != 0 ) {
84
- return res_add;
85
- }
86
- if (is_active != old_is_active) {
87
- old_is_active = is_active;
88
- }
89
- count += 1 ;
90
- if (is_last) {
91
- // periods_print(per);
92
- std::cout << " total frames " << count << " \n " ;
93
- }
94
15
}
0 commit comments