Spaces:
Running
Running
Noise reduction working, bug in plosive detection algo
Browse files
app.py
CHANGED
@@ -1,37 +1,72 @@
|
|
1 |
import streamlit as st
|
2 |
import noisereduce as nr
|
3 |
-
|
4 |
-
from pedalboard import
|
5 |
import numpy as np
|
6 |
from io import BytesIO
|
7 |
-
|
8 |
-
# Function to remove background noise and process audio
|
9 |
|
10 |
|
11 |
def process_audio(input_file):
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
# Reduce stationary noise using noisereduce
|
16 |
-
reduced_noise = nr.reduce_noise(y=data, sr=sr)
|
17 |
|
18 |
-
#
|
19 |
-
|
20 |
-
|
21 |
-
compressor = Compressor(threshold_db=-25, ratio=3,
|
22 |
-
attack_ms=10, release_ms=200)
|
23 |
|
24 |
-
#
|
25 |
-
board =
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
|
28 |
-
processed_audio = board(np.array([reduced_noise]))
|
29 |
|
30 |
return processed_audio, sr
|
31 |
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
def main():
|
34 |
-
st.title("
|
35 |
|
36 |
# Upload audio file
|
37 |
uploaded_file = st.file_uploader("Upload audio file", type=["wav"])
|
@@ -42,23 +77,37 @@ def main():
|
|
42 |
processed_audio, sr = process_audio(uploaded_file)
|
43 |
|
44 |
# Display processing progress
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
#
|
52 |
-
st.subheader("Play Audio")
|
53 |
-
st.audio(uploaded_file, format='audio/wav',
|
54 |
-
|
55 |
-
st.audio(
|
56 |
-
start_time=0,
|
57 |
|
58 |
# Allow user to download processed audio
|
59 |
-
st.subheader("Download
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
|
64 |
if __name__ == "__main__":
|
|
|
1 |
import streamlit as st
|
2 |
import noisereduce as nr
|
3 |
+
from pedalboard.io import AudioFile
|
4 |
+
from pedalboard import *
|
5 |
import numpy as np
|
6 |
from io import BytesIO
|
7 |
+
import librosa
|
|
|
8 |
|
9 |
|
10 |
def process_audio(input_file):
|
11 |
+
sr = 44100
|
12 |
+
with AudioFile(input_file).resampled_to(sr) as f:
|
13 |
+
audio = f.read(f.frames)
|
|
|
|
|
14 |
|
15 |
+
# Reduce stationary noise
|
16 |
+
reduced_noise = nr.reduce_noise(
|
17 |
+
y=audio, sr=sr, stationary=True, prop_decrease=0.75)
|
|
|
|
|
18 |
|
19 |
+
# Apply audio effects
|
20 |
+
board = Pedalboard([
|
21 |
+
NoiseGate(threshold_db=-30, ratio=1.5, release_ms=250),
|
22 |
+
Compressor(threshold_db=-16, ratio=2.5),
|
23 |
+
LowShelfFilter(cutoff_frequency_hz=400, gain_db=10, q=1),
|
24 |
+
Gain(gain_db=10)
|
25 |
+
])
|
26 |
|
27 |
+
processed_audio = board(reduced_noise, sr)
|
|
|
28 |
|
29 |
return processed_audio, sr
|
30 |
|
31 |
|
32 |
+
def detect_plosives(audio, sr):
|
33 |
+
# Define frequency bands for plosive detection
|
34 |
+
frequency_bands = [(0, 200), (200, 500)]
|
35 |
+
|
36 |
+
# Calculate short-time Fourier transform (STFT)
|
37 |
+
stft = librosa.stft(audio)
|
38 |
+
|
39 |
+
# Convert amplitude to energy (power) spectrogram
|
40 |
+
power = np.abs(stft) ** 2
|
41 |
+
|
42 |
+
# Calculate energy in each frequency band
|
43 |
+
band_energies = []
|
44 |
+
for band in frequency_bands:
|
45 |
+
freq_range = librosa.core.fft_frequencies(sr=sr)
|
46 |
+
indices = np.where((freq_range >= band[0]) & (freq_range < band[1]))[0]
|
47 |
+
if len(indices) > 0: # Check if indices exist before proceeding
|
48 |
+
# Sum along frequency axis
|
49 |
+
band_energy = np.sum(power[indices, :], axis=0)
|
50 |
+
band_energies.append(band_energy)
|
51 |
+
|
52 |
+
if not band_energies: # If no bands have energy, return empty plosives
|
53 |
+
return []
|
54 |
+
|
55 |
+
# Calculate overall energy as the sum of energy in all bands
|
56 |
+
overall_energy = np.sum(band_energies, axis=0)
|
57 |
+
|
58 |
+
# Apply thresholding to detect plosives
|
59 |
+
threshold = np.max(overall_energy) * 0.5
|
60 |
+
plosive_frames = np.where(overall_energy > threshold)[0]
|
61 |
+
|
62 |
+
# Convert frame indices to time in seconds
|
63 |
+
plosives = librosa.frames_to_time(plosive_frames, sr=sr)
|
64 |
+
|
65 |
+
return plosives
|
66 |
+
|
67 |
+
|
68 |
def main():
|
69 |
+
st.title("Audio Enhancement")
|
70 |
|
71 |
# Upload audio file
|
72 |
uploaded_file = st.file_uploader("Upload audio file", type=["wav"])
|
|
|
77 |
processed_audio, sr = process_audio(uploaded_file)
|
78 |
|
79 |
# Display processing progress
|
80 |
+
st.write("Audio processed successfully!")
|
81 |
+
|
82 |
+
# Allow user to play original and processed audio
|
83 |
+
# st.subheader("Play Audio")
|
84 |
+
# st.audio(uploaded_file, format='audio/wav', start_time=0)
|
85 |
+
# st.audio(processed_audio, format='audio/wav',
|
86 |
+
# start_time=0, sample_rate=None)
|
87 |
+
st.subheader("Play Origional Audio")
|
88 |
+
st.audio(uploaded_file, format='audio/wav', start_time=0)
|
89 |
+
st.subheader("Play Enhanced Audio")
|
90 |
+
st.audio(processed_audio, format='audio/wav',
|
91 |
+
start_time=0, sample_rate=sr)
|
92 |
|
93 |
# Allow user to download processed audio
|
94 |
+
st.subheader("Download Enhanced Audio")
|
95 |
+
processed_audio_bytes = BytesIO()
|
96 |
+
with AudioFile(processed_audio_bytes, 'w', sr, len(processed_audio), format='wav') as f:
|
97 |
+
f.write(processed_audio)
|
98 |
+
st.download_button("Download", processed_audio_bytes.getvalue(),
|
99 |
+
file_name="processed_audio.wav", mime='audio/wav')
|
100 |
+
|
101 |
+
# Button to detect plosives
|
102 |
+
if st.button("Detect Plosives"):
|
103 |
+
st.write("Detecting plosives...")
|
104 |
+
plosives = detect_plosives(processed_audio, sr)
|
105 |
+
if plosives:
|
106 |
+
st.subheader("Detected Plosives")
|
107 |
+
st.write("Plosives detected at time(s):", ", ".join(
|
108 |
+
[str(round(p, 2)) for p in plosives]))
|
109 |
+
else:
|
110 |
+
st.write("No plosives detected.")
|
111 |
|
112 |
|
113 |
if __name__ == "__main__":
|