Commit fa793acf authored by Anika Treffehn's avatar Anika Treffehn
Browse files

moved test for item length to processing splitting scaling

parent 1882d29d
Loading
Loading
Loading
Loading
Loading
+1 −4
Original line number Diff line number Diff line
@@ -43,7 +43,6 @@ from ivas_processing_scripts.constants import (
)
from ivas_processing_scripts.processing import chains, config
from ivas_processing_scripts.processing.processing import (
    compare_wav_lengths,
    multiple_of_frame_size,
    preprocess,
    preprocess_2,
@@ -131,7 +130,7 @@ def main(args):
        cfg.metadata_path = metadata

        # checking if audio is a multiple of frame size
        multiple_of_frame_size(cfg, logger)
        multiple_of_frame_size(cfg)

        # run preprocessing only once
        if hasattr(cfg, "preprocessing"):
@@ -176,8 +175,6 @@ def main(args):
                "mp" if cfg.multiprocessing else None,
            )

        compare_wav_lengths(cfg.input_path, cfg.output_path, logger)

    # 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)
+8 −31
Original line number Diff line number Diff line
@@ -177,6 +177,12 @@ def concat_teardown(x, splits, out_fmt, fs, in_fs, meta, logger: logging.Logger)
        new_splits.append(int(float(split_i) * relative_fs_change))
    splits = new_splits

    # check if last split ending coincides with last sample of signal
    if splits[-1] > len(x):
        raise ValueError(f"Last split index {splits[-1]} is larger than the signal length {len(x)}")
    elif splits[-1] < len(x):
        warn(f"Last split index {splits[-1]} is smaller that the signal length {len(x)}")

    split_old = 0
    split_signals = []
    split_meta = []
@@ -448,7 +454,6 @@ def preprocess_background_noise(cfg):

def multiple_of_frame_size(
    cfg: TestConfig,
    logger: logging.Logger,
    frame_size_in_ms: Optional[int] = 20,
) -> np.ndarray:
    """
@@ -515,9 +520,8 @@ def multiple_of_frame_size(
                    f"The length ({n_samples_x} samples) of audio ({item.name}) is not a multiple of frame length (20 ms). Padding to the nearest integer multiple."
                )

                # Create and appending zeros
                padding_array = np.zeros((padding_samples, n_chan_x))
                padded_data = np.vstack((x, padding_array))
                # Create and append zeros
                padded_data = trim(x, sampling_rate, (0, -padding_samples), pad_noise=True, samples=True)
                # Write padded data to output directory
                write(output_dir / item.name, padded_data, fs)
        else:
@@ -545,30 +549,3 @@ def multiple_of_frame_size(

    # Make the output path as the new input path
    cfg.input_path = output_dir
    

def compare_wav_lengths(input_path: Path, output_path: Path, logger: logging.Logger):
    """
    The function compares the number of samples of the files present in the input directory
    to the corresponding output files in the subdirectories which start with "c" followed by 2 digits.

    Parameters
    ----------
    input_path: Path
        Path to input directory
    output_path: Path
        Path to output directory
    """
    for subdir in output_path.iterdir():
        if subdir.is_dir() and subdir.name.startswith("c"):
            for file in subdir.glob("*.wav"):
                input_file = input_path / file.name
                output_file = file
                input_array, input_fs = read(str(input_file))
                output_array, output_fs = read(str(output_file))
                input_length, input_channels = input_array.shape
                output_length, output_channles = output_array.shape
                if input_length != output_length:
                    logger.info(
                        f"The {input_file.name} has {input_length} samples and the output condition {subdir.name} {output_file.name} has {output_length} samples and the difference between the two is {input_length - output_length} samples.\n"
                    )
+8 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ import logging
import re
from itertools import repeat
from pathlib import Path
from warnings import warn

import numpy as np

@@ -239,6 +240,13 @@ class Processing_splitting_scaling(Processing):
                out_meta = repeat(None)

        else:
            # check length of output signals
            input_aligned_file = in_file.parent.parent / "20ms_aligned_files" / f"{Path(in_file.stem).stem}.wav"
            input_aligned_array, _ = read(input_aligned_file)
            if (len_inp := len(input_aligned_array)) != (len_out := len(x)):
                warn(f"For file {out_file} the length is {len_out} and does not match the (frame aligned) input length {len_inp}.")

            # set output values
            out_files = [out_file]
            file_splits = [x]
            if isinstance(audio.fromtype(self.out_fmt), audio.ObjectBasedAudio):