File size: 3,232 Bytes
0834d5a
 
cc18dfd
1f06ccd
 
 
cc18dfd
0834d5a
 
43cae61
0834d5a
43cae61
 
 
 
0834d5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73ce115
0834d5a
 
 
cc18dfd
0834d5a
cc18dfd
0834d5a
 
1f06ccd
 
0834d5a
 
 
 
73ce115
0834d5a
 
 
 
73ce115
0834d5a
 
 
 
33b91bd
0834d5a
def0448
 
0834d5a
 
 
 
33b91bd
0834d5a
def0448
 
0834d5a
 
73ce115
 
 
 
 
 
 
 
0834d5a
 
 
 
 
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
python3 total_duration.py --noise_dir /data/tianxing/HuggingDatasets/nx_noise/data/noise
python3 total_duration.py --noise_dir /data/tianxing/HuggingDatasets/nx_noise/data/speech

"""
import argparse
from collections import defaultdict
import os
from pathlib import Path
import sys

pwd = os.path.abspath(os.path.dirname(__file__))
sys.path.append(os.path.join(pwd, "../"))

import librosa
from tqdm import tqdm

from project_settings import project_path


def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--noise_dir",
        default=(project_path / "data/noise").as_posix(),
        type=str
    )
    args = parser.parse_args()
    return args


def main():
    args = get_args()

    counter = defaultdict(int)
    duration_counter = defaultdict(float)
    two_second_duration_counter = defaultdict(float)
    three_second_duration_counter = defaultdict(float)

    noise_dir = Path(args.noise_dir)
    for filename in tqdm(list(noise_dir.glob("**/*.wav"))):
        if filename.parts[-4] in ("noise", "speech"):
            language = filename.parts[-3]
        elif filename.parts[-3] in ("noise", "speech"):
            language = filename.parts[-2]
        else:
            # raise AssertionError
            continue

        y, sr = librosa.load(filename, sr=None)
        duration = librosa.get_duration(y=y, sr=sr)
        two_second_duration = duration // 2 * 2
        three_second_duration = duration // 3 * 3

        counter[language] += 1
        duration_counter[language] += round(duration, 4)
        two_second_duration_counter[language] += round(two_second_duration, 4)
        three_second_duration_counter[language] += round(three_second_duration, 4)

    total_count = sum(counter.values())
    total_duration = sum(duration_counter.values())
    row = "\nduration; \n\n"
    row += "| language  | count | duration (m) | duration (h) |\n| :---: | :---: | :---: | :---: |\n"
    for k, v in duration_counter.items():
        row += f"| {k} | {counter[k]} | {round(v / 60, 2)} | {round(v / 3600, 2)} |\n"
    row += f"| TOTAL | {total_count} | {round(total_duration, 2)} | {round(total_duration / 3600, 2)} |\n"
    print(row)

    total_duration = sum(two_second_duration_counter.values())
    row = "\ntwo second duration; \n\n"
    row += "| language | count | duration (m) | duration (h) |\n| :---: | :---: | :---: | :---: |\n"
    for k, v in two_second_duration_counter.items():
        row += f"| {k} | {counter[k]} | {round(v / 60, 2)} | {round(v / 3600, 2)} |\n"
    row += f"| TOTAL | {total_count} | {round(total_duration, 2)} | {round(total_duration / 3600, 2)} |\n"
    print(row)

    total_duration = sum(three_second_duration_counter.values())
    row = "\nthree second duration; \n\n"
    row += "| language | count | duration (m) | duration (h) |\n| :---: | :---: | :---: | :---: |\n"
    for k, v in three_second_duration_counter.items():
        row += f"| {k} | {counter[k]} | {round(v / 60, 2)} | {round(v / 3600, 2)} |\n"
    row += f"| TOTAL | {total_count} | {round(total_duration, 2)} | {round(total_duration / 3600, 2)} |\n"
    print(row)

    return


if __name__ == '__main__':
    main()