Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
import spaces
|
2 |
import gradio as gr
|
3 |
import edge_tts
|
@@ -14,6 +16,31 @@ from pydub import AudioSegment
|
|
14 |
from pydub.playback import play
|
15 |
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
def get_silence(duration_ms=1000):
|
18 |
# Create silent audio segment with specified parameters
|
19 |
silent_audio = AudioSegment.silent(
|
@@ -253,6 +280,10 @@ async def transcript_to_speech(transcript_text, voice, rate, pitch, speed_adjust
|
|
253 |
final_audio = AudioSegment.silent(duration=max_end_time_ms, frame_rate=24000)
|
254 |
for segment in timed_audio_segments:
|
255 |
final_audio = final_audio.overlay(segment['audio'], position=segment['start'])
|
|
|
|
|
|
|
|
|
256 |
|
257 |
combined_audio_path = tempfile.mktemp(suffix=".mp3")
|
258 |
final_audio.export(combined_audio_path, format="mp3")
|
|
|
1 |
+
## Low pass filtering applied to final Audio
|
2 |
+
|
3 |
import spaces
|
4 |
import gradio as gr
|
5 |
import edge_tts
|
|
|
16 |
from pydub.playback import play
|
17 |
|
18 |
|
19 |
+
def apply_low_pass_filter(audio_segment, cutoff_freq, sample_rate, order=5):
|
20 |
+
"""Applies a low-pass filter to a pydub AudioSegment."""
|
21 |
+
audio_np = np.array(audio_segment.get_array_of_samples()).astype(np.float32) / (2**15 - 1)
|
22 |
+
if audio_segment.channels == 2:
|
23 |
+
audio_np = audio_np.reshape(-1, 2)
|
24 |
+
|
25 |
+
nyquist_freq = 0.5 * sample_rate
|
26 |
+
normalized_cutoff = cutoff_freq / nyquist_freq
|
27 |
+
b, a = butter(order, normalized_cutoff, btype='low', analog=False)
|
28 |
+
|
29 |
+
filtered_data = np.zeros_like(audio_np, dtype=np.float32)
|
30 |
+
if audio_segment.channels == 1:
|
31 |
+
filtered_data = lfilter(b, a, audio_np)
|
32 |
+
else:
|
33 |
+
for channel in range(audio_segment.channels):
|
34 |
+
filtered_data[:, channel] = lfilter(b, a, audio_np[:, channel])
|
35 |
+
|
36 |
+
filtered_data_int16 = (filtered_data * (2**15 - 1)).astype(np.int16)
|
37 |
+
filtered_audio = AudioSegment(filtered_data_int16.tobytes(),
|
38 |
+
frame_rate=sample_rate,
|
39 |
+
sample_width=audio_segment.sample_width,
|
40 |
+
channels=audio_segment.channels)
|
41 |
+
return filtered_audio
|
42 |
+
|
43 |
+
|
44 |
def get_silence(duration_ms=1000):
|
45 |
# Create silent audio segment with specified parameters
|
46 |
silent_audio = AudioSegment.silent(
|
|
|
280 |
final_audio = AudioSegment.silent(duration=max_end_time_ms, frame_rate=24000)
|
281 |
for segment in timed_audio_segments:
|
282 |
final_audio = final_audio.overlay(segment['audio'], position=segment['start'])
|
283 |
+
|
284 |
+
# Apply the low-pass filter here
|
285 |
+
cutoff_frequency = 3500 # 3.5 kHz (you can make this a user-configurable parameter later)
|
286 |
+
filtered_final_audio = apply_low_pass_filter(final_audio, cutoff_frequency, final_audio.frame_rate)
|
287 |
|
288 |
combined_audio_path = tempfile.mktemp(suffix=".mp3")
|
289 |
final_audio.export(combined_audio_path, format="mp3")
|