Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -73,21 +73,25 @@ def vad_plot(y, sr, title):
|
|
| 73 |
y = np.pad(y, (0, pad_len))
|
| 74 |
|
| 75 |
frames = np.split(y, len(y) // frame_size)
|
| 76 |
-
|
| 77 |
|
| 78 |
for frame in frames:
|
| 79 |
pcm = (frame * 32767).astype(np.int16).tobytes()
|
| 80 |
try:
|
| 81 |
-
|
| 82 |
except Exception:
|
| 83 |
-
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
plt.
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
plt.tight_layout()
|
| 92 |
buf = io.BytesIO()
|
| 93 |
plt.savefig(buf, format='png')
|
|
@@ -95,6 +99,14 @@ def vad_plot(y, sr, title):
|
|
| 95 |
buf.seek(0)
|
| 96 |
return buf
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
def compute_pesq_mfcc_stoi(original_path, enhanced_path):
|
| 99 |
sr = 16000
|
| 100 |
original, _ = librosa.load(original_path, sr=sr)
|
|
@@ -174,6 +186,9 @@ def process_files(
|
|
| 174 |
if lang_tune:
|
| 175 |
y = language_aware_tuning(y, sr)
|
| 176 |
|
|
|
|
|
|
|
|
|
|
| 177 |
# Extract extension and construct filenames
|
| 178 |
base_name, ext = os.path.splitext(file_obj.name)
|
| 179 |
ext = ext.lower()
|
|
|
|
| 73 |
y = np.pad(y, (0, pad_len))
|
| 74 |
|
| 75 |
frames = np.split(y, len(y) // frame_size)
|
| 76 |
+
voiced_flags = []
|
| 77 |
|
| 78 |
for frame in frames:
|
| 79 |
pcm = (frame * 32767).astype(np.int16).tobytes()
|
| 80 |
try:
|
| 81 |
+
voiced_flags.append(vad.is_speech(pcm, sr))
|
| 82 |
except Exception:
|
| 83 |
+
voiced_flags.append(False)
|
| 84 |
|
| 85 |
+
times = np.arange(len(voiced_flags)) * frame_duration_ms / 1000.0
|
| 86 |
+
|
| 87 |
+
plt.figure(figsize=(10, 2))
|
| 88 |
+
librosa.display.waveshow(y, sr=sr, alpha=0.6)
|
| 89 |
+
|
| 90 |
+
for i, is_voiced in enumerate(voiced_flags):
|
| 91 |
+
if is_voiced:
|
| 92 |
+
plt.axvspan(times[i], times[i] + frame_duration_ms / 1000.0, color='red', alpha=0.3)
|
| 93 |
+
|
| 94 |
+
plt.title(title + " (Voice regions shaded)")
|
| 95 |
plt.tight_layout()
|
| 96 |
buf = io.BytesIO()
|
| 97 |
plt.savefig(buf, format='png')
|
|
|
|
| 99 |
buf.seek(0)
|
| 100 |
return buf
|
| 101 |
|
| 102 |
+
def amplify_voice(y, target_db=-20):
|
| 103 |
+
rms = np.sqrt(np.mean(y**2))
|
| 104 |
+
if rms > 0:
|
| 105 |
+
current_db = 20 * np.log10(rms)
|
| 106 |
+
gain = 10 ** ((target_db - current_db) / 20)
|
| 107 |
+
y = y * gain
|
| 108 |
+
return y
|
| 109 |
+
|
| 110 |
def compute_pesq_mfcc_stoi(original_path, enhanced_path):
|
| 111 |
sr = 16000
|
| 112 |
original, _ = librosa.load(original_path, sr=sr)
|
|
|
|
| 186 |
if lang_tune:
|
| 187 |
y = language_aware_tuning(y, sr)
|
| 188 |
|
| 189 |
+
# Amplify voice as final step
|
| 190 |
+
y = amplify_voice(y)
|
| 191 |
+
|
| 192 |
# Extract extension and construct filenames
|
| 193 |
base_name, ext = os.path.splitext(file_obj.name)
|
| 194 |
ext = ext.lower()
|