Commit 509d6ab0 authored by Lauros Pajunen's avatar Lauros Pajunen
Browse files

Add tests for orientation output functions

parent 4629489b
Loading
Loading
Loading
Loading
Loading
+47 −0
Original line number Diff line number Diff line
@@ -559,6 +559,10 @@ def test_ambisonics_binaural_headrotation_refrotzero(
            "trj_file": HR_TRAJECTORY_DIR.joinpath(f"{trj_file}.csv"),
            "refrot_file": HR_TRAJECTORY_DIR.joinpath("const000.csv"),
            "frame_size": "5",
            "moof_file": OUTPUT_PATH_CUT.joinpath(f"moof_output_ambisonics_binaural_headrotation_refrotzero_{in_fmt}_{out_fmt}_{trj_file}.csv"),
            "trof_file": OUTPUT_PATH_CUT.joinpath(f"trof_output_ambisonics_binaural_headrotation_refrotzero_{in_fmt}_{out_fmt}_{trj_file}.csv"),
            "coof_file": OUTPUT_PATH_CUT.joinpath(f"coof_output_ambisonics_binaural_headrotation_refrotzero_{in_fmt}_{out_fmt}_{trj_file}.csv"),
            "reference_in_file": HR_TRAJECTORY_DIR.joinpath(f"{trj_file}.csv"),
        },
    )

@@ -591,6 +595,11 @@ def test_ambisonics_binaural_headrotation_refrotequal(
                "azi_plus_2-ele_plus_2-every-25-rows.csv"
            ),
            "frame_size": "5",
            "moof_file": OUTPUT_PATH_CUT.joinpath(f"moof_output_ambisonics_binaural_headrotation_refrotequal_{in_fmt}_{out_fmt}.csv"),
            "trof_file": OUTPUT_PATH_CUT.joinpath(f"trof_output_ambisonics_binaural_headrotation_refrotequal_{in_fmt}_{out_fmt}.csv"),
            "coof_file": OUTPUT_PATH_CUT.joinpath(f"coof_output_ambisonics_binaural_headrotation_refrotequal_{in_fmt}_{out_fmt}.csv"),
            "refrot_per_frame": True,
            "reference_in_file": HR_TRAJECTORY_DIR.joinpath("const000.csv"),
        },
    )

@@ -622,6 +631,10 @@ def test_ambisonics_binaural_headrotation_refveczero(
            "trj_file": HR_TRAJECTORY_DIR.joinpath(f"{trj_file}.csv"),
            "refvec_file": HR_TRAJECTORY_DIR.joinpath("const000-Vector3.csv"),
            "frame_size": "5",
            "moof_file": OUTPUT_PATH_CUT.joinpath(f"moof_output_ambisonics_binaural_headrotation_refveczero_{in_fmt}_{out_fmt}_{trj_file}.csv"),
            "trof_file": OUTPUT_PATH_CUT.joinpath(f"trof_output_ambisonics_binaural_headrotation_refveczero_{in_fmt}_{out_fmt}_{trj_file}.csv"),
            "coof_file": OUTPUT_PATH_CUT.joinpath(f"coof_output_ambisonics_binaural_headrotation_refveczero_{in_fmt}_{out_fmt}_{trj_file}.csv"),
            "reference_in_file": HR_TRAJECTORY_DIR.joinpath(f"{trj_file}.csv"),
        },
    )

@@ -659,6 +672,10 @@ def test_ambisonics_binaural_headrotation_refvecequal(
                    "full-circle-with-up-and-down-4s-Vector3.csv"
                ),
                "frame_size": "5",
                "moof_file": OUTPUT_PATH_CUT.joinpath(f"moof_output_ambisonics_binaural_headrotation_refvecequal_{in_fmt}_{out_fmt}.csv"),
                "trof_file": OUTPUT_PATH_CUT.joinpath(f"trof_output_ambisonics_binaural_headrotation_refvecequal_{in_fmt}_{out_fmt}.csv"),
                "coof_file": OUTPUT_PATH_CUT.joinpath(f"coof_output_ambisonics_binaural_headrotation_refvecequal_{in_fmt}_{out_fmt}.csv"),
                "reference_in_file": HR_TRAJECTORY_DIR.joinpath("const000.csv"),
            },
        )

@@ -697,6 +714,14 @@ def test_ambisonics_binaural_headrotation_refvec_rotating(
                    "full-circle-with-up-and-down-4s-ccw-Vector3.csv"
                ),
                "frame_size": "5",
                "moof_file": OUTPUT_PATH_CUT.joinpath(f"moof_output_ambisonics_binaural_headrotation_refvec_rotating_{in_fmt}_{out_fmt}.csv"),
                "trof_file": OUTPUT_PATH_CUT.joinpath(f"trof_output_ambisonics_binaural_headrotation_refvec_rotating_{in_fmt}_{out_fmt}.csv"),
                "coof_file": OUTPUT_PATH_CUT.joinpath(f"coof_output_ambisonics_binaural_headrotation_refvec_rotating_{in_fmt}_{out_fmt}.csv"),
                "reference_in_file": HR_TRAJECTORY_DIR.joinpath(
                    "full-circle-with-up-and-down-4s-ccw-Vector3.csv"
                ),
                "flip_trof_output": True,
                "flip_coof_output": True,
            },
        )

@@ -767,6 +792,12 @@ def test_ambisonics_binaural_headrotation_refveclev_vs_refvec(
            "trj_file": HR_TRAJECTORY_DIR.joinpath("const000.csv"),
            "refvec_file": HR_TRAJECTORY_DIR.joinpath("full-circle-4s-Vector3.csv"),
            "frame_size": "5",
            "moof_file": OUTPUT_PATH_CUT.joinpath(f"moof_output_ambisonics_binaural_headrotation_refveclev_vs_refvec_{in_fmt}_{out_fmt}.csv"),
            "trof_file": OUTPUT_PATH_CUT.joinpath(f"trof_output_ambisonics_binaural_headrotation_refveclev_vs_refvec_{in_fmt}_{out_fmt}.csv"),
            "coof_file": OUTPUT_PATH_CUT.joinpath(f"coof_output_ambisonics_binaural_headrotation_refveclev_vs_refvec_{in_fmt}_{out_fmt}.csv"),
            "reference_in_file": HR_TRAJECTORY_DIR.joinpath("full-circle-4s-Vector3.csv"),
            "flip_trof_output": True,
            "flip_coof_output": True,
        },
    )

@@ -804,6 +835,14 @@ def test_multichannel_binaural_headrotation_refvec_rotating(
                "full-circle-with-up-and-down-4s-ccw-Vector3.csv"
            ),
            "frame_size": "5",
            "moof_file": OUTPUT_PATH_CUT.joinpath(f"moof_output_multichannel_binaural_headrotation_refvec_rotating_{in_fmt}_{out_fmt}.csv"),
            "trof_file": OUTPUT_PATH_CUT.joinpath(f"trof_output_multichannel_binaural_headrotation_refvec_rotating_{in_fmt}_{out_fmt}.csv"),
            "coof_file": OUTPUT_PATH_CUT.joinpath(f"coof_output_multichannel_binaural_headrotation_refvec_rotating_{in_fmt}_{out_fmt}.csv"),
            "reference_in_file": HR_TRAJECTORY_DIR.joinpath(
                "full-circle-with-up-and-down-4s-ccw-Vector3.csv"
            ),
            "flip_trof_output": True,
            "flip_coof_output": True,
        },
    )

@@ -845,5 +884,13 @@ def test_ism_binaural_headrotation_refvec_rotating(
            ),
            "in_meta_files": in_meta_files,
            "frame_size": "5",
            "moof_file": OUTPUT_PATH_CUT.joinpath(f"moof_output_ism_binaural_headrotation_refvec_rotating_{in_fmt}_{out_fmt}.csv"),
            "trof_file": OUTPUT_PATH_CUT.joinpath(f"trof_output_ism_binaural_headrotation_refvec_rotating_{in_fmt}_{out_fmt}.csv"),
            "coof_file": OUTPUT_PATH_CUT.joinpath(f"coof_output_ism_binaural_headrotation_refvec_rotating_{in_fmt}_{out_fmt}.csv"),
            "reference_in_file": HR_TRAJECTORY_DIR.joinpath(
                "full-circle-with-up-and-down-4s-ccw-Vector3.csv"
            ),
            "flip_trof_output": True,
            "flip_coof_output": True,
        },
    )
+127 −0
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ from typing import Dict, Optional

import numpy as np
import pytest
import csv

from .compare_audio import compare_audio_arrays
from .constants import *
@@ -115,6 +116,13 @@ def run_renderer(
    binary_suffix: str = "",
    frame_size: Optional[str] = "20ms",
    hrtf_file: Optional[str] = None,
    moof_file: Optional[str] = None,
    trof_file:  Optional[str] = None,
    coof_file:  Optional[str] = None,
    reference_in_file=None,
    flip_trof_output=None,
    flip_coof_output=None,
    refrot_per_frame=None,
    get_mld=False,
    mld_lim=0,
) -> str:
@@ -223,6 +231,15 @@ def run_renderer(
    if frame_size:
        cmd.extend(["-fr", str(frame_size.replace("ms", ""))])

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

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

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

    # Set env variables for UBSAN
    env = os.environ.copy()
    if test_info.node.name and "UBSAN_OPTIONS" in env.keys():
@@ -294,3 +311,113 @@ def compare_renderer_args(
    )
    cut, cut_fs = readfile(out_file_cut)
    check_BE(test_info, ref, ref_fs, cut, cut_fs)
    compare_orientations(
        reference_in_file=cut_kwargs["reference_in_file"] if "reference_in_file" in cut_kwargs else None,
        refrot_file=cut_kwargs["refrot_file"] if "refrot_file" in cut_kwargs else None,
        refvec_file=cut_kwargs["refvec_file"] if "refvec_file" in cut_kwargs else None,
        flip_trof_output=cut_kwargs["flip_trof_output"] if "flip_trof_output" in cut_kwargs else False,
        flip_coof_output=cut_kwargs["flip_coof_output"] if "flip_coof_output" in cut_kwargs else False,
        refrot_per_frame=cut_kwargs["refrot_per_frame"] if "refrot_per_frame" in cut_kwargs else False,
        moof_file=cut_kwargs["moof_file"] if "moof_file" in cut_kwargs else None,
        trof_file=cut_kwargs["trof_file"] if "trof_file" in cut_kwargs else None,
        coof_file=cut_kwargs["coof_file"] if "coof_file" in cut_kwargs else None, )


def compare_orientations(
    reference_in_file: Optional[str] = None,
    refrot_file: Optional[str] = None,
    refvec_file: Optional[str] = None,
    flip_trof_output: Optional = False,
    flip_coof_output: Optional = False,
    refrot_per_frame: Optional = False,
    moof_file: Optional[str] = None,
    trof_file:  Optional[str] = None,
    coof_file:  Optional[str] = None,
):
    error_margin = 0.001

    if reference_in_file is not None:
        reference_in_file = Path(str(reference_in_file).replace("-Vector3", ""))
    if refrot_file is not None:
        refrot_file = Path(str(refrot_file).replace("-Vector3", ""))
    if refvec_file is not None:
        refvec_file = Path(str(refvec_file).replace("-Vector3", ""))

    if moof_file is not None and refrot_file is not None:
        compare_orientation_files(moof_file, refrot_file, refrot_per_frame=refrot_per_frame)
    if moof_file is not None and refvec_file is not None:
        compare_orientation_files(moof_file, refvec_file, refrot_per_frame=refrot_per_frame)

    if trof_file is not None and reference_in_file is not None:
        output_multipliers = [1.0, 1.0, 1.0, 1.0]
        if flip_trof_output:
            output_multipliers = [1.0, 1.0, -1.0, -1.0]
        compare_orientation_files(trof_file, reference_in_file, output_multipliers)

    if coof_file is not None and reference_in_file is not None:
        output_multipliers = [1.0, 1.0, 1.0, 1.0]
        if flip_coof_output:
            output_multipliers = [1.0, 1.0, -1.0, -1.0]
        skip_first_output_row = True
        compare_orientation_files(coof_file, reference_in_file, output_multipliers, skip_first_output_row)


def compare_orientation_files(
    output_file_path,
    input_file_path,
    output_multipliers: Optional = None,
    skip_first_output_row: Optional = False,
    refrot_per_frame: Optional = False,
):
    if output_multipliers is None:
        output_multipliers = [1.0, 1.0, 1.0, 1.0]
    error_margin = 0.001

    loop_input_file = False
    if "const" in str(input_file_path):
        loop_input_file = True

    with open(output_file_path, 'r') as output_file:
        with open(input_file_path, 'r') as input_file:
            out_reader = csv.reader(output_file)
            in_reader = csv.reader(input_file)
            row_count_out = sum(1 for row in out_reader)
            row_count_in = sum(1 for row in in_reader)
            if loop_input_file:
                row_count = max(row_count_out, row_count_in)
            else:
                row_count = min(row_count_out, row_count_in)
            output_file.seek(0)
            input_file.seek(0)
            in_row_counter = 0
            if skip_first_output_row:
                next(out_reader)
                row_count -= 1
            for i in range(0, row_count):
                out_row = next(out_reader)
                if refrot_per_frame:
                    if i % 4 == 0:
                        in_row = next(in_reader)
                else:
                    in_row = next(in_reader)
                w_out = float(out_row[0]) * output_multipliers[0]
                x_out = float(out_row[1]) * output_multipliers[1]
                y_out = float(out_row[2]) * output_multipliers[2]
                z_out = float(out_row[3]) * output_multipliers[3]
                w_in = float(in_row[0])
                x_in = float(in_row[1])
                y_in = float(in_row[2])
                z_in = float(in_row[3])
                if abs(w_out - w_in) > error_margin:
                    pytest.fail(f"Rotation output differs for files {output_file_path} and {input_file_path}")
                if abs(x_out - x_in) > error_margin:
                    pytest.fail(f"Rotation output differs for files {output_file_path} and {input_file_path}")
                if abs(y_out - y_in) > error_margin:
                    pytest.fail(f"Rotation output differs for files {output_file_path} and {input_file_path}")
                if abs(z_out - z_in) > error_margin:
                    pytest.fail(f"Rotation output differs for files {output_file_path} and {input_file_path}")
                if loop_input_file:
                    in_row_counter += 1
                    if in_row_counter == row_count_in:
                        input_file.seek(0)
                        in_row_counter = 0