Commit a5d3feb2 authored by Jan Kiene's avatar Jan Kiene
Browse files

make batch_comp_audio work with MLD tool for mono files

parent 788c16aa
Loading
Loading
Loading
Loading
+44 −14
Original line number Diff line number Diff line
@@ -48,8 +48,9 @@ DIFF_EXPR = r"Max Diff\s+=\s+(\d+)"

def main(args):

    if shutil.which("CompAudio") is None:
        print("CompAudio not in PATH - abort.")
    tool = args.tool
    if shutil.which(tool) is None:
        print(f"{tool} not in PATH - abort.")
        sys.exit(-1)

    num_files_diff = 0
@@ -76,20 +77,25 @@ def main(args):
                    repeat(fol1),
                    repeat(fol2),
                    repeat(outputs),
                    repeat(tool)
                )
        else:
            # if only one thread is passed, do everything in the main thread
            # to allow for meaningful debugging if needed
            for f in common_files:
                compare_files(f, fol1, fol2, outputs)
                compare_files(f, fol1, fol2, outputs, tool)

        if args.sort:
            out = dict(sorted(outputs.items(), key=lambda item: item[1]))
            out = dict(sorted(outputs.items(), key=SORT_FUNC[tool]))
        else:
            out = outputs

        for f, output_tuple in out.items():
            diff, snr, gain, seg_snr = output_tuple
        for f, tool_output in out.items():

            if tool == "CompAudio":
                diff, snr, gain, seg_snr = tool_output
            elif tool == "mld":
                diff = tool_output

            if diff > 0:
                num_files_diff = num_files_diff + 1
@@ -99,9 +105,9 @@ def main(args):
                    label = "[OKAY]"
                else:
                    label = "[FAIL]"
                result = f"{label} Max. diff (PCM) for file {f}: {diff}"
                result = DIFF_STR[tool].format(label=label, f=f, diff=diff)

                if args.verbose and diff != 0.0:
                if tool == "CompAudio" and args.verbose and diff != 0.0:
                    result += f", SNR = {snr:4.2f} dB (File 2 Gain = {gain:4.3f})"
                    result += f", Seg. SNR = {seg_snr:4.2f} dB"

@@ -113,24 +119,24 @@ def main(args):
            print(f"All files are bitexact", file=out_file)


def compare_files(f, fol1, fol2, outputs_dict):
def compare_files(f, fol1, fol2, outputs_dict, tool):
    """
    Compare file f in both folders fol1 and fol2 using CompAudio and
    Compare file f in both folders fol1 and fol2 using the given tool and
    store the parsed difference in outputs_dict.
    """
    f1 = os.path.join(fol1, f)
    f2 = os.path.join(fol2, f)
    cmd = f"CompAudio {f1} {f2}"
    cmd = f"{tool} {f1} {f2}"
    try:
        output = subprocess.check_output(cmd.split(" "))
    except subprocess.CalledProcessError:
        print("CompAudio returned a non-zero exit status. Check your files.")
        print(f"{tool} returned a non-zero exit status. Check your files.")
        sys.exit(-1)

    output_tuple = _parse_comp_audio(output)
    tool_output = PARSE_FUNC[tool](output)

    with threading.Lock():
        outputs_dict.update({f: output_tuple})
        outputs_dict.update({f: tool_output})


def get_common_files(fol1, fol2):
@@ -205,6 +211,27 @@ def _parse_comp_audio(output):
    return diff, snr, gain, seg_snr


def _parse_mld(output):
    output = output.decode("utf-8")
    mld = float(output.split()[-1])

    return mld


PARSE_FUNC = {
    "CompAudio": _parse_comp_audio,
    "mld": _parse_mld,
        }
SORT_FUNC = {
        "CompAudio": lambda item: item[1],
        "mld": lambda item: item,
        }
DIFF_STR = {
    "CompAudio": "{label} Max. diff (PCM) for file {f}: {diff}",
    "mld": "{label} MLD diff for file {f}: {diff}",
        }


class OutFileManager:
    def __init__(self, out_file):
        self.out_file = out_file
@@ -256,6 +283,9 @@ if __name__ == "__main__":
    parser.add_argument(
        "-t", "--num_threads", type=int, default=1, help="Number of threads to use"
    )
    parser.add_argument(
            "--tool", choices=["mld", "CompAudio"], default="CompAudio", help="Compare tool to run"
            )
    args = parser.parse_args()

    main(args)