Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
audio = audio.set_channels(1)
|
121 |
audio = audio.set_frame_rate(SAMPLE_RATE)
|
122 |
|
123 |
-
#
|
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 |
-
#
|
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 |
-
#
|
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)
|