Commit ad6fa9f7 authored by janssontoftg's avatar janssontoftg
Browse files

Merge remote-tracking branch 'remotes/origin/main' into ericsson/review-item-creation-stereo

parents 8cfdaa57 5eb4e32e
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@ experiments/selection/*/proc_input/*.wav
experiments/selection/*/proc_input/*.pcm
experiments/selection/*/proc_output/
*~
tests/tmp_output_*
tests/temp_output_*
tests/cut
tests/ref
tests/concatenation_folder
 No newline at end of file
+3 −2
Original line number Diff line number Diff line
@@ -25,11 +25,12 @@
### Any relative paths will be interpreted relative to the working directory the script is called from!
### Usage of absolute paths is recommended.
### Do not use file names with dots "." in them! This is not supported, use "_" instead
### For Windows user: please use double back slash '\\' in paths and add '.exe' to executable definitions
### Do not use "tmp_" in file or folder names ("temp_" is fine)
### For Windows user: please use double back slash '\\' in paths
### REQUIRED: Input path or file
input_path: ".../ivas/items/HOA3"
### REQUIRED: Output path or file
output_path: ".../tmp_output"
output_path: ".../temp_output"
### Metadata path or file(s)
### If input format is ISM{1-4} a path for the metadata files can be specified;
### default = null (for ISM search for item_name.{wav, raw, pcm}.{0-3}.csv in input folder, otherise ignored)
+0 −15
Original line number Diff line number Diff line
@@ -48,10 +48,7 @@ from ivas_processing_scripts.processing.processing import (
    preprocess_2,
    preprocess_background_noise,
    process_item,
    rename_generated_conditions,
    reorder_items_list,
    reverse_process_2,
    scale_resulting_files,
)
from ivas_processing_scripts.utils import DirManager, apply_func_parallel

@@ -178,18 +175,6 @@ def main(args):
                "mp" if cfg.multiprocessing else None,
            )

        # remove preamble and split signals
        if hasattr(cfg, "preprocessing_2"):
            reverse_process_2(cfg, logger)

        # scale individual files
        if cfg.postprocessing.get("loudness", False):
            scale_resulting_files(cfg, logger)

        # rename output with condition name
        if cfg.condition_in_output_filename:
            rename_generated_conditions(cfg.output_path)

    # copy configuration to output directory
    with open(cfg.output_path.joinpath(f"{cfg.name}.yml"), "w") as f:
        yaml.safe_dump(cfg._yaml_dump, f)
+1 −1
Original line number Diff line number Diff line
@@ -293,7 +293,7 @@ def process_audio(
            logger.debug(
                f"Applying loudness adjustment to {loudness} LKFS for format {loudness_fmt} using ITU STL bs1770demo"
            )
        x.audio = loudness_norm(x, loudness, loudness_fmt, logger=logger)
        x.audio, _ = loudness_norm(x, loudness, loudness_fmt, logger=logger)

    """limiting"""
    if limit:
+7 −64
Original line number Diff line number Diff line
@@ -225,7 +225,7 @@ def loudness_norm(
    rms: Optional[bool] = False,
    logger: Optional[logging.Logger] = None,
    file_name_logging: Optional[Union[str, Path]] = None,
) -> np.ndarray:
) -> Tuple[np.ndarray, float]:
    """
    Iterative loudness normalization using ITU-R BS.1770-4
    Signal is iteratively scaled after rendering to the specified format
@@ -250,11 +250,14 @@ def loudness_norm(
    -------
    norm: Audio
        Normalized audio
    scaling_factor: float
        applied scaling factor
    """

    measured_loudness = np.inf
    num_iter = 1
    scaled_input = copy.deepcopy(input)
    scaling_factor = 1.0

    # save loudness before and after scaling for the logger info
    loudness_before, scale_factor_new, loundness_fmt_used = get_loudness(
@@ -265,6 +268,7 @@ def loudness_norm(
    while np.abs(measured_loudness - target_loudness) > 0.5 and num_iter <= 10:
        # scale input
        scaled_input.audio *= scale_factor_new
        scaling_factor *= scale_factor_new

        # measure loudness and get scaling factor
        measured_loudness, scale_factor_new, _ = get_loudness(
@@ -291,65 +295,4 @@ def loudness_norm(
            f"Loudness did not converge to desired value, stopping at: {loudness_after:.2f}"
        )

    return scaled_input.audio


def scale_files(
    file_list: list[list[Union[Path, str]]],
    fmt: str,
    loudness: float,
    loudness_format: Optional[str] = None,
    fs: Optional[int] = 48000,
    in_meta: Optional[list] = None,
    logger: Optional[logging.Logger] = None,
) -> None:
    """
    Scales audio files to desired loudness

    Parameters
    ----------
    file_list : list[list[Union[Path, str]]]
        List of file paths in a list of the condition folders
    fmt: str
        Audio format of files in list
    loudness: float
        Desired loudness level in LKFS/dBov
    loudness_format: Optional[str]
        Format for loudness measurement
    fs: Optional[int]
        Sampling rate
    in_meta: Optional[list]
        Metadata for ISM with same structure as file_list but one layer more
        for the list of metadata for one file
    logger: Optional[logging.Logger]
        Logger to log loudness information
    """

    if fmt.startswith("ISM"):
        if in_meta:
            meta_bool = True
        else:
            raise ValueError("No metadata available for loudness measurement")
    else:
        in_meta = copy.copy(file_list)
        meta_bool = False

    for folder, meta_folder in zip(file_list, in_meta):
        for file, meta in zip(folder, meta_folder):
            # create audio object
            if meta_bool:
                audio_obj = audio.fromfile(fmt, file, fs, meta)
            else:
                audio_obj = audio.fromfile(fmt, file, fs)

            # adjust loudness
            scaled_audio = loudness_norm(
                audio_obj,
                loudness,
                loudness_format,
                logger=logger,
                file_name_logging=file,
            )

            # write into file
            write(file, scaled_audio, audio_obj.fs)
    return scaled_input.audio, scaling_factor
Loading