Commit a383c9c5 authored by Tim Janik's avatar Tim Janik

SRC: audiowmark.cc: write --json output into an output file

Restore the ability to print out detection and debug info, while
writing the detection results into a separate file in JSON format.
Signed-off-by: 's avatarTim Janik <timj@gnu.org>
parent 696421cf
......@@ -58,7 +58,7 @@ print_usage()
printf ("\n");
printf ("Global options:\n");
printf (" --strength <s> set watermark strength [%.6g]\n", Params::water_delta * 1000);
printf (" --json produce JSON output\n");
printf (" --json <file> write JSON results into file\n");
printf (" --linear disable non-linear bit storage\n");
printf (" --short <bits> enable short payload mode\n");
printf (" --key <file> load watermarking key from file\n");
......@@ -457,9 +457,9 @@ parse_shared_options (ArgParser& ap)
{
Params::water_delta = f / 1000;
}
if (ap.parse_opt ("--json"))
if (ap.parse_opt ("--json", s))
{
Params::json_output = true;
Params::json_output = s;
}
if (ap.parse_opt ("--key", s))
{
......
......@@ -22,7 +22,6 @@
int Params::frames_per_bit = 2;
double Params::water_delta = 0.01;
bool Params::json_output = false;
bool Params::mix = true;
bool Params::hard = false; // hard decode bits? (soft decoding is better)
bool Params::snr = false; // compute/show snr while adding watermark
......@@ -44,6 +43,7 @@ RawFormat Params::raw_output_format;
int Params::hls_bit_rate = 0;
std::string Params::json_output;
std::string Params::input_label;
std::string Params::output_label;
......
......@@ -40,7 +40,7 @@ public:
static constexpr int min_band = 20;
static double water_delta;
static bool json_output;
static std::string json_output;
static bool mix;
static bool hard; // hard decode bits? (soft decoding is better)
static bool snr; // compute/show snr while adding watermark
......
......@@ -167,8 +167,14 @@ public:
patterns.push_back (p);
}
void
print_json (const WavData& wav_data)
print_json (const WavData& wav_data, const std::string &json_file)
{
FILE *outfile = fopen (json_file.c_str(), "w");
if (!outfile)
{
perror (("audiowmark: failed to open \"" + json_file + "\":").c_str());
exit (127);
}
std::stable_sort (patterns.begin(), patterns.end(), [](const Pattern& p1, const Pattern& p2) {
const int all1 = p1.type == Type::ALL;
const int all2 = p2.type == Type::ALL;
......@@ -178,18 +184,18 @@ public:
return p1.sync_score.index < p2.sync_score.index;
});
const size_t time_length = (wav_data.samples().size() / wav_data.n_channels() + wav_data.sample_rate()/2) / wav_data.sample_rate();
printf ("{ \"length\": \"%ld:%02ld\",\n", time_length / 60, time_length % 60);
printf (" \"matches\": [\n");
fprintf (outfile, "{ \"length\": \"%ld:%02ld\",\n", time_length / 60, time_length % 60);
fprintf (outfile, " \"matches\": [\n");
int nth = 0;
for (const auto& pattern : patterns)
{
if (nth++ != 0)
printf (",\n");
fprintf (outfile, ",\n");
if (pattern.type == Type::ALL) /* this is the combined pattern "all" */
{
printf (" { \"pos\": \"0:00\", \"bits\": \"%s\", \"quality\": %.5f, \"error\": %.6f, \"clip\": false, \"type\": \"ALL\" }",
bit_vec_to_str (pattern.bit_vec).c_str(),
pattern.sync_score.quality, pattern.decode_error);
fprintf (outfile, " { \"pos\": \"0:00\", \"bits\": \"%s\", \"quality\": %.5f, \"error\": %.6f, \"clip\": false, \"type\": \"ALL\" }",
bit_vec_to_str (pattern.bit_vec).c_str(),
pattern.sync_score.quality, pattern.decode_error);
}
else
{
......@@ -201,14 +207,15 @@ public:
case ConvBlockType::ab: blockc = "\"AB\""; break;
}
const int seconds = pattern.sync_score.index / Params::mark_sample_rate;
printf (" { \"pos\": \"%d:%02d\", \"bits\": \"%s\", \"quality\": %.5f, \"error\": %.6f, \"clip\": %s, \"type\": %s }",
seconds / 60, seconds % 60,
bit_vec_to_str (pattern.bit_vec).c_str(),
pattern.sync_score.quality, pattern.decode_error,
pattern.type == Type::CLIP ? "true" : "false", blockc);
fprintf (outfile, " { \"pos\": \"%d:%02d\", \"bits\": \"%s\", \"quality\": %.5f, \"error\": %.6f, \"clip\": %s, \"type\": %s }",
seconds / 60, seconds % 60,
bit_vec_to_str (pattern.bit_vec).c_str(),
pattern.sync_score.quality, pattern.decode_error,
pattern.type == Type::CLIP ? "true" : "false", blockc);
}
}
printf (" ]\n}\n");
fprintf (outfile, " ]\n}\n");
fclose (outfile);
}
void
print()
......@@ -619,11 +626,12 @@ decode_and_report (const WavData& wav_data, const string& orig_pattern)
ClipDecoder clip_decoder;
clip_decoder.run (wav_data, result_set);
if (Params::json_output)
{
result_set.print_json (wav_data);
}
else if (!orig_pattern.empty())
if (!Params::json_output.empty())
result_set.print_json (wav_data, Params::json_output);
result_set.print();
if (!orig_pattern.empty())
{
int match_count = result_set.print_match_count (orig_pattern);
......
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