Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import numpy as np
|
3 |
+
from pydub import AudioSegment
|
4 |
+
import streamlit as st
|
5 |
+
from io import BytesIO
|
6 |
+
|
7 |
+
# Noise generation functions
|
8 |
+
def generate_gaussian_noise(duration, sample_rate=44100, std_dev=0.1):
|
9 |
+
"""Generate Gaussian noise."""
|
10 |
+
samples = np.random.normal(0, std_dev, size=(duration * sample_rate,))
|
11 |
+
audio_segment = AudioSegment(
|
12 |
+
samples.tobytes(),
|
13 |
+
frame_rate=sample_rate,
|
14 |
+
sample_width=2, # 16-bit audio
|
15 |
+
channels=1
|
16 |
+
)
|
17 |
+
return audio_segment
|
18 |
+
|
19 |
+
def generate_impulse_noise(duration, sample_rate=44100, impulse_rate=0.05):
|
20 |
+
"""Generate impulse noise."""
|
21 |
+
samples = np.zeros(int(duration * sample_rate))
|
22 |
+
num_impulses = int(sample_rate * duration * impulse_rate)
|
23 |
+
impulse_indices = np.random.randint(0, len(samples), num_impulses)
|
24 |
+
samples[impulse_indices] = np.random.uniform(-1, 1, size=num_impulses)
|
25 |
+
|
26 |
+
audio_segment = AudioSegment(
|
27 |
+
samples.tobytes(),
|
28 |
+
frame_rate=sample_rate,
|
29 |
+
sample_width=2, # 16-bit audio
|
30 |
+
channels=1
|
31 |
+
)
|
32 |
+
return audio_segment
|
33 |
+
|
34 |
+
def add_noise(audio_file, noise_type='gaussian', noise_level=0.1, start_time=None, end_time=None):
|
35 |
+
"""Add noise to a clean audio file."""
|
36 |
+
audio = AudioSegment.from_file(audio_file)
|
37 |
+
duration = len(audio)
|
38 |
+
|
39 |
+
# Check for specific duration
|
40 |
+
if start_time is not None and end_time is not None:
|
41 |
+
start_time = int(start_time * 1000)
|
42 |
+
end_time = int(end_time * 1000)
|
43 |
+
if start_time < 0 or end_time > duration or start_time >= end_time:
|
44 |
+
st.error("Invalid start or end time.")
|
45 |
+
return None
|
46 |
+
noise_duration = (end_time - start_time) // 1000
|
47 |
+
else:
|
48 |
+
start_time = 0
|
49 |
+
end_time = duration
|
50 |
+
noise_duration = duration // 1000
|
51 |
+
|
52 |
+
# Generate noise
|
53 |
+
if noise_type == 'gaussian':
|
54 |
+
noise = generate_gaussian_noise(noise_duration)
|
55 |
+
elif noise_type == 'impulse':
|
56 |
+
noise = generate_impulse_noise(noise_duration)
|
57 |
+
else:
|
58 |
+
st.error("Invalid noise type.")
|
59 |
+
return None
|
60 |
+
|
61 |
+
# Normalize noise
|
62 |
+
noise_array = np.array(noise.get_array_of_samples())
|
63 |
+
noise_array = noise_array * noise_level / np.max(np.abs(noise_array))
|
64 |
+
noise_array = np.clip(noise_array, -1.0, 1.0)
|
65 |
+
|
66 |
+
noise = AudioSegment(
|
67 |
+
noise_array.tobytes(),
|
68 |
+
frame_rate=noise.frame_rate,
|
69 |
+
sample_width=noise.sample_width,
|
70 |
+
channels=noise.channels
|
71 |
+
)
|
72 |
+
|
73 |
+
# Overlay noise for the specified segment
|
74 |
+
silence = AudioSegment.silent(duration=end_time - start_time)
|
75 |
+
noisy_segment = silence.overlay(noise)
|
76 |
+
|
77 |
+
noisy_audio = audio[:start_time] + noisy_segment + audio[end_time:]
|
78 |
+
return noisy_audio
|
79 |
+
|
80 |
+
# Streamlit App
|
81 |
+
def main():
|
82 |
+
st.title("🎵 Audio Noise Generator")
|
83 |
+
st.sidebar.title("Instructions")
|
84 |
+
st.sidebar.write("""
|
85 |
+
- Upload one or more audio files (.wav or .mp3).
|
86 |
+
- Choose noise type (Gaussian or Impulse).
|
87 |
+
- Add noise to the full audio or a specific time duration.
|
88 |
+
- Save the processed audio files.
|
89 |
+
""")
|
90 |
+
|
91 |
+
# Upload files
|
92 |
+
uploaded_files = st.file_uploader("Upload Audio Files", type=["wav", "mp3"], accept_multiple_files=True)
|
93 |
+
|
94 |
+
# Noise type selection
|
95 |
+
noise_type = st.selectbox("Select Noise Type", ["gaussian", "impulse"])
|
96 |
+
|
97 |
+
# Noise level
|
98 |
+
noise_level = st.slider("Select Noise Level", 0.01, 1.0, 0.1, 0.01)
|
99 |
+
|
100 |
+
# Duration selection
|
101 |
+
add_to_specific_duration = st.checkbox("Add noise to specific duration")
|
102 |
+
if add_to_specific_duration:
|
103 |
+
start_time = st.number_input("Start Time (in seconds)", min_value=0.0, value=0.0, step=0.1)
|
104 |
+
end_time = st.number_input("End Time (in seconds)", min_value=0.1, value=1.0, step=0.1)
|
105 |
+
else:
|
106 |
+
start_time, end_time = None, None
|
107 |
+
|
108 |
+
# Process and save files
|
109 |
+
if st.button("Process Audio"):
|
110 |
+
if uploaded_files:
|
111 |
+
noisy_audios = {}
|
112 |
+
for uploaded_file in uploaded_files:
|
113 |
+
st.write(f"Processing {uploaded_file.name}...")
|
114 |
+
audio_data = BytesIO(uploaded_file.read())
|
115 |
+
|
116 |
+
noisy_audio = add_noise(audio_data, noise_type, noise_level, start_time, end_time)
|
117 |
+
if noisy_audio:
|
118 |
+
# Save to in-memory buffer
|
119 |
+
buffer = BytesIO()
|
120 |
+
noisy_audio.export(buffer, format="wav")
|
121 |
+
buffer.seek(0)
|
122 |
+
noisy_audios[uploaded_file.name] = buffer
|
123 |
+
st.success(f"Added {noise_type} noise to {uploaded_file.name}.")
|
124 |
+
|
125 |
+
if noisy_audios:
|
126 |
+
st.write("### Download Processed Files")
|
127 |
+
for name, buffer in noisy_audios.items():
|
128 |
+
st.download_button(label=f"Download {name}", data=buffer, file_name=f"noisy_{name}", mime="audio/wav")
|
129 |
+
else:
|
130 |
+
st.warning("Please upload at least one audio file.")
|
131 |
+
|
132 |
+
if __name__ == "__main__":
|
133 |
+
main()
|