Commit 48039fc6 authored by Archit Tamarapu's avatar Archit Tamarapu
Browse files

[cleanup] move item generation scripts into subfolder generation; see

notes below

- created __init__.py and __main__.py for generation module
    !! now use python -m ivas_processing_scripts.generation !!
- moved reverb.py wrapper to main wrappers folder
- moved modified config.py to generation/config.py
- moved modified constants.py to generation/constants.py
- moved process_{ism,stereo}_items to generation/
- integrated modifications to audiotools.audiofile.py
parent 8a6542d4
Loading
Loading
Loading
Loading
Loading
+0 −922

File deleted.

Preview size limit exceeded, changes collapsed.

+0 −286
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 argparse
from itertools import repeat
from pathlib import Path

from item_generation_scripts.audiotools.constants import AUDIO_FORMATS
from item_generation_scripts.audiotools.convert import convert_file
from item_generation_scripts.utils import apply_func_parallel


def add_processing_args(group, input=True):
    # set up prefixes to avoid argument collision
    if input:
        p = "in"
        ps = "i"
    else:
        p = "out"
        ps = "o"

    group.add_argument(
        f"-{ps}",
        f"--{p}",
        dest=f"{p}put",
        required=True,
        type=Path,
        help="Path to *.{wav, pcm, raw} file or directory",
    )
    group.add_argument(
        f"-{ps}f",
        f"--{p}_fmt",
        required=input,
        type=str,
        help="Audio format (use -l, --list for a list / -L, --long for a detailed list)",
        default=None,
    )
    group.add_argument(
        f"-{ps}s",
        f"--{p}_fs",
        type=int,
        help="Sampling rate (Hz) (deduced for .wav input, same as input if output not specified, default = %(default)s)",
        default=48000,
    )
    group.add_argument(
        f"-{ps}fc",
        f"--{p}_cutoff",
        type=int,
        help="Cut-off frequency for low-pass filtering (default = %(default)s)",
        default=None,
    )
    group.add_argument(
        f"-{ps}hp",
        f"--{p}_hp50",
        help="Apply 50 Hz high-pass filtering (default = %(default)s)",
        action="store_true",
    )
    group.add_argument(
        f"-{ps}w",
        f"--{p}_window",
        type=float,
        help="Window the start/end of the signal by this amount in milliseconds (default = %(default)s)",
        default=None,
    )
    group.add_argument(
        f"-{ps}t",
        f"--{p}_trim",
        type=float,
        nargs=2,
        metavar=("PRE_TRIM", "POST_TRIM"),
        help="Pre-/post-trim the signal by this amount in milliseconds (negative values pad silence), (default = %(default)s)",
    )
    group.add_argument(
        f"-{ps}pn",
        f"--{p}_pad_noise",
        help="Flag for padding with noise instead of zeros",
        action="store_true",
    )
    group.add_argument(
        f"-{ps}d",
        f"--{p}_delay",
        type=float,
        help="Delay the signal by this amount in milliseconds (negative values advance, default = %(default)s)",
        default=None,
    )
    group.add_argument(
        f"-{ps}l",
        f"--{p}_loudness",
        type=float,
        help="Normalize to given loudness with BS 1770-4 (default = %(default)s)",
        default=None,
    )
    group.add_argument(
        f"-{ps}nf",
        f"--{p}_loudness_fmt",
        type=str,
        help=f"Format used for loudness computation (only valid with with -{ps}l/--{p}_loudness, default = {p.upper()}_FMT)",
        default=None,
    )


def get_args():
    parser = argparse.ArgumentParser(
        description="Audiotools: Convert/Manipulate spatial audio files."
    )

    """ Input file arguments """
    input_parser = parser.add_argument_group("Input (pre-) processing options")

    # add common arguments
    add_processing_args(input_parser)

    # input only arguments
    input_parser.add_argument(
        "-im",
        "--in_meta",
        type=str,
        nargs="+",
        help="list of input metadata files (only relevant for ISM and MASA input)",
        default=None,
    )

    """ Output file arguments """
    output_parser = parser.add_argument_group("Output (post-) processing options")

    # add common arguments
    add_processing_args(output_parser, False)

    # output only arguments
    output_parser.add_argument(
        "-lm",
        "--limit",
        help="Apply limiting to output (default = %(default)s)",
        action="store_true",
    )
    output_parser.add_argument(
        "-t",
        "--trajectory",
        type=str,
        help="Head-tracking trajectory file for binaural output (default = %(default)s)",
        default=None,
    )
    output_parser.add_argument(
        "-bd",
        "--bin_dataset",
        type=str,
        help="Use a custom binaural dataset (see README.md and audiotools/binaural_datasets/README.txt for further information)",
        default=None,
    )
    output_parser.add_argument(
        "-bl",
        "--bin_lfe_gain",
        type=float,
        help="Render LFE to binaural output with the specified gain (only valid for channel-based input, default = %(default)s)",
        default=None,
    )
    output_parser.add_argument(
        "-mnru",
        "--mnru_q",
        type=float,
        help="Flag for MNRU processing",
        default=None,
    )
    output_parser.add_argument(
        "-esdru",
        "--esdru_alpha",
        type=float,
        help="Flag for ESDRU processing",
        default=None,
    )

    misc_parser = parser.add_argument_group("General options")

    """ Miscellaneous or meta arguments """
    misc_parser.add_argument(
        "-l",
        "--list",
        help="list all supported audio formats and exit",
        action="store_true",
    )
    misc_parser.add_argument(
        "-L",
        "--long",
        help="list all supported audio formats with long description and exit",
        action="store_true",
    )
    misc_parser.add_argument(
        "-mp",
        "--multiprocessing",
        help="Enable multiprocessing (default = %(default)s)",
        action="store_true",
    )

    return parser.parse_args()


def main():
    args = get_args()

    if args.list is True or args.long is True:
        for fmt in AUDIO_FORMATS:
            if args.long:
                for f, d in fmt.items():
                    print(f)
                    [print(f"\t{k}: {v}", end=None) for k, v in d.items()]
            else:
                print(", ".join(fmt.keys()))
        exit()

    elif args.input is not None:
        if not args.out_fs:
            args.out_fs = args.in_fs

        if not args.out_fmt:
            args.out_fmt = args.in_fmt

        if not args.out_loudness_fmt:
            args.out_loudness_fmt = args.out_fmt

        # List input files
        args.input = Path(args.input)
        in_files = []
        if args.input.exists():
            if args.input.is_dir():
                in_files.extend(args.input.glob("*.wav"))
                in_files.extend(args.input.glob("*.pcm"))
                in_files.extend(args.input.glob("*.raw"))
            else:
                in_files = [args.input]
        else:
            raise ValueError(f"Input path {args.input} does not exist!")

        if len(in_files) == 0:
            raise ValueError(f"Input directory {args.input} empty!")

        # Create output directory
        args.output = Path(args.output)

        if len(in_files) == 1 and args.input.is_file():
            out_files = [args.output]
        else:
            args.output.mkdir(exist_ok=True)
            out_files = [args.output.joinpath(i.name) for i in in_files]

        # Multiprocessing
        enable_multiprocessing = args.multiprocessing

        # Remove unneeded keys to avoid passing to convert_file()
        for k in ["list", "long", "multiprocessing", "input", "output"]:
            args.__dict__.pop(k)

        apply_func_parallel(
            convert_file,
            zip(in_files, out_files),
            repeat(args.__dict__),
            "mp" if enable_multiprocessing else None,
        )
Loading