Skip to content

Commit a35af07

Browse files
author
litongmacos
committed
add audio_vad
1 parent fce85a4 commit a35af07

File tree

7 files changed

+106
-1
lines changed

7 files changed

+106
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,5 @@ cmake-build-debug-mingw/
151151
venv/
152152
.vs/
153153
Debug/
154-
*.bin
154+
*.bin
155+
*.wav

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ message(STATUS "SDL2 libraries: ${SDL2_LIBRARIES}")
1919

2020
include_directories(${SDL2_INCLUDE_DIRS})
2121

22+
# webrtc
23+
include_directories(webrtc)
24+
include_directories(.)
25+
# find cpp files
26+
file(GLOB SRC_FILES simplevad/*.c simplevad/*.h
27+
webrtc/common_audio/*/*.c webrtc/rtc_base/*.c*)
28+
29+
add_executable(audio_vad examples/audio_vad.cpp ${SRC_FILES})
30+
target_link_libraries(audio_vad pthread)
31+
32+
2233
# Detecting Operating Systems
2334
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
2435
# macOS
@@ -68,3 +79,4 @@ else ()
6879
endif ()
6980

7081

82+

examples/audio_vad.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include <cstdio>
2+
#include <cstdlib>
3+
#include <iostream>
4+
#include <memory>
5+
#include <filesystem>
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) {
17+
//default cmake-build-debug/main
18+
const char filename[] = "../pcm/16k_1.pcm";
19+
const char output_dir[] = "output_pcm";
20+
const char output_filename_prefix[] = "16k_1.pcm";
21+
if (!std::filesystem::exists(output_dir)) {
22+
std::filesystem::create_directories(output_dir);
23+
}
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(simple_vad_create(), &simple_vad_free);
32+
if (!vad) {
33+
return 4;
34+
}
35+
36+
std::unique_ptr<FILE, decltype(&fclose)> fp2(fopen(filename, "rb"), &fclose);
37+
std::unique_ptr<struct cut_info, decltype(&cut_info_free)> cut(cut_info_create(fp2.get()), &cut_info_free);
38+
39+
snprintf(cut->output_filename_prefix, sizeof(cut->output_filename_prefix), "%s", output_filename_prefix);
40+
snprintf(cut->output_file_dir, sizeof(cut->output_file_dir), "%s", output_dir);
41+
42+
int res = run(fp.get(), vad.get(), cut.get());
43+
44+
std::cout << "PROGRAM FINISH\n";
45+
return res;
46+
}
47+
48+
int run(FILE *fp, simple_vad *vad, struct cut_info *cut) {
49+
int16_t data[FRAME_SIZE];
50+
int res = 0;
51+
struct periods *per = periods_create();
52+
53+
while (res == 0) {
54+
res = read_int16_bytes(fp, data);
55+
if (res <= 1) {
56+
int is_last = (res == 1);
57+
int is_active = process_vad(vad, data);
58+
add_period_activity(per, is_active, is_last);
59+
int vad_file_res = cut_add_vad_activity(cut, is_active, is_last);
60+
if (vad_file_res < 0) {
61+
std::cout << "file write success " << cut->result_filename << "\n";
62+
}
63+
} else if (ferror(fp)) {
64+
std::cout << "read failed ferror result : " << ferror(fp) << "\n";
65+
}
66+
}
67+
68+
periods_free(per);
69+
70+
if (res != 1) {
71+
std::cerr << "read file error " << res << "\n";
72+
return res;
73+
}
74+
return 0;
75+
}
76+
77+
int add_period_activity(struct periods *per, int is_active, int is_last) {
78+
static int old_is_active = 0;
79+
static int count = 0;
80+
int res_add = period_add_vad_activity(per, is_active, is_last);
81+
if (res_add != 0) {
82+
return res_add;
83+
}
84+
if (is_active != old_is_active) {
85+
old_is_active = is_active;
86+
}
87+
count += 1;
88+
if (is_last) {
89+
//periods_print(per);
90+
std::cout << "total frames " << count << "\n";
91+
}
92+
}

pcm/16k_1.pcm

3.71 MB
Binary file not shown.

pcm/16k_57test.pcm

1.72 MB
Binary file not shown.

pcm/16k_test.pcm

127 KB
Binary file not shown.

pcm/nopause.pcm

19.3 MB
Binary file not shown.

0 commit comments

Comments
 (0)