Commit c4ca66f4 authored by ale's avatar ale

add options to control the output format

parent 5e4f3452
...@@ -4,6 +4,9 @@ ...@@ -4,6 +4,9 @@
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <getopt.h>
#include <unistd.h>
#include <immsutil.h> #include <immsutil.h>
#include <appname.h> #include <appname.h>
#include <base64.h> #include <base64.h>
...@@ -39,6 +42,19 @@ void dump_features_json(Features *f) { ...@@ -39,6 +42,19 @@ void dump_features_json(Features *f) {
<< "}" << endl; << "}" << endl;
} }
void dump_features_raw(Features *f, int do_base64) {
int n = Features::SerializedSize;
char buf[n];
f->serialize(buf, n);
if (do_base64) {
cout << base64::base64_encode((unsigned char *)buf, n)
<< endl;
} else {
write(1, buf, n);
}
}
Features *analyze(FILE *stream) { Features *analyze(FILE *stream) {
int n; int n;
char buf[1024]; char buf[1024];
...@@ -52,18 +68,43 @@ Features *analyze(FILE *stream) { ...@@ -52,18 +68,43 @@ Features *analyze(FILE *stream) {
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if (argc > 1) int opt;
{ int output_base64 = 0;
cout << "usage: analyzer" << endl int output_json = 0;
<< "File data must be passed on standard input as:" << endl
<< " raw PCM, 16-bit unsigned, mono, " << SAMPLERATE << " Hz" while ((opt = getopt(argc, argv, "bj")) > 0) {
<< endl << endl; switch (opt) {
return -1; case 'b':
output_base64 = 1;
break;
case 'j':
output_json = 1;
break;
} }
}
if (optind < argc) {
cout << "usage: analyzer [-b|-j]" << endl
<< "File data must be passed on standard input as:" << endl
<< " raw PCM, 16-bit unsigned, mono, " << SAMPLERATE << " Hz"
<< endl << endl
<< "The output vector will be dumped on standard output." << endl
<< "With the -b option, the output will be base64-encoded." << endl
<< "With the -j option, the output will be a JSON object." << endl
<< endl;
return 1;
}
Features *f = analyze(stdin);
if (!f) {
LOG(ERROR) << "Could not process input data." << endl;
} else {
if (output_json == 1) {
dump_features_json(f);
} else {
dump_features_raw(f, output_base64);
}
}
Features *f = analyze(stdin); return 0;
if (!f)
LOG(ERROR) << "Could not process input data." << endl;
else
dump_features_json(f);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment