|
|
|
|
|
|
|
|
import argparse |
|
|
from collections import defaultdict |
|
|
from pathlib import Path |
|
|
|
|
|
import librosa |
|
|
from mpmath.libmp import round_down |
|
|
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) |
|
|
|
|
|
noise_dir = Path(args.noise_dir) |
|
|
for filename in tqdm(list(noise_dir.glob("**/*.wav"))): |
|
|
if filename.parts[-4] == "noise": |
|
|
language = filename.parts[-3] |
|
|
elif filename.parts[-3] == "noise": |
|
|
language = filename.parts[-2] |
|
|
else: |
|
|
raise AssertionError |
|
|
|
|
|
y, sr = librosa.load(filename, sr=None) |
|
|
duration = librosa.get_duration(y=y, sr=sr) |
|
|
two_second_duration = duration // 2 * 2 |
|
|
|
|
|
counter[language] += 1 |
|
|
duration_counter[language] += round(duration, 4) |
|
|
two_second_duration_counter[language] += round(two_second_duration, 4) |
|
|
|
|
|
total_count = sum(counter.values()) |
|
|
total_duration = sum(duration_counter.values()) |
|
|
row = "\nduration; \n\n" |
|
|
row += "| language | count | duration (s) | duration (h) |\n| :---: | :---: | :---: | :---: |\n" |
|
|
for k, v in duration_counter.items(): |
|
|
row += f"| {k} | {counter[k]} | {round(v, 4)} | {round(v / 3600, 4)} |\n" |
|
|
row += f"| total | {total_count} | {round(total_duration, 4)} | {round(total_duration / 3600, 4)} |\n" |
|
|
print(row) |
|
|
|
|
|
total_duration = sum(two_second_duration_counter.values()) |
|
|
row = "\ntwo second duration; \n\n" |
|
|
row += "| language | count | duration (s) | duration (h) |\n| :---: | :---: | :---: | :---: |\n" |
|
|
for k, v in two_second_duration_counter.items(): |
|
|
row += f"| {k} | {counter[k]} | {round(v, 4)} | {round(v / 3600, 4)} |\n" |
|
|
row += f"| total | {total_count} | {round(total_duration, 4)} | {round(total_duration / 3600, 4)} |\n" |
|
|
print(row) |
|
|
|
|
|
return |
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
main() |
|
|
|