Spaces:
Running
Running
File size: 5,782 Bytes
1af34cd d32c7e7 1af34cd d32c7e7 1af34cd d32c7e7 1af34cd d32c7e7 1af34cd d32c7e7 1af34cd d32c7e7 1af34cd d32c7e7 1af34cd d32c7e7 1af34cd d32c7e7 1af34cd d32c7e7 1af34cd d32c7e7 1af34cd |
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import argparse
from glob import glob
import json
import os
from pathlib import Path
import random
import sys
from typing import List
pwd = os.path.abspath(os.path.dirname(__file__))
sys.path.append(os.path.join(pwd, "../../"))
import librosa
import numpy as np
from tqdm import tqdm
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--noise_patterns",
default=r"D:\Users\tianx\HuggingDatasets\nx_noise\data\noise\**\*.wav",
type=str
)
parser.add_argument(
"--speech_patterns",
default=r"D:\Users\tianx\HuggingDatasets\nx_noise\data\speech\**\*.wav",
type=str
)
parser.add_argument("--train_dataset", default="train.jsonl", type=str)
parser.add_argument("--valid_dataset", default="valid.jsonl", type=str)
parser.add_argument("--duration", default=2.0, type=float)
parser.add_argument("--min_snr_db", default=-10, type=float)
parser.add_argument("--max_snr_db", default=20, type=float)
parser.add_argument("--target_sample_rate", default=8000, type=int)
parser.add_argument("--max_count", default=-1, type=int)
args = parser.parse_args()
return args
def filename_generator(data_dir: str):
data_dir = Path(data_dir)
for filename in data_dir.glob("**/*.wav"):
yield filename.as_posix()
def target_second_signal_generator(filename_patterns: List[str],
duration: int = 2,
sample_rate: int = 8000,
max_epoch: int = 20000
):
for epoch_idx in range(max_epoch):
for filename_pattern in filename_patterns:
for filename in glob(filename_pattern, recursive=True):
signal, _ = librosa.load(filename, sr=sample_rate)
raw_duration = librosa.get_duration(y=signal, sr=sample_rate)
if raw_duration < duration:
# print(f"duration less than {duration} s. skip filename: {filename.as_posix()}")
continue
if signal.ndim != 1:
raise AssertionError(f"expected ndim 1, instead of {signal.ndim}")
signal_length = len(signal)
win_size = int(duration * sample_rate)
for begin in range(0, signal_length - win_size, win_size):
if np.sum(signal[begin: begin+win_size]) == 0:
continue
row = {
"epoch_idx": epoch_idx,
"filename": filename,
"raw_duration": round(raw_duration, 4),
"offset": round(begin / sample_rate, 4),
"duration": round(duration, 4),
}
yield row
def main():
args = get_args()
noise_patterns = args.noise_patterns
noise_patterns = noise_patterns.split(" ")
print(f"noise_patterns: {noise_patterns}")
speech_patterns = args.speech_patterns
speech_patterns = speech_patterns.split(" ")
print(f"speech_patterns: {speech_patterns}")
train_dataset = Path(args.train_dataset)
valid_dataset = Path(args.valid_dataset)
train_dataset.parent.mkdir(parents=True, exist_ok=True)
valid_dataset.parent.mkdir(parents=True, exist_ok=True)
noise_generator = target_second_signal_generator(
noise_patterns,
duration=args.duration,
sample_rate=args.target_sample_rate,
max_epoch=100000,
)
speech_generator = target_second_signal_generator(
speech_patterns,
duration=args.duration,
sample_rate=args.target_sample_rate,
max_epoch=1,
)
count = 0
process_bar = tqdm(desc="build dataset jsonl")
with open(args.train_dataset, "w", encoding="utf-8") as ftrain, open(args.valid_dataset, "w", encoding="utf-8") as fvalid:
for noise, speech in zip(noise_generator, speech_generator):
if count >= args.max_count > 0:
break
noise_filename = noise["filename"]
noise_raw_duration = noise["raw_duration"]
noise_offset = noise["offset"]
noise_duration = noise["duration"]
speech_filename = speech["filename"]
speech_raw_duration = speech["raw_duration"]
speech_offset = speech["offset"]
speech_duration = speech["duration"]
random1 = random.random()
random2 = random.random()
row = {
"count": count,
"noise_filename": noise_filename,
"noise_raw_duration": noise_raw_duration,
"noise_offset": noise_offset,
"noise_duration": noise_duration,
"speech_filename": speech_filename,
"speech_raw_duration": speech_raw_duration,
"speech_offset": speech_offset,
"speech_duration": speech_duration,
"snr_db": random.uniform(args.min_snr_db, args.max_snr_db),
"random1": random1,
}
row = json.dumps(row, ensure_ascii=False)
if random2 < (1 / 300 / 1):
fvalid.write(f"{row}\n")
else:
ftrain.write(f"{row}\n")
count += 1
duration_seconds = count * args.duration
duration_hours = duration_seconds / 3600
process_bar.update(n=1)
process_bar.set_postfix({
# "duration_seconds": round(duration_seconds, 4),
"duration_hours": round(duration_hours, 4),
})
return
if __name__ == "__main__":
main()
|