#!/usr/bin/python3 # -*- coding: utf-8 -*- import argparse import os from pathlib import Path import librosa import numpy as np from scipy.io import wavfile from tqdm import tqdm from project_settings import project_path def get_args(): parser = argparse.ArgumentParser() parser.add_argument( "--wav_dir", # default=(project_path / "data/early_media/60/wav").as_posix(), default=(project_path / "data/calling/60/wav").as_posix(), type=str ) parser.add_argument("--to_sample_rate", default=16000, type=int) parser.add_argument( "--output_dir", # default="./early_media", default="./calling", type=str ) args = parser.parse_args() return args def main(): args = get_args() wav_dir = Path(args.wav_dir) output_dir = Path(args.output_dir) max_value = (2 << 15) for filename in tqdm(wav_dir.glob("**/*.wav")): filename = Path(filename) signal, sample_rate = librosa.load(filename.as_posix(), sr=args.to_sample_rate) signal = np.array(signal * max_value, dtype=np.int16) if signal.ndim != 2: signal2 = np.zeros(shape=signal.shape, dtype=np.int16) signal = np.stack([signal, signal2], axis=-1) to_filename = output_dir / filename.relative_to(wav_dir) to_filename.parent.mkdir(parents=True, exist_ok=True) wavfile.write(to_filename.as_posix(), rate=args.to_sample_rate, data=signal) return if __name__ == '__main__': main()