File size: 1,641 Bytes
1ddca60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import glob
import os
import subprocess
from multiprocessing import Pool
from pathlib import Path
from shutil import move

from tqdm import tqdm


def convert_sr(audio_path: str) -> None:
    """
    Chuyển đổi tần số lấy mẫu của file âm thanh thành 24kHz.
    """
    audio_path = Path(audio_path)
    output_path = audio_path.with_name(f"{audio_path.stem}_24k.wav")
    subprocess.run(
        ["sox", str(audio_path), "-r", "24000", "-c", "1", str(output_path)],
        check=True
    )


def remove_original(audio_path: str) -> None:
    """
    Xóa file gốc nếu nó không phải là file đã được chuyển đổi sang 24kHz.
    """
    if "_24k.wav" not in audio_path:
        os.remove(audio_path)


def rename_audio(audio_path: str) -> None:
    """
    Xóa hậu tố '_24k' khỏi tên file để đặt lại tên cho đúng chuẩn.
    """
    audio_path = Path(audio_path)
    new_path = audio_path.with_name(audio_path.stem.replace("_24k", "") + ".wav")
    move(audio_path, new_path)


def process_audio_files(function, wav_paths):
    """
    Xử lý các file âm thanh với hàm được chỉ định sử dụng đa luồng.
    """
    with Pool(processes=16) as pool:
        list(tqdm(pool.imap(function, wav_paths), total=len(wav_paths)))


if __name__ == "__main__":
    dataset_path = "data/your_dataset/*.wav"

    # Chuyển đổi sample rate
    process_audio_files(convert_sr, glob.glob(dataset_path))

    # Xóa file gốc
    process_audio_files(remove_original, glob.glob(dataset_path))

    # Đổi tên file
    process_audio_files(rename_audio, glob.glob(dataset_path))