batra43pvd commited on
Commit
e46d13d
·
verified ·
1 Parent(s): c6a1de6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -4
app.py CHANGED
@@ -116,22 +116,29 @@ def process_audio_file(file_path):
116
  # 1. Dùng pydub để mở file audio (hỗ trợ nhiều định dạng)
117
  audio = AudioSegment.from_file(file_path)
118
 
119
- # 2. Đảm bảo audio mono (1 kênh) và có sample rate đúng
 
 
 
 
 
 
 
120
  audio = audio.set_channels(1)
121
  audio = audio.set_frame_rate(SAMPLE_RATE)
122
 
123
- # 3. Chuyển đổi audio của pydub thành mảng NumPy cho librosa
124
  # Chuẩn hóa về khoảng [-1, 1]
125
  samples = np.array(audio.get_array_of_samples()).astype(np.float32)
126
  y = samples / (2**(audio.sample_width * 8 - 1))
127
 
128
- # 4. Chuẩn hóa độ dài audio về MAX_SAMPLES
129
  if len(y) > MAX_SAMPLES:
130
  y = y[:MAX_SAMPLES]
131
  else:
132
  y = np.pad(y, (0, MAX_SAMPLES - len(y)), mode='constant')
133
 
134
- # 5. Trích xuất đồng thời các bộ đặc trưng (code này không đổi)
135
  traditional_features = _extract_traditional_features(y, SAMPLE_RATE)
136
  wav2vec_features = _extract_wav2vec_features(y, SAMPLE_RATE)
137
  spectrogram = _create_spectrogram_image(y, SAMPLE_RATE)
 
116
  # 1. Dùng pydub để mở file audio (hỗ trợ nhiều định dạng)
117
  audio = AudioSegment.from_file(file_path)
118
 
119
+ # 2. **BƯỚC MỚI: CHUẨN HÓA ÂM LƯỢNG**
120
+ # Chuẩn hóa âm lượng về một mức tiêu chuẩn (-20 dBFS).
121
+ # Điều này giúp giảm sự khác biệt về âm lượng giữa các bản ghi.
122
+ target_dbfs = -20.0
123
+ change_in_dbfs = target_dbfs - audio.dBFS
124
+ audio = audio.apply_gain(change_in_dbfs)
125
+
126
+ # 3. Đảm bảo audio là mono (1 kênh) và có sample rate đúng
127
  audio = audio.set_channels(1)
128
  audio = audio.set_frame_rate(SAMPLE_RATE)
129
 
130
+ # 4. Chuyển đổi audio của pydub thành mảng NumPy cho librosa
131
  # Chuẩn hóa về khoảng [-1, 1]
132
  samples = np.array(audio.get_array_of_samples()).astype(np.float32)
133
  y = samples / (2**(audio.sample_width * 8 - 1))
134
 
135
+ # 5. Chuẩn hóa độ dài audio về MAX_SAMPLES
136
  if len(y) > MAX_SAMPLES:
137
  y = y[:MAX_SAMPLES]
138
  else:
139
  y = np.pad(y, (0, MAX_SAMPLES - len(y)), mode='constant')
140
 
141
+ # 6. Trích xuất đồng thời các bộ đặc trưng (code này không đổi)
142
  traditional_features = _extract_traditional_features(y, SAMPLE_RATE)
143
  wav2vec_features = _extract_wav2vec_features(y, SAMPLE_RATE)
144
  spectrogram = _create_spectrogram_image(y, SAMPLE_RATE)