Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -18,8 +18,7 @@ sample_rate = 44100
|
|
18 |
amplitude_scaling_factor = 15.0
|
19 |
|
20 |
|
21 |
-
# ----------------Useless----------------
|
22 |
-
|
23 |
def delete_file(file_path):
|
24 |
try:
|
25 |
os.remove(file_path)
|
@@ -39,7 +38,7 @@ def signal_function(frequency, time):
|
|
39 |
return np.sin(2 * np.pi * frequency * time)
|
40 |
|
41 |
|
42 |
-
def generate_silence(duration):
|
43 |
return np.zeros(int(sample_rate * duration))
|
44 |
|
45 |
|
@@ -119,19 +118,19 @@ def binary_to_signal(binary_string):
|
|
119 |
|
120 |
|
121 |
def encode_and_generate_audio(text):
|
122 |
-
#delete_file(
|
123 |
-
#delete_file(
|
124 |
binary_string_to_send = text_to_binary(text)
|
125 |
signal = binary_to_signal(binary_string_to_send)
|
126 |
-
write(
|
127 |
main()
|
128 |
return "WAV file generated and ready to be sent."
|
129 |
|
130 |
|
131 |
# -----------------Filter----------------- #
|
132 |
|
133 |
-
def butter_bandpass(lowcut, highcut,
|
134 |
-
nyquist = 0.5 *
|
135 |
low = lowcut / nyquist
|
136 |
high = highcut / nyquist
|
137 |
coef = butter(order, [low, high], btype='band')
|
@@ -140,21 +139,23 @@ def butter_bandpass(lowcut, highcut, sr, order=5):
|
|
140 |
return b, a
|
141 |
|
142 |
|
143 |
-
def butter_bandpass_filter(data, lowcut, highcut,
|
144 |
-
b, a = butter_bandpass(lowcut, highcut,
|
145 |
y = lfilter(b, a, data)
|
146 |
return y
|
147 |
|
148 |
|
149 |
def main():
|
|
|
|
|
150 |
lowcut = 18000
|
151 |
highcut = 19000
|
152 |
|
153 |
try:
|
154 |
-
|
155 |
|
156 |
-
filtered_data = butter_bandpass_filter(data, lowcut, highcut,
|
157 |
-
write(output_file,
|
158 |
return "Filtered Audio Generated"
|
159 |
except Exception as e:
|
160 |
return f"Error: {str(e)}"
|
@@ -167,9 +168,9 @@ def play_sound():
|
|
167 |
return gr.Audio(output_file, autoplay=True)
|
168 |
|
169 |
|
170 |
-
# -----------------Interface
|
171 |
|
172 |
-
# Define the Gradio interface
|
173 |
with gr.Blocks() as demo:
|
174 |
name = gr.Textbox(label="Your Text")
|
175 |
output = gr.Textbox(label="Output")
|
|
|
18 |
amplitude_scaling_factor = 15.0
|
19 |
|
20 |
|
21 |
+
# ----------------Useless---------------- #
|
|
|
22 |
def delete_file(file_path):
|
23 |
try:
|
24 |
os.remove(file_path)
|
|
|
38 |
return np.sin(2 * np.pi * frequency * time)
|
39 |
|
40 |
|
41 |
+
def generate_silence(duration, sample_rate=44100):
|
42 |
return np.zeros(int(sample_rate * duration))
|
43 |
|
44 |
|
|
|
118 |
|
119 |
|
120 |
def encode_and_generate_audio(text):
|
121 |
+
# delete_file("output_text.wav")
|
122 |
+
# delete_file("output_filtered.wav")
|
123 |
binary_string_to_send = text_to_binary(text)
|
124 |
signal = binary_to_signal(binary_string_to_send)
|
125 |
+
write('output_text.wav', 44100, signal.astype(np.int16))
|
126 |
main()
|
127 |
return "WAV file generated and ready to be sent."
|
128 |
|
129 |
|
130 |
# -----------------Filter----------------- #
|
131 |
|
132 |
+
def butter_bandpass(lowcut, highcut, fs, order=5):
|
133 |
+
nyquist = 0.5 * fs
|
134 |
low = lowcut / nyquist
|
135 |
high = highcut / nyquist
|
136 |
coef = butter(order, [low, high], btype='band')
|
|
|
139 |
return b, a
|
140 |
|
141 |
|
142 |
+
def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
|
143 |
+
b, a = butter_bandpass(lowcut, highcut, fs, order=order)
|
144 |
y = lfilter(b, a, data)
|
145 |
return y
|
146 |
|
147 |
|
148 |
def main():
|
149 |
+
input_file = 'output_text.wav'
|
150 |
+
output_file = 'output_filtered_sender.wav'
|
151 |
lowcut = 18000
|
152 |
highcut = 19000
|
153 |
|
154 |
try:
|
155 |
+
fs, data = read(input_file)
|
156 |
|
157 |
+
filtered_data = butter_bandpass_filter(data, lowcut, highcut, fs)
|
158 |
+
write(output_file, fs, np.int16(filtered_data))
|
159 |
return "Filtered Audio Generated"
|
160 |
except Exception as e:
|
161 |
return f"Error: {str(e)}"
|
|
|
168 |
return gr.Audio(output_file, autoplay=True)
|
169 |
|
170 |
|
171 |
+
# -----------------Interface----------------- #
|
172 |
|
173 |
+
# Define the Gradio interface
|
174 |
with gr.Blocks() as demo:
|
175 |
name = gr.Textbox(label="Your Text")
|
176 |
output = gr.Textbox(label="Output")
|