-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
144 lines (126 loc) · 3.74 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* File: main.cpp
* Author: Vladimir Zyablitskiy <https://github.com/rainlabs>
*
* Created on 10 апреля 2014 г., 19:55
*/
#include "stranger.hpp"
#include <iostream>
#include <fstream>
using namespace Stranger;
std::string gFile = "";
std::string gSvmDb = "data/svm.model";
SizeType gDuration = 30;
SizeType gShift = 15;
SizeType gFrameSize = 1;
int gLifter = 16;
SizeType gMode = Vad::PREDICT;
void processArguments(int argc, char *argv[]);
void progress(float percent) {
std::cout << "\r" << percent << "% completed." << std::flush;
}
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
int main(int argc, char* argv[]) {
processArguments(argc, argv);
Vad vad(gSvmDb, gMode);
std::size_t i;
std::vector<SampleType> values;
vad.setDuration(gDuration)
.setFrame(gFrameSize)
.setShift(gShift)
.setLifterIndex(gLifter);
if (gMode == Vad::PREDICT) {
vad.loadSignal(gFile);
values = vad.process();
for(i = 0; i < values.size(); i++) {
std::cout << i << "\t" << values[i] << std::endl;
}
} else {
std::ifstream file(gFile);
int type;
SizeType procent = 100.0 / std::count(std::istreambuf_iterator<char>(file),
std::istreambuf_iterator<char>(), '\n');
int n = 0;
file.close();
file.open(gFile);
for (std::string line; std::getline(file, line); ) {
progress(procent * n++);
auto x = split(line, '\t');
type = atoi(x.front().c_str());
vad.loadSignal(x[1]).destroyMfcc();
if (type > 0) {
vad.setVoice();
} else {
vad.setSilence();
}
vad.process();
}
vad.saveSVM();
}
return 0;
}
/**
* -m - mode (0-TRAIN|1-PREDICT, default: PREDICT)
* -t - duration in ms (default: 30)
* -s - shift in ms (default: 15)
* -d - svm model db (default: "svm.model")
* -f - frame size (default: 1)
* -l - lifter coefficient (default: 16, without liftering)
* @param argc
* @param argv
*/
void processArguments(int argc, char *argv[]) {
char* s1;
if (argc <= 1) {
fprintf(stderr, "arguments are not found\n");
exit(1);
}
for(int i = 1; i < argc; i++) {
if (argv[i][0] == '-' && argv[i][1] != 0) {
s1 = "";
if (i + 1 >= argc) {
fprintf(stderr, "not valid argument %s\n", argv[i]);
exit(2);
}
s1 = argv[i + 1];
switch (argv[i][1]) {
case 't':
gDuration = atoi(s1);
break;
case 'd':
gSvmDb = s1;
break;
case 's':
gShift = atoi(s1);
break;
case 'f':
gFrameSize = atoi(s1);
break;
case 'l':
gLifter = atoi(s1);
break;
case 'm':
gMode = atoi(s1);
break;
}
} else {
gFile = argv[i];
}
}
if (gFile.size() == 0) {
fprintf(stderr, "sound file is not specified for PREDICT mode\n");
exit(3);
}
}