From 0a331f04109c903174889cc436473749a26b164c Mon Sep 17 00:00:00 2001 From: Tilman Kamp <5991088+tilmankamp@users.noreply.github.com> Date: Fri, 24 Apr 2020 17:26:32 +0200 Subject: [PATCH] Utility for comparing samples --- bin/compare_samples.py | 46 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) mode change 100644 => 100755 bin/compare_samples.py diff --git a/bin/compare_samples.py b/bin/compare_samples.py old mode 100644 new mode 100755 index e69de29bb2..2db6d14ba3 --- a/bin/compare_samples.py +++ b/bin/compare_samples.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python +""" +Tool for comparing two wav samples +""" +import sys +import argparse + +from deepspeech_training.util.audio import AUDIO_TYPE_NP, compute_dbfs +from deepspeech_training.util.sample_collections import load_sample + + +def fail(message): + print(message, file=sys.stderr, flush=True) + sys.exit(1) + + +def compare_samples(): + sample1 = load_sample(CLI_ARGS.sample1) + sample2 = load_sample(CLI_ARGS.sample2) + if sample1.audio_format != sample2.audio_format: + fail('Samples differ on: audio-format') + if sample1.duration != sample2.duration: + fail('Samples differ on: duration') + sample1.change_audio_type(AUDIO_TYPE_NP) + sample2.change_audio_type(AUDIO_TYPE_NP) + audio_diff = sample1.audio - sample2.audio + dbfs, _ = compute_dbfs(audio_diff) + if dbfs > CLI_ARGS.threshold: + fail('Samples differ on: sample data ({:0.2f} dB difference) '.format(dbfs)) + print('Samples are considered equal ({:0.2f} dB difference)'.format(dbfs), file=sys.stderr, flush=True) + + +def handle_args(): + parser = argparse.ArgumentParser( + description="Tool for checking similarity of two samples" + ) + parser.add_argument("sample1", help="Filename of sample 1 to compare") + parser.add_argument("sample2", help="Filename of sample 2 to compare") + parser.add_argument("--threshold", type=float, default=-50.0, + help="Maximum dB difference of samples before they are considered different") + return parser.parse_args() + + +if __name__ == "__main__": + CLI_ARGS = handle_args() + compare_samples()