Spaces:
Sleeping
Sleeping
Update src/audio_preprocessing.py
Browse files- src/audio_preprocessing.py +11 -20
src/audio_preprocessing.py
CHANGED
|
@@ -155,28 +155,20 @@ def denoise_audio(input_audio_path):
|
|
| 155 |
assert isinstance(input_audio_path, str), "Input path must be a string"
|
| 156 |
output_audio_path = input_audio_path.replace(".wav", "_denoised.wav")
|
| 157 |
|
| 158 |
-
from pydub import AudioSegment
|
| 159 |
-
import numpy as np
|
| 160 |
-
from scipy.io import wavfile
|
| 161 |
-
|
| 162 |
try:
|
| 163 |
-
#
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
# Convert to mono
|
| 167 |
-
|
| 168 |
-
data = data.mean(axis=1)
|
| 169 |
-
|
| 170 |
-
# Convert to float32 and normalize
|
| 171 |
-
data = data.astype(np.float32) / np.iinfo(np.int16).max
|
| 172 |
|
| 173 |
-
#
|
| 174 |
-
|
| 175 |
-
|
| 176 |
|
| 177 |
-
# Run denoising
|
| 178 |
result = subprocess.run(
|
| 179 |
-
["denoise",
|
| 180 |
check=True,
|
| 181 |
capture_output=True,
|
| 182 |
text=True
|
|
@@ -184,8 +176,7 @@ def denoise_audio(input_audio_path):
|
|
| 184 |
print(result.stdout)
|
| 185 |
|
| 186 |
# Clean up
|
| 187 |
-
|
| 188 |
-
os.remove(temp_path)
|
| 189 |
|
| 190 |
except subprocess.CalledProcessError as e:
|
| 191 |
print(f"Error: {e}")
|
|
|
|
| 155 |
assert isinstance(input_audio_path, str), "Input path must be a string"
|
| 156 |
output_audio_path = input_audio_path.replace(".wav", "_denoised.wav")
|
| 157 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
try:
|
| 159 |
+
# Load audio and convert to required format
|
| 160 |
+
audio = AudioSegment.from_wav(input_audio_path)
|
| 161 |
+
audio = audio.set_frame_rate(48000) # Set to 48 kHz
|
| 162 |
+
audio = audio.set_channels(1) # Convert to mono
|
| 163 |
+
audio = audio.set_sample_width(2) # Set to 16-bit
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
+
# Export as RAW PCM
|
| 166 |
+
temp_raw = "temp_audio.raw"
|
| 167 |
+
audio.export(temp_raw, format="raw")
|
| 168 |
|
| 169 |
+
# Run denoising on the RAW PCM data
|
| 170 |
result = subprocess.run(
|
| 171 |
+
["denoise", temp_raw, output_audio_path, "--plot"],
|
| 172 |
check=True,
|
| 173 |
capture_output=True,
|
| 174 |
text=True
|
|
|
|
| 176 |
print(result.stdout)
|
| 177 |
|
| 178 |
# Clean up
|
| 179 |
+
os.remove(temp_raw)
|
|
|
|
| 180 |
|
| 181 |
except subprocess.CalledProcessError as e:
|
| 182 |
print(f"Error: {e}")
|