Commit a2b69678 authored by Archit Tamarapu's avatar Archit Tamarapu
Browse files

Merge branch 'lp16k_script' into 'main'

add a script for 16 kHz LPF

See merge request !121
parents 0b6fc44b 788d68e6
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@ import argparse
from itertools import repeat
from pathlib import Path

from ivas_processing_scripts.audiotools.constants import AUDIO_FORMATS
from ivas_processing_scripts.audiotools.constants import AUDIO_FORMATS, BINAURAL_LFE_GAIN
from ivas_processing_scripts.audiotools.convert import convert_file
from ivas_processing_scripts.utils import apply_func_parallel

@@ -82,7 +82,7 @@ def add_processing_args(group, input=True):
        f"-{ps}mk",
        f"--{p}_mask",
        type=str,
        help="Apply filtering with mask ((HP50, 20KBP or None; default = %(default)s)",
        help="Apply filtering with mask (HP50, 20KBP or None; default = %(default)s)",
        default=None,
    )
    group.add_argument(
@@ -182,7 +182,7 @@ def get_args():
        "--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,
        default=BINAURAL_LFE_GAIN,
    )
    output_parser.add_argument(
        "-mnru",

other/lp16k.py

0 → 100755
+76 −0
Original line number Diff line number Diff line
#!/usr/bin/python3
import argparse
import multiprocessing as mp
import sys
from pathlib import Path
from time import sleep

sys.path.append(str(Path(__file__).parent.parent))
from ivas_processing_scripts.audiotools.audio import fromfile
from ivas_processing_scripts.audiotools.audiofile import write
from ivas_processing_scripts.audiotools.wrappers.filter import resample_itu
from ivas_processing_scripts.utils import progressbar_update, spinner


def lp16k(in_file, out_file):
    x = fromfile("STEREO", in_file)
    if x.fs != 48000:
        raise ValueError(
            f"Unsupported sampling rate {x.fs//1000} kHz for input file {in_file} - only 48 kHz is supported!"
        )

    # resample ITU only returns the audio array
    # but uses the sampling rate stored in the audio object
    # so we need to correctly set it after each call
    x.audio = resample_itu(x, 96000)
    x.fs = 96000

    x.audio = resample_itu(x, 32000)
    x.fs = 32000

    x.audio = resample_itu(x, 96000)
    x.fs = 96000

    x.audio = resample_itu(x, 48000)
    x.fs = 48000

    write(out_file, x.audio, x.fs)


def main(args):
    # list input files and create function arguments
    in_files = list(args.in_dir.glob("*.wav"))
    func_args = [(f, args.out_dir.joinpath(f.name)) for f in in_files]

    if not args.out_dir.exists():
        args.out_dir.mkdir()

    # progressbar and multiprocessing setup
    count = len(func_args)
    width = 80

    p = mp.Pool()
    results = p.starmap_async(lp16k, func_args)

    # apply function in parallel
    progressbar_update(0, count, width)
    while not results.ready():
        progressbar_update(count - int(results._number_left), count, width)
        spinner()
        sleep(0.1)
    progressbar_update(count, count, width)
    print("\n", flush=True, file=sys.stdout)
    results.get()

    p.close()
    p.join()


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Script to lowpass audio to 16 kHz")
    parser.add_argument("in_dir", help="Input directory", type=Path)
    parser.add_argument("out_dir", help="Output directory", type=Path)

    args = parser.parse_args()

    main(args)