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
|
/* Command line front end: a 16 kHz mono WAV in, segments or frame scores out. */
#include "pyseg.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wav.h"
static const char *usage = "usage: pyseg_cli [options] file.wav\n"
"\n"
" --mode speech|overlap|count|frames what to print (default speech)\n"
" --step SECONDS window hop, default 1.0 (a tenth of the window)\n"
" --threads N windows to run in parallel, default 1\n"
" --batch N windows per engine call, default 4\n"
" --weights FILE weight blob; default is the embedded copy\n"
" --onset X score above which a region opens, default 0.5\n"
" --offset X score below which it closes, default 0.5\n"
" --min-on SECONDS drop regions shorter than this, default 0\n"
" --min-off SECONDS fill gaps shorter than this, default 0\n"
" --scalar portable C kernels instead of SIMD\n"
"\n"
"speech and overlap print [start end] pairs in seconds; count and frames\n"
"print one line per frame.\n";
enum { MODE_SPEECH, MODE_OVERLAP, MODE_COUNT, MODE_FRAMES };
static int parse_mode(const char *s, int *mode) {
if (strcmp(s, "speech") == 0 || strcmp(s, "vad") == 0)
*mode = MODE_SPEECH;
else if (strcmp(s, "overlap") == 0 || strcmp(s, "osd") == 0)
*mode = MODE_OVERLAP;
else if (strcmp(s, "count") == 0)
*mode = MODE_COUNT;
else if (strcmp(s, "frames") == 0)
*mode = MODE_FRAMES;
else
return -1;
return 0;
}
int main(int argc, char **argv) {
pyseg_slide_params slide;
pyseg_binarize_params bin;
pyseg_slide_params_default(&slide);
pyseg_binarize_params_default(&bin);
int mode = MODE_SPEECH;
const char *path = NULL;
for (int i = 1; i < argc; ++i) {
const char *a = argv[i];
const char *v = (i + 1 < argc) ? argv[i + 1] : NULL;
#define NEED(name) \
if (!v) { \
fprintf(stderr, "%s needs a value\n", name); \
return 2; \
}
if (strcmp(a, "--mode") == 0) {
NEED("--mode");
if (parse_mode(v, &mode) != 0) {
fprintf(stderr, "unknown mode: %s\n", v);
return 2;
}
++i;
} else if (strcmp(a, "--step") == 0) {
NEED("--step");
slide.step = atof(v);
++i;
} else if (strcmp(a, "--threads") == 0) {
NEED("--threads");
slide.threads = atoi(v);
++i;
} else if (strcmp(a, "--batch") == 0) {
NEED("--batch");
slide.batch = atoi(v);
++i;
} else if (strcmp(a, "--weights") == 0) {
NEED("--weights");
slide.weights = v;
++i;
} else if (strcmp(a, "--onset") == 0) {
NEED("--onset");
bin.onset = atof(v);
++i;
} else if (strcmp(a, "--offset") == 0) {
NEED("--offset");
bin.offset = atof(v);
++i;
} else if (strcmp(a, "--min-on") == 0) {
NEED("--min-on");
bin.min_duration_on = atof(v);
++i;
} else if (strcmp(a, "--min-off") == 0) {
NEED("--min-off");
bin.min_duration_off = atof(v);
++i;
} else if (strcmp(a, "--scalar") == 0) {
slide.flags |= PYSEG_SCALAR_KERNELS;
} else if (strcmp(a, "-h") == 0 || strcmp(a, "--help") == 0) {
fputs(usage, stdout);
return 0;
} else if (a[0] == '-') {
fprintf(stderr, "unknown option: %s\n%s", a, usage);
return 2;
} else {
path = a;
}
#undef NEED
}
if (!path) {
fputs(usage, stderr);
return 2;
}
wav_t wav;
const char *err = NULL;
if (wav_read_mono_s16(path, &wav, &err) != 0) {
fprintf(stderr, "%s: %s\n", path, err ? err : "read failed");
return 1;
}
if (wav.sample_rate != PYSEG_SAMPLE_RATE) {
fprintf(stderr, "%s: expected %d Hz, got %d\n", path, PYSEG_SAMPLE_RATE, wav.sample_rate);
wav_free(&wav);
return 1;
}
const size_t count = wav.count;
const size_t frames = pyseg_slide_frames(count, slide.step);
float *tracks = malloc(frames * PYSEG_TRACKS * sizeof(float));
if (!tracks) {
wav_free(&wav);
return 1;
}
const size_t got = pyseg_slide_s16(wav.samples, count, &slide, tracks);
wav_free(&wav);
if (got == 0) {
fprintf(stderr, "inference failed: no engine could be created%s\n",
slide.weights ? " from the given weight file"
: "; this build may carry no embedded weights, "
"in which case pass --weights pyseg_weights.bin");
free(tracks);
return 1;
}
if (mode == MODE_FRAMES || mode == MODE_COUNT) {
for (size_t f = 0; f < got; ++f) {
const float *row = tracks + f * PYSEG_TRACKS;
if (mode == MODE_COUNT)
printf("%.3f %.4f\n", pyseg_frame_time(f), row[PYSEG_TRACK_COUNT]);
else
printf("%.3f %.4f %.4f %.4f\n", pyseg_frame_time(f), row[PYSEG_TRACK_SPEECH],
row[PYSEG_TRACK_OVERLAP], row[PYSEG_TRACK_COUNT]);
}
free(tracks);
return 0;
}
const int track = mode == MODE_SPEECH ? PYSEG_TRACK_SPEECH : PYSEG_TRACK_OVERLAP;
const size_t n = pyseg_binarize(tracks, got, track, &bin, NULL, 0);
pyseg_segment *segments = n ? malloc(n * sizeof *segments) : NULL;
if (n && !segments) {
free(tracks);
return 1;
}
const size_t written = pyseg_binarize(tracks, got, track, &bin, segments, n);
for (size_t i = 0; i < written; ++i)
printf("%.3f %.3f\n", segments[i].start, segments[i].end);
free(segments);
free(tracks);
return 0;
}
|