Skip to content

Commit

Permalink
Utility for comparing samples
Browse files Browse the repository at this point in the history
  • Loading branch information
tilmankamp committed Apr 27, 2020
1 parent 1913765 commit 0a331f0
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions bin/compare_samples.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 0a331f0

Please sign in to comment.