Commit 4a75fbba authored by Jan Kiene's avatar Jan Kiene
Browse files

add wrapper for masaAnalyzer

parent e4b2bb96
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -524,7 +524,7 @@ The following additional executables are needed for the different processing ste
| Filtering, Resampling                           | filter                | https://www.3gpp.org/ftp/tsg_sa/WG4_CODEC/TSGS4_76/docs/S4-131277.zip                                       |
| Random offset/seed generation (necessary for background noise and FER bitstream processing)   | random                | https://www.3gpp.org/ftp/tsg_sa/WG4_CODEC/TSGS4_76/docs/S4-131277.zip                                       |
| JBM network simulator                           | networkSimulator_g192 | https://www.3gpp.org/ftp/tsg_sa/WG4_CODEC/TSGS4_76/docs/S4-131277.zip                                       |
| MASA rendering (also used in loudness measurement of MASA items)        | masaRenderer        | https://www.3gpp.org/ftp/TSG_SA/WG4_CODEC/TSGS4_122_Athens/Docs/S4-230221.zip         |
| MASA rendering (also used in loudness measurement of MASA items)        | masaRenderer, masaAnalyzer   | https://www.3gpp.org/ftp/TSG_SA/WG4_CODEC/TSGS4_122_Athens/Docs/S4-230221.zip         |
| EVS reference conditions        | EVS_cod, EVS_dec      | https://www.3gpp.org/ftp/Specs/archive/26_series/26.443/26443-h00.zip                                       |

The necessary binaries have to be either placed in the [ivas_processing_scripts/bin](./ivas_processing_scripts/bin) folder or the path has to be specified in 
+114 −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
from tempfile import TemporaryDirectory

from ivas_processing_scripts.audiotools import audio
from ivas_processing_scripts.audiotools.audiofile import read, write
from ivas_processing_scripts.audiotools.wrappers.filter import resample_itu
from ivas_processing_scripts.constants import DEFAULT_CONFIG_BINARIES
from ivas_processing_scripts.utils import find_binary, run


def masaAnalyzer(
    sba: audio.SceneBasedAudio,
    num_tcs: int,
    num_dirs: int,
    metadata_out_path: Path
) -> audio.MetadataAssistedSpatialAudio:
    """
    Wrapper for masaAnalyzer (from MASA reference software)

    Parameters
    ----------
    num_tcs: int
        Number of MASA transport channels (1 - monoMASA, 2- stereoMASA)
    num_dirs: int
        Number of directions
    metadata_out_path:
        Path to output the Metadata file to

    Returns
    -------
    masa: audio.MetadataAssistedSpatialAudio
        generated MASA
    """

    if "masaAnalyzer" in DEFAULT_CONFIG_BINARIES["binary_paths"]:
        binary = find_binary(
            DEFAULT_CONFIG_BINARIES["binary_paths"]["masaAnalyzer"].name,
            binary_path=DEFAULT_CONFIG_BINARIES["binary_paths"]["masaAnalyzer"].parent,
        )
    else:
        binary = find_binary("masaAnalyzer")

    if num_tcs not in [1, 2]:
        raise ValueError(f"Only 1 or 2 TCs supported, but {num_tcs} was given.")
    
    if num_dirs not in [1, 2]:
        raise ValueError(f"Only 1 or 2 directions supported, but {num_dirs} was given.")

    if sba.name not in ["FOA", "HOA2"]:
        raise ValueError(f"Only FOA or HOA2 suported, but {sba.name} was given.")

    cmd = [
        str(binary),
        "-mono" if num_tcs == 1 else "-stereo",
        f"-{num_dirs}dir",
        "-foa" if sba.name == "FOA" else "-hoa2",
        "",  # 4 -> inputPcm
        "",  # 5 -> outputPcm
        "",  # 6 -> output metadata file
    ]

    with TemporaryDirectory() as tmp_dir:
        tmp_dir = Path(tmp_dir)
        tmp_in = tmp_dir.joinpath("tmp_masaAnaIn.pcm")
        tmp_out_pcm = tmp_dir.joinpath("tmp_masaAnaOut.pcm")

        cmd[4] = str(tmp_in)
        cmd[5] = str(tmp_out_pcm)
        cmd[6] = str(metadata_out_path)

        tmp_audio = resample_itu(sba, 48000)
        write(tmp_in, tmp_audio, 48000)

        # we need to run in the masaAnalyzer directory to use the .bin files it requires
        print(cmd)
        run(cmd, cwd=binary.resolve().parent)

        fmt = f"MASA{num_tcs}"
        masa = audio.fromfile(fmt, tmp_out_pcm, 48000, [metadata_out_path])

        return masa