Commit 399fabe9 authored by norvell's avatar norvell
Browse files

Refactoring/cleanup and add support for test_sba.py

parent e2e47c5a
Loading
Loading
Loading
Loading
+10 −111
Original line number Diff line number Diff line
@@ -118,14 +118,6 @@ INPUT_FMT = [
    (r".*", "MONO"),
]

PEAQ_SUPPORTED_FMT = [
    "MONO",
    "STEREO",
    "BINAURAL",
    "BINAURAL_ROOM_IR",
    "BINAURAL_ROOM_REVERB",    
]

PARAM_FILE_ID = (
    "stv"
    if PARAM_FILE.stem == "self_test"
@@ -448,9 +440,6 @@ def test_param_file_tests(
    # Check if we need to render to stereo or binaural for PEAQ comparison
    if peaq_binaural:

        dut_output_file_binaural = dut_output_file[0:-4]+".BINAURAL.wav"
        ref_output_file_binaural = ref_output_file[0:-4]+".BINAURAL.wav"

        # Disable tracefile supported comparison -- no tracefile available for input
        tracefile_dec = ""

@@ -462,111 +451,21 @@ def test_param_file_tests(
        ][0][1]

        record_property = dut_decoder_frontend.record_property if dut_decoder_frontend else ref_decoder_frontend.record_property
        in_meta_files = [str(SCRIPTS_DIR.joinpath(m)) for m in re.findall(r'\b\S+\.csv\b', enc_opts)] # All .csv files in enc_opts are ISM metadata files.
        in_meta_files = in_meta_files + [str(SCRIPTS_DIR.joinpath(m)) for m in re.findall(r'\b\S+\.met\b', enc_opts)] # All .met files in enc_opts are MASA metadata files.
        n_obj = len(in_meta_files)
        out_meta_files = None
        if output_config == "EXT":
            if "ISM" in in_fmt and n_obj > 0:
                out_meta_files = [f"{dut_output_file}.{i}.csv" for i in range(0,n_obj)]
            if "MASA" in in_fmt:
                out_meta_files = [f"{dut_output_file}.met"]
        if output_config == "EXT":
            output_config = in_fmt
        if output_config == "":
            output_config = "MONO" # EVS mono
        metadata_input = None # Todo: This can be used for OMASA/OSBA rendering
        trj_file = findstr(r'-t\s+(\S+)', dec_opts)
        non_diegetic_pan = findstr(r'-non_diegetic_pan\s+(\S+)', dec_opts)
        if non_diegetic_pan is not None:
            output_config = "STEREO"
        name_extension = None
        refrot_file = findstr(r'-rf\s+(\S+)', dec_opts)
        rot_tmp_file = findstr(r'-rvf\s+(\S+)', dec_opts)
        refveclev_file = None
        refvec_file = None
        if "-otr ref_vec_lev".upper() in dec_opts.upper():  
            refveclev_file = rot_tmp_file
        else:
            if "-otr ref_vec".upper() in dec_opts.upper():
                refvec_file = rot_tmp_file
        config_file = findstr(r'-render_config\s+(\S+)', dec_opts)
        binary_suffix = "_ref"
        frame_size = findstr(r'-fr\s+(\S+)', dec_opts)
        # hrtf_file = findstr(r'-hrtf\s+(\S+)', dec_opts)
        hrtf_file = None # Default HRTFs used for binaural rendering of output

        aeid = findstr(r'-aeid\s+(\S+)', dec_opts)


        if not output_config.upper() in PEAQ_SUPPORTED_FMT:
            # Render output to BINAURAL
            output_reformat = "BINAURAL"

            check_and_makedir(str(Path(dut_output_file_binaural).parent))

            rend.run_renderer(
                record_property,
                props_to_record,
                test_info,
                output_config,
                output_reformat,
                metadata_input,
                out_meta_files,
                trj_file,
                non_diegetic_pan,
                name_extension,
                refrot_file,
                refvec_file,
                refveclev_file,
                config_file,
                binary_suffix,
                frame_size,
                hrtf_file,
                aeid,
                in_file=dut_output_file,
                out_file=dut_output_file_binaural,
                sr=out_sr,
            )
            # Update output_config to rendered format
            output_config = output_reformat
            # Use binauralized file for comparison
            dut_output_file = dut_output_file_binaural

        if in_fmt.upper() != output_config.upper():
            # Render input to match output_config
            out_fmt = output_config
            
            check_and_makedir(str(Path(ref_output_file_binaural).parent))

            rend.run_renderer(
        (ref_output_file, dut_output_file) = rend.binauralize_input_and_output(
                record_property,
                props_to_record,
                test_info,
                testv_file,
                dut_output_file,
                ref_output_file,
                in_fmt,
                out_fmt,
                metadata_input,
                in_meta_files,
                trj_file,
                non_diegetic_pan,
                name_extension,
                refrot_file,
                refvec_file,
                refveclev_file,
                config_file,
                binary_suffix,
                frame_size,
                hrtf_file,
                aeid,
                in_file=testv_file,
                out_file=ref_output_file_binaural,
                sr=in_sr,
                output_config,
                enc_opts,
                dec_opts,
                in_sr,
                out_sr,
        )
            # Use binauralized file for comparison
            ref_output_file = ref_output_file_binaural
        else:
            ref_output_file = testv_file


    if update_ref in [0, 2]:

+75 −1
Original line number Diff line number Diff line
@@ -44,7 +44,7 @@ from ..conftest import parse_properties
from ..cmp_stats_files import cmp_stats_files
from ..constants import TESTV_DIR, MIN_ENC_FILE_LENGTH_DIFF, MIN_ENC_STATS_DIFF
from tests.testconfig import use_ltv

import tests.renderer.utils as rend

tag_list = ["ltvFOA" if use_ltv else "stvFOA"]
tag_list_HOA2 = ["ltvHOA2" if use_ltv else "stv2OA"]
@@ -79,6 +79,8 @@ tag_list_plc = tag_list
plc_patterns = ["PLperc12mblen5", "PLperc40mblen50", "PLperc42mblen2"]
ivas_br_plc = ["13200", "16400", "32000", "64000", "96000", "256000"]

# SBA order to IVAS_rend format table
SBA_FORMAT={1: "FOA", 2: "HOA2", 3: "HOA3"}

def check_and_makedir(dir_path):
    if not os.path.exists(dir_path):
@@ -92,6 +94,7 @@ def check_and_makedir(dir_path):
@pytest.mark.parametrize("tag", tag_list)
@pytest.mark.parametrize("sampling_rate", sample_rate_list)
def test_pca_enc(
    test_info,
    props_to_record,
    dut_encoder_frontend: EncoderFrontend,
    dut_decoder_frontend: DecoderFrontend,
@@ -137,6 +140,8 @@ def test_pca_enc(
    else:
        assert 0

    input_config = SBA_FORMAT(abs(int(sba_order)))

    if not decoder_only:
        sba_enc(
            dut_encoder_frontend,
@@ -164,6 +169,7 @@ def test_pca_enc(

    if not encoder_only:
        sba_dec(
            test_info,
            props_to_record,
            test_vector_path,
            dut_decoder_frontend,
@@ -176,6 +182,7 @@ def test_pca_enc(
            dtx,
            sid,
            max_bw,
            input_config,
            output_config,
            update_ref,
            gain_flag,
@@ -189,6 +196,7 @@ def test_pca_enc(
            get_ssnr=get_ssnr,
            get_odg=get_odg,
            compare_to_input=compare_to_input,
            peaq_binaural=peaq_binaural,
        )


@@ -199,6 +207,7 @@ def test_pca_enc(
@pytest.mark.parametrize("gain_flag", gain_list)
@pytest.mark.parametrize("sid", SID_list)
def test_sba_enc_system(
    test_info,
    props_to_record,
    dut_encoder_frontend: EncoderFrontend,
    dut_decoder_frontend: DecoderFrontend,
@@ -272,6 +281,8 @@ def test_sba_enc_system(
    else:
        cut_gain = "1.0"

    input_config = SBA_FORMAT(abs(int(sba_order)))

    if not decoder_only:
        ref_stats_file, dut_stats_file = sba_enc(
            dut_encoder_frontend,
@@ -334,6 +345,7 @@ def test_sba_enc_system(

    if not encoder_only:
        sba_dec(
            test_info,
            props_to_record,
            test_vector_path,
            dut_decoder_frontend,
@@ -346,6 +358,7 @@ def test_sba_enc_system(
            dtx,
            sid,
            max_bw,
            input_config,
            output_config,
            update_ref,
            gain_flag,
@@ -359,12 +372,14 @@ def test_sba_enc_system(
            get_ssnr=get_ssnr,
            get_odg=get_odg,
            compare_to_input=compare_to_input,
            peaq_binaural=peaq_binaural,
        )


@pytest.mark.parametrize("bitrate", ivas_br_HOA2)
@pytest.mark.parametrize("tag", tag_list_HOA2)
def test_spar_hoa2_enc_system(
    test_info,
    props_to_record,
    dut_encoder_frontend: EncoderFrontend,
    dut_decoder_frontend: DecoderFrontend,
@@ -408,6 +423,8 @@ def test_spar_hoa2_enc_system(
    else:
        assert 0

    input_config = SBA_FORMAT(abs(int(sba_order)))

    if not decoder_only:
        ref_stats_file, dut_stats_file = sba_enc(
            dut_encoder_frontend,
@@ -470,6 +487,7 @@ def test_spar_hoa2_enc_system(

    if not encoder_only:
        sba_dec(
            test_info,
            props_to_record,
            test_vector_path,
            dut_decoder_frontend,
@@ -482,6 +500,7 @@ def test_spar_hoa2_enc_system(
            dtx,
            sid,
            max_bw,
            input_config,
            output_config,
            update_ref,
            gain_flag,
@@ -495,12 +514,14 @@ def test_spar_hoa2_enc_system(
            get_ssnr=get_ssnr,
            get_odg=get_odg,
            compare_to_input=compare_to_input,
            peaq_binaural=peaq_binaural,
        )


@pytest.mark.parametrize("bitrate", ivas_br_HOA3)
@pytest.mark.parametrize("tag", tag_list_HOA3)
def test_spar_hoa3_enc_system(
    test_info,
    props_to_record,
    dut_encoder_frontend: EncoderFrontend,
    dut_decoder_frontend: DecoderFrontend,
@@ -544,6 +565,8 @@ def test_spar_hoa3_enc_system(
    else:
        assert 0

    input_config = SBA_FORMAT(abs(int(sba_order)))

    if not decoder_only:
        ref_stats_file, dut_stats_file = sba_enc(
            dut_encoder_frontend,
@@ -600,6 +623,7 @@ def test_spar_hoa3_enc_system(

    if not encoder_only:
        sba_dec(
            test_info,
            props_to_record,
            test_vector_path,
            dut_decoder_frontend,
@@ -612,6 +636,7 @@ def test_spar_hoa3_enc_system(
            dtx,
            sid,
            max_bw,
            input_config,
            output_config,
            update_ref,
            gain_flag,
@@ -625,6 +650,7 @@ def test_spar_hoa3_enc_system(
            get_ssnr=get_ssnr,
            get_odg=get_odg,
            compare_to_input=compare_to_input,
            peaq_binaural=peaq_binaural,
        )


@@ -633,6 +659,7 @@ def test_spar_hoa3_enc_system(
@pytest.mark.parametrize("tag", tag_list_bw_force)
@pytest.mark.parametrize("sample_rate_bw_idx", sample_rate_bw_idx_list)
def test_sba_enc_BWforce_system(
    test_info,
    props_to_record,
    dut_encoder_frontend: EncoderFrontend,
    dut_decoder_frontend: DecoderFrontend,
@@ -688,6 +715,8 @@ def test_sba_enc_BWforce_system(
    else:
        assert 0

    input_config = SBA_FORMAT(abs(int(sba_order)))

    if not decoder_only:
        ref_stats_file, dut_stats_file = sba_enc(
            dut_encoder_frontend,
@@ -750,6 +779,7 @@ def test_sba_enc_BWforce_system(

    if not encoder_only:
        sba_dec(
            test_info,
            props_to_record,
            test_vector_path,
            dut_decoder_frontend,
@@ -762,6 +792,7 @@ def test_sba_enc_BWforce_system(
            dtx,
            sid,
            max_bw,
            input_config,
            output_config,
            update_ref,
            gain_flag,
@@ -775,6 +806,7 @@ def test_sba_enc_BWforce_system(
            get_ssnr=get_ssnr,
            get_odg=get_odg,
            compare_to_input=compare_to_input,
            peaq_binaural=peaq_binaural,
        )


@@ -785,6 +817,7 @@ def test_sba_enc_BWforce_system(
@pytest.mark.parametrize("sampling_rate", sample_rate_list)
@pytest.mark.parametrize("gain_flag", gain_list)
def test_sba_plc_system(
    test_info,
    props_to_record,
    dut_encoder_frontend: EncoderFrontend,
    dut_decoder_frontend: DecoderFrontend,
@@ -854,6 +887,8 @@ def test_sba_plc_system(
    else:
        cut_gain = "1.0"

    input_config = SBA_FORMAT(abs(int(sba_order)))

    if not decoder_only:
        sba_enc(
            dut_encoder_frontend,
@@ -881,6 +916,7 @@ def test_sba_plc_system(

    if not encoder_only:
        sba_dec(
            test_info,
            props_to_record,
            test_vector_path,
            dut_decoder_frontend,
@@ -893,6 +929,7 @@ def test_sba_plc_system(
            dtx,
            sid,
            max_bw,
            input_config,
            output_config,
            update_ref,
            gain_flag,
@@ -906,6 +943,7 @@ def test_sba_plc_system(
            get_ssnr=get_ssnr,
            get_odg=get_odg,
            compare_to_input=compare_to_input,
            peaq_binaural=peaq_binaural,
        )


@@ -1036,6 +1074,7 @@ def sba_enc(


def sba_dec(
    test_info,
    props_to_record,
    test_vector_path,
    dut_decoder_frontend,
@@ -1048,6 +1087,7 @@ def sba_dec(
    dtx,
    sid,
    max_bw,
    input_config,
    output_config,
    update_ref,
    gain_flag,
@@ -1061,6 +1101,7 @@ def sba_dec(
    get_ssnr=False,
    get_odg=False,
    compare_to_input=False,
    peaq_binaural=False,
):
    dut_pkt_dir = f"{dut_base_path}/sba_bs/pkt"
    ref_pkt_dir = f"{reference_path}/sba_bs/pkt"
@@ -1132,8 +1173,41 @@ def sba_dec(
        if compare_to_input:
            # If comparing to input, set input as reference and allow different length
            ref_out_file = f"{test_vector_path}/{tag}.wav"
            if gain_flag != -1:
                ref_out_file = f"{test_vector_path}/{tag}_Gain{gain_flag}.wav"
            else:
                ref_out_file = f"{test_vector_path}/{tag}.wav"

            allow_differing_lengths = True


        if peaq_binaural:

            enc_opts = ""
            dec_opts = ""
            if gain_flag != -1:
                input_file = f"{test_vector_path}/{tag}_Gain{gain_flag}.wav"
            else:
                input_file = f"{test_vector_path}/{tag}.wav"
            in_fmt = input_config
            in_sr = sampling_rate
            out_sr = sampling_rate

            (ref_out_file, dut_out_file) = rend.binauralize_input_and_output(
                dut_decoder_frontend.record_property,
                props_to_record,
                test_info,
                input_file,
                dut_out_file,
                ref_out_file,
                in_fmt,
                output_config,
                enc_opts,
                dec_opts,
                in_sr,
                out_sr,
            )

        sampling_rate_Hz = int(sampling_rate) * 1000
        cmp_result, reason = cmp_pcm(
            dut_out_file,
+8 −0
Original line number Diff line number Diff line
@@ -340,3 +340,11 @@ HR_TRAJECTORIES_TO_TEST = [

""" Frame Size """
FRAMING_TO_TEST = ["5ms", "20ms"]

PEAQ_SUPPORTED_FMT = [
    "MONO",
    "STEREO",
    "BINAURAL",
    "BINAURAL_ROOM_IR",
    "BINAURAL_ROOM_REVERB",
]
 No newline at end of file
+146 −0
Original line number Diff line number Diff line
@@ -40,6 +40,8 @@ from typing import Dict, Optional, Union

import numpy as np
import pytest
import re
import errno

from .compare_audio import compare_audio_arrays
from .constants import (
@@ -52,6 +54,7 @@ from .constants import (
    FORMAT_TO_FILE_SMOKETEST,
    RENDERER_CMD,
    BIN_SUFFIX_MERGETARGET,
    PEAQ_SUPPORTED_FMT,
)

sys.path.append(SCRIPTS_DIR)
@@ -373,3 +376,146 @@ def compare_renderer_args(
        pytest.fail(
            f"CuT not BE to REF! SNR : {snr:3.2f} dB, Gain CuT: {gain_b:1.3f}, Max Diff = {int(max_diff)}"
        )

def binauralize_input_and_output(
    record_property,
    props_to_record,
    test_info,
    input_file,
    dut_output_file,
    ref_output_file,
    in_fmt,
    output_config,
    enc_opts,
    dec_opts,
    in_sr,
    out_sr,
):

    # File names for binauralized input, if needed
    dut_output_file_binaural = dut_output_file[0:-4]+".BINAURAL.wav"
    ref_output_file_binaural = ref_output_file[0:-4]+".BINAURAL.wav"

    # Identify metadata
    in_meta_files = [str(SCRIPTS_DIR.joinpath(m)) for m in re.findall(r'\b\S+\.csv\b', enc_opts)] # All .csv files in enc_opts are ISM metadata files.
    in_meta_files = in_meta_files + [str(SCRIPTS_DIR.joinpath(m)) for m in re.findall(r'\b\S+\.met\b', enc_opts)] # All .met files in enc_opts are MASA metadata files.
    n_obj = len(in_meta_files)
    out_meta_files = None
    if output_config == "EXT":
        if "ISM" in in_fmt and n_obj > 0:
            out_meta_files = [f"{dut_output_file}.{i}.csv" for i in range(0,n_obj)]
        if "MASA" in in_fmt:
            out_meta_files = [f"{dut_output_file}.met"]
    if output_config == "EXT":
        output_config = in_fmt
    if output_config == "":
        output_config = "MONO" # EVS mono
    metadata_input = None # Todo: This can be used for OMASA/OSBA rendering

    # Identify headtracking and orientation trajectories
    trj_file = findstr(r'-t\s+(\S+)', dec_opts)
    non_diegetic_pan = findstr(r'-non_diegetic_pan\s+(\S+)', dec_opts)
    if non_diegetic_pan is not None:
        output_config = "STEREO"
    name_extension = None
    refrot_file = findstr(r'-rf\s+(\S+)', dec_opts)
    rot_tmp_file = findstr(r'-rvf\s+(\S+)', dec_opts)
    refveclev_file = None
    refvec_file = None
    if "-otr ref_vec_lev".upper() in dec_opts.upper():
        refveclev_file = rot_tmp_file
    else:
        if "-otr ref_vec".upper() in dec_opts.upper():
            refvec_file = rot_tmp_file

    # Rendering configuration
    config_file = findstr(r'-render_config\s+(\S+)', dec_opts)
    binary_suffix = "_ref"
    frame_size = findstr(r'-fr\s+(\S+)', dec_opts)
    # hrtf_file = findstr(r'-hrtf\s+(\S+)', dec_opts)
    hrtf_file = None # Default HRTFs used for binaural rendering of output

    aeid = findstr(r'-aeid\s+(\S+)', dec_opts)

    if not output_config.upper() in PEAQ_SUPPORTED_FMT:
        # Render output to BINAURAL
        output_reformat = "BINAURAL"

        check_and_makedir(str(Path(dut_output_file_binaural).parent))

        run_renderer(
            record_property,
            props_to_record,
            test_info,
            output_config,
            output_reformat,
            metadata_input,
            out_meta_files,
            trj_file,
            non_diegetic_pan,
            name_extension,
            refrot_file,
            refvec_file,
            refveclev_file,
            config_file,
            binary_suffix,
            frame_size,
            hrtf_file,
            aeid,
            in_file=dut_output_file,
            out_file=dut_output_file_binaural,
            sr=out_sr,
        )
        # Update output_config to rendered format
        output_config = output_reformat
    else:
        dut_output_file_binaural = dut_output_file

    if in_fmt.upper() != output_config.upper():
        # Render input to match output_config
        out_fmt = output_config

        check_and_makedir(str(Path(ref_output_file_binaural).parent))

        run_renderer(
            record_property,
            props_to_record,
            test_info,
            in_fmt,
            out_fmt,
            metadata_input,
            in_meta_files,
            trj_file,
            non_diegetic_pan,
            name_extension,
            refrot_file,
            refvec_file,
            refveclev_file,
            config_file,
            binary_suffix,
            frame_size,
            hrtf_file,
            aeid,
            in_file=input_file,
            out_file=ref_output_file_binaural,
            sr=in_sr,
        )
    else:
        ref_output_file_binaural = ref_output_file
    return (ref_output_file_binaural, dut_output_file_binaural)

def findstr(exp, s, one_element=True):
    result = [SCRIPTS_DIR.joinpath(x) for x in re.findall(exp, s)]
    if len(result) == 0:
        return None
    if one_element:
        return result[0]
    return result

def check_and_makedir(dir_path):
    if not os.path.exists(dir_path):
        try:
            os.makedirs(dir_path)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise  # raises the error again