Commit 8e7f4429 authored by lefort's avatar lefort
Browse files

Pytest for binaural rendering (binary vs rom tables) added.

parent ab79e0e3
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
!.gitignore
+105 −0
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
+64 −0
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.
"""

from pathlib import Path


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", "bin")
DEC_BINARY_DIR = TESTS_DIR.joinpath("dec_out_bin")


ENCODER_CMD = [
    str(TESTS_DIR.parent.parent.joinpath("IVAS_cod"))
]

DECODER_CMD = [
    str(TESTS_DIR.parent.parent.joinpath("IVAS_dec"))
]

HRTF_BINARY_FILE = "default_rom_{}kHz.bin"
SAMPLE_RATE = ["16", "32", "48"]
FORMATS_MC = ["5_1", "5_1_2", "5_1_4", "7_1", "7_1_4"]
OUTPUT_FORMATS_BINAURAL = ["BINAURAL", "BINAURAL_ROOM_IR"] # "BINAURAL_ROOM_REVERB"

FORMAT_TO_FILE_WOEXT = {
    "5_1": "stv51MC{}c",
    "7_1": "stv71MC{}c",
    "5_1_2": "stv512MC{}c",
    "5_1_4": "stv514MC{}c",
    "7_1_4": "stv714MC{}c",
}
 No newline at end of file
+1 −0
Original line number Diff line number Diff line
!.gitignore
+1 −0
Original line number Diff line number Diff line
!.gitignore
Loading