-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdenoise2.py
More file actions
71 lines (59 loc) · 2.58 KB
/
denoise2.py
File metadata and controls
71 lines (59 loc) · 2.58 KB
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
import librosa
import soundfile as sf
import pyloudnorm as pyln
import numpy as np
from pathlib import Path
from concurrent.futures import ProcessPoolExecutor
from tqdm import tqdm
import warnings
# Suppress librosa warnings about PySoundFile for clean terminal output
warnings.filterwarnings('ignore')
# --- CONFIGURATION ---
INPUT_DIR = Path("data/Kashmiri/wavs_clean") # From your denoising step
OUTPUT_DIR = Path("data/Kashmiri/wavs_final") # The final destination
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
TARGET_LUFS = -23.0
TARGET_SR = 22050 # Matcha-TTS LJSpeech config expects 22.05 kHz
def process_file(wav_path):
try:
# 1. Load audio and force sample rate to 22050Hz (Matcha-TTS standard)
y, sr = librosa.load(wav_path, sr=TARGET_SR, mono=True)
# 2. Trim Silence
# top_db=40 means anything 40 decibels quieter than the peak volume is considered silence
y_trimmed, _ = librosa.effects.trim(y, top_db=40, frame_length=1024, hop_length=256)
# Skip if the file is completely empty or just a micro-glitch (less than 0.2 seconds)
if len(y_trimmed) < sr * 0.2:
return f"Skipped (Too Short): {wav_path.name}"
# 3. Volume Normalization (LUFS)
meter = pyln.Meter(sr)
loudness = meter.integrated_loudness(y_trimmed)
# Prevent math errors on pure silence clips that slipped through
if np.isinf(loudness):
return f"Skipped (Infinite Loudness): {wav_path.name}"
y_normalized = pyln.normalize.loudness(y_trimmed, loudness, TARGET_LUFS)
# 4. Save Final Audio
out_path = OUTPUT_DIR / wav_path.name
sf.write(out_path, y_normalized, sr)
return None # Success
except Exception as e:
return f"Error on {wav_path.name}: {str(e)}"
def main():
wav_files = list(INPUT_DIR.glob("*.wav"))
print(f"[*] Found {len(wav_files)} files. Starting Multiprocessing (8 Cores)...")
errors = []
# Process files across all available CPU cores
with ProcessPoolExecutor(max_workers=8) as executor:
# Use tqdm to show a progress bar
results = list(tqdm(executor.map(process_file, wav_files), total=len(wav_files)))
# Collect and print any skips/errors
for r in results:
if r is not None:
errors.append(r)
print(f"\n[*] Processing Complete! Saved to {OUTPUT_DIR}")
if errors:
print(f"[*] Note: {len(errors)} files were skipped or had errors.")
# Print first 5 errors as a sample
for e in errors[:5]:
print(f" - {e}")
if __name__ == "__main__":
main()