File size: 1,831 Bytes
a158d14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7661f93
a158d14
 
 
 
 
 
7661f93
a158d14
 
 
 
af42785
a158d14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
ASR:
https://cloud.tencent.com/product/asr#mod2

https://huggingface.co/spaces/sanchit-gandhi/whisper-large-v2
https://huggingface.co/spaces/hf-audio/whisper-large-v3
"""
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 = 60


def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--filename",
        default=(project_path / "data/early_media/60/wav/voice/early_vm_8ecb7ad6-0781-405e-9ede-c4a7622d53f9.wav").as_posix(),
        type=str
    )
    parser.add_argument(
        "--templates_segmented_dir",
        default=(project_path / "data/early_media/{area_code}/temp".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=0.5, type=float)
    args = parser.parse_args()
    return args


def main():
    args = get_args()

    filename = Path(args.filename)
    templates_segmented_dir = Path(args.templates_segmented_dir)

    templates_segmented_dir.mkdir(parents=True, exist_ok=True)

    sample_rate, signal = wavfile.read(filename)

    frames = sigproc.framesig(
        sig=signal,
        frame_len=args.win_size * sample_rate,
        frame_step=args.win_len * sample_rate,
        # winfunc=np.hamming
    )

    for j, frame in enumerate(frames):
        to_filename = templates_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()