Commit cb81bb9a authored by Jan Kiene's avatar Jan Kiene
Browse files

add --dry and --all arguments

parent 7601472d
Loading
Loading
Loading
Loading
Loading
+37 −10
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ Create short (5sec) testvectors.

import argparse
import sys
import logging
from pathlib import Path

from cut_pcm import cut_samples
@@ -81,29 +82,39 @@ FILE_IDS_LTV = [
]


def collect_files(use_ltv: bool = False, ltv_dir: Path = None):
    IDS = FILE_IDS_LTV if use_ltv else FILE_IDS
    SEARCH_DIR = ltv_dir if use_ltv and ltv_dir else TEST_VECTOR_DIR
def collect_files(use_ltv: bool = False, ltv_dir: Path = None, all: bool = False):
    ids = FILE_IDS_LTV if use_ltv else FILE_IDS
    search_dir = ltv_dir if use_ltv and ltv_dir else TEST_VECTOR_DIR

    files = [
        f.absolute()
        for f in SEARCH_DIR.iterdir()
        for f in search_dir.iterdir()
        if f.suffix == ".wav"
        and any([id in f.name for id in IDS])
        and "_cut" not in f.name
        and ((any([id in f.name for id in ids]) and "_cut" not in f.name) or all)
    ]
    return files


def create_short_testvectors(
    cut_len=5.0, use_ltv: bool = False, ltv_dir: Path = None, inplace: bool = False
    cut_len=5.0,
    use_ltv: bool = False,
    ltv_dir: Path = None,
    inplace: bool = False,
    all: bool = False,
    dry: bool = False,
):
    files = collect_files(use_ltv, ltv_dir)
    files = collect_files(use_ltv, ltv_dir, all)

    logging.info(f"Cutting {len(files)} files to {cut_len} seconds")

    for f in files:
        suffix = "" if use_ltv or inplace else "_cut"
        out_file = TEST_VECTOR_DIR.joinpath(f.stem + suffix + f.suffix)
        num_channels = audiofile.get_wav_file_info(f)["channels"]

        logging.info(f"{str(f)} -> {out_file}")

        if not dry:
            cut_samples(f, out_file, num_channels, CUT_FROM, f"{cut_len}", GAIN)


@@ -139,10 +150,26 @@ if __name__ == "__main__":
        default=False,
        help="Set this to operate inplace (i.e. overwrite the signals, instead of creating new ones with _cut suffix)",
    )
    parser.add_argument(
        "--all",
        action="store_true",
        default=False,
        help="If given, operate on ALL .wav files in scripts/testv, not just the predefined set.",
    )
    parser.add_argument(
        "--dry",
        action="store_true",
        default=False,
        help="Only print filenames that would be operated on, but don't actually do anything",
    )
    args = parser.parse_args()

    assert not (args.use_ltv and args.ltv_dir is None)

    logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")

    sys.exit(
        create_short_testvectors(args.cut_len, args.use_ltv, args.ltv_dir, args.inplace)
        create_short_testvectors(
            args.cut_len, args.use_ltv, args.ltv_dir, args.inplace, args.all, args.dry
        )
    )