Commit faffe3d1 authored by Archit Tamarapu's avatar Archit Tamarapu
Browse files

avoid code duplication by importing from existing files, add a TODO

parent c579125c
Loading
Loading
Loading
Loading

tests/binaural/compare_audio.py

deleted100644 → 0
+0 −105
Original line number Diff line number Diff line
#!/usr/bin/env python3

"""
   (C) 2022-2023 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.
"""

import sys
import warnings
from typing import Tuple

import numpy as np

from .constants import SCRIPTS_DIR

sys.path.append(str(SCRIPTS_DIR))
from pyaudio3dtools.audioarray import getdelay


def compare_audio_arrays(
    left: np.ndarray, left_fs: int, right: np.ndarray, right_fs: int
) -> Tuple[float, float]:
    if left_fs != right_fs:
        return ValueError(f"Differing samplerates: {left_fs} vs {right_fs}!")

    if left.shape[1] != right.shape[1]:
        cmp_ch = min(left.shape[1], right.shape[1])
        warnings.warn(
            f"Differing number of channels: {left.shape[1]} vs {right.shape[1]}! Comparing first {cmp_ch} channel(s)",
            category=RuntimeWarning,
        )
        left = left[:, :cmp_ch]
        right = right[:, :cmp_ch]

    if left.shape[0] != right.shape[0]:
        cmp_smp = min(left.shape[0], right.shape[0])
        warnings.warn(
            f"Warning - different durations: {left.shape[0] / left_fs:.2f}s vs {right.shape[0] / right_fs:.2f}s! Comparing first {cmp_smp / left_fs : .2f} sample(s)",
            category=RuntimeWarning,
        )
        left = left[:cmp_smp, :]
        right = right[:cmp_smp, :]

    if not np.array_equal(left, right):
        delay = getdelay(left, right)
        delay_abs = np.abs(delay)
        # getdelay can return large values if signals are quite different
        # limit any delay compensation to 20 ms
        if delay != 0 and (delay_abs < left_fs / 50):
            warnings.warn(
                f"File B is delayed by {delay} samples ({delay*1000 / left_fs : .2f}ms)!",
                category=RuntimeWarning,
            )

            # shift array
            left = np.roll(left, delay, axis=0)

            # zero shifted out samples
            if delay < 0:
                left[-np.abs(delay) :, :] = 0
            elif delay > 0:
                left[: np.abs(delay), :] = 0
        """
        http://www-mmsp.ece.mcgill.ca/Documents/Software/Packages/AFsp/AFsp/CompAudio.html
        """
        num = np.sum(left * right)
        den = np.sqrt(np.sum(left**2) * np.sum(right**2))
        if den > 0:
            r = num / den
        else:
            r = np.inf
        snr = 10 * np.log10(1 / (1 - (r**2)))
        gain_b = num / np.sum(right**2)
        max_diff = np.abs(np.max(left - right))
    else:
        snr = np.inf
        gain_b = 1
        max_diff = 0

    return snr, gain_b, max_diff
+7 −4
Original line number Diff line number Diff line
@@ -32,10 +32,14 @@

from pathlib import Path
import re
from tests.renderer.constants import (
    TESTS_DIR,
    SCRIPTS_DIR,
    TEST_VECTOR_DIR,
    TESTV_DIR,
    OUTPUT_FORMATS_BINAURAL
)

TESTS_DIR = Path(__file__).parent
SCRIPTS_DIR = TESTS_DIR.parents[1].joinpath("scripts").resolve()
TESTV_DIR = SCRIPTS_DIR.joinpath("testv")
BITSTREAM_DIR = TESTS_DIR.joinpath("bitstream")
DEC_ROM_DIR = TESTS_DIR.joinpath("dec_out_rom")
HRTF_BINARY_DIR = SCRIPTS_DIR.joinpath("binauralRenderer_interface", "binaural_renderers_hrtf_data")
@@ -91,7 +95,6 @@ FORMAT_TO_METADATA_FILES = {
    "ISM" : "stvISM{}.csv"
}

OUTPUT_FORMATS_BINAURAL = ["BINAURAL", "BINAURAL_ROOM_IR", "BINAURAL_ROOM_REVERB"]
HR_TRAJECTORIES_TO_TEST = ["headrot_case00_3000_q", "headrot"]


+3 −50
Original line number Diff line number Diff line
@@ -30,66 +30,19 @@
   the United Nations Convention on Contracts on the International Sales of Goods.
"""

import logging
import pytest
import subprocess as sp
import sys
from typing import Dict, Optional, Tuple
import numpy as np
from typing import Dict, Optional
import os
import uuid

from .constants import *
from .compare_audio import compare_audio_arrays
from tests.renderer.compare_audio import compare_audio_arrays
from tests.renderer.utils import check_BE, test_info, run_cmd

sys.path.append(SCRIPTS_DIR)
import pyaudio3dtools

# fixture returns test information, enabling per-testcase SNR
@pytest.fixture
def test_info(request):
    return request


def run_cmd(cmd, env=None):
    logging.info(f"\nRunning command\n{' '.join(cmd)}\n")
    try:
        sp.run(cmd, check=True, capture_output=True, text=True)
    except sp.CalledProcessError as e:
        raise SystemError(
            f"Command returned non-zero exit status ({e.returncode}): {' '.join(e.cmd)}\n{e.stderr}\n{e.stdout}"
        )


def check_BE(
    test_info,
    afrom: np.ndarray,
    afrom_fs: int,
    afbin: np.ndarray,
    afbin_fs: int,
):
    if afrom is None or np.array_equal(afrom, np.zeros_like(afrom)):
        pytest.fail("Signal from ROM does not exist or is zero!")

    if afbin is None or np.array_equal(afbin, np.zeros_like(afbin)):
        pytest.fail("Signal from binary file does not exist or is zero!")

    snr, gain_b, max_diff = compare_audio_arrays(afrom, afrom_fs, afbin, afbin_fs)

    if np.isnan(snr) or gain_b == 0:
        pytest.fail("Invalid comparison result, check your signals!")

    if afrom.shape[0] < afbin.shape[0]:
        afrom = np.pad(afrom, [(0, afbin.shape[0] - afrom.shape[0]), (0, 0)])
    elif afrom.shape[0] > afbin.shape[0]:
        afbin = np.pad(afbin, [(0, afrom.shape[0] - afbin.shape[0]), (0, 0)])

    # check max_diff as well, since compare_audio_arrays will try to adjust for small delay differences
    if not np.allclose(afrom, afbin, rtol=0, atol=2) and max_diff > 2:
        pytest.fail(
            f"Signal from binary not BE to ROM! SNR : {snr:3.2f} dB, Gain : {gain_b:1.3f}, Max Diff = {int(max_diff)}"
        )


def run_encoder(
    bitrate: int,
+1 −1
Original line number Diff line number Diff line
@@ -408,7 +408,7 @@ class DecoderFrontend:
        if add_option_list is not None:
            command.extend(add_option_list)

        # add to fix test failed to be removed after merge             
        # TODO add to fix test failed to be removed after merge             
        if self._type == "REF":
            if '-hrtf' in command:
                command[command.index('-hrtf') + 1] = command[command.index('-hrtf') + 1] + "old" 
+0 −2
Original line number Diff line number Diff line
@@ -40,7 +40,6 @@ from typing import Dict, Optional
import numpy as np
import pytest


from .compare_audio import compare_audio_arrays
from .constants import *

@@ -195,7 +194,6 @@ def run_renderer(

    if non_diegetic_pan is not None:
        cmd.extend(["-non_diegetic_pan", str(non_diegetic_pan)])

    if refrot_file is not None:
        cmd.extend(["-rf", str(refrot_file)])
        cmd.extend(["-otr", "ref"])