mfrng commited on
Commit
10d92be
·
verified ·
1 Parent(s): 2ed41fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -11
app.py CHANGED
@@ -59,21 +59,15 @@ def compute_snr(original, enhanced):
59
  return snr
60
 
61
  def vad_plot(y, sr, title):
62
- # webrtcvad requires 16-bit mono PCM, sample rate 16000, 10/20/30 ms chunks
63
- import webrtcvad
64
- import numpy as np
65
-
66
  vad = webrtcvad.Vad(2)
67
 
68
  if sr != 16000:
69
- import librosa
70
  y = librosa.resample(y, orig_sr=sr, target_sr=16000)
71
  sr = 16000
72
 
73
- frame_duration_ms = 30 # Can be 10, 20, or 30
74
- frame_size = int(sr * frame_duration_ms / 1000) # samples per frame
75
 
76
- # Pad signal to be multiple of frame_size
77
  if len(y) % frame_size != 0:
78
  pad_len = frame_size - (len(y) % frame_size)
79
  y = np.pad(y, (0, pad_len))
@@ -85,11 +79,21 @@ def vad_plot(y, sr, title):
85
  pcm = (frame * 32767).astype(np.int16).tobytes()
86
  try:
87
  voiced.append(vad.is_speech(pcm, sr))
88
- except Exception as e:
89
- print("VAD error:", e)
90
  voiced.append(False)
91
 
92
- return voiced
 
 
 
 
 
 
 
 
 
 
 
93
 
94
  def compute_pesq_mfcc_stoi(original_path, enhanced_path):
95
  sr = 16000
 
59
  return snr
60
 
61
  def vad_plot(y, sr, title):
 
 
 
 
62
  vad = webrtcvad.Vad(2)
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
+ frame_size = int(sr * frame_duration_ms / 1000)
70
 
 
71
  if len(y) % frame_size != 0:
72
  pad_len = frame_size - (len(y) % frame_size)
73
  y = np.pad(y, (0, pad_len))
 
79
  pcm = (frame * 32767).astype(np.int16).tobytes()
80
  try:
81
  voiced.append(vad.is_speech(pcm, sr))
82
+ except Exception:
 
83
  voiced.append(False)
84
 
85
+ # Plot the VAD decisions
86
+ plt.figure(figsize=(10, 1.5))
87
+ plt.plot(voiced, drawstyle='steps-mid')
88
+ plt.title(title)
89
+ plt.xlabel("Frame Index")
90
+ plt.ylabel("Speech")
91
+ plt.tight_layout()
92
+ buf = io.BytesIO()
93
+ plt.savefig(buf, format='png')
94
+ plt.close()
95
+ buf.seek(0)
96
+ return buf
97
 
98
  def compute_pesq_mfcc_stoi(original_path, enhanced_path):
99
  sr = 16000