Commit 8d616694 authored by Jan Kiene's avatar Jan Kiene
Browse files

Merge branch 'ci/hrtf-loading-test' into fix_binary_loading_tests

parents b1f56f60 6ee8b46a
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -440,7 +440,7 @@ def pre_proc_input(testv_file, fs):
        num_channel = "16"
    cut_file = testv_file.replace(".wav", num_channel + "chn_" + cut_gain + ".wav")
    cut_samples(
        testv_file, cut_file, num_channel, fs + "000", cut_from, cut_len, cut_gain
        testv_file, cut_file, num_channel, cut_from, cut_len, cut_gain
    )
    return cut_file

+1 −1
Original line number Diff line number Diff line
@@ -556,10 +556,10 @@ def sba_enc(
                input_path,
                cut_file,
                num_channels,
                sampling_rate + "000",
                cut_from,
                cut_len,
                cut_gain,
                sample_rate=sampling_rate + "000",
            )
        input_path = cut_file

+33 −20
Original line number Diff line number Diff line
@@ -35,37 +35,50 @@ Create short (5sec) testvectors.
"""

import sys
import argparse
from pathlib import Path

from cut_pcm import cut_samples

HERE = Path(__file__).parent.resolve()
TEST_VECTOR_DIR = str(HERE.joinpath("../scripts/testv").resolve())
TEST_VECTOR_DIR = HERE.joinpath("../scripts/testv").resolve()

NUM_CHANNELS = "4"  # currently only FOA
CUT_FROM = "0.0"
CUT_LEN = "5.0"

FILE_IDS = ["stvFOA", "stv20A", "stv3OA", "stv51MC", "stv71MC", "stv512MC", "stv514MC", "stv714MC", "ISM", "MASA"]
GAINS = ["1.0", "16.0", ".004"]

def create_short_testvectors():
    for fs in ["48", "32", "16"]:
        in_file = f"{TEST_VECTOR_DIR}/stvFOA{fs}c.wav"
        cut_gain = "1.0"
        cut_file = f"{TEST_VECTOR_DIR}/stvFOA{fs}c_cut.wav"
        cut_samples(
            in_file, cut_file, NUM_CHANNELS, fs + "000", CUT_FROM, CUT_LEN, cut_gain
        )
        cut_gain = "16.0"
        cut_file = f"{TEST_VECTOR_DIR}/stvFOA{fs}c_cut_{cut_gain}.wav"
        cut_samples(
            in_file, cut_file, NUM_CHANNELS, fs + "000", CUT_FROM, CUT_LEN, cut_gain
        )
        cut_gain = ".004"
        cut_file = f"{TEST_VECTOR_DIR}/stvFOA{fs}c_cut_{cut_gain}.wav"
        cut_samples(
            in_file, cut_file, NUM_CHANNELS, fs + "000", CUT_FROM, CUT_LEN, cut_gain
        )
def collect_files(file_ids):
    files = [
        f.absolute()
        for f in TEST_VECTOR_DIR.iterdir()
        if f.suffix == ".wav" and any([id in f.name for id in file_ids]) and not "_cut" in f.name
    ]
    return files


def create_short_testvectors(which="foa"):
    file_ids = []
    if which == "all":
        file_ids = FILE_IDS
    elif which == "foa":
        file_ids = FILE_IDS[:1]

    for f in collect_files(file_ids):
        for g in GAINS:
            suffix = "_cut"
            if g != "1.0":
                suffix += f"_{g}"

            out_file = f.parent.joinpath(f.stem + suffix + f.suffix)
            cut_samples(f, out_file, NUM_CHANNELS, CUT_FROM, CUT_LEN, g)


if __name__ == "__main__":
    sys.exit(create_short_testvectors())
    parser = argparse.ArgumentParser()
    parser.add_argument("which", choices=["foa", "all"])
    args = parser.parse_args()
    sys.exit(create_short_testvectors(args.which))
+9 −8
Original line number Diff line number Diff line
@@ -60,9 +60,7 @@ def usage():
    return 1


def cut_samples(
    in_file, out_file, num_channels, sample_rate, start, duration, gain="1.0"
):
def cut_samples(in_file, out_file, num_channels, start, duration, gain="1.0", sample_rate=None):
    """
    Function to cut samples from an audio file (wav or pcm)
    """
@@ -74,6 +72,12 @@ def cut_samples(
            + platform.python_version()
        )

    if sample_rate is None and not str(in_file).endswith(".wav"):
        raise ValueError(f"For non-wav files, samplerate must be explicitly given")
    elif sample_rate is None:
        # set to default of pyaudio3dtools.audiofile.readfile -> for wav files it will be ignored anyway
        sample_rate = 48000

    # all input parameters are strings - convert some
    fs = int(sample_rate)
    start_sec = float(start)
@@ -85,11 +89,8 @@ def cut_samples(
    num_in_samples = s.shape[0]
    num_samples_to_skip = int(start_sec * fs)
    dur_samples = int(dur_sec * fs)
    if num_samples_to_skip + dur_samples > num_in_samples:
        sys.exit(
            f"requested too many samples ({num_samples_to_skip}+{dur_samples})"
            + f" - input is too short ({num_in_samples})"
        )
    if num_samples_to_skip > dur_samples:
        raise ValueError(f"Requested to skip {num_samples_to_skip}, but file only has {dur_samples} samples")

    s_out = s[num_samples_to_skip : num_samples_to_skip + dur_samples, :] * gain_f

+1 −1
Original line number Diff line number Diff line
@@ -439,7 +439,7 @@ def pre_proc_input(testv_file, fs):
        num_channel = "16"
    cut_file = testv_file.replace(".wav", num_channel + "chn_" + cut_gain + ".wav")
    cut_samples(
        testv_file, cut_file, num_channel, fs + "000", cut_from, cut_len, cut_gain
        testv_file, cut_file, num_channel, cut_from, cut_len, cut_gain
    )
    return cut_file