Commit 0c363ed0 authored by Jan Kiene's avatar Jan Kiene
Browse files

Merge branch 'use-tracefiles-for-mld' into 'main'

[BASOP-CI] take tracefiles into account for MLD comparisons

See merge request !1773
parents d3393667 0ae60c3c
Loading
Loading
Loading
Loading
Loading
+67 −24
Original line number Diff line number Diff line
@@ -239,6 +239,8 @@ def compare(
    ssnr_thresh_high: float = np.inf,
    apply_thresholds_to_ref_only: bool = False,
    test_start_offset_ms: int = 0,
    ref_jbm_tf: Optional[Path] = None,
    test_jbm_tf: Optional[Path] = None,
) -> dict:
    """Compare two audio arrays

@@ -284,6 +286,10 @@ def compare(
    test = test[test_start_offset_samples:, :]

    framesize = fs // 50
    if ref.shape[0] != test.shape[0]:
        min_len = min(ref.shape[0], test.shape[0])
        diff = abs(test[:min_len, :] - ref[:min_len, :])
    else:
        diff = abs(test - ref)
    max_diff = int(diff.max())
    result = {
@@ -355,37 +361,74 @@ def compare(
            result["nframes_diff_percentage"] = nframes_diff_percentage

        if get_mld:

            def parse_wav_diff(proc: subprocess.CompletedProcess) -> float:
                line = proc.stdout.splitlines()[-1].strip()
                start = line.find(">") + 1
                stop = line.rfind("<")
                mld = float(line[start:stop].strip())

                return mld

                # TODO probably needs a fix to show up in pytest
                if proc.returncode:
                    print(f"{proc.stderr}\n{proc.stdout}")
                return mld_max

            mld_max = 0
            toolsdir = Path(__file__).parent.parent.joinpath("tools")

            curr_platform = platform.system()
            if curr_platform not in {"Windows", "Linux", "Darwin"}:
                raise NotImplementedError(f"MLD tool not available for {curr_platform}")
                raise NotImplementedError(
                    f"wav-diff tool not available for {curr_platform}"
                )
            search_path = toolsdir.joinpath(curr_platform.replace("Windows", "Win32"))
            mld = search_path.joinpath("mld")
            wdiff = search_path.joinpath("wav-diff")

            if not mld.exists():
                mld = shutil.which("mld")
                if mld is None:
                    raise FileNotFoundError(f"MLD tool not found in {search_path} or PATH!")
                warnings.warn(f"MLD binary not found in {search_path}! Falling back to {mld}!")
            if not wdiff.exists():
                wdiff = shutil.which("wav-diff")
                if wdiff is None:
                    raise FileNotFoundError(
                        f"wav-diff tool not found in {search_path} or PATH!"
                    )

            with tempfile.TemporaryDirectory() as tmpdir:
                for i in range(nchannels):
                    tmpfile_ref = Path(tmpdir).joinpath(f"ref_ch{i+1}.wav")
                    tmpfile_test = Path(tmpdir).joinpath(f"test_ch{i+1}.wav")
                    r48 = np.clip(
                        resample(ref[:, i].astype(float), fs, 48000), -32768, 32767
                    ).astype(
                        np.int16
                    )  # Convert to float for resample, then to int16 for wavfile.write
                    t48 = np.clip(
                        resample(test[:, i].astype(float), fs, 48000), -32768, 32767
                    ).astype(np.int16)
                    wavfile.write(str(tmpfile_ref), 48000, r48)
                    wavfile.write(str(tmpfile_test), 48000, t48)
                    out = subprocess.check_output([mld, tmpfile_ref, tmpfile_test])
                    mld_max = max(mld_max, float(out.split()[3]))
                tmpfile_ref = Path(tmpdir).joinpath("ref.wav")
                tmpfile_test = Path(tmpdir).joinpath("test.wav")

                ### need to resample to 48kHz for MLD computation to be correct
                if fs != 48000:
                    ref_tmp = np.clip(
                        resample(ref.astype(float), fs, 48000), -32768, 32767
                    )
                    test_tmp = np.clip(
                        resample(test.astype(float), fs, 48000), -32768, 32767
                    )
                else:
                    ref_tmp = ref.copy()
                    test_tmp = test.copy()

                wavfile.write(str(tmpfile_ref), fs, ref_tmp.astype(np.int16))
                wavfile.write(str(tmpfile_test), fs, test_tmp.astype(np.int16))

                cmd = [
                    str(wdiff),
                    "--print-ctest-measurement",
                    str(tmpfile_ref),
                    str(tmpfile_test),
                ]
                if ref_jbm_tf and test_jbm_tf:
                    cmd.extend(
                        [
                            "--ref-jbm-trace",
                            str(ref_jbm_tf),
                            "--cut-jbm-trace",
                            str(test_jbm_tf),
                        ]
                    )
                proc = subprocess.run(cmd, capture_output=True, text=True)
                mld_max = parse_wav_diff(proc)

            result["MLD"] = mld_max

+31 −0
Original line number Diff line number Diff line
/******************************************************************************************************

   (C) 2022-2024 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
   Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
   Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
   Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
   contributors to this repository. All Rights Reserved.

   This software is protected by copyright law and by international treaties.
   The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
   Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
   Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
   Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
   contributors to this repository retain full ownership rights in their respective contributions in
   the software. This notice grants no license of any kind, including but not limited to patent
   license, nor is any license granted by implication, estoppel or otherwise.

   Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
   contributions.

   This software is provided "AS IS", without any express or implied warranties. The software is in the
   development stage. It is intended exclusively for experts who have experience with such software and
   solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
   and fitness for a particular purpose are hereby disclaimed and excluded.

   Any dispute, controversy or claim arising under or in relation to providing this software shall be
   submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
   accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
   the United Nations Convention on Contracts on the International Sales of Goods.

*******************************************************************************************************/
 No newline at end of file
+1817 −0

File added.

Preview size limit exceeded, changes collapsed.

+1.5 MiB

File added.

No diff preview for this file type.

+31 −0
Original line number Diff line number Diff line
/******************************************************************************************************

   (C) 2022-2024 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
   Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
   Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
   Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
   contributors to this repository. All Rights Reserved.

   This software is protected by copyright law and by international treaties.
   The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
   Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
   Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
   Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
   contributors to this repository retain full ownership rights in their respective contributions in
   the software. This notice grants no license of any kind, including but not limited to patent
   license, nor is any license granted by implication, estoppel or otherwise.

   Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
   contributions.

   This software is provided "AS IS", without any express or implied warranties. The software is in the
   development stage. It is intended exclusively for experts who have experience with such software and
   solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
   and fitness for a particular purpose are hereby disclaimed and excluded.

   Any dispute, controversy or claim arising under or in relation to providing this software shall be
   submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
   accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
   the United Nations Convention on Contracts on the International Sales of Goods.

*******************************************************************************************************/
 No newline at end of file
Loading