Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -59,59 +59,74 @@ def compute_snr(original, enhanced):
|
|
| 59 |
return snr
|
| 60 |
|
| 61 |
def vad_plot(y, sr, title):
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
if sr != 16000:
|
| 65 |
-
y = librosa.resample(y, orig_sr=sr, target_sr=16000)
|
| 66 |
-
sr = 16000
|
| 67 |
-
|
| 68 |
frame_duration_ms = 30
|
| 69 |
-
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
|
|
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
try:
|
| 81 |
-
voiced_flags.append(vad.is_speech(pcm, sr))
|
| 82 |
-
except Exception:
|
| 83 |
-
voiced_flags.append(False)
|
| 84 |
|
| 85 |
-
|
|
|
|
| 86 |
|
| 87 |
-
|
| 88 |
-
librosa.
|
| 89 |
|
| 90 |
-
# Merge
|
| 91 |
intervals = []
|
| 92 |
start = None
|
| 93 |
-
for i, voiced in enumerate(
|
| 94 |
if voiced and start is None:
|
| 95 |
start = times[i]
|
| 96 |
elif not voiced and start is not None:
|
| 97 |
intervals.append((start, times[i]))
|
| 98 |
start = None
|
| 99 |
-
# Catch if last frames are voiced
|
| 100 |
if start is not None:
|
| 101 |
-
intervals.append((start, times[-1]
|
| 102 |
|
| 103 |
-
# Plot
|
|
|
|
|
|
|
| 104 |
for (start_t, end_t) in intervals:
|
| 105 |
-
plt.axvspan(start_t, end_t, color='
|
| 106 |
-
|
| 107 |
-
plt.title(title + " (Voice regions shaded)")
|
| 108 |
plt.tight_layout()
|
|
|
|
| 109 |
buf = io.BytesIO()
|
| 110 |
plt.savefig(buf, format='png')
|
| 111 |
plt.close()
|
| 112 |
buf.seek(0)
|
| 113 |
return buf
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
def amplify_voice(y, target_db=-20):
|
| 116 |
rms = np.sqrt(np.mean(y**2))
|
| 117 |
if rms > 0:
|
|
@@ -195,6 +210,7 @@ def process_files(
|
|
| 195 |
if reverb_clean:
|
| 196 |
y = reverb_cleanup(y, sr)
|
| 197 |
if vol_norm:
|
|
|
|
| 198 |
y = volume_normalize(y)
|
| 199 |
if lang_tune:
|
| 200 |
y = language_aware_tuning(y, sr)
|
|
|
|
| 59 |
return snr
|
| 60 |
|
| 61 |
def vad_plot(y, sr, title):
|
| 62 |
+
# Parameters
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
frame_duration_ms = 30
|
| 64 |
+
hop_length = int(sr * frame_duration_ms / 1000)
|
| 65 |
+
n_fft = 2048
|
| 66 |
|
| 67 |
+
# Compute STFT
|
| 68 |
+
S = np.abs(librosa.stft(y, n_fft=n_fft, hop_length=hop_length)) ** 2
|
| 69 |
+
freqs = librosa.fft_frequencies(sr=sr, n_fft=n_fft)
|
| 70 |
|
| 71 |
+
# Focus on 80–3000 Hz (speech band)
|
| 72 |
+
speech_band = np.where((freqs >= 80) & (freqs <= 3000))[0]
|
| 73 |
+
speech_energy = S[speech_band, :].mean(axis=0)
|
| 74 |
|
| 75 |
+
# Normalize energy
|
| 76 |
+
speech_energy /= np.max(speech_energy) + 1e-6
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
# Threshold for voice activity (tune as needed)
|
| 79 |
+
voice_mask = speech_energy > 0.1
|
| 80 |
|
| 81 |
+
# Time axis for plotting
|
| 82 |
+
times = librosa.frames_to_time(np.arange(len(voice_mask)), sr=sr, hop_length=hop_length)
|
| 83 |
|
| 84 |
+
# Merge voiced intervals
|
| 85 |
intervals = []
|
| 86 |
start = None
|
| 87 |
+
for i, voiced in enumerate(voice_mask):
|
| 88 |
if voiced and start is None:
|
| 89 |
start = times[i]
|
| 90 |
elif not voiced and start is not None:
|
| 91 |
intervals.append((start, times[i]))
|
| 92 |
start = None
|
|
|
|
| 93 |
if start is not None:
|
| 94 |
+
intervals.append((start, times[-1]))
|
| 95 |
|
| 96 |
+
# Plot waveform + shaded voice regions
|
| 97 |
+
plt.figure(figsize=(10, 2))
|
| 98 |
+
librosa.display.waveshow(y, sr=sr, alpha=0.6)
|
| 99 |
for (start_t, end_t) in intervals:
|
| 100 |
+
plt.axvspan(start_t, end_t, color='green', alpha=0.3)
|
| 101 |
+
plt.title(title + " (Voice Regions: 80–3000Hz energy)")
|
|
|
|
| 102 |
plt.tight_layout()
|
| 103 |
+
|
| 104 |
buf = io.BytesIO()
|
| 105 |
plt.savefig(buf, format='png')
|
| 106 |
plt.close()
|
| 107 |
buf.seek(0)
|
| 108 |
return buf
|
| 109 |
|
| 110 |
+
def amplify_voice_fft(y, sr, gain_db=10):
|
| 111 |
+
# Short-Time Fourier Transform
|
| 112 |
+
hop_length = 512
|
| 113 |
+
D = librosa.stft(y, n_fft=2048, hop_length=hop_length)
|
| 114 |
+
mag, phase = np.abs(D), np.angle(D)
|
| 115 |
+
|
| 116 |
+
freqs = librosa.fft_frequencies(sr=sr, n_fft=2048)
|
| 117 |
+
voice_band = np.where((freqs >= 80) & (freqs <= 3000))[0]
|
| 118 |
+
|
| 119 |
+
# Convert gain from dB to amplitude
|
| 120 |
+
gain_amp = 10 ** (gain_db / 20.0)
|
| 121 |
+
|
| 122 |
+
# Amplify only the voice frequency band
|
| 123 |
+
mag[voice_band, :] *= gain_amp
|
| 124 |
+
|
| 125 |
+
# Reconstruct
|
| 126 |
+
D_new = mag * np.exp(1j * phase)
|
| 127 |
+
y_out = librosa.istft(D_new, hop_length=hop_length)
|
| 128 |
+
return y_out
|
| 129 |
+
|
| 130 |
def amplify_voice(y, target_db=-20):
|
| 131 |
rms = np.sqrt(np.mean(y**2))
|
| 132 |
if rms > 0:
|
|
|
|
| 210 |
if reverb_clean:
|
| 211 |
y = reverb_cleanup(y, sr)
|
| 212 |
if vol_norm:
|
| 213 |
+
y = amplify_voice_fft(y, sr, gain_db=8)
|
| 214 |
y = volume_normalize(y)
|
| 215 |
if lang_tune:
|
| 216 |
y = language_aware_tuning(y, sr)
|