Spaces:
Running
Running
Update preprocessing.py
Browse files- preprocessing.py +80 -17
preprocessing.py
CHANGED
@@ -3,24 +3,87 @@ from pydub import AudioSegment
|
|
3 |
import noisereduce as nr
|
4 |
import webrtcvad
|
5 |
from ..config import settings
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
def clean_audio(
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
def _apply_vad(audio: AudioSegment, vad: webrtcvad.Vad) -> AudioSegment:
|
26 |
frame_duration = 30
|
|
|
3 |
import noisereduce as nr
|
4 |
import webrtcvad
|
5 |
from ..config import settings
|
6 |
+
import numpy as np
|
7 |
+
import librosa
|
8 |
+
import soundfile as sf
|
9 |
+
from typing import Optional
|
10 |
|
11 |
+
def clean_audio(input_file: str, output_file: Optional[str] = None) -> str:
|
12 |
+
"""
|
13 |
+
Ses dosyasını temizleyerek gürültüyü azaltır ve kaliteyi artırır
|
14 |
+
|
15 |
+
Args:
|
16 |
+
input_file: Girdi ses dosyasının yolu
|
17 |
+
output_file: Çıktı ses dosyasının yolu (None ise otomatik oluşturulur)
|
18 |
+
|
19 |
+
Returns:
|
20 |
+
Temizlenmiş ses dosyasının yolu
|
21 |
+
"""
|
22 |
+
# Çıktı dosya adını oluştur
|
23 |
+
if output_file is None:
|
24 |
+
output_file = input_file.replace('.wav', '_cleaned.wav')
|
25 |
+
|
26 |
+
try:
|
27 |
+
# Ses dosyasını yükle
|
28 |
+
audio, sr = librosa.load(input_file, sr=None)
|
29 |
+
|
30 |
+
# 1. Gürültü azaltma
|
31 |
+
reduced_noise = nr.reduce_noise(y=audio, sr=sr, stationary=True)
|
32 |
+
|
33 |
+
# 2. Ses seviyesi normalizasyonu
|
34 |
+
normalized = librosa.util.normalize(reduced_noise)
|
35 |
+
|
36 |
+
# 3. Sessiz bölümleri kırp
|
37 |
+
non_silent_intervals = librosa.effects.split(normalized, top_db=30)
|
38 |
+
if len(non_silent_intervals) > 0:
|
39 |
+
non_silent_audio = np.concatenate([
|
40 |
+
normalized[start:end] for start, end in non_silent_intervals
|
41 |
+
])
|
42 |
+
# Çok kısa ise orijinali kullan
|
43 |
+
if len(non_silent_audio) > 0.5 * len(normalized):
|
44 |
+
normalized = non_silent_audio
|
45 |
+
|
46 |
+
# 4. Band-pass filtresi (insan sesi için optimize - 80Hz-8000Hz)
|
47 |
+
y_filter = librosa.effects.preemphasis(normalized)
|
48 |
+
|
49 |
+
# Temizlenmiş sesi kaydet
|
50 |
+
sf.write(output_file, y_filter, sr)
|
51 |
+
|
52 |
+
return output_file
|
53 |
+
|
54 |
+
except Exception as e:
|
55 |
+
print(f"Ses temizleme hatası: {str(e)}")
|
56 |
+
# Hata durumunda orijinal dosyayı döndür
|
57 |
+
return input_file
|
58 |
+
|
59 |
+
def clean_audio_streaming(input_file: str, output_file: Optional[str] = None, chunk_seconds: int = 30) -> str:
|
60 |
+
"""Büyük dosyaları parça parça işle"""
|
61 |
+
if output_file is None:
|
62 |
+
output_file = input_file.replace('.wav', '_cleaned.wav')
|
63 |
+
|
64 |
+
try:
|
65 |
+
# Temel bilgileri al
|
66 |
+
y, sr = librosa.load(input_file, sr=None, duration=10) # Sadece örnek rate için
|
67 |
+
|
68 |
+
# Çıktı dosyasını hazırla
|
69 |
+
with sf.SoundFile(output_file, 'w', sr, channels=1) as out_f:
|
70 |
+
# Dosyayı parça parça işle
|
71 |
+
for i, (audio_chunk, sr) in enumerate(librosa.stream(input_file,
|
72 |
+
block_length=chunk_seconds,
|
73 |
+
frame_length=2048,
|
74 |
+
hop_length=512)):
|
75 |
+
# Her parça için gürültü azaltma
|
76 |
+
reduced_noise = nr.reduce_noise(y=audio_chunk, sr=sr)
|
77 |
+
# Normalize
|
78 |
+
normalized = librosa.util.normalize(reduced_noise)
|
79 |
+
# Yaz
|
80 |
+
out_f.write(normalized)
|
81 |
+
|
82 |
+
return output_file
|
83 |
+
|
84 |
+
except Exception as e:
|
85 |
+
print(f"Ses temizleme hatası: {str(e)}")
|
86 |
+
return input_file
|
87 |
|
88 |
def _apply_vad(audio: AudioSegment, vad: webrtcvad.Vad) -> AudioSegment:
|
89 |
frame_duration = 30
|