Commit 67389472 authored by lefort's avatar lefort
Browse files

Code cleaned and MASA added.

parent c3926bb7
Loading
Loading
Loading
Loading
+31 −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.
"""
+22 −13
Original line number Diff line number Diff line
@@ -52,23 +52,32 @@ DECODER_CMD = [
HRTF_BINARY_FILE = "default_rom_{}kHz.bin"
SAMPLE_RATE = ["16", "32", "48"]

INPUT_FORMATS_MC = ["MONO", "STEREO", "5_1", "5_1_2", "5_1_4", "7_1", "7_1_4"]
INPUT_FORMATS_SBA = {
    "FOA":"1", 
    "HOA2":"2", 
    "HOA3":"3"
INPUT_FORMATS_MC = ["5_1", "5_1_2", "5_1_4", "7_1", "7_1_4"] # "MONO", "STEREO", 
INPUT_FORMATS_SBA = ["1", "2", "3"]
INPUT_FORMATS_ISM = ["1", "2", "3", "4"]
INPUT_FORMATS_MASA = {
    "tc" : ["1", "2"],
    "dir" : ["1", "2"]
}

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

FORMAT_TO_FILE_WOEXT = {
FORMAT_TO_FILE_MC_WOEXT = {
    "5_1": "stv51MC{}c",
    "7_1": "stv71MC{}c",
    "5_1_2": "stv512MC{}c",
    "5_1_4": "stv514MC{}c",
    "7_1_4": "stv714MC{}c",
    "FOA": "stvFOA{}c",
    "HOA2": "stv2OA{}c",
    "HOA3": "stv3OA{}c",
}
FORMAT_TO_FILE_SBA_WOEXT = {
    "1": "stvFOA{}c",
    "2": "stv2OA{}c",
    "3": "stv3OA{}c",
}
FORMAT_TO_FILE_ISM_WOEXT = "stv{}ISM{}s"
FORMAT_TO_FILE_MASA_WOEXT = "stv{}MASA{}TC{}c"

FORMAT_TO_METADATA_FILES = {
    "MASA": "stv{}MASA{}TC{}c.met",
}

OUTPUT_FORMATS_BINAURAL = ["BINAURAL", "BINAURAL_ROOM_IR"] # "BINAURAL_ROOM_REVERB"
HR_TRAJECTORIES_TO_TEST = ["headrot_case00_3000_q", "headrot"]
 No newline at end of file
+113 −17
Original line number Diff line number Diff line
@@ -40,28 +40,50 @@ from tests.binaural.utils import *
@pytest.mark.parametrize("in_fmt", INPUT_FORMATS_MC)
def test_multichannel_binaural_static(test_info, in_fmt, out_fmt, out_fs):

    if in_fmt in ["MONO", "STEREO"]:
        pytest.skip("MONO or STEREO to Binaural rendering unsupported")
    #if in_fmt in ["MONO", "STEREO"]:
    #    pytest.skip("MONO or STEREO to Binaural rendering unsupported")

    compare_multichannel_rom_vs_binary(
    # -mc InputConf : Multi-channel format
    #                 where InputConf specifies the channel configuration (5_1, 7_1, 5_1_2, 5_1_4, 7_1_4)

    bitrate = 512000
    in_fs = 48
    option_list = ["-mc", in_fmt]
    in_file = FORMAT_TO_FILE_MC_WOEXT[in_fmt].format(in_fs)

    compare_rom_vs_binary(
        test_info, 
        in_fmt,
        option_list,
        bitrate,
        in_file,
        in_fs,
        out_fmt, 
        out_fs
        out_fs,
    )

@pytest.mark.parametrize("trj_file", HR_TRAJECTORIES_TO_TEST)
@pytest.mark.parametrize("trj_file", [HR_TRAJECTORIES_TO_TEST[0]])
@pytest.mark.parametrize("out_fmt", OUTPUT_FORMATS_BINAURAL)
@pytest.mark.parametrize("out_fs", SAMPLE_RATE)
@pytest.mark.parametrize("in_fmt", INPUT_FORMATS_MC)
def test_multichannel_binaural_headrotation(test_info, in_fmt, out_fmt, out_fs, trj_file):

    if in_fmt in ["MONO", "STEREO"]:
        pytest.skip("MONO or STEREO to Binaural rendering unsupported")
    #if in_fmt in ["MONO", "STEREO"]:
    #    pytest.skip("MONO or STEREO to Binaural rendering unsupported")

    # -mc InputConf : Multi-channel format
    #                 where InputConf specifies the channel configuration (5_1, 7_1, 5_1_2, 5_1_4, 7_1_4)

    compare_multichannel_rom_vs_binary(
    bitrate = 512000
    in_fs = 48
    option_list = ["-mc", in_fmt]
    in_file = FORMAT_TO_FILE_MC_WOEXT[in_fmt].format(in_fs)

    compare_rom_vs_binary(
        test_info, 
        in_fmt,
        option_list,
        bitrate,
        in_file,
        in_fs,
        out_fmt, 
        out_fs,
        trj_file=str(TESTV_DIR.joinpath(f"{trj_file}.csv"))
@@ -74,23 +96,97 @@ def test_multichannel_binaural_headrotation(test_info, in_fmt, out_fmt, out_fs,
@pytest.mark.parametrize("in_fmt", INPUT_FORMATS_SBA)
def test_sba_binaural_static(test_info, in_fmt, fs, out_fmt):

    compare_ambisonics_rom_vs_binary(
    # -sba +/-Order : Scene Based Audio input format (Ambisonics ACN/SN3D)
    #                 where Order specifies the Ambisionics order (1-3)

    bitrate = 256000
    option_list = ["sba", in_fmt]
    in_file = FORMAT_TO_FILE_SBA_WOEXT[in_fmt].format(fs)

    compare_rom_vs_binary(
        test_info, 
        in_fmt, 
        option_list,
        bitrate,
        in_file,
        fs,
        out_fmt, 
        fs,
    )

@pytest.mark.parametrize("trj_file", HR_TRAJECTORIES_TO_TEST)
@pytest.mark.parametrize("trj_file", [HR_TRAJECTORIES_TO_TEST[0]])
@pytest.mark.parametrize("out_fmt", OUTPUT_FORMATS_BINAURAL)
@pytest.mark.parametrize("fs", SAMPLE_RATE[1:])
@pytest.mark.parametrize("in_fmt", INPUT_FORMATS_SBA)
def test_sba_binaural_headrotation(test_info, in_fmt, fs, out_fmt, trj_file):

    compare_ambisonics_rom_vs_binary(
    # -sba +/-Order : Scene Based Audio input format (Ambisonics ACN/SN3D)
    #                 where Order specifies the Ambisionics order (1-3)

    bitrate = 256000
    option_list = ["-sba", in_fmt]
    in_file = FORMAT_TO_FILE_SBA_WOEXT[in_fmt].format(fs)

    compare_rom_vs_binary(
        test_info, 
        option_list,
        bitrate,
        in_file,
        fs,
        out_fmt, 
        fs,
        trj_file=str(TESTV_DIR.joinpath(f"{trj_file}.csv"))
    )

""" MASA """

@pytest.mark.parametrize("out_fmt", OUTPUT_FORMATS_BINAURAL)
@pytest.mark.parametrize("fs", SAMPLE_RATE[-1:])
@pytest.mark.parametrize("in_dir", INPUT_FORMATS_MASA["dir"])
@pytest.mark.parametrize("in_tc", INPUT_FORMATS_MASA["tc"])
def test_masa_binaural_static(test_info, in_tc, in_dir, fs, out_fmt):

    # -masa Ch File : MASA format
    #                 where Ch specifies the number of MASA input/transport channels (1 or 2)
    #                 and File specifies input file containing parametric MASA metadata

    bitrate = 256000
    test = FORMAT_TO_METADATA_FILES["MASA"].format(in_dir, in_tc, fs)
    metadata_file=str(TESTV_DIR.joinpath(test))
    option_list = ["-masa", in_tc, metadata_file]
    in_file = FORMAT_TO_FILE_MASA_WOEXT.format(in_dir, in_tc, fs)

    compare_rom_vs_binary(
        test_info, 
        option_list,
        bitrate,
        in_file,
        fs,
        out_fmt, 
        fs,
    )

@pytest.mark.parametrize("trj_file", [HR_TRAJECTORIES_TO_TEST[1]])
@pytest.mark.parametrize("out_fmt", OUTPUT_FORMATS_BINAURAL)
@pytest.mark.parametrize("fs", SAMPLE_RATE[-1:])
@pytest.mark.parametrize("in_dir", INPUT_FORMATS_MASA["dir"])
@pytest.mark.parametrize("in_tc", INPUT_FORMATS_MASA["tc"])
def test_masa_binaural_headrotation(test_info, in_tc, in_dir, fs, out_fmt, trj_file):

    # -masa Ch File : MASA format
    #                 where Ch specifies the number of MASA input/transport channels (1 or 2)
    #                 and File specifies input file containing parametric MASA metadata

    bitrate = 256000
    test = FORMAT_TO_METADATA_FILES["MASA"].format(in_dir, in_tc, fs)
    metadata_file=str(TESTV_DIR.joinpath(test))
    option_list = ["-masa", in_tc, metadata_file]
    in_file = FORMAT_TO_FILE_MASA_WOEXT.format(in_dir, in_tc, fs)

    compare_rom_vs_binary(
        test_info, 
        in_fmt, 
        option_list,
        bitrate,
        in_file,
        fs,
        out_fmt, 
        fs,
+30 −55
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import subprocess as sp
import sys
from typing import Dict, Optional, Tuple
import numpy as np
import os

from .constants import *
from .compare_audio import compare_audio_arrays
@@ -172,78 +173,52 @@ def run_decoder(
    run_cmd(cmd)


def compare_multichannel_rom_vs_binary(
    test_info, 
    in_fmt, 
    out_fmt, 
    out_fs,
    trj_file: Optional[str] = None
):
    ivas_format = "mc"
    option_list = [f"-{ivas_format}", in_fmt]
    in_fs = 48
    bitrate = 512000
def get_option_list_str(option_list):

    filename_ext = f"_{ivas_format}_{bitrate}_{in_fmt}_{in_fs}-{out_fmt}_{out_fs}"
    option_list_str = option_list.copy()
    for i in range(len(option_list_str)):
        if option_list_str[i][0] == '-':
            option_list_str[i] = option_list_str[i][1:]
        if os.path.isfile(option_list_str[i]):
            option_list_str[i] = Path(option_list_str[i]).stem

    input_path = TESTV_DIR.joinpath(FORMAT_TO_FILE_WOEXT[in_fmt].format(in_fs)).with_suffix(".wav")
    bitstream_path = BITSTREAM_DIR.joinpath(FORMAT_TO_FILE_WOEXT[in_fmt].format(in_fs) + filename_ext)
    run_encoder(bitrate, in_fs, input_path, bitstream_path, add_option_list=option_list)
    return option_list_str

    out_rom_path = DEC_ROM_DIR.joinpath(FORMAT_TO_FILE_WOEXT[in_fmt].format(in_fs) + filename_ext).with_suffix(".wav")
    if trj_file is not None:
        option_list = ["-t", trj_file]
    else :
        option_list = None
    run_decoder(out_fmt, out_fs, bitstream_path, out_rom_path, add_option_list=option_list)
    out_rom, out_rom_fs = pyaudio3dtools.audiofile.readfile(out_rom_path)

    out_bin_path = DEC_BINARY_DIR.joinpath(FORMAT_TO_FILE_WOEXT[in_fmt].format(in_fs) + filename_ext).with_suffix(".wav")
    hrtf_file = HRTF_BINARY_FILE.format(out_fs)
    if trj_file is not None:
        option_list.extend(["-hrtf", str(HRTF_BINARY_DIR.joinpath(hrtf_file))])
    else :
        option_list = ["-hrtf", str(HRTF_BINARY_DIR.joinpath(hrtf_file))]
    run_decoder(out_fmt, out_fs, bitstream_path, out_bin_path, add_option_list=option_list)
    out_bin, out_bin_fs = pyaudio3dtools.audiofile.readfile(out_rom_path)

    check_BE(test_info, out_rom, out_rom_fs, out_bin, out_bin_fs)


def compare_ambisonics_rom_vs_binary(
def compare_rom_vs_binary(
    test_info, 
    in_fmt, 
    option_list_enc,
    bitrate,
    in_file,
    in_fs,
    out_fmt, 
    out_fs,
    trj_file: Optional[str] = None
):
    ivas_format = "sba"
    option_list = [f"-{ivas_format}", INPUT_FORMATS_SBA[in_fmt]]
    bitrate = 256000
    
    filename_ext = f"_{ivas_format}_{bitrate}_{in_fmt}_{in_fs}-{out_fmt}_{out_fs}"
    option_str = "_".join(get_option_list_str(option_list_enc))
    bit_file_ext = f"_{option_str}_{bitrate}_{in_fs}"
    out_file_ext = bit_file_ext + f"-{out_fs}_{out_fmt}"

    input_path = TESTV_DIR.joinpath(FORMAT_TO_FILE_WOEXT[in_fmt].format(in_fs)).with_suffix(".wav")
    bitstream_path = BITSTREAM_DIR.joinpath(FORMAT_TO_FILE_WOEXT[in_fmt].format(in_fs) + filename_ext)
    input_path = TESTV_DIR.joinpath(in_file).with_suffix(".wav")
    bitstream_path = BITSTREAM_DIR.joinpath(in_file + bit_file_ext)
    run_encoder(bitrate, in_fs, input_path, bitstream_path, add_option_list=option_list_enc)
    
    run_encoder(bitrate, in_fs, input_path, bitstream_path, add_option_list=option_list)

    out_rom_path = DEC_ROM_DIR.joinpath(FORMAT_TO_FILE_WOEXT[in_fmt].format(in_fs) + filename_ext).with_suffix(".wav")
    if trj_file is not None:
        option_list = ["-t", trj_file]
        option_list_dec = ["-t", trj_file]
        out_file_ext += "_headrot"
    else :
        option_list = None
    run_decoder(out_fmt, out_fs, bitstream_path, out_rom_path, add_option_list=option_list)
        option_list_dec = None
    out_rom_path = DEC_ROM_DIR.joinpath(in_file + out_file_ext).with_suffix(".wav")
    run_decoder(out_fmt, out_fs, bitstream_path, out_rom_path, add_option_list=option_list_dec)
    out_rom, out_rom_fs = pyaudio3dtools.audiofile.readfile(out_rom_path)

    out_bin_path = DEC_BINARY_DIR.joinpath(FORMAT_TO_FILE_WOEXT[in_fmt].format(in_fs) + filename_ext).with_suffix(".wav")
    hrtf_file = HRTF_BINARY_FILE.format(out_fs)
    if trj_file is not None:
        option_list.extend(["-hrtf", str(HRTF_BINARY_DIR.joinpath(hrtf_file))])
        option_list_dec.extend(["-hrtf", str(HRTF_BINARY_DIR.joinpath(hrtf_file))])
    else :
        option_list = ["-hrtf", str(HRTF_BINARY_DIR.joinpath(hrtf_file))]
    run_decoder(out_fmt, out_fs, bitstream_path, out_bin_path, add_option_list=option_list)
        option_list_dec = ["-hrtf", str(HRTF_BINARY_DIR.joinpath(hrtf_file))]
    out_bin_path = DEC_BINARY_DIR.joinpath(in_file + out_file_ext).with_suffix(".wav")
    run_decoder(out_fmt, out_fs, bitstream_path, out_bin_path, add_option_list=option_list_dec)
    out_bin, out_bin_fs = pyaudio3dtools.audiofile.readfile(out_rom_path)

    check_BE(test_info, out_rom, out_rom_fs, out_rom, out_rom_fs)
 No newline at end of file
    check_BE(test_info, out_rom, out_rom_fs, out_bin, out_bin_fs)
 No newline at end of file