|
|
|
|
|
|
|
|
import argparse |
|
|
from pathlib import Path |
|
|
|
|
|
import numpy as np |
|
|
from python_speech_features import sigproc |
|
|
from scipy.io import wavfile |
|
|
from tqdm import tqdm |
|
|
|
|
|
from project_settings import project_path |
|
|
|
|
|
|
|
|
area_code = 1 |
|
|
|
|
|
|
|
|
def get_args(): |
|
|
parser = argparse.ArgumentParser() |
|
|
parser.add_argument( |
|
|
"--segmented_dir", |
|
|
default=(project_path / "data/2s_wav/{area_code}".format(area_code=area_code)).as_posix(), |
|
|
type=str |
|
|
) |
|
|
parser.add_argument( |
|
|
"--templates_dir", |
|
|
default=(project_path / "data/early_media_template/{area_code}".format(area_code=area_code)).as_posix(), |
|
|
type=str |
|
|
) |
|
|
parser.add_argument( |
|
|
"--wav_dir", |
|
|
default=(project_path / "data/wav/{area_code}".format(area_code=area_code)).as_posix(), |
|
|
type=str |
|
|
) |
|
|
parser.add_argument("--win_size", default=2.0, type=float) |
|
|
parser.add_argument("--win_len", default=2.0, type=float) |
|
|
args = parser.parse_args() |
|
|
return args |
|
|
|
|
|
|
|
|
def main(): |
|
|
args = get_args() |
|
|
|
|
|
segmented_dir = Path(args.segmented_dir) |
|
|
templates_dir = Path(args.templates_dir) |
|
|
wav_dir = Path(args.wav_dir) |
|
|
|
|
|
segmented_dir.mkdir(parents=True, exist_ok=True) |
|
|
templates_dir.mkdir(parents=True, exist_ok=True) |
|
|
|
|
|
for filename in tqdm(wav_dir.glob("*/*.wav")): |
|
|
|
|
|
|
|
|
sample_rate, signal = wavfile.read(filename) |
|
|
if len(signal) < args.win_size * sample_rate: |
|
|
continue |
|
|
|
|
|
frames = sigproc.framesig( |
|
|
sig=signal, |
|
|
frame_len=args.win_size * sample_rate, |
|
|
frame_step=args.win_len * sample_rate, |
|
|
|
|
|
) |
|
|
|
|
|
for j, frame in enumerate(frames): |
|
|
to_filename = segmented_dir / "{}_{}.wav".format(filename.stem, j) |
|
|
|
|
|
frame = np.array(frame, dtype=np.int16) |
|
|
wavfile.write( |
|
|
filename=to_filename, |
|
|
rate=sample_rate, |
|
|
data=frame |
|
|
) |
|
|
|
|
|
return |
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
main() |
|
|
|