File size: 2,008 Bytes
abf9c24
 
 
 
 
 
d775ebe
abf9c24
 
 
 
404c102
d775ebe
abf9c24
 
d775ebe
abf9c24
d775ebe
 
abf9c24
d775ebe
abf9c24
 
 
d194fbb
 
abf9c24
 
d775ebe
abf9c24
 
 
d775ebe
 
 
 
abf9c24
404c102
d775ebe
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
from os.path import join as p_join
from tqdm import tqdm
from typing import Dict
from glob import glob
import soundfile as sf

from datasets import Audio

# dataset config
direction = os.getenv("DIRECTION", "enA-jpn")
direction_speech = os.getenv("DIRECTION_SPEECH", "enA")
cache_dir_audio = p_join("download", "audio", direction)
cache_dir_feature = p_join("download", "feature", direction)
cache_dir_audio_fixed = p_join("download", "audio_fixed", direction, direction_speech)
os.makedirs(cache_dir_feature, exist_ok=True)
os.makedirs(cache_dir_audio, exist_ok=True)
os.makedirs(cache_dir_audio_fixed, exist_ok=True)
line_no_start = int(os.getenv("LINE_NO_START", 0))
line_no_end = int(os.getenv("LINE_NO_END", 100))


def loader(feature: str) -> Dict:
    with open(feature) as f_reader:
        return json.load(f_reader)


# feature dictionary
files = {
    int(os.path.basename(i).replace(".json", "")): i for i in glob(p_join(cache_dir_feature, "*.json"))
}
# feature files
files_list = [k for k in files.keys() if line_no_start <= k <= line_no_end]
fixed_audio_list = [int(os.path.basename(i).replace(".mp3", "")) for i in glob(p_join(cache_dir_audio_fixed, "*"))]
# remove broken audio files
audio_loader = Audio()
index_list = [i for i in list(range(line_no_start, line_no_end)) if i in files_list and i not in fixed_audio_list]
print(f"filtering {len(index_list)} files....")
for i in tqdm(index_list):
    features = loader(files[i])
    audio_file = features[f"{direction_speech}.path"]
    start, end = features[f"{direction_speech}.duration_start"], features[f"{direction_speech}.duration_end"]
    if os.path.exists(audio_file):
        try:
            wav = audio_loader.decode_example({"path": audio_file, "bytes": None})
            if start < end < len(wav["array"]):
                sf.write(p_join(cache_dir_audio_fixed, f"{i}.mp3"), wav["array"][start:end], wav["sampling_rate"])
        except Exception as e:
            print(e)
        os.remove(audio_file)