Repository navigation
Expand file tree
/
Copy pathten_vad_bench.cpp
More file actions
177 lines (158 loc) · 6.27 KB
/
Copy pathten_vad_bench.cpp
File metadata and controls
177 lines (158 loc) · 6.27 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// ten_vad_bench.cpp - Benchmark the C++ TEN-VAD port: peak memory, per-frame
// latency and real-time factor (RTF = processing time / audio duration).
//
// Usage: ten_vad_bench model.bin [seconds]
//
// Audio: loops test/vowel.wav (2 s) to the requested length; falls back to a
// synthesized speech-like tone if the file is missing.
#include <windows.h>
#include <psapi.h>
#pragma comment(lib, "psapi.lib")
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include "ten_vad/ten_vad.h"
#include "ten_vad_impl.h"
namespace {
struct WavData {
std::vector<int16_t> samples;
int sample_rate = 0;
int channels = 0;
};
bool loadWav(const char *path, WavData &out) {
std::FILE *fp = std::fopen(path, "rb");
if (!fp) return false;
char riff[4];
if (std::fread(riff, 1, 4, fp) != 4 || std::memcmp(riff, "RIFF", 4) != 0) {
std::fclose(fp);
return false;
}
uint32_t riff_size = 0;
std::fread(&riff_size, 4, 1, fp);
char wave[4];
std::fread(wave, 1, 4, fp);
char chunk_id[4];
while (std::fread(chunk_id, 1, 4, fp) == 4) {
uint32_t chunk_size = 0;
if (std::fread(&chunk_size, 4, 1, fp) != 1) break;
if (std::memcmp(chunk_id, "fmt ", 4) == 0) {
uint16_t a, ch, ba, bits;
uint32_t sr, br;
std::fread(&a, 2, 1, fp);
std::fread(&ch, 2, 1, fp);
std::fread(&sr, 4, 1, fp);
std::fread(&br, 4, 1, fp);
std::fread(&ba, 2, 1, fp);
std::fread(&bits, 2, 1, fp);
if (chunk_size > 16) std::fseek(fp, static_cast<long>(chunk_size - 16), SEEK_CUR);
} else if (std::memcmp(chunk_id, "data", 4) == 0) {
const size_t n_samples = chunk_size / 2;
out.samples.resize(n_samples);
if (n_samples > 0) {
const size_t got = std::fread(out.samples.data(), 2, n_samples, fp);
out.samples.resize(got);
}
} else {
std::fseek(fp, static_cast<long>(chunk_size + (chunk_size & 1)), SEEK_CUR);
}
}
std::fclose(fp);
out.sample_rate = 16000;
out.channels = 1;
return true;
}
struct MemInfo {
size_t peak_ws;
size_t current_ws;
};
MemInfo getMem() {
PROCESS_MEMORY_COUNTERS pmc;
if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) {
return {pmc.PeakWorkingSetSize, pmc.WorkingSetSize};
}
return {0, 0};
}
} // namespace
int main(int argc, char **argv) {
if (argc < 2) {
std::fprintf(stderr, "Usage: %s model.bin [seconds]\n", argv[0]);
return 1;
}
const char *model_path = argv[1];
const double seconds = argc > 2 ? std::atof(argv[2]) : 30.0;
const size_t total_samples = static_cast<size_t>(seconds * 16000);
// Build the input signal: loop test/vowel.wav, else synthesize.
WavData wav;
std::vector<int16_t> signal;
if (loadWav("test/vowel.wav", wav) && !wav.samples.empty()) {
signal.reserve(total_samples);
while (signal.size() < total_samples) {
const size_t need = total_samples - signal.size();
const size_t take = need < wav.samples.size() ? need : wav.samples.size();
signal.insert(signal.end(), wav.samples.begin(), wav.samples.begin() + take);
}
std::fprintf(stderr, "input: looped test/vowel.wav\n");
} else {
signal.resize(total_samples);
for (size_t n = 0; n < total_samples; ++n) {
const double t = static_cast<double>(n) / 16000.0;
const double env = 0.5 + 0.5 * std::sin(2.0 * 3.14159265358979 * 2.0 * t);
signal[n] = static_cast<int16_t>(
env * (9000.0 * std::sin(2.0 * 3.14159265358979 * 220.0 * t) +
3000.0 * std::sin(2.0 * 3.14159265358979 * 440.0 * t)));
}
std::fprintf(stderr, "input: synthesized speech-like tone\n");
}
const MemInfo mem_before = getMem();
ten_vad_ctx *vad = ten_vad_create(model_path);
if (!vad) {
std::fprintf(stderr, "error: cannot load model: %s\n", model_path);
return 1;
}
// Model loaded but nothing processed yet.
const MemInfo mem_after_load = getMem();
// Process all frames.
const size_t n_frames = signal.size() / TEN_VAD_HOP_SIZE;
LARGE_INTEGER freq, start, end;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&start);
for (size_t f = 0; f < n_frames; ++f) {
const int16_t *buf = &signal[f * TEN_VAD_HOP_SIZE];
(void)ten_vad_process(vad, buf, TEN_VAD_HOP_SIZE);
}
QueryPerformanceCounter(&end);
const double elapsed = static_cast<double>(end.QuadPart - start.QuadPart) /
static_cast<double>(freq.QuadPart);
const double audio_sec = static_cast<double>(n_frames) * 0.016;
// Inference-only timing (LSTM + dense, no feature extraction). Uses a zero
// conv input and resets the states so timing is independent of history.
{
float h[2][ten_vad::impl::kHiddenDim] = {};
float c[2][ten_vad::impl::kHiddenDim] = {};
float conv[ten_vad::conv::kConvOutLen] = {0.0f};
LARGE_INTEGER s2, e2;
QueryPerformanceCounter(&s2);
for (size_t f = 0; f < n_frames; ++f) {
(void)ten_vad::impl::runNetwork(vad->model, conv, h, c);
}
QueryPerformanceCounter(&e2);
const double t2 = static_cast<double>(e2.QuadPart - s2.QuadPart) /
static_cast<double>(freq.QuadPart);
std::printf("inference only : %.3f ms total, %.2f us/frame, RTF %.6f\n",
t2 * 1e3, t2 * 1e6 / n_frames, t2 / audio_sec);
}
const MemInfo mem_after = getMem();
std::printf("audio duration : %.3f s (%zu frames)\n", audio_sec, n_frames);
std::printf("total time : %.3f ms\n", elapsed * 1e3);
std::printf("per-frame : %.2f us\n", elapsed * 1e6 / n_frames);
std::printf("RTF : %.6f\n", elapsed / audio_sec);
std::printf("peak working set: %.2f MB\n", mem_after.peak_ws / 1048576.0);
std::printf("working set after model load : %.2f MB\n", mem_after_load.current_ws / 1048576.0);
std::printf("working set after processing : %.2f MB\n", mem_after.current_ws / 1048576.0);
(void)mem_before;
ten_vad_destroy(vad);
return 0;
}